meta.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * @group meta
  4. */
  5. class Tests_Meta extends WP_UnitTestCase {
  6. function setUp() {
  7. parent::setUp();
  8. $this->author = new WP_User( $this->factory->user->create( array( 'role' => 'author' ) ) );
  9. $this->meta_id = add_metadata( 'user', $this->author->ID, 'meta_key', 'meta_value' );
  10. $this->delete_meta_id = add_metadata( 'user', $this->author->ID, 'delete_meta_key', 'delete_meta_value' );
  11. }
  12. function _meta_sanitize_cb ( $meta_value, $meta_key, $meta_type ) {
  13. return 'sanitized';
  14. }
  15. function test_sanitize_meta() {
  16. $meta = sanitize_meta( 'some_meta', 'unsanitized', 'post' );
  17. $this->assertEquals( 'unsanitized', $meta );
  18. register_meta( 'post', 'some_meta', array( $this, '_meta_sanitize_cb' ) );
  19. $meta = sanitize_meta( 'some_meta', 'unsanitized', 'post' );
  20. $this->assertEquals( 'sanitized', $meta );
  21. }
  22. function test_delete_metadata_by_mid() {
  23. // Let's try and delete a non-existing ID, non existing meta
  24. $this->assertFalse( delete_metadata_by_mid( 'user', 0 ) );
  25. $this->assertFalse( delete_metadata_by_mid( 'non_existing_meta', $this->delete_meta_id ) );
  26. // Now let's delete the real meta data
  27. $this->assertTrue( delete_metadata_by_mid( 'user', $this->delete_meta_id ) );
  28. // And make sure it's been deleted
  29. $this->assertFalse( get_metadata_by_mid( 'user', $this->delete_meta_id ) );
  30. // Make sure the caches are cleared
  31. $this->assertFalse( (bool) get_user_meta( $this->author->ID, 'delete_meta_key' ) );
  32. }
  33. function test_update_metadata_by_mid() {
  34. // Setup
  35. $meta = get_metadata_by_mid( 'user', $this->meta_id );
  36. // Update the meta value
  37. $this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, 'meta_new_value' ) );
  38. $meta = get_metadata_by_mid( 'user', $this->meta_id );
  39. $this->assertEquals( 'meta_new_value', $meta->meta_value );
  40. // Update the meta value
  41. $this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, 'meta_new_value', 'meta_new_key' ) );
  42. $meta = get_metadata_by_mid( 'user', $this->meta_id );
  43. $this->assertEquals( 'meta_new_key', $meta->meta_key );
  44. // Update the key and value
  45. $this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, 'meta_value', 'meta_key' ) );
  46. $meta = get_metadata_by_mid( 'user', $this->meta_id );
  47. $this->assertEquals( 'meta_key', $meta->meta_key );
  48. $this->assertEquals( 'meta_value', $meta->meta_value );
  49. // Update the value that has to be serialized
  50. $this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, array( 'first', 'second' ) ) );
  51. $meta = get_metadata_by_mid( 'user', $this->meta_id );
  52. $this->assertEquals( array( 'first', 'second' ), $meta->meta_value );
  53. // Let's try some invalid meta data
  54. $this->assertFalse( update_metadata_by_mid( 'user', 0, 'meta_value' ) );
  55. $this->assertFalse( update_metadata_by_mid( 'user', $this->meta_id, 'meta_value', array('invalid', 'key' ) ) );
  56. // Let's see if caches get cleared after updates.
  57. $meta = get_metadata_by_mid( 'user', $this->meta_id );
  58. $first = get_user_meta( $meta->user_id, $meta->meta_key );
  59. $this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, 'other_meta_value' ) );
  60. $second = get_user_meta( $meta->user_id, $meta->meta_key );
  61. $this->assertFalse( $first === $second );
  62. }
  63. function test_metadata_exists() {
  64. $this->assertFalse( metadata_exists( 'user', $this->author->ID, 'foobarbaz' ) );
  65. $this->assertTrue( metadata_exists( 'user', $this->author->ID, 'meta_key' ) );
  66. $this->assertFalse( metadata_exists( 'user', 1234567890, 'foobarbaz' ) );
  67. $this->assertFalse( metadata_exists( 'user', 1234567890, 'meta_key' ) );
  68. }
  69. /**
  70. * @ticket 18158
  71. */
  72. function test_user_metadata_not_exists() {
  73. $u = get_users( array(
  74. 'meta_query' => array(
  75. array( 'key' => 'meta_key', 'compare' => 'NOT EXISTS' )
  76. )
  77. ) );
  78. $this->assertEquals( 1, count( $u ) );
  79. // User found is not locally defined author (it's the admin)
  80. $this->assertNotEquals( $this->author->user_login, $u[0]->user_login );
  81. // Test EXISTS and NOT EXISTS together, no users should be found
  82. $this->assertEquals( 0, count( get_users( array(
  83. 'meta_query' => array(
  84. array( 'key' => 'meta_key', 'compare' => 'NOT EXISTS' ),
  85. array( 'key' => 'delete_meta_key', 'compare' => 'EXISTS' )
  86. )
  87. ) ) ) );
  88. $this->assertEquals( 2, count( get_users( array(
  89. 'meta_query' => array(
  90. array( 'key' => 'non_existing_meta', 'compare' => 'NOT EXISTS' )
  91. )
  92. ) ) ) );
  93. delete_metadata( 'user', $this->author->ID, 'meta_key' );
  94. $this->assertEquals( 2, count( get_users( array(
  95. 'meta_query' => array(
  96. array( 'key' => 'meta_key', 'compare' => 'NOT EXISTS' )
  97. )
  98. ) ) ) );
  99. }
  100. function test_metadata_slashes() {
  101. $key = rand_str();
  102. $value = 'Test\\singleslash';
  103. $expected = 'Testsingleslash';
  104. $value2 = 'Test\\\\doubleslash';
  105. $expected2 = 'Test\\doubleslash';
  106. $this->assertFalse( metadata_exists( 'user', $this->author->ID, $key ) );
  107. $this->assertFalse( delete_metadata( 'user', $this->author->ID, $key ) );
  108. $this->assertSame( '', get_metadata( 'user', $this->author->ID, $key, true ) );
  109. $this->assertInternalType( 'int', add_metadata( 'user', $this->author->ID, $key, $value ) );
  110. $this->assertEquals( $expected, get_metadata( 'user', $this->author->ID, $key, true ) );
  111. $this->assertTrue( delete_metadata( 'user', $this->author->ID, $key ) );
  112. $this->assertSame( '', get_metadata( 'user', $this->author->ID, $key, true ) );
  113. $this->assertInternalType( 'int', update_metadata( 'user', $this->author->ID, $key, $value ) );
  114. $this->assertEquals( $expected, get_metadata( 'user', $this->author->ID, $key, true ) );
  115. $this->assertTrue( update_metadata( 'user', $this->author->ID, $key, 'blah' ) );
  116. $this->assertEquals( 'blah', get_metadata( 'user', $this->author->ID, $key, true ) );
  117. $this->assertTrue( delete_metadata( 'user', $this->author->ID, $key ) );
  118. $this->assertSame( '', get_metadata( 'user', $this->author->ID, $key, true ) );
  119. $this->assertFalse( metadata_exists( 'user', $this->author->ID, $key ) );
  120. // Test overslashing
  121. $this->assertInternalType( 'int', add_metadata( 'user', $this->author->ID, $key, $value2 ) );
  122. $this->assertEquals( $expected2, get_metadata( 'user', $this->author->ID, $key, true ) );
  123. $this->assertTrue( delete_metadata( 'user', $this->author->ID, $key ) );
  124. $this->assertSame( '', get_metadata( 'user', $this->author->ID, $key, true ) );
  125. $this->assertInternalType( 'int', update_metadata( 'user', $this->author->ID, $key, $value2 ) );
  126. $this->assertEquals( $expected2, get_metadata( 'user', $this->author->ID, $key, true ) );
  127. }
  128. function test_meta_type_cast() {
  129. $post_id1 = $this->factory->post->create();
  130. add_post_meta( $post_id1, 'num_as_longtext', 123 );
  131. $post_id2 = $this->factory->post->create();
  132. add_post_meta( $post_id2, 'num_as_longtext', 99 );
  133. $posts = new WP_Query( array(
  134. 'fields' => 'ids',
  135. 'post_type' => 'any',
  136. 'meta_key' => 'num_as_longtext',
  137. 'meta_value' => '0',
  138. 'meta_compare' => '>',
  139. 'meta_type' => 'UNSIGNED',
  140. 'orderby' => 'meta_value',
  141. 'order' => 'ASC'
  142. ) );
  143. $this->assertEquals( array( $post_id2, $post_id1 ), $posts->posts );
  144. $this->assertEquals( 2, substr_count( $posts->request, 'CAST(' ) );
  145. }
  146. function test_meta_cache_order_asc() {
  147. $post_id = $this->factory->post->create();
  148. $colors = array( 'red', 'blue', 'yellow', 'green' );
  149. foreach ( $colors as $color )
  150. add_post_meta( $post_id, 'color', $color );
  151. foreach ( range( 1, 10 ) as $i ) {
  152. $meta = get_post_meta( $post_id, 'color' );
  153. $this->assertEquals( $meta, $colors );
  154. if ( 0 === $i % 2 )
  155. wp_cache_delete( $post_id, 'post_meta' );
  156. }
  157. }
  158. }