- Timestamp:
- 06/21/2018 09:06:50 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-term-meta-fields.php
r43340 r43378 13 13 protected static $wp_meta_keys_saved; 14 14 protected static $category_id; 15 protected static $customtax_term_id; 15 16 16 17 public static function wpSetUpBeforeClass( $factory ) { 18 register_taxonomy( 'customtax', 'post', array( 19 'show_in_rest' => true, 20 ) ); 21 17 22 self::$wp_meta_keys_saved = isset( $GLOBALS['wp_meta_keys'] ) ? $GLOBALS['wp_meta_keys'] : array(); 18 23 self::$category_id = $factory->category->create(); 24 self::$customtax_term_id = $factory->term->create( array( 'taxonomy' => 'customtax' ) ); 19 25 } 20 26 … … 22 28 $GLOBALS['wp_meta_keys'] = self::$wp_meta_keys_saved; 23 29 wp_delete_term( self::$category_id, 'category' ); 30 wp_delete_term( self::$customtax_term_id, 'customtax' ); 31 32 unregister_taxonomy( 'customtax' ); 24 33 } 25 34 … … 121 130 ); 122 131 132 register_taxonomy( 'customtax', 'post', array( 133 'show_in_rest' => true, 134 ) ); 135 136 register_term_meta( 'customtax', 'test_customtax_single', array( 137 'show_in_rest' => true, 138 'single' => true, 139 ) ); 140 141 register_term_meta( 'customtax', 'test_customtax_multi', array( 142 'show_in_rest' => true, 143 'single' => false, 144 ) ); 145 146 // Register 'test_single' on subtype to override for bad auth. 147 register_term_meta( 'customtax', 'test_single', array( 148 'show_in_rest' => true, 149 'single' => true, 150 'auth_callback' => '__return_false', 151 ) ); 152 123 153 /** @var WP_REST_Server $wp_rest_server */ 124 154 global $wp_rest_server; … … 1049 1079 1050 1080 /** 1081 * @ticket 38323 1082 * @dataProvider data_get_subtype_meta_value 1083 */ 1084 public function test_get_subtype_meta_value( $taxonomy, $meta_key, $single, $in_taxonomy ) { 1085 $term_id = self::$category_id; 1086 $endpoint = 'categories'; 1087 if ( 'customtax' === $taxonomy ) { 1088 $term_id = self::$customtax_term_id; 1089 $endpoint = 'customtax'; 1090 } 1091 1092 $meta_value = 'testvalue'; 1093 1094 add_term_meta( $term_id, $meta_key, $meta_value ); 1095 1096 $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/%s/%d', $endpoint, $term_id ) ); 1097 $response = rest_get_server()->dispatch( $request ); 1098 1099 $this->assertEquals( 200, $response->get_status() ); 1100 1101 $data = $response->get_data(); 1102 1103 $this->assertArrayHasKey( 'meta', $data ); 1104 $this->assertInternalType( 'array', $data['meta'] ); 1105 1106 if ( $in_taxonomy ) { 1107 $expected_value = $meta_value; 1108 if ( ! $single ) { 1109 $expected_value = array( $expected_value ); 1110 } 1111 1112 $this->assertArrayHasKey( $meta_key, $data['meta'] ); 1113 $this->assertEquals( $expected_value, $data['meta'][ $meta_key ] ); 1114 } else { 1115 $this->assertArrayNotHasKey( $meta_key, $data['meta'] ); 1116 } 1117 } 1118 1119 public function data_get_subtype_meta_value() { 1120 return array( 1121 array( 'customtax', 'test_customtax_single', true, true ), 1122 array( 'customtax', 'test_customtax_multi', false, true ), 1123 array( 'customtax', 'test_single', true, true ), 1124 array( 'customtax', 'test_multi', false, true ), 1125 array( 'category', 'test_customtax_single', true, false ), 1126 array( 'category', 'test_customtax_multi', false, false ), 1127 array( 'category', 'test_single', true, true ), 1128 array( 'category', 'test_multi', false, true ), 1129 ); 1130 } 1131 1132 /** 1133 * @ticket 38323 1134 * @dataProvider data_set_subtype_meta_value 1135 */ 1136 public function test_set_subtype_meta_value( $taxonomy, $meta_key, $single, $in_taxonomy, $can_write ) { 1137 $term_id = self::$category_id; 1138 $endpoint = 'categories'; 1139 if ( 'customtax' === $taxonomy ) { 1140 $term_id = self::$customtax_term_id; 1141 $endpoint = 'customtax'; 1142 } 1143 1144 $meta_value = 'value_to_set'; 1145 1146 $this->grant_write_permission(); 1147 1148 $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/%s/%d', $endpoint, $term_id ) ); 1149 $request->set_body_params( array( 1150 'meta' => array( 1151 $meta_key => $meta_value, 1152 ), 1153 ) ); 1154 1155 $response = rest_get_server()->dispatch( $request ); 1156 if ( ! $can_write ) { 1157 $this->assertEquals( 403, $response->get_status() ); 1158 $this->assertEmpty( get_term_meta( $term_id, $meta_key, $single ) ); 1159 return; 1160 } 1161 1162 $this->assertEquals( 200, $response->get_status() ); 1163 1164 $data = $response->get_data(); 1165 $this->assertArrayHasKey( 'meta', $data ); 1166 $this->assertInternalType( 'array', $data['meta'] ); 1167 1168 if ( $in_taxonomy ) { 1169 $expected_value = $meta_value; 1170 if ( ! $single ) { 1171 $expected_value = array( $expected_value ); 1172 } 1173 1174 $this->assertEquals( $expected_value, get_term_meta( $term_id, $meta_key, $single ) ); 1175 $this->assertArrayHasKey( $meta_key, $data['meta'] ); 1176 $this->assertEquals( $expected_value, $data['meta'][ $meta_key ] ); 1177 } else { 1178 $this->assertEmpty( get_term_meta( $term_id, $meta_key, $single ) ); 1179 $this->assertArrayNotHasKey( $meta_key, $data['meta'] ); 1180 } 1181 } 1182 1183 public function data_set_subtype_meta_value() { 1184 $data = $this->data_get_subtype_meta_value(); 1185 1186 foreach ( $data as $index => $dataset ) { 1187 $can_write = true; 1188 1189 // This combination is not writable because of an auth callback of '__return_false'. 1190 if ( 'customtax' === $dataset[0] && 'test_single' === $dataset[1] ) { 1191 $can_write = false; 1192 } 1193 1194 $data[ $index ][] = $can_write; 1195 } 1196 1197 return $data; 1198 } 1199 1200 /** 1051 1201 * Internal function used to disable an insert query which 1052 1202 * will trigger a wpdb error for testing purposes.
Note: See TracChangeset
for help on using the changeset viewer.