| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293 |
- <?php
- /**
- * Unit tests covering WP_REST_Themes_Controller functionality.
- *
- * @package WordPress
- * @subpackage REST API
- */
- /**
- * @group restapi-themes
- * @group restapi
- */
- class WP_Test_REST_Themes_Controller extends WP_Test_REST_Controller_Testcase {
- /**
- * Subscriber user ID.
- *
- * @since 5.0.0
- *
- * @var int $subscriber_id
- */
- protected static $subscriber_id;
- /**
- * Contributor user ID.
- *
- * @since 5.0.0
- *
- * @var int $contributor_id
- */
- protected static $contributor_id;
- /**
- * Admin user ID.
- *
- * @since 5.7.0
- *
- * @var int $admin_id
- */
- protected static $admin_id;
- /**
- * The current theme object.
- *
- * @since 5.0.0
- *
- * @var WP_Theme $current_theme
- */
- protected static $current_theme;
- /**
- * The REST API route for themes.
- *
- * @since 5.0.0
- *
- * @var string $themes_route
- */
- protected static $themes_route = '/wp/v2/themes';
- /**
- * Performs a REST API request for the active theme.
- *
- * @since 5.0.0
- *
- * @param string $method Optional. Request method. Default GET.
- * @return WP_REST_Response The request's response.
- */
- protected function perform_active_theme_request( $method = 'GET' ) {
- $request = new WP_REST_Request( $method, self::$themes_route );
- $request->set_param( 'status', 'active' );
- return rest_get_server()->dispatch( $request );
- }
- /**
- * Check that common properties are included in a response.
- *
- * @since 5.0.0
- *
- * @param WP_REST_Response $response Current REST API response.
- */
- protected function check_get_theme_response( $response ) {
- if ( $response instanceof WP_REST_Response ) {
- $headers = $response->get_headers();
- $response = $response->get_data();
- } else {
- $headers = array();
- }
- $this->assertArrayHasKey( 'X-WP-Total', $headers );
- $this->assertSame( 1, $headers['X-WP-Total'] );
- $this->assertArrayHasKey( 'X-WP-TotalPages', $headers );
- $this->assertSame( 1, $headers['X-WP-TotalPages'] );
- }
- /**
- * Set up class test fixtures.
- *
- * @since 5.0.0
- *
- * @param WP_UnitTest_Factory $factory WordPress unit test factory.
- */
- public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
- self::$admin_id = $factory->user->create(
- array(
- 'role' => 'administrator',
- )
- );
- self::$subscriber_id = $factory->user->create(
- array(
- 'role' => 'subscriber',
- )
- );
- self::$contributor_id = $factory->user->create(
- array(
- 'role' => 'contributor',
- )
- );
- self::$current_theme = wp_get_theme();
- wp_set_current_user( self::$contributor_id );
- }
- /**
- * Clean up test fixtures.
- *
- * @since 5.0.0
- */
- public static function wpTearDownAfterClass() {
- self::delete_user( self::$subscriber_id );
- self::delete_user( self::$contributor_id );
- self::delete_user( self::$admin_id );
- }
- /**
- * Set up each test method.
- *
- * @since 5.0.0
- */
- public function setUp() {
- parent::setUp();
- wp_set_current_user( self::$contributor_id );
- switch_theme( 'rest-api' );
- }
- /**
- * Theme routes should be registered correctly.
- *
- * @ticket 45016
- */
- public function test_register_routes() {
- $routes = rest_get_server()->get_routes();
- $this->assertArrayHasKey( self::$themes_route, $routes );
- $this->assertArrayHasKey( self::$themes_route . '/(?P<stylesheet>[\\w-]+)', $routes );
- }
- /**
- * Test retrieving a collection of themes.
- *
- * @ticket 45016
- */
- public function test_get_items() {
- $response = self::perform_active_theme_request();
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $this->check_get_theme_response( $response );
- $fields = array(
- '_links',
- 'author',
- 'author_uri',
- 'description',
- 'name',
- 'requires_php',
- 'requires_wp',
- 'screenshot',
- 'status',
- 'stylesheet',
- 'tags',
- 'template',
- 'textdomain',
- 'theme_supports',
- 'theme_uri',
- 'version',
- );
- $this->assertSameSets( $fields, array_keys( $data[0] ) );
- }
- /**
- * Test retrieving a collection of inactive themes.
- *
- * @ticket 50152
- */
- public function test_get_items_inactive() {
- wp_set_current_user( self::$admin_id );
- $request = new WP_REST_Request( 'GET', self::$themes_route );
- $request->set_param( 'status', 'inactive' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $fields = array(
- '_links',
- 'author',
- 'author_uri',
- 'description',
- 'name',
- 'requires_php',
- 'requires_wp',
- 'screenshot',
- 'status',
- 'stylesheet',
- 'tags',
- 'template',
- 'textdomain',
- 'theme_uri',
- 'version',
- );
- $this->assertEqualSets( $fields, array_keys( $data[0] ) );
- $this->assertContains( 'twentytwenty', wp_list_pluck( $data, 'stylesheet' ) );
- $this->assertNotContains( get_stylesheet(), wp_list_pluck( $data, 'stylesheet' ) );
- }
- /**
- * Test retrieving a collection of inactive themes.
- *
- * @ticket 50152
- */
- public function test_get_items_active_and_inactive() {
- wp_set_current_user( self::$admin_id );
- $request = new WP_REST_Request( 'GET', self::$themes_route );
- $request->set_param( 'status', array( 'active', 'inactive' ) );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $this->assertContains( 'twentytwenty', wp_list_pluck( $data, 'stylesheet' ) );
- $this->assertContains( get_stylesheet(), wp_list_pluck( $data, 'stylesheet' ) );
- }
- /**
- * @ticket 46723
- * @ticket 50152
- * @dataProvider data_get_items_by_status
- */
- public function test_get_items_logged_out( $status, $error_code ) {
- wp_set_current_user( 0 );
- $request = new WP_REST_Request( 'GET', self::$themes_route );
- $request->set_param( 'status', $status );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( $error_code, $response, 401 );
- }
- /**
- * An error should be returned when the user does not have the edit_posts capability.
- *
- * @ticket 45016
- * @ticket 50152
- * @dataProvider data_get_items_by_status
- */
- public function test_get_items_no_permission( $status, $error_code ) {
- wp_set_current_user( self::$subscriber_id );
- $request = new WP_REST_Request( 'GET', self::$themes_route );
- $request->set_param( 'status', $status );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( $error_code, $response, 403 );
- }
- public function data_get_items_by_status() {
- return array(
- array( 'active', 'rest_cannot_view_active_theme' ),
- array( 'active, inactive', 'rest_cannot_view_themes' ),
- array( 'inactive', 'rest_cannot_view_themes' ),
- array( '', 'rest_cannot_view_themes' ),
- );
- }
- /**
- * @ticket 50152
- * @dataProvider data_get_items_by_status_for_contributor
- */
- public function test_get_items_contributor( $status, $error_code ) {
- wp_set_current_user( self::$contributor_id );
- $request = new WP_REST_Request( 'GET', self::$themes_route );
- $request->set_param( 'status', $status );
- $response = rest_get_server()->dispatch( $request );
- if ( $error_code ) {
- $this->assertErrorResponse( $error_code, $response, 403 );
- } else {
- $this->assertSame( 200, $response->get_status() );
- }
- }
- public function data_get_items_by_status_for_contributor() {
- return array(
- array( 'active', '' ),
- array( 'active, inactive', 'rest_cannot_view_themes' ),
- array( 'inactive', 'rest_cannot_view_themes' ),
- array( '', 'rest_cannot_view_themes' ),
- );
- }
- /**
- * @ticket 46723
- */
- public function test_get_item_single_post_type_cap() {
- $user = self::factory()->user->create_and_get();
- $user->add_cap( 'edit_pages' );
- wp_set_current_user( $user->ID );
- $response = self::perform_active_theme_request();
- $this->assertSame( 200, $response->get_status() );
- }
- /**
- * Test an item is prepared for the response.
- *
- * @ticket 45016
- */
- public function test_prepare_item() {
- $response = self::perform_active_theme_request();
- $this->assertSame( 200, $response->get_status() );
- $this->check_get_theme_response( $response );
- }
- /**
- * Verify the theme schema.
- *
- * @ticket 45016
- */
- public function test_get_item_schema() {
- $response = self::perform_active_theme_request( 'OPTIONS' );
- $data = $response->get_data();
- $properties = $data['schema']['properties'];
- $this->assertSame( 15, count( $properties ) );
- $this->assertArrayHasKey( 'author', $properties );
- $this->assertArrayHasKey( 'raw', $properties['author']['properties'] );
- $this->assertArrayHasKey( 'rendered', $properties['author']['properties'] );
- $this->assertArrayHasKey( 'author_uri', $properties );
- $this->assertArrayHasKey( 'raw', $properties['author_uri']['properties'] );
- $this->assertArrayHasKey( 'rendered', $properties['author_uri']['properties'] );
- $this->assertArrayHasKey( 'description', $properties );
- $this->assertArrayHasKey( 'raw', $properties['description']['properties'] );
- $this->assertArrayHasKey( 'rendered', $properties['description']['properties'] );
- $this->assertArrayHasKey( 'name', $properties );
- $this->assertArrayHasKey( 'raw', $properties['name']['properties'] );
- $this->assertArrayHasKey( 'rendered', $properties['name']['properties'] );
- $this->assertArrayHasKey( 'requires_php', $properties );
- $this->assertArrayHasKey( 'requires_wp', $properties );
- $this->assertArrayHasKey( 'screenshot', $properties );
- $this->assertArrayHasKey( 'status', $properties );
- $this->assertArrayHasKey( 'stylesheet', $properties );
- $this->assertArrayHasKey( 'tags', $properties );
- $this->assertArrayHasKey( 'raw', $properties['tags']['properties'] );
- $this->assertArrayHasKey( 'items', $properties['tags']['properties']['raw'] );
- $this->assertArrayHasKey( 'rendered', $properties['tags']['properties'] );
- $this->assertArrayHasKey( 'template', $properties );
- $this->assertArrayHasKey( 'textdomain', $properties );
- $this->assertArrayHasKey( 'theme_supports', $properties );
- $this->assertArrayHasKey( 'theme_uri', $properties );
- $this->assertArrayHasKey( 'raw', $properties['theme_uri']['properties'] );
- $this->assertArrayHasKey( 'rendered', $properties['theme_uri']['properties'] );
- $this->assertArrayHasKey( 'version', $properties );
- $theme_supports = $properties['theme_supports']['properties'];
- $this->assertArrayHasKey( 'align-wide', $theme_supports );
- $this->assertArrayHasKey( 'automatic-feed-links', $theme_supports );
- $this->assertArrayHasKey( 'custom-header', $theme_supports );
- $this->assertArrayHasKey( 'custom-background', $theme_supports );
- $this->assertArrayHasKey( 'custom-logo', $theme_supports );
- $this->assertArrayHasKey( 'customize-selective-refresh-widgets', $theme_supports );
- $this->assertArrayHasKey( 'title-tag', $theme_supports );
- $this->assertArrayHasKey( 'dark-editor-style', $theme_supports );
- $this->assertArrayHasKey( 'disable-custom-font-sizes', $theme_supports );
- $this->assertArrayHasKey( 'disable-custom-gradients', $theme_supports );
- $this->assertArrayHasKey( 'editor-color-palette', $theme_supports );
- $this->assertArrayHasKey( 'editor-font-sizes', $theme_supports );
- $this->assertArrayHasKey( 'editor-gradient-presets', $theme_supports );
- $this->assertArrayHasKey( 'editor-styles', $theme_supports );
- $this->assertArrayHasKey( 'formats', $theme_supports );
- $this->assertArrayHasKey( 'html5', $theme_supports );
- $this->assertArrayHasKey( 'post-thumbnails', $theme_supports );
- $this->assertArrayHasKey( 'responsive-embeds', $theme_supports );
- $this->assertArrayHasKey( 'title-tag', $theme_supports );
- $this->assertArrayHasKey( 'wp-block-styles', $theme_supports );
- $this->assertCount( 20, $theme_supports );
- }
- /**
- * @ticket 49906
- */
- public function test_theme_author() {
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'author', $result[0] );
- $this->assertSame( 'Michael Heilemann', $result[0]['author']['raw'] );
- $this->assertSame(
- '<a href="http://binarybonsai.com/?search=1&term=2">Michael Heilemann</a>',
- $result[0]['author']['rendered']
- );
- }
- /**
- * @ticket 49906
- */
- public function test_theme_author_uri() {
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'author_uri', $result[0] );
- $this->assertSame( 'http://binarybonsai.com/?search=1&term=2', $result[0]['author_uri']['raw'] );
- $this->assertSame( 'http://binarybonsai.com/?search=1&term=2', $result[0]['author_uri']['rendered'] );
- }
- /**
- * @ticket 49906
- */
- public function test_theme_description() {
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'description', $result[0] );
- $this->assertSame(
- 'The 9\' foot tall theme.',
- $result[0]['description']['raw']
- );
- $this->assertSame(
- 'The 9′ foot tall theme.',
- $result[0]['description']['rendered']
- );
- }
- /**
- * @ticket 49906
- */
- public function test_theme_requires_php() {
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'requires_php', $result[0] );
- $this->assertSame( '5.6', $result[0]['requires_php'] );
- }
- /**
- * @ticket 49906
- */
- public function test_theme_requires_wp() {
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'requires_wp', $result[0] );
- $this->assertSame( '5.3', $result[0]['requires_wp'] );
- }
- /**
- * @ticket 49906
- */
- public function test_theme_name() {
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'name', $result[0] );
- $this->assertSame( 'REST Theme', $result[0]['name']['raw'] );
- $this->assertSame( 'REST Theme', $result[0]['name']['rendered'] );
- }
- /**
- * @ticket 49906
- */
- public function test_theme_screenshot() {
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'screenshot', $result[0] );
- $this->assertSame( '', $result[0]['screenshot'] ); // No screenshot for default theme
- }
- /**
- * @ticket 49906
- */
- public function test_theme_stylesheet() {
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'stylesheet', $result[0] );
- $this->assertSame( 'rest-api', $result[0]['stylesheet'] );
- }
- /**
- * @ticket 49906
- */
- public function test_theme_tags() {
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'tags', $result[0] );
- $this->assertSame( array( 'holiday', 'custom-menu' ), $result[0]['tags']['raw'] );
- $this->assertSame( 'holiday, custom-menu', $result[0]['tags']['rendered'] );
- }
- /**
- * @ticket 49906
- */
- public function test_theme_template() {
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'template', $result[0] );
- $this->assertSame( 'default', $result[0]['template'] );
- }
- /**
- * @ticket 49906
- */
- public function test_theme_textdomain() {
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'textdomain', $result[0] );
- $this->assertSame( 'rest-api', $result[0]['textdomain'] );
- }
- public function test_theme_theme_uri() {
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_uri', $result[0] );
- $this->assertSame( 'http://wordpress.org/?search=1&term=2', $result[0]['theme_uri']['raw'] );
- $this->assertSame( 'http://wordpress.org/?search=1&term=2', $result[0]['theme_uri']['rendered'] );
- }
- /**
- * @ticket 49906
- */
- public function test_theme_version() {
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'version', $result[0] );
- $this->assertSame( '1.6', $result[0]['version'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_supports_disable_custom_colors_false() {
- remove_theme_support( 'disable-custom-colors' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertArrayHasKey( 'disable-custom-colors', $result[0]['theme_supports'] );
- $this->assertFalse( $result[0]['theme_supports']['disable-custom-colors'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_supports_disable_custom_colors_true() {
- remove_theme_support( 'disable-custom-colors' );
- add_theme_support( 'disable-custom-colors' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertTrue( $result[0]['theme_supports']['disable-custom-colors'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_supports_disable_custom_font_sizes_false() {
- remove_theme_support( 'disable-custom-font-sizes' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertArrayHasKey( 'disable-custom-font-sizes', $result[0]['theme_supports'] );
- $this->assertFalse( $result[0]['theme_supports']['disable-custom-font-sizes'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_supports_disable_custom_font_sizes_true() {
- remove_theme_support( 'disable-custom-font-sizes' );
- add_theme_support( 'disable-custom-font-sizes' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertTrue( $result[0]['theme_supports']['disable-custom-font-sizes'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_supports_editor_font_sizes_false() {
- remove_theme_support( 'editor-font-sizes' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertArrayHasKey( 'editor-font-sizes', $result[0]['theme_supports'] );
- $this->assertFalse( $result[0]['theme_supports']['editor-font-sizes'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_supports_editor_font_sizes_array() {
- remove_theme_support( 'editor-font-sizes' );
- $tiny = array(
- 'name' => 'Tiny',
- 'size' => 8,
- 'slug' => 'tiny',
- );
- add_theme_support( 'editor-font-sizes', array( $tiny ) );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertArrayHasKey( 'editor-font-sizes', $result[0]['theme_supports'] );
- $this->assertEquals( array( $tiny ), $result[0]['theme_supports']['editor-font-sizes'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_supports_editor_color_palette_false() {
- remove_theme_support( 'editor-color-palette' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertArrayHasKey( 'editor-color-palette', $result[0]['theme_supports'] );
- $this->assertFalse( $result[0]['theme_supports']['editor-color-palette'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_supports_editor_color_palette_array() {
- remove_theme_support( 'editor-color-palette' );
- $wordpress_blue = array(
- 'name' => 'WordPress Blue',
- 'slug' => 'wordpress-blue',
- 'color' => '#0073AA',
- );
- add_theme_support( 'editor-color-palette', array( $wordpress_blue ) );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertSame( array( $wordpress_blue ), $result[0]['theme_supports']['editor-color-palette'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_supports_enable_automatic_feed_links() {
- remove_theme_support( 'automatic-feed-links' );
- add_theme_support( 'automatic-feed-links' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertTrue( $result[0]['theme_supports']['automatic-feed-links'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_supports_does_not_enable_automatic_feed_links() {
- remove_theme_support( 'automatic-feed-links' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertArrayHasKey( 'automatic-feed-links', $result[0]['theme_supports'] );
- $this->assertFalse( $result[0]['theme_supports']['automatic-feed-links'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_does_not_support_custom_logo() {
- remove_theme_support( 'custom-logo' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertArrayHasKey( 'custom-logo', $result[0]['theme_supports'] );
- $this->assertFalse( $result[0]['theme_supports']['custom-logo'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_supports_custom_logo() {
- remove_theme_support( 'custom-logo' );
- $wordpress_logo = array(
- 'width' => 400,
- 'height' => 100,
- 'flex-width' => true,
- 'flex-height' => true,
- 'header-text' => array( 'site-title', 'site-description' ),
- 'unlink-homepage-logo' => false,
- );
- add_theme_support( 'custom-logo', $wordpress_logo );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertSame( $wordpress_logo, $result[0]['theme_supports']['custom-logo'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_does_not_support_custom_header() {
- remove_theme_support( 'custom-header' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertArrayHasKey( 'custom-header', $result[0]['theme_supports'] );
- $this->assertFalse( $result[0]['theme_supports']['custom-header'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_supports_custom_header() {
- remove_theme_support( 'custom-header' );
- $wordpress_header = array(
- 'default-image' => '',
- 'random-default' => false,
- 'width' => 0,
- 'height' => 0,
- 'flex-height' => false,
- 'flex-width' => false,
- 'default-text-color' => '',
- 'header-text' => true,
- 'uploads' => true,
- 'wp-head-callback' => '',
- 'admin-head-callback' => '',
- 'admin-preview-callback' => '',
- 'video' => false,
- 'video-active-callback' => 'is_front_page',
- );
- $excluded = array(
- 'wp-head-callback',
- 'admin-head-callback',
- 'admin-preview-callback',
- 'video-active-callback',
- );
- add_theme_support( 'custom-header', $wordpress_header );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $expected = array_diff_key( $wordpress_header, array_flip( $excluded ) );
- $this->assertSame( $expected, $result[0]['theme_supports']['custom-header'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_does_not_support_custom_background() {
- remove_theme_support( 'custom-background' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertArrayHasKey( 'custom-background', $result[0]['theme_supports'] );
- $this->assertFalse( $result[0]['theme_supports']['custom-background'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_supports_custom_background() {
- remove_theme_support( 'custom-background' );
- $background = array(
- 'default-image' => '',
- 'default-preset' => 'default',
- 'default-position-x' => 'left',
- 'default-position-y' => 'top',
- 'default-size' => 'auto',
- 'default-repeat' => 'repeat',
- 'default-attachment' => 'scroll',
- 'default-color' => '',
- 'wp-head-callback' => '_custom_background_cb',
- 'admin-head-callback' => '',
- 'admin-preview-callback' => '',
- );
- $excluded = array(
- 'wp-head-callback',
- 'admin-head-callback',
- 'admin-preview-callback',
- );
- add_theme_support( 'custom-background', $background );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $expected = array_diff_key( $background, array_flip( $excluded ) );
- $this->assertSame( $expected, $result[0]['theme_supports']['custom-background'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_does_not_support_html5() {
- remove_theme_support( 'html5' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertArrayHasKey( 'html5', $result[0]['theme_supports'] );
- $this->assertFalse( $result[0]['theme_supports']['html5'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_supports_html5() {
- remove_theme_support( 'html5' );
- $html5 = array(
- 'search-form',
- 'comment-form',
- 'comment-list',
- 'gallery',
- 'caption',
- 'script',
- 'style',
- );
- add_theme_support( 'html5', $html5 );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertSame( $html5, $result[0]['theme_supports']['html5'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_cannot_manage_title_tag() {
- remove_theme_support( 'title-tag' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertArrayHasKey( 'title-tag', $result[0]['theme_supports'] );
- $this->assertFalse( $result[0]['theme_supports']['title-tag'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_can_manage_title_tag() {
- global $_wp_theme_features;
- $_wp_theme_features['title-tag'] = true;
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertTrue( $result[0]['theme_supports']['title-tag'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_cannot_manage_selective_refresh_for_widgets() {
- remove_theme_support( 'customize-selective-refresh-widgets' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertArrayHasKey( 'customize-selective-refresh-widgets', $result[0]['theme_supports'] );
- $this->assertFalse( $result[0]['theme_supports']['customize-selective-refresh-widgets'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_can_manage_selective_refresh_for_widgets() {
- remove_theme_support( 'customize-selective-refresh-widgets' );
- add_theme_support( 'customize-selective-refresh-widgets' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertTrue( $result[0]['theme_supports']['customize-selective-refresh-widgets'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_no_wp_block_styles() {
- remove_theme_support( 'wp-block-styles' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertArrayHasKey( 'wp-block-styles', $result[0]['theme_supports'] );
- $this->assertFalse( $result[0]['theme_supports']['wp-block-styles'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_wp_block_styles_optin() {
- remove_theme_support( 'wp-block-styles' );
- add_theme_support( 'wp-block-styles' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertTrue( $result[0]['theme_supports']['wp-block-styles'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_no_align_wide() {
- remove_theme_support( 'align-wide' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertArrayHasKey( 'align-wide', $result[0]['theme_supports'] );
- $this->assertFalse( $result[0]['theme_supports']['align-wide'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_align_wide_optin() {
- remove_theme_support( 'align-wide' );
- add_theme_support( 'align-wide' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertTrue( $result[0]['theme_supports']['align-wide'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_no_editor_styles() {
- remove_theme_support( 'editor-styles' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertArrayHasKey( 'editor-styles', $result[0]['theme_supports'] );
- $this->assertFalse( $result[0]['theme_supports']['editor-styles'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_editor_styles_optin() {
- remove_theme_support( 'editor-styles' );
- add_theme_support( 'editor-styles' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertTrue( $result[0]['theme_supports']['editor-styles'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_no_dark_editor_style() {
- remove_theme_support( 'dark-editor-style' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertArrayHasKey( 'dark-editor-style', $result[0]['theme_supports'] );
- $this->assertFalse( $result[0]['theme_supports']['dark-editor-style'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_dark_editor_style_optin() {
- remove_theme_support( 'dark-editor-style' );
- add_theme_support( 'dark-editor-style' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertTrue( $result[0]['theme_supports']['dark-editor-style'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_no_disable_custom_gradients() {
- remove_theme_support( 'disable-custom-gradients' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertArrayHasKey( 'disable-custom-gradients', $result[0]['theme_supports'] );
- $this->assertFalse( $result[0]['theme_supports']['disable-custom-gradients'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_disable_custom_gradients() {
- remove_theme_support( 'disable-custom-gradients' );
- add_theme_support( 'disable-custom-gradients' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertTrue( $result[0]['theme_supports']['disable-custom-gradients'] );
- }
- /**
- * @ticket 49037
- */
- public function test_theme_supports_editor_gradient_presets_array() {
- remove_theme_support( 'editor-gradient-presets' );
- $gradient = array(
- 'name' => __( 'Vivid cyan blue to vivid purple', 'themeLangDomain' ),
- 'gradient' => 'linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)',
- 'slug' => 'vivid-cyan-blue-to-vivid-purple',
- );
- add_theme_support( 'editor-gradient-presets', array( $gradient ) );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertArrayHasKey( 'theme_supports', $result[0] );
- $this->assertSame( array( $gradient ), $result[0]['theme_supports']['editor-gradient-presets'] );
- }
- /**
- * Should include relevant data in the 'theme_supports' key.
- *
- * @ticket 45016
- */
- public function test_theme_supports_formats() {
- remove_theme_support( 'post-formats' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertTrue( isset( $result[0]['theme_supports'] ) );
- $this->assertTrue( isset( $result[0]['theme_supports']['formats'] ) );
- $this->assertSame( array( 'standard' ), $result[0]['theme_supports']['formats'] );
- }
- /**
- * Test when a theme only supports some post formats.
- *
- * @ticket 45016
- */
- public function test_theme_supports_formats_non_default() {
- add_theme_support( 'post-formats', array( 'aside', 'video' ) );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertTrue( isset( $result[0]['theme_supports'] ) );
- $this->assertTrue( isset( $result[0]['theme_supports']['formats'] ) );
- $this->assertSame( array( 'standard', 'aside', 'video' ), $result[0]['theme_supports']['formats'] );
- }
- /**
- * Test when a theme does not support responsive embeds.
- *
- * @ticket 45016
- */
- public function test_theme_supports_responsive_embeds_false() {
- remove_theme_support( 'responsive-embeds' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertTrue( isset( $result[0]['theme_supports'] ) );
- $this->assertTrue( isset( $result[0]['theme_supports']['responsive-embeds'] ) );
- $this->assertFalse( $result[0]['theme_supports']['responsive-embeds'] );
- }
- /**
- * Test when a theme supports responsive embeds.
- *
- * @ticket 45016
- */
- public function test_theme_supports_responsive_embeds_true() {
- remove_theme_support( 'responsive-embeds' );
- add_theme_support( 'responsive-embeds' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertTrue( isset( $result[0]['theme_supports'] ) );
- $this->assertTrue( $result[0]['theme_supports']['responsive-embeds'] );
- }
- /**
- * Test when a theme does not support post thumbnails.
- *
- * @ticket 45016
- */
- public function test_theme_supports_post_thumbnails_false() {
- remove_theme_support( 'post-thumbnails' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertTrue( isset( $result[0]['theme_supports'] ) );
- $this->assertTrue( isset( $result[0]['theme_supports']['post-thumbnails'] ) );
- $this->assertFalse( $result[0]['theme_supports']['post-thumbnails'] );
- }
- /**
- * Test when a theme supports all post thumbnails.
- *
- * @ticket 45016
- */
- public function test_theme_supports_post_thumbnails_true() {
- remove_theme_support( 'post-thumbnails' );
- add_theme_support( 'post-thumbnails' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertTrue( isset( $result[0]['theme_supports'] ) );
- $this->assertTrue( $result[0]['theme_supports']['post-thumbnails'] );
- }
- /**
- * Test when a theme only supports post thumbnails for certain post types.
- *
- * @ticket 45016
- */
- public function test_theme_supports_post_thumbnails_array() {
- remove_theme_support( 'post-thumbnails' );
- add_theme_support( 'post-thumbnails', array( 'post' ) );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertTrue( isset( $result[0]['theme_supports'] ) );
- $this->assertSame( array( 'post' ), $result[0]['theme_supports']['post-thumbnails'] );
- }
- /**
- * @ticket 49406
- */
- public function test_variadic_theme_support() {
- register_theme_feature(
- 'test-feature',
- array(
- 'type' => 'array',
- 'variadic' => true,
- 'show_in_rest' => array(
- 'schema' => array(
- 'items' => array(
- 'type' => 'string',
- ),
- ),
- ),
- )
- );
- add_theme_support( 'test-feature', 'a', 'b', 'c' );
- $response = self::perform_active_theme_request();
- $result = $response->get_data();
- $this->assertTrue( isset( $result[0]['theme_supports'] ) );
- $this->assertSame( array( 'a', 'b', 'c' ), $result[0]['theme_supports']['test-feature'] );
- }
- /**
- * It should be possible to register custom fields to the endpoint.
- *
- * @ticket 45016
- */
- public function test_get_additional_field_registration() {
- $schema = array(
- 'type' => 'integer',
- 'description' => 'Some integer of mine',
- 'enum' => array( 1, 2, 3, 4 ),
- );
- register_rest_field(
- 'theme',
- 'my_custom_int',
- array(
- 'schema' => $schema,
- 'get_callback' => array( $this, 'additional_field_get_callback' ),
- )
- );
- $response = self::perform_active_theme_request( 'OPTIONS' );
- $data = $response->get_data();
- $this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] );
- $this->assertSame( $schema, $data['schema']['properties']['my_custom_int'] );
- $response = self::perform_active_theme_request( 'GET' );
- $data = $response->get_data();
- $this->assertArrayHasKey( 'my_custom_int', $data[0] );
- $this->assertSame( 2, $data[0]['my_custom_int'] );
- global $wp_rest_additional_fields;
- $wp_rest_additional_fields = array();
- }
- /**
- * Return a value for the custom field.
- *
- * @since 5.0.0
- *
- * @param array $theme Theme data array.
- * @return int Additional field value.
- */
- public function additional_field_get_callback( $theme ) {
- return 2;
- }
- /**
- * The create_item() method does not exist for themes.
- */
- public function test_create_item() {}
- /**
- * The update_item() method does not exist for themes.
- */
- public function test_update_item() {}
- /**
- * Test single theme.
- *
- * @ticket 50152
- */
- public function test_get_item() {
- wp_set_current_user( self::$admin_id );
- $route = sprintf( '%s/%s', self::$themes_route, WP_DEFAULT_THEME );
- $request = new WP_REST_Request( 'GET', $route );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- $data = $response->get_data();
- $links = $response->get_links();
- $fields = array(
- 'author',
- 'author_uri',
- 'description',
- 'name',
- 'requires_php',
- 'requires_wp',
- 'screenshot',
- 'status',
- 'stylesheet',
- 'tags',
- 'template',
- 'textdomain',
- 'theme_uri',
- 'version',
- );
- $fields_links = array( 'collection', 'self' );
- $this->assertEqualSets( $fields, array_keys( $data ) );
- $this->assertEqualSets( $fields_links, array_keys( $links ) );
- }
- /**
- * @ticket 50152
- */
- public function test_get_item_no_permission() {
- wp_set_current_user( self::$subscriber_id );
- $request = new WP_REST_Request( 'GET', self::$themes_route . '/' . WP_DEFAULT_THEME );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_cannot_view_themes', $response, 403 );
- }
- /**
- * @ticket 50152
- */
- public function test_get_active_item_no_permission() {
- wp_set_current_user( self::$subscriber_id );
- $request = new WP_REST_Request( 'GET', self::$themes_route . '/' . get_stylesheet() );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_cannot_view_active_theme', $response, 403 );
- }
- /**
- * @ticket 50152
- */
- public function test_get_item_invalid() {
- wp_set_current_user( self::$admin_id );
- $request = new WP_REST_Request( 'GET', self::$themes_route . '/invalid' );
- $response = rest_get_server()->dispatch( $request );
- $this->assertErrorResponse( 'rest_theme_not_found', $response, 404 );
- }
- /**
- * @ticket 50152
- */
- public function test_get_active_item_as_contributor() {
- $route = sprintf( '%s/%s', self::$themes_route, get_stylesheet() );
- $request = new WP_REST_Request( 'GET', $route );
- $response = rest_get_server()->dispatch( $request );
- $this->assertSame( 200, $response->get_status() );
- }
- /**
- * The delete_item() method does not exist for themes.
- */
- public function test_delete_item() {}
- /**
- * Context is not supported for themes.
- */
- public function test_context_param() {}
- }
|