| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454 |
- <?php
- /**
- * Unit tests covering WP_REST_Terms_Controller functionality, used for Tags.
- *
- * @package WordPress
- * @subpackage REST API
- */
- /**
- * @group restapi
- */
- class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase {
- protected static $superadmin;
- protected static $administrator;
- protected static $editor;
- protected static $contributor;
- protected static $subscriber;
- protected static $tag_ids = array();
- protected static $total_tags = 30;
- protected static $per_page = 50;
- public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
- self::$superadmin = $factory->user->create(
- array(
- 'role' => 'administrator',
- 'user_login' => 'superadmin',
- )
- );
- self::$administrator = $factory->user->create(
- array(
- 'role' => 'administrator',
- )
- );
- self::$editor = $factory->user->create(
- array(
- 'role' => 'editor',
- )
- );
- self::$contributor = $factory->user->create(
- array(
- 'role' => 'contributor',
- )
- );
- self::$subscriber = $factory->user->create(
- array(
- 'role' => 'subscriber',
- )
- );
- if ( is_multisite() ) {
- update_site_option( 'site_admins', array( 'superadmin' ) );
- }
- // Set up tags for pagination tests.
- for ( $i = 0; $i < self::$total_tags; $i++ ) {
- $tag_ids[] = $factory->tag->create(
- array(
- 'name' => "Tag {$i}",
- )
- );
- }
- }
- public static function wpTearDownAfterClass() {
- self::delete_user( self::$superadmin );
- self::delete_user( self::$administrator );
- self::delete_user( self::$editor );
- self::delete_user( self::$subscriber );
- // Remove tags for pagination tests.
- foreach ( self::$tag_ids as $tag_id ) {
- wp_delete_term( $tag_id, 'post_tag' );
- }
- }
- public function setUp() {
- parent::setUp();
- register_meta(
- 'term',
- 'test_single',
- array(
- 'show_in_rest' => true,
- 'single' => true,
- 'type' => 'string',
- )
- );
- register_meta(
- 'term',
- 'test_multi',
- array(
- 'show_in_rest' => true,
- 'single' => false,
- 'type' => 'string',
- )
- );
- register_term_meta(
- 'post_tag',
- 'test_tag_single',
- array(
- 'show_in_rest' => true,
- 'single' => true,
- 'type' => 'string',
- )
- );
- register_term_meta(
- 'post_tag',
- 'test_tag_multi',
- array(
- 'show_in_rest' => true,
- 'single' => false,
- 'type' => 'string',
- )
- );
- register_term_meta(
- 'category',
- 'test_cat_meta',
- array(
- 'show_in_rest' => true,
- 'single' => true,
- 'type' => 'string',
- )
- );
- }
- public function test_register_routes() {
- $routes = rest_get_server()->get_routes();
- $this->assertArrayHasKey( '/wp/v2/tags', $routes );
- $this->assertArrayHasKey( '/wp/v2/tags/(?P<id>[\d]+)', $routes );
- }
- public function test_context_param() {
- // Collection.
- $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/tags' );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
- $this->assertSameSets( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
- // Single.
- $tag1 = $this->factory->tag->create( array( 'name' => 'Season 5' ) );
- $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/tags/' . $tag1 );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
- $this->assertSameSets( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
- }
- public function test_registered_query_params() {
- $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/tags' );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- $keys = array_keys( $data['endpoints'][0]['args'] );
- sort( $keys );
- $this->assertSame(
- array(
- 'context',
- 'exclude',
- 'hide_empty',
- 'include',
- 'offset',
- 'order',
- 'orderby',
- 'page',
- 'per_page',
- 'post',
- 'search',
- 'slug',
- ),
- $keys
- );
- }
- public function test_get_items() {
- $this->factory->tag->create();
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'per_page', self::$per_page );
- $response = rest_get_server()->dispatch( $request );
- $this->check_get_taxonomy_terms_response( $response );
- }
- public function test_get_items_invalid_permission_for_context() {
- wp_set_current_user( 0 );
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'context', 'edit' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
- }
- public function test_get_items_hide_empty_arg() {
- $post_id = $this->factory->post->create();
- $tag1 = $this->factory->tag->create( array( 'name' => 'Season 5' ) );
- $tag2 = $this->factory->tag->create( array( 'name' => 'The Be Sharps' ) );
- wp_set_object_terms( $post_id, array( $tag1, $tag2 ), 'post_tag' );
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'hide_empty', true );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- $this->assertSame( 2, count( $data ) );
- $this->assertSame( 'Season 5', $data[0]['name'] );
- $this->assertSame( 'The Be Sharps', $data[1]['name'] );
- // Invalid 'hide_empty' should error.
- $request->set_param( 'hide_empty', 'nothanks' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
- }
- public function test_get_items_include_query() {
- $id1 = $this->factory->tag->create();
- $id2 = $this->factory->tag->create();
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- // 'orderby' => 'asc'.
- $request->set_param( 'include', array( $id2, $id1 ) );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- $this->assertSame( 2, count( $data ) );
- $this->assertSame( $id1, $data[0]['id'] );
- // 'orderby' => 'include'.
- $request->set_param( 'orderby', 'include' );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- $this->assertSame( 2, count( $data ) );
- $this->assertSame( $id2, $data[0]['id'] );
- // Invalid 'include' should error.
- $request->set_param( 'include', array( 'myterm' ) );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
- }
- public function test_get_items_exclude_query() {
- $id1 = $this->factory->tag->create();
- $id2 = $this->factory->tag->create();
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'per_page', self::$per_page );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- $ids = wp_list_pluck( $data, 'id' );
- $this->assertTrue( in_array( $id1, $ids, true ) );
- $this->assertTrue( in_array( $id2, $ids, true ) );
- $request->set_param( 'exclude', array( $id2 ) );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- $ids = wp_list_pluck( $data, 'id' );
- $this->assertTrue( in_array( $id1, $ids, true ) );
- $this->assertFalse( in_array( $id2, $ids, true ) );
- // Invalid 'exclude' should error.
- $request->set_param( 'exclude', array( 'invalid' ) );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
- }
- public function test_get_items_offset_query() {
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'per_page', self::$per_page );
- $request->set_param( 'offset', 1 );
- $response = rest_get_server()->dispatch( $request );
- $this->assertCount( self::$total_tags - 1, $response->get_data() );
- // 'offset' works with 'per_page'.
- $request->set_param( 'per_page', 2 );
- $response = rest_get_server()->dispatch( $request );
- $this->assertCount( 2, $response->get_data() );
- // 'offset' takes priority over 'page'.
- $request->set_param( 'page', 3 );
- $response = rest_get_server()->dispatch( $request );
- $this->assertCount( 2, $response->get_data() );
- // Invalid 'offset' should error.
- $request->set_param( 'offset', 'moreplease' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
- }
- public function test_get_items_orderby_args() {
- $tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) );
- $tag2 = $this->factory->tag->create( array( 'name' => 'Zucchini' ) );
- /*
- * Tests:
- * - orderby
- * - order
- * - per_page
- */
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'orderby', 'name' );
- $request->set_param( 'order', 'desc' );
- $request->set_param( 'per_page', 1 );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $this->assertSame( 1, count( $data ) );
- $this->assertSame( 'Zucchini', $data[0]['name'] );
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'orderby', 'name' );
- $request->set_param( 'order', 'asc' );
- $request->set_param( 'per_page', 2 );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $this->assertSame( 2, count( $data ) );
- $this->assertSame( 'Apple', $data[0]['name'] );
- // Invalid 'orderby' should error.
- $request->set_param( 'orderby', 'invalid' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
- }
- public function test_get_items_orderby_id() {
- $tag0 = $this->factory->tag->create( array( 'name' => 'Cantaloupe' ) );
- $tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) );
- $tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) );
- // Defaults to 'orderby' => 'name', 'order' => 'asc'.
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $this->assertSame( 'Apple', $data[0]['name'] );
- $this->assertSame( 'Banana', $data[1]['name'] );
- $this->assertSame( 'Cantaloupe', $data[2]['name'] );
- // 'orderby' => 'id', with default 'order' => 'asc'.
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'orderby', 'id' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $this->assertSame( 'Tag 0', $data[0]['name'] );
- $this->assertSame( 'Tag 1', $data[1]['name'] );
- $this->assertSame( 'Tag 2', $data[2]['name'] );
- // 'orderby' => 'id', 'order' => 'desc'.
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'orderby', 'id' );
- $request->set_param( 'order', 'desc' );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- $this->assertSame( 200, $response->get_status() );
- $this->assertSame( 'Banana', $data[0]['name'] );
- $this->assertSame( 'Apple', $data[1]['name'] );
- $this->assertSame( 'Cantaloupe', $data[2]['name'] );
- }
- public function test_get_items_orderby_slugs() {
- $this->factory->tag->create( array( 'name' => 'Burrito' ) );
- $this->factory->tag->create( array( 'name' => 'Taco' ) );
- $this->factory->tag->create( array( 'name' => 'Chalupa' ) );
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'orderby', 'include_slugs' );
- $request->set_param( 'slug', array( 'taco', 'burrito', 'chalupa' ) );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- $this->assertSame( 200, $response->get_status() );
- $this->assertSame( 'taco', $data[0]['slug'] );
- $this->assertSame( 'burrito', $data[1]['slug'] );
- $this->assertSame( 'chalupa', $data[2]['slug'] );
- }
- public function test_get_items_post_args() {
- $post_id = $this->factory->post->create();
- $tag1 = $this->factory->tag->create( array( 'name' => 'DC' ) );
- $tag2 = $this->factory->tag->create( array( 'name' => 'Marvel' ) );
- $this->factory->tag->create( array( 'name' => 'Dark Horse' ) );
- wp_set_object_terms( $post_id, array( $tag1, $tag2 ), 'post_tag' );
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'post', $post_id );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $this->assertSame( 2, count( $data ) );
- $this->assertSame( 'DC', $data[0]['name'] );
- // Invalid 'post' should error.
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'post', 'invalid-post' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
- }
- public function test_get_terms_post_args_paging() {
- $post_id = $this->factory->post->create();
- wp_set_object_terms( $post_id, self::$tag_ids, 'post_tag' );
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'post', $post_id );
- $request->set_param( 'page', 1 );
- $request->set_param( 'per_page', 15 );
- $request->set_param( 'orderby', 'id' );
- $response = rest_get_server()->dispatch( $request );
- $tags = $response->get_data();
- $i = 0;
- foreach ( $tags as $tag ) {
- $this->assertSame( $tag['name'], "Tag {$i}" );
- $i++;
- }
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'post', $post_id );
- $request->set_param( 'page', 2 );
- $request->set_param( 'per_page', 15 );
- $request->set_param( 'orderby', 'id' );
- $response = rest_get_server()->dispatch( $request );
- $tags = $response->get_data();
- foreach ( $tags as $tag ) {
- $this->assertSame( $tag['name'], "Tag {$i}" );
- $i++;
- }
- }
- public function test_get_items_post_empty() {
- $post_id = $this->factory->post->create();
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'post', $post_id );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $this->assertCount( 0, $data );
- }
- public function test_get_items_custom_tax_post_args() {
- register_taxonomy( 'batman', 'post', array( 'show_in_rest' => true ) );
- $controller = new WP_REST_Terms_Controller( 'batman' );
- $controller->register_routes();
- $term1 = $this->factory->term->create(
- array(
- 'name' => 'Cape',
- 'taxonomy' => 'batman',
- )
- );
- $term2 = $this->factory->term->create(
- array(
- 'name' => 'Mask',
- 'taxonomy' => 'batman',
- )
- );
- $this->factory->term->create(
- array(
- 'name' => 'Car',
- 'taxonomy' => 'batman',
- )
- );
- $post_id = $this->factory->post->create();
- wp_set_object_terms( $post_id, array( $term1, $term2 ), 'batman' );
- $request = new WP_REST_Request( 'GET', '/wp/v2/batman' );
- $request->set_param( 'post', $post_id );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $this->assertSame( 2, count( $data ) );
- $this->assertSame( 'Cape', $data[0]['name'] );
- }
- public function test_get_items_search_args() {
- $tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) );
- $tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) );
- /*
- * Tests:
- * - search
- */
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'search', 'App' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $this->assertSame( 1, count( $data ) );
- $this->assertSame( 'Apple', $data[0]['name'] );
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'search', 'Garbage' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $this->assertSame( 0, count( $data ) );
- }
- public function test_get_items_slug_arg() {
- $tag1 = $this->factory->tag->create( array( 'name' => 'Apple' ) );
- $tag2 = $this->factory->tag->create( array( 'name' => 'Banana' ) );
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'slug', 'apple' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $this->assertSame( 1, count( $data ) );
- $this->assertSame( 'Apple', $data[0]['name'] );
- }
- public function test_get_items_slug_array_arg() {
- $id1 = $this->factory->tag->create( array( 'name' => 'Taco' ) );
- $id2 = $this->factory->tag->create( array( 'name' => 'Enchilada' ) );
- $id3 = $this->factory->tag->create( array( 'name' => 'Burrito' ) );
- $this->factory->tag->create( array( 'name' => 'Pizza' ) );
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param(
- 'slug',
- array(
- 'taco',
- 'burrito',
- 'enchilada',
- )
- );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $names = wp_list_pluck( $data, 'name' );
- sort( $names );
- $this->assertSame( array( 'Burrito', 'Enchilada', 'Taco' ), $names );
- }
- public function test_get_items_slug_csv_arg() {
- $id1 = $this->factory->tag->create( array( 'name' => 'Taco' ) );
- $id2 = $this->factory->tag->create( array( 'name' => 'Enchilada' ) );
- $id3 = $this->factory->tag->create( array( 'name' => 'Burrito' ) );
- $this->factory->tag->create( array( 'name' => 'Pizza' ) );
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'slug', 'taco,burrito, enchilada' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $names = wp_list_pluck( $data, 'name' );
- sort( $names );
- $this->assertSame( array( 'Burrito', 'Enchilada', 'Taco' ), $names );
- }
- public function test_get_terms_private_taxonomy() {
- register_taxonomy( 'robin', 'post', array( 'public' => false ) );
- $term1 = $this->factory->term->create(
- array(
- 'name' => 'Cape',
- 'taxonomy' => 'robin',
- )
- );
- $term2 = $this->factory->term->create(
- array(
- 'name' => 'Mask',
- 'taxonomy' => 'robin',
- )
- );
- $request = new WP_REST_Request( 'GET', '/wp/v2/terms/robin' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_no_route', $response, 404 );
- }
- public function test_get_terms_pagination_headers() {
- $total_tags = self::$total_tags;
- $total_pages = (int) ceil( $total_tags / 10 );
- // Start of the index.
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $response = rest_get_server()->dispatch( $request );
- $headers = $response->get_headers();
- $this->assertSame( $total_tags, $headers['X-WP-Total'] );
- $this->assertSame( $total_pages, $headers['X-WP-TotalPages'] );
- $next_link = add_query_arg(
- array(
- 'page' => 2,
- ),
- rest_url( 'wp/v2/tags' )
- );
- $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
- $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
- // 3rd page.
- $this->factory->tag->create();
- $total_tags++;
- $total_pages++;
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'page', 3 );
- $response = rest_get_server()->dispatch( $request );
- $headers = $response->get_headers();
- $this->assertSame( $total_tags, $headers['X-WP-Total'] );
- $this->assertSame( $total_pages, $headers['X-WP-TotalPages'] );
- $prev_link = add_query_arg(
- array(
- 'page' => 2,
- ),
- rest_url( 'wp/v2/tags' )
- );
- $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
- $next_link = add_query_arg(
- array(
- 'page' => 4,
- ),
- rest_url( 'wp/v2/tags' )
- );
- $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
- // Last page.
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'page', $total_pages );
- $response = rest_get_server()->dispatch( $request );
- $headers = $response->get_headers();
- $this->assertSame( $total_tags, $headers['X-WP-Total'] );
- $this->assertSame( $total_pages, $headers['X-WP-TotalPages'] );
- $prev_link = add_query_arg(
- array(
- 'page' => $total_pages - 1,
- ),
- rest_url( 'wp/v2/tags' )
- );
- $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
- $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
- // Out of bounds.
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'page', 100 );
- $response = rest_get_server()->dispatch( $request );
- $headers = $response->get_headers();
- $this->assertSame( $total_tags, $headers['X-WP-Total'] );
- $this->assertSame( $total_pages, $headers['X-WP-TotalPages'] );
- $prev_link = add_query_arg(
- array(
- 'page' => $total_pages,
- ),
- rest_url( 'wp/v2/tags' )
- );
- $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
- $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
- }
- public function test_get_items_invalid_context() {
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'context', 'banana' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
- }
- public function test_get_item() {
- $id = $this->factory->tag->create();
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id );
- $response = rest_get_server()->dispatch( $request );
- $this->check_get_taxonomy_term_response( $response, $id );
- }
- /**
- * @ticket 39122
- */
- public function test_get_item_meta() {
- $id = $this->factory->tag->create();
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- $this->assertArrayHasKey( 'meta', $data );
- $meta = (array) $data['meta'];
- $this->assertArrayHasKey( 'test_single', $meta );
- $this->assertSame( $meta['test_single'], '' );
- $this->assertArrayHasKey( 'test_multi', $meta );
- $this->assertSame( $meta['test_multi'], array() );
- $this->assertArrayHasKey( 'test_tag_single', $meta );
- $this->assertSame( $meta['test_tag_single'], '' );
- $this->assertArrayHasKey( 'test_tag_multi', $meta );
- $this->assertSame( $meta['test_tag_multi'], array() );
- }
- /**
- * @ticket 39122
- */
- public function test_get_item_meta_registered_for_different_taxonomy() {
- $id = $this->factory->tag->create();
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- $this->assertArrayHasKey( 'meta', $data );
- $meta = (array) $data['meta'];
- $this->assertFalse( isset( $meta['test_cat_meta'] ) );
- }
- public function test_get_term_invalid_term() {
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_term_invalid', $response, 404 );
- }
- public function test_get_item_invalid_permission_for_context() {
- $id = $this->factory->tag->create();
- wp_set_current_user( 0 );
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $id );
- $request->set_param( 'context', 'edit' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
- }
- public function test_get_term_private_taxonomy() {
- register_taxonomy( 'robin', 'post', array( 'public' => false ) );
- $term1 = $this->factory->term->create(
- array(
- 'name' => 'Cape',
- 'taxonomy' => 'robin',
- )
- );
- $request = new WP_REST_Request( 'GET', '/wp/v2/terms/robin/' . $term1 );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_no_route', $response, 404 );
- }
- public function test_get_item_incorrect_taxonomy() {
- register_taxonomy( 'robin', 'post' );
- $term1 = $this->factory->term->create(
- array(
- 'name' => 'Cape',
- 'taxonomy' => 'robin',
- )
- );
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $term1 );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_term_invalid', $response, 404 );
- }
- public function test_create_item() {
- wp_set_current_user( self::$administrator );
- $request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
- $request->set_param( 'name', 'My Awesome Term' );
- $request->set_param( 'description', 'This term is so awesome.' );
- $request->set_param( 'slug', 'so-awesome' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 201, $response->get_status() );
- $headers = $response->get_headers();
- $data = $response->get_data();
- $this->assertContains( '/wp/v2/tags/' . $data['id'], $headers['Location'] );
- $this->assertSame( 'My Awesome Term', $data['name'] );
- $this->assertSame( 'This term is so awesome.', $data['description'] );
- $this->assertSame( 'so-awesome', $data['slug'] );
- }
- public function test_create_item_contributor() {
- wp_set_current_user( self::$contributor );
- $request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
- $request->set_param( 'name', 'My Awesome Term' );
- $request->set_param( 'description', 'This term is so awesome.' );
- $request->set_param( 'slug', 'so-awesome' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 201, $response->get_status() );
- $headers = $response->get_headers();
- $data = $response->get_data();
- $this->assertContains( '/wp/v2/tags/' . $data['id'], $headers['Location'] );
- $this->assertSame( 'My Awesome Term', $data['name'] );
- $this->assertSame( 'This term is so awesome.', $data['description'] );
- $this->assertSame( 'so-awesome', $data['slug'] );
- }
- public function test_create_item_incorrect_permissions() {
- wp_set_current_user( self::$subscriber );
- $request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
- $request->set_param( 'name', 'Incorrect permissions' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_cannot_create', $response, 403 );
- }
- public function test_create_item_missing_arguments() {
- wp_set_current_user( self::$administrator );
- $request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_missing_callback_param', $response, 400 );
- }
- public function test_create_item_parent_non_hierarchical_taxonomy() {
- wp_set_current_user( self::$administrator );
- $request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
- $request->set_param( 'name', 'My Awesome Term' );
- $request->set_param( 'parent', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_taxonomy_not_hierarchical', $response, 400 );
- }
- public function test_create_item_with_meta() {
- wp_set_current_user( self::$administrator );
- $request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
- $request->set_param( 'name', 'My Awesome Term' );
- $request->set_param( 'meta', array( 'test_tag_single' => 'hello' ) );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 201, $response->get_status() );
- $headers = $response->get_headers();
- $data = $response->get_data();
- $this->assertContains( '/wp/v2/tags/' . $data['id'], $headers['Location'] );
- $this->assertSame( 'My Awesome Term', $data['name'] );
- $this->assertSame( 'hello', get_term_meta( $data['id'], 'test_tag_single', true ) );
- }
- public function test_create_item_with_meta_wrong_id() {
- wp_set_current_user( self::$administrator );
- $existing_tag_id = $this->factory->tag->create( array( 'name' => 'My Not So Awesome Term' ) );
- $request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
- $request->set_param( 'name', 'My Awesome Term' );
- $request->set_param( 'meta', array( 'test_tag_single' => 'hello' ) );
- $request->set_param( 'id', $existing_tag_id );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 201, $response->get_status() );
- $headers = $response->get_headers();
- $data = $response->get_data();
- $this->assertContains( '/wp/v2/tags/' . $data['id'], $headers['Location'] );
- $this->assertSame( 'My Awesome Term', $data['name'] );
- $this->assertSame( '', get_term_meta( $existing_tag_id, 'test_tag_single', true ) );
- $this->assertSame( 'hello', get_term_meta( $data['id'], 'test_tag_single', true ) );
- }
- public function test_update_item() {
- wp_set_current_user( self::$administrator );
- $orig_args = array(
- 'name' => 'Original Name',
- 'description' => 'Original Description',
- 'slug' => 'original-slug',
- );
- $term = get_term_by( 'id', $this->factory->tag->create( $orig_args ), 'post_tag' );
- $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id );
- $request->set_param( 'name', 'New Name' );
- $request->set_param( 'description', 'New Description' );
- $request->set_param( 'slug', 'new-slug' );
- $request->set_param(
- 'meta',
- array(
- 'test_single' => 'just meta',
- 'test_tag_single' => 'tag-specific meta',
- 'test_cat_meta' => 'category-specific meta',
- )
- );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $this->assertSame( 'New Name', $data['name'] );
- $this->assertSame( 'New Description', $data['description'] );
- $this->assertSame( 'new-slug', $data['slug'] );
- $this->assertSame( 'just meta', $data['meta']['test_single'] );
- $this->assertSame( 'tag-specific meta', $data['meta']['test_tag_single'] );
- $this->assertFalse( isset( $data['meta']['test_cat_meta'] ) );
- }
- public function test_update_item_no_change() {
- wp_set_current_user( self::$administrator );
- $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
- $request = new WP_REST_Request( 'PUT', '/wp/v2/tags/' . $term->term_id );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $request->set_param( 'slug', $term->slug );
- // Run twice to make sure that the update still succeeds
- // even if no DB rows are updated.
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- }
- public function test_update_item_invalid_term() {
- wp_set_current_user( self::$administrator );
- $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
- $request->set_param( 'name', 'Invalid Term' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_term_invalid', $response, 404 );
- }
- public function test_update_item_incorrect_permissions() {
- wp_set_current_user( self::$subscriber );
- $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
- $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id );
- $request->set_param( 'name', 'Incorrect permissions' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_cannot_update', $response, 403 );
- }
- /**
- * @ticket 38505
- */
- public function test_update_item_with_edit_term_cap_granted() {
- wp_set_current_user( self::$subscriber );
- $term = $this->factory->tag->create_and_get();
- $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id );
- $request->set_param( 'name', 'New Name' );
- add_filter( 'map_meta_cap', array( $this, 'grant_edit_term' ), 10, 2 );
- $response = rest_get_server()->dispatch( $request );
- remove_filter( 'user_has_cap', array( $this, 'grant_edit_term' ), 10, 2 );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $this->assertSame( 'New Name', $data['name'] );
- }
- public function grant_edit_term( $caps, $cap ) {
- if ( 'edit_term' === $cap ) {
- $caps = array( 'read' );
- }
- return $caps;
- }
- /**
- * @ticket 38505
- */
- public function test_update_item_with_edit_term_cap_revoked() {
- wp_set_current_user( self::$administrator );
- $term = $this->factory->tag->create_and_get();
- $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id );
- $request->set_param( 'name', 'New Name' );
- add_filter( 'map_meta_cap', array( $this, 'revoke_edit_term' ), 10, 2 );
- $response = rest_get_server()->dispatch( $request );
- remove_filter( 'user_has_cap', array( $this, 'revoke_edit_term' ), 10, 2 );
- $this->assertErrorResponse( 'rest_cannot_update', $response, 403 );
- }
- public function revoke_edit_term( $caps, $cap ) {
- if ( 'edit_term' === $cap ) {
- $caps = array( 'do_not_allow' );
- }
- return $caps;
- }
- public function test_update_item_parent_non_hierarchical_taxonomy() {
- wp_set_current_user( self::$administrator );
- $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
- $request = new WP_REST_Request( 'POST', '/wp/v2/tags/' . $term->term_id );
- $request->set_param( 'parent', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_taxonomy_not_hierarchical', $response, 400 );
- }
- public function verify_tag_roundtrip( $input = array(), $expected_output = array() ) {
- // Create the tag.
- $request = new WP_REST_Request( 'POST', '/wp/v2/tags' );
- foreach ( $input as $name => $value ) {
- $request->set_param( $name, $value );
- }
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 201, $response->get_status() );
- $actual_output = $response->get_data();
- // Compare expected API output to actual API output.
- $this->assertSame( $expected_output['name'], $actual_output['name'] );
- $this->assertSame( $expected_output['description'], $actual_output['description'] );
- // Compare expected API output to WP internal values.
- $tag = get_term_by( 'id', $actual_output['id'], 'post_tag' );
- $this->assertSame( $expected_output['name'], $tag->name );
- $this->assertSame( $expected_output['description'], $tag->description );
- // Update the tag.
- $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/tags/%d', $actual_output['id'] ) );
- foreach ( $input as $name => $value ) {
- $request->set_param( $name, $value );
- }
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $actual_output = $response->get_data();
- // Compare expected API output to actual API output.
- $this->assertSame( $expected_output['name'], $actual_output['name'] );
- $this->assertSame( $expected_output['description'], $actual_output['description'] );
- // Compare expected API output to WP internal values.
- $tag = get_term_by( 'id', $actual_output['id'], 'post_tag' );
- $this->assertSame( $expected_output['name'], $tag->name );
- $this->assertSame( $expected_output['description'], $tag->description );
- }
- public function test_tag_roundtrip_as_editor() {
- wp_set_current_user( self::$editor );
- $this->assertSame( ! is_multisite(), current_user_can( 'unfiltered_html' ) );
- $this->verify_tag_roundtrip(
- array(
- 'name' => '\o/ ¯\_(ツ)_/¯',
- 'description' => '\o/ ¯\_(ツ)_/¯',
- ),
- array(
- 'name' => '\o/ ¯\_(ツ)_/¯',
- 'description' => '\o/ ¯\_(ツ)_/¯',
- )
- );
- }
- public function test_tag_roundtrip_as_editor_html() {
- wp_set_current_user( self::$editor );
- if ( is_multisite() ) {
- $this->assertFalse( current_user_can( 'unfiltered_html' ) );
- $this->verify_tag_roundtrip(
- array(
- 'name' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
- 'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
- ),
- array(
- 'name' => 'div strong',
- 'description' => 'div <strong>strong</strong> oh noes',
- )
- );
- } else {
- $this->assertTrue( current_user_can( 'unfiltered_html' ) );
- $this->verify_tag_roundtrip(
- array(
- 'name' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
- 'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
- ),
- array(
- 'name' => 'div strong',
- 'description' => 'div <strong>strong</strong> oh noes',
- )
- );
- }
- }
- public function test_tag_roundtrip_as_superadmin() {
- wp_set_current_user( self::$superadmin );
- $this->assertTrue( current_user_can( 'unfiltered_html' ) );
- $this->verify_tag_roundtrip(
- array(
- 'name' => '\\\&\\\ & &invalid; < < &lt;',
- 'description' => '\\\&\\\ & &invalid; < < &lt;',
- ),
- array(
- 'name' => '\\\&\\\ & &invalid; < < &lt;',
- 'description' => '\\\&\\\ & &invalid; < < &lt;',
- )
- );
- }
- public function test_tag_roundtrip_as_superadmin_html() {
- wp_set_current_user( self::$superadmin );
- $this->assertTrue( current_user_can( 'unfiltered_html' ) );
- $this->verify_tag_roundtrip(
- array(
- 'name' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
- 'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
- ),
- array(
- 'name' => 'div strong',
- 'description' => 'div <strong>strong</strong> oh noes',
- )
- );
- }
- public function test_delete_item() {
- wp_set_current_user( self::$administrator );
- $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' );
- $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id );
- $request->set_param( 'force', true );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $this->assertTrue( $data['deleted'] );
- $this->assertSame( 'Deleted Tag', $data['previous']['name'] );
- }
- public function test_delete_item_no_trash() {
- wp_set_current_user( self::$administrator );
- $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' );
- $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
- $request->set_param( 'force', 'false' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 );
- }
- public function test_delete_item_invalid_term() {
- wp_set_current_user( self::$administrator );
- $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_term_invalid', $response, 404 );
- }
- public function test_delete_item_incorrect_permissions() {
- wp_set_current_user( self::$subscriber );
- $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
- $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
- }
- /**
- * @ticket 38505
- */
- public function test_delete_item_with_delete_term_cap_granted() {
- wp_set_current_user( self::$subscriber );
- $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' );
- $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id );
- $request->set_param( 'force', true );
- add_filter( 'map_meta_cap', array( $this, 'grant_delete_term' ), 10, 2 );
- $response = rest_get_server()->dispatch( $request );
- remove_filter( 'map_meta_cap', array( $this, 'grant_delete_term' ), 10, 2 );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $this->assertTrue( $data['deleted'] );
- $this->assertSame( 'Deleted Tag', $data['previous']['name'] );
- }
- public function grant_delete_term( $caps, $cap ) {
- if ( 'delete_term' === $cap ) {
- $caps = array( 'read' );
- }
- return $caps;
- }
- /**
- * @ticket 38505
- */
- public function test_delete_item_with_delete_term_cap_revoked() {
- wp_set_current_user( self::$administrator );
- $term = get_term_by( 'id', $this->factory->tag->create( array( 'name' => 'Deleted Tag' ) ), 'post_tag' );
- $request = new WP_REST_Request( 'DELETE', '/wp/v2/tags/' . $term->term_id );
- $request->set_param( 'force', true );
- add_filter( 'map_meta_cap', array( $this, 'revoke_delete_term' ), 10, 2 );
- $response = rest_get_server()->dispatch( $request );
- remove_filter( 'map_meta_cap', array( $this, 'revoke_delete_term' ), 10, 2 );
- $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
- }
- public function revoke_delete_term( $caps, $cap ) {
- if ( 'delete_term' === $cap ) {
- $caps = array( 'do_not_allow' );
- }
- return $caps;
- }
- public function test_prepare_item() {
- $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $term->term_id );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- $this->check_taxonomy_term( $term, $data, $response->get_links() );
- }
- public function test_prepare_item_limit_fields() {
- $request = new WP_REST_Request;
- $endpoint = new WP_REST_Terms_Controller( 'post_tag' );
- $request->set_param( '_fields', 'id,name' );
- $term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
- $response = $endpoint->prepare_item_for_response( $term, $request );
- $this->assertSame(
- array(
- 'id',
- 'name',
- ),
- array_keys( $response->get_data() )
- );
- }
- public function test_get_item_schema() {
- $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/tags' );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- $properties = $data['schema']['properties'];
- $this->assertSame( 8, count( $properties ) );
- $this->assertArrayHasKey( 'id', $properties );
- $this->assertArrayHasKey( 'count', $properties );
- $this->assertArrayHasKey( 'description', $properties );
- $this->assertArrayHasKey( 'link', $properties );
- $this->assertArrayHasKey( 'meta', $properties );
- $this->assertArrayHasKey( 'name', $properties );
- $this->assertArrayHasKey( 'slug', $properties );
- $this->assertArrayHasKey( 'taxonomy', $properties );
- $this->assertSame( array( 'post_tag' ), $properties['taxonomy']['enum'] );
- }
- public function test_get_item_schema_non_hierarchical() {
- $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/tags' );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- $properties = $data['schema']['properties'];
- $this->assertArrayHasKey( 'id', $properties );
- $this->assertFalse( isset( $properties['parent'] ) );
- }
- public function test_get_additional_field_registration() {
- $schema = array(
- 'type' => 'integer',
- 'description' => 'Some integer of mine',
- 'enum' => array( 1, 2, 3, 4 ),
- 'context' => array( 'view', 'edit' ),
- );
- register_rest_field(
- 'tag',
- 'my_custom_int',
- array(
- 'schema' => $schema,
- 'get_callback' => array( $this, 'additional_field_get_callback' ),
- )
- );
- $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/tags' );
- $response = rest_get_server()->dispatch( $request );
- $data = $response->get_data();
- $this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] );
- $this->assertSame( $schema, $data['schema']['properties']['my_custom_int'] );
- $tag_id = $this->factory->tag->create();
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags/' . $tag_id );
- $response = rest_get_server()->dispatch( $request );
- $this->assertArrayHasKey( 'my_custom_int', $response->data );
- global $wp_rest_additional_fields;
- $wp_rest_additional_fields = array();
- }
- public function test_additional_field_update_errors() {
- $schema = array(
- 'type' => 'integer',
- 'description' => 'Some integer of mine',
- 'enum' => array( 1, 2, 3, 4 ),
- 'context' => array( 'view', 'edit' ),
- );
- register_rest_field(
- 'tag',
- 'my_custom_int',
- array(
- 'schema' => $schema,
- 'get_callback' => array( $this, 'additional_field_get_callback' ),
- 'update_callback' => array( $this, 'additional_field_update_callback' ),
- )
- );
- wp_set_current_user( self::$administrator );
- $tag_id = $this->factory->tag->create();
- // Check for error on update.
- $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/tags/%d', $tag_id ) );
- $request->set_body_params(
- array(
- 'my_custom_int' => 'returnError',
- )
- );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
- global $wp_rest_additional_fields;
- $wp_rest_additional_fields = array();
- }
- /**
- * @ticket 38504
- */
- public function test_object_term_queries_are_cached() {
- global $wpdb;
- $tags = $this->factory->tag->create_many( 2 );
- $p = $this->factory->post->create();
- wp_set_object_terms( $p, $tags[0], 'post_tag' );
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'post', $p );
- $response = rest_get_server()->dispatch( $request );
- $found_1 = wp_list_pluck( $response->data, 'id' );
- unset( $request, $response );
- $num_queries = $wpdb->num_queries;
- $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
- $request->set_param( 'post', $p );
- $response = rest_get_server()->dispatch( $request );
- $found_2 = wp_list_pluck( $response->data, 'id' );
- $this->assertSameSets( $found_1, $found_2 );
- $this->assertSame( $num_queries, $wpdb->num_queries );
- }
- /**
- * @ticket 41411
- */
- public function test_editable_response_uses_edit_context() {
- wp_set_current_user( self::$administrator );
- $view_field = 'view_only_field';
- $edit_field = 'edit_only_field';
- register_rest_field(
- 'tag',
- $view_field,
- array(
- 'context' => array( 'view' ),
- 'get_callback' => '__return_empty_string',
- )
- );
- register_rest_field(
- 'tag',
- $edit_field,
- array(
- 'context' => array( 'edit' ),
- 'get_callback' => '__return_empty_string',
- )
- );
- $create = new WP_REST_Request( 'POST', '/wp/v2/tags' );
- $create->set_param( 'name', 'My New Term' );
- $response = rest_get_server()->dispatch( $create );
- $this->assertSame( 201, $response->get_status() );
- $data = $response->get_data();
- $this->assertArrayHasKey( $edit_field, $data );
- $this->assertArrayNotHasKey( $view_field, $data );
- $update = new WP_REST_Request( 'PUT', '/wp/v2/tags/' . $data['id'] );
- $update->set_param( 'name', 'My Awesome New Term' );
- $response = rest_get_server()->dispatch( $update );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $this->assertArrayHasKey( $edit_field, $data );
- $this->assertArrayNotHasKey( $view_field, $data );
- }
- public function additional_field_get_callback( $object, $request ) {
- return 123;
- }
- public function additional_field_update_callback( $value, $tag ) {
- if ( 'returnError' === $value ) {
- return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
- }
- }
- protected function check_get_taxonomy_terms_response( $response ) {
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $args = array(
- 'hide_empty' => false,
- );
- $tags = get_terms( 'post_tag', $args );
- $this->assertSame( count( $tags ), count( $data ) );
- $this->assertSame( $tags[0]->term_id, $data[0]['id'] );
- $this->assertSame( $tags[0]->name, $data[0]['name'] );
- $this->assertSame( $tags[0]->slug, $data[0]['slug'] );
- $this->assertSame( $tags[0]->taxonomy, $data[0]['taxonomy'] );
- $this->assertSame( $tags[0]->description, $data[0]['description'] );
- $this->assertSame( $tags[0]->count, $data[0]['count'] );
- }
- protected function check_taxonomy_term( $term, $data, $links ) {
- $this->assertSame( $term->term_id, $data['id'] );
- $this->assertSame( $term->name, $data['name'] );
- $this->assertSame( $term->slug, $data['slug'] );
- $this->assertSame( $term->description, $data['description'] );
- $this->assertSame( get_term_link( $term ), $data['link'] );
- $this->assertSame( $term->count, $data['count'] );
- $taxonomy = get_taxonomy( $term->taxonomy );
- if ( $taxonomy->hierarchical ) {
- $this->assertSame( $term->parent, $data['parent'] );
- } else {
- $this->assertFalse( isset( $data['parent'] ) );
- }
- $expected_links = array(
- 'self',
- 'collection',
- 'about',
- 'https://api.w.org/post_type',
- );
- if ( $taxonomy->hierarchical && $term->parent ) {
- $expected_links[] = 'up';
- }
- $this->assertSameSets( $expected_links, array_keys( $links ) );
- $this->assertContains( 'wp/v2/taxonomies/' . $term->taxonomy, $links['about'][0]['href'] );
- $this->assertSame( add_query_arg( 'tags', $term->term_id, rest_url( 'wp/v2/posts' ) ), $links['https://api.w.org/post_type'][0]['href'] );
- }
- protected function check_get_taxonomy_term_response( $response, $id ) {
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $tag = get_term( $id, 'post_tag' );
- $this->check_taxonomy_term( $tag, $data, $response->get_links() );
- }
- }
|