wpengine / atlas-content-modeler

Content modeling plugin for WordPress
https://developers.wpengine.com/docs/atlas-content-modeler
GNU General Public License v2.0
165 stars 13 forks source link

NOJIRA: Fix validate_number_type to accept decimals and 0 values #589

Closed nickcernis closed 2 years ago

nickcernis commented 2 years ago

Description

Fixes validate_number_type() so that:

Currently on main, running wp eval "acm_test_import_post_content()"; after activating the test plugin below produces errors:

<?php
/* Plugin Name: WPE Test Plugin */

use function WPE\AtlasContentModeler\API\insert_model_entry;

function acm_test_import_post_content() {   
    $model_slug = 'rabbit';
    $field_data = [
        'name' => 'Peter',      
        'speedDec' => 2.2,
        'speedInt' => 0,
    ];

    $post_id = insert_model_entry( $model_slug, $field_data );

    var_dump( $post_id );
}

Error sample:

class WPE\AtlasContentModeler\WP_Error#2322 (3) {
  public $errors =>
  array(2) {
    'speedInt' =>
    array(1) {
      [0] =>
      string(32) "speedInt must be of type integer"
    }
    'speedDec' =>
    array(1) {
      [0] =>
      string(32) "speedDec must be of type decimal"
    }
  }
  public $error_data =>
  array(0) {
  }
  protected $additional_data =>
  array(0) {
  }
}

Checklist

I have:

Testing

Extended unit tests extensively to test validate_number_type more thoroughly.

To test manually:

  1. Create a rabbits model with 'name' (text field), 'speedDec' (numeric field, decimal) and 'speedInt` (numeric field, integer) fields.
  2. Create a file named test-plugin.php in wp-content/plugins/ with the PHP content above.
  3. Activate the "WPE Test Plugin" plugin.
  4. Run wp eval "acm_test_import_post_content();" in the command line.

You should see output including the generated post ID such as:

> wp eval "acm_test_import_post_content();"
/Users/[user.name]/Sites/local/app/public/wp-content/plugins/test.php:16:
int(2939)