[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @group meta
   5   */
   6  class GP_Test_Meta extends GP_UnitTestCase {
   7  
   8      /**
   9       * @dataProvider data_meta_keys
  10       */
  11  	function test_gp_sanitize_meta_key( $expected, $meta_key ) {
  12          $this->assertSame( $expected, gp_sanitize_meta_key( $meta_key ) );
  13      }
  14  
  15  	function data_meta_keys() {
  16          return array(
  17              array( 'foo', 'foo' ),
  18              array( 'fooBar', 'fooBar' ),
  19              array( 'foobar', 'foo-bar' ),
  20              array( 'foobar', 'foo.bar' ),
  21              array( 'foobar', 'foo:bar' ),
  22              array( 'foo_bar', 'foo_bar' ),
  23              array( 'foobar123', 'foobar123' ),
  24              array( 'foobar', 'foo?#+bar' ),
  25          );
  26      }
  27  
  28      function test_gp_get_meta_returns_false_for_falsely_object_ids() {
  29          $this->assertFalse( gp_get_meta( 'foo', null ) );
  30          $this->assertFalse( gp_get_meta( 'foo', false ) );
  31          $this->assertFalse( gp_get_meta( 'foo', 0 ) );
  32          $this->assertFalse( gp_get_meta( 'foo', '' ) );
  33          $this->assertFalse( gp_get_meta( 'foo', 'bar' ) );
  34      }
  35  
  36      function test_gp_get_meta_returns_false_for_falsely_object_types() {
  37          $this->assertFalse( gp_get_meta( null, 1 ) );
  38          $this->assertFalse( gp_get_meta( false, 1 ) );
  39          $this->assertFalse( gp_get_meta( '', 1 ) );
  40          $this->assertFalse( gp_get_meta( 0, 1 ) );
  41      }
  42  
  43      function test_gp_update_meta_returns_false_for_falsely_object_ids() {
  44          $this->assertFalse( gp_update_meta( null, 'key', 'value', 'type' ) );
  45          $this->assertFalse( gp_update_meta( false, 'key', 'value', 'type' ) );
  46          $this->assertFalse( gp_update_meta( 0, 'key', 'value', 'type' ) );
  47          $this->assertFalse( gp_update_meta( '', 'key', 'value', 'type' ) );
  48          $this->assertFalse( gp_update_meta( 'bar', 'key', 'value', 'type' ) );
  49      }
  50  
  51      function test_gp_delete_meta_returns_false_for_falsely_object_ids() {
  52          $this->assertFalse( gp_delete_meta( null, 'key', 'value', 'type' ) );
  53          $this->assertFalse( gp_delete_meta( false, 'key', 'value', 'type' ) );
  54          $this->assertFalse( gp_delete_meta( 0, 'key', 'value', 'type' ) );
  55          $this->assertFalse( gp_delete_meta( '', 'key', 'value', 'type' ) );
  56          $this->assertFalse( gp_delete_meta( 'bar', 'key', 'value', 'type' ) );
  57      }
  58  
  59  	function test_update_meta_should_set_meta() {
  60          gp_update_meta( '1', 'foo', 'bar', 'thing' );
  61          $this->assertEquals( 'bar', gp_get_meta( 'thing', '1', 'foo' ) );
  62      }
  63  
  64      function test_gp_update_meta_updates_an_existing_meta_value() {
  65          $this->assertInternalType( 'int', gp_update_meta( '1', 'key', 'value-1', 'thing' ) );
  66          $this->assertTrue( gp_update_meta( '1', 'key', 'value-2', 'thing'  ) );
  67          $this->assertSame( 'value-2', gp_get_meta( 'thing', '1', 'key' ) );
  68      }
  69  
  70      function test_delete_meta_without_value_should_delete_meta() {
  71          gp_update_meta( '1', 'foo', 'bar', 'thing' );
  72          gp_delete_meta( '1', 'foo', null, 'thing' );
  73          $this->assertEquals( null, gp_get_meta( 'thing', '1', 'foo' ) );
  74      }
  75  
  76      function test_delete_meta_with_value_should_delete_only_meta_with_value() {
  77          gp_update_meta( '1', 'foo', 'bar', 'thing' );
  78          gp_delete_meta( '1', 'foo', 'bar', 'thing' );
  79          $this->assertEquals( null, gp_get_meta( 'thing', '1', 'foo' ) );
  80  
  81          gp_update_meta( '1', 'foo', 'foo', 'thing' );
  82          gp_delete_meta( '1', 'foo', 'bar', 'thing' );
  83          $this->assertNotEquals( null, gp_get_meta( 'thing', '1', 'foo' ) );
  84      }
  85  
  86      function test_gp_update_meta_does_not_update_if_prev_value_equals_new_value() {
  87          $this->assertInternalType( 'int', gp_update_meta( '1', 'foo', 'foo', 'thing' ) );
  88          $this->assertTrue( gp_update_meta( '1', 'foo', 'foo', 'thing' ) ); // @todo Is this the correct return value?
  89      }
  90  
  91      /**
  92       * @ticket 480
  93       */
  94  	function test_get_meta_uses_cache() {
  95          global $wpdb;
  96  
  97          gp_update_meta( '1', 'foo', 'bar', 'thing' );
  98  
  99          $num_queries = $wpdb->num_queries;
 100  
 101          // Cache is not primed, expect 1 query.
 102          gp_get_meta( 'thing', '1', 'foo' );
 103          $this->assertEquals( $num_queries + 1, $wpdb->num_queries );
 104  
 105          $num_queries = $wpdb->num_queries;
 106  
 107          // Cache is primed, expect no queries.
 108          gp_get_meta( 'thing', '1', 'foo' );
 109          $this->assertEquals( $num_queries, $wpdb->num_queries );
 110      }
 111  
 112      /**
 113       * @ticket 480
 114       */
 115  	function test_get_meta_without_meta_key() {
 116          gp_update_meta( '1', 'key1', 'foo', 'thing' );
 117          gp_update_meta( '1', 'key2', 'foo', 'thing' );
 118  
 119          $meta = gp_get_meta( 'thing', '1' );
 120          $this->assertCount( 2, $meta );
 121          $this->assertEqualSets( array( 'key1', 'key2' ), array_keys( $meta ) );
 122      }
 123  }


Generated: Tue Mar 19 01:01:21 2024 Cross-referenced by PHPXref 0.7.1