[ 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  	function test_one_of() {
  59          $callback = GP_Validators::get( 'one_of' );
  60          $f = $callback['positive'];
  61          $this->assertEquals( true, $f( 'a', array( 'a', 'b' ) ) );
  62          $this->assertEquals( false, $f( 'c', array( 'a', 'b' ) ) );
  63          $this->assertEquals( true, $f( 3, array( 1, 2, 3 ) ) );
  64          $this->assertEquals( false, $f( '1', array( 1, 2, 3 ) ) );
  65      }
  66  
  67  	function test_is_ascii_string() {
  68          $callback = GP_Validators::get( 'consisting_only_of_ASCII_characters' );
  69          $f = $callback['positive'];
  70          $this->assertEquals( true, $f( 'a' ) );
  71          $this->assertEquals( true, $f( 'AbC' ) );
  72          $this->assertEquals( true, $f( 'foo bar' ) );
  73          $this->assertEquals( true, $f( '&' ) );
  74          $this->assertEquals( true, $f( '123abc' ) );
  75          $this->assertEquals( false, $f( 'äbc' ) );
  76          $this->assertEquals( false, $f( 'ãbc' ) );
  77      }
  78  
  79      function test_is_starting_and_ending_with_a_word_character() {
  80          $callback = GP_Validators::get( 'starting_and_ending_with_a_word_character' );
  81          $f = $callback['positive'];
  82          $this->assertEquals( true, $f( 'a' ) );
  83          $this->assertEquals( true, $f( 'foo bar' ) );
  84          $this->assertEquals( true, $f( 'a & b' ) );
  85          $this->assertEquals( false, $f( 'a ' ) );
  86          $this->assertEquals( false, $f( '-foo' ) );
  87          $this->assertEquals( false, $f( 'Hello world.' ) );
  88      }
  89  }


Generated: Thu Nov 21 01:01:07 2024 Cross-referenced by PHPXref 0.7.1