| 1 | <?php |
| 2 | /** |
| 3 | * @group meta |
| 4 | */ |
| 5 | class Tests_Meta_Register_Meta extends WP_UnitTestCase { |
| 6 | protected static $editor_id; |
| 7 | protected static $post_id; |
| 8 | protected static $comment_id; |
| 9 | |
| 10 | public static function wpSetUpBeforeClass( $factory ) { |
| 11 | self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) ); |
| 12 | self::$post_id = $factory->post->create(); |
| 13 | self::$comment_id = $factory->comment->create( array( 'comment_post_ID' => self::$post_id ) ); |
| 14 | } |
| 15 | |
| 16 | public static function wpTearDownAfterClass() { |
| 17 | self::delete_user( self::$editor_id ); |
| 18 | wp_delete_comment( self::$comment_id, true ); |
| 19 | wp_delete_post( self::$post_id, true ); |
| 20 | } |
| 21 | |
| 22 | function setUp() { |
| 23 | parent::setUp(); |
| 24 | wp_set_current_user( self::$editor_id ); |
| 25 | } |
| 26 | |
| 27 | public function _old_sanitize_meta_cb( $meta_value, $meta_key, $meta_type ) { |
| 28 | return $meta_key . ' sanitized'; |
| 29 | } |
| 30 | |
| 31 | public function _new_sanitize_meta_cb( $meta_value, $meta_key, $object_type, $object_subtype ) { |
| 32 | return $meta_key . ' sanitized'; |
| 33 | } |
| 34 | |
| 35 | public function test_register_meta() { |
| 36 | $this->assertTrue( register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) ) ); |
| 37 | } |
| 38 | |
| 39 | public function test_register_meta_without_subtype() { |
| 40 | $this->assertFalse( register_meta( 'post', 'flight_number', array( $this, '_new_sanitize_meta_cb' ) ) ); |
| 41 | } |
| 42 | |
| 43 | public function test_register_meta_without_sanitize_callback_registers_meta_key() { |
| 44 | register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) ); |
| 45 | $this->assertTrue( registered_meta_key_exists( 'post', 'post', 'flight_number' ) ); |
| 46 | } |
| 47 | |
| 48 | public function test_register_meta_for_a_category() { |
| 49 | register_meta( 'term', 'category_icon', array( 'object_subtype' => 'category' ) ); |
| 50 | $this->assertTrue( registered_meta_key_exists( 'term', 'category', 'category_icon' ) ); |
| 51 | } |
| 52 | |
| 53 | public function test_register_meta_for_a_comment() { |
| 54 | register_meta( 'comment', 'comment_rating', array( 'object_subtype' => 'comment' ) ); |
| 55 | $this->assertTrue( registered_meta_key_exists( 'comment', 'comment', 'comment_rating' ) ); |
| 56 | } |
| 57 | |
| 58 | public function test_register_meta_with_sanitize_callback_registers_meta_key() { |
| 59 | register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post', 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) ); |
| 60 | $this->assertTrue( registered_meta_key_exists( 'post', 'post', 'flight_number' ) ); |
| 61 | } |
| 62 | |
| 63 | public function test_register_meta_valid_object_type_with_valid_type() { |
| 64 | register_meta( 'post', 'longitude', array( 'object_subtype' => 'post' ) ); |
| 65 | $this->assertTrue( registered_meta_key_exists( 'post', 'post', 'longitude' ) ); |
| 66 | } |
| 67 | |
| 68 | public function test_register_meta_valid_object_type_with_invalid_subtype() { |
| 69 | register_meta( 'post', 'latitude', array( 'object_subtype' => 'post' ) ); |
| 70 | $this->assertFalse( registered_meta_key_exists( 'post', 'invalid-type', 'latitude' ) ); |
| 71 | } |
| 72 | |
| 73 | public function test_register_meta_with_old_sanitize_callback_parameter() { |
| 74 | register_meta( 'post', 'old_sanitized_key', array( $this, '_old_sanitize_meta_cb' ) ); |
| 75 | $meta = sanitize_meta( 'old_sanitized_key', 'unsanitized', 'post', 'post' ); |
| 76 | |
| 77 | $this->assertEquals( 'old_sanitized_key sanitized', $meta ); |
| 78 | } |
| 79 | |
| 80 | public function test_register_meta_with_new_sanitize_callback_parameter() { |
| 81 | register_meta( 'post', 'new_sanitized_key', array( 'object_subtype' => 'post', 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) ); |
| 82 | $meta = sanitize_meta( 'new_sanitized_key', 'unsanitized', 'post', 'post' ); |
| 83 | |
| 84 | $this->assertEquals( 'new_sanitized_key sanitized', $meta ); |
| 85 | } |
| 86 | |
| 87 | public function test_unregister_meta_key() { |
| 88 | register_meta( 'post', 'registered_key', array( 'object_subtype' => 'post' ) ); |
| 89 | |
| 90 | $this->assertTrue( unregister_meta_key( 'post', 'post', 'registered_key' ) ); |
| 91 | } |
| 92 | |
| 93 | public function test_unregister_meta_key_with_invalid_key() { |
| 94 | $this->assertWPError( unregister_meta_key( 'post', 'post', 'not_a_registered_key' ) ); |
| 95 | } |
| 96 | |
| 97 | public function test_get_registered_meta_keys() { |
| 98 | register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post' ) ); |
| 99 | register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'post' ) ); |
| 100 | |
| 101 | $meta_keys = get_registered_meta_keys( 'post', 'post' ); |
| 102 | |
| 103 | $this->assertArrayHasKey( 'registered_key1', $meta_keys ); |
| 104 | $this->assertArrayHasKey( 'registered_key2', $meta_keys ); |
| 105 | } |
| 106 | |
| 107 | public function test_get_registered_meta_keys_subtype_is_empty() { |
| 108 | register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post' ) ); |
| 109 | register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'post' ) ); |
| 110 | |
| 111 | $meta_keys = get_registered_meta_keys( 'post', 'page' ); |
| 112 | |
| 113 | $this->assertEmpty( $meta_keys ); |
| 114 | } |
| 115 | |
| 116 | public function test_get_registered_meta_keys_invalid_type_is_empty() { |
| 117 | register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post' ) ); |
| 118 | register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'post' ) ); |
| 119 | |
| 120 | $meta_keys = get_registered_meta_keys( 'invalid-type' ); |
| 121 | |
| 122 | $this->assertEmpty( $meta_keys ); |
| 123 | } |
| 124 | |
| 125 | public function test_get_registered_meta_keys_has_count() { |
| 126 | register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post' ) ); |
| 127 | register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'page' ) ); |
| 128 | |
| 129 | $meta_keys = get_registered_meta_keys( 'post' ); |
| 130 | |
| 131 | $this->assertCount( 2, $meta_keys ); |
| 132 | } |
| 133 | |
| 134 | public function test_get_registered_meta_keys_type_arg() { |
| 135 | register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post', 'description' => 'I\'m just a field, take a good look at me' ) ); |
| 136 | |
| 137 | $meta_keys = get_registered_meta_keys( 'post', 'post' ); |
| 138 | |
| 139 | $this->assertEquals( 'I\'m just a field, take a good look at me', $meta_keys['registered_key1']['description'] ); |
| 140 | } |
| 141 | |
| 142 | public function test_get_registered_meta_keys_invalid_arg() { |
| 143 | register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post', 'invalid_arg' => 'invalid' ) ); |
| 144 | |
| 145 | $meta_keys = get_registered_meta_keys( 'post', 'post' ); |
| 146 | |
| 147 | $this->assertArrayNotHasKey( 'invalid_arg', $meta_keys['registered_key1'] ); |
| 148 | } |
| 149 | |
| 150 | public function test_get_registered_metadata() { |
| 151 | register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) ); |
| 152 | |
| 153 | add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' ); |
| 154 | |
| 155 | $meta = get_registered_metadata( 'post', 'post', self::$post_id ); |
| 156 | |
| 157 | $this->assertEquals( 'Oceanic 815', $meta['flight_number'][0] ); |
| 158 | } |
| 159 | |
| 160 | public function test_get_registered_metadata_by_key() { |
| 161 | register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) ); |
| 162 | |
| 163 | add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' ); |
| 164 | |
| 165 | $meta = get_registered_metadata( 'post', 'post', self::$post_id, 'flight_number' ); |
| 166 | |
| 167 | $this->assertEquals( 'Oceanic 815', $meta[0] ); |
| 168 | } |
| 169 | |
| 170 | public function test_get_registered_metadata_by_key_single() { |
| 171 | register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post', 'single' => true ) ); |
| 172 | |
| 173 | add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' ); |
| 174 | |
| 175 | $meta = get_registered_metadata( 'post', 'post', self::$post_id, 'flight_number' ); |
| 176 | |
| 177 | $this->assertEquals( 'Oceanic 815', $meta ); |
| 178 | } |
| 179 | |
| 180 | public function test_get_registered_metadata_by_invalid_key() { |
| 181 | register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) ); |
| 182 | |
| 183 | add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' ); |
| 184 | |
| 185 | $meta = get_registered_metadata( 'post', 'post', self::$post_id, 'flight_pilot' ); |
| 186 | |
| 187 | $this->assertWPError( $meta ); |
| 188 | } |
| 189 | |
| 190 | public function test_get_registered_metadata_invalid_object_type() { |
| 191 | register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) ); |
| 192 | |
| 193 | add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' ); |
| 194 | |
| 195 | $meta = get_registered_metadata( 'invalid-type', 'invalid-subtype', self::$post_id ); |
| 196 | |
| 197 | $this->assertWPError( $meta ); |
| 198 | } |
| 199 | |
| 200 | public function test_get_registered_metadata_invalid() { |
| 201 | $meta = get_registered_metadata( 'invalid-type', 'invalid-subtype', self::$post_id ); |
| 202 | |
| 203 | $this->assertWPError( $meta ); |
| 204 | } |
| 205 | } |
| 206 | No newline at end of file |