[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/tests/phpunit/testcases/ -> test_validation.php (source)

   1  <?php
   2  
   3  class Mouse extends GP_Thing {
   4      var $table_basename = 'mice';
   5      var $field_names = array( 'id', 'name', 'rating', 'cat_id' );
   6      var $non_updatable_attributes = array( 'id', );
   7  
   8  	function restrict_fields( $rules ) {
   9          $rules->name_should_not_be('empty');
  10          $rules->rating_should_be('positive_int');
  11      }
  12  
  13  	function normalize_fields( $args ) {
  14          $args = (array)$args;
  15          if ( isset( $args['cat_id'] ) ) {
  16              $args['cat_id'] = $this->force_false_to_null( $args['cat_id'] );
  17          }
  18          return $args;
  19      }
  20  }
  21  
  22  
  23  class GP_Test_Validation extends GP_UnitTestCase {
  24  	function setUp() {
  25          parent::setUp();
  26  
  27          global $wpdb;
  28          $wpdb->mice = '';
  29      }
  30  
  31  	function test_basic() {
  32          $mickey = new Mouse( array( 'id' => 5, 'name' => 'Mickey', 'rating' => 11, 'cat_id' => 1, ) );
  33          $this->assertEquals( true, $mickey->validate() );
  34          $minnie = new Mouse( array( 'id' => 5, 'name' => '', 'rating' => 11, 'cat_id' => 1, ) );
  35          $this->assertEquals( false, $minnie->validate() );
  36      }
  37  
  38  	function test_is_int() {
  39         $callback = GP_Validators::get( 'int' );
  40         $f = $callback['positive'];
  41         $this->assertEquals( true, $f('0') );
  42         $this->assertEquals( true, $f('1') );
  43         $this->assertEquals( true, $f('-1') );
  44         $this->assertEquals( true, $f('514') );
  45         $this->assertEquals( true, $f('-514') );
  46         $this->assertEquals( false, $f('aaa1aaa') );
  47         $this->assertEquals( false, $f('2.3') );
  48         $this->assertEquals( false, $f('aaa1') );
  49         $this->assertEquals( false, $f('1aaa') );
  50      }
  51  
  52  	function test_between() {
  53         $callback = GP_Validators::get( 'between' );
  54         $f = $callback['positive'];
  55         $this->assertEquals( true, $f( 0, -1, 2 ) );
  56      }
  57  
  58  }


Generated: Mon Nov 18 01:01:56 2019 Cross-referenced by PHPXref 0.7.1