Make WordPress Core


Ignore:
Timestamp:
01/15/2025 12:44:43 PM (19 months ago)
Author:
swissspidy
Message:

REST API: Improve autosave and revision endpoints for templates and template parts.

Fixes those endpoints for file-based templates and template parts, as templates based on theme files can't be revisioned or autosaved.

Props antonvlasenko, swissspidy, spacedmonkey, kadamwhite.
Fixes #61970.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/tests/phpunit/tests/rest-api/wpRestTemplateAutosavesController.php

    r59073 r59605  
    2323         * @var string
    2424         */
    25         const PARENT_POST_TYPE = 'wp_template';
     25        const TEMPLATE_PART_NAME = 'my_template_part';
     26
     27        /**
     28         * @var string
     29         */
     30        const TEMPLATE_POST_TYPE = 'wp_template';
     31
     32        /**
     33         * @var string
     34         */
     35        const TEMPLATE_PART_POST_TYPE = 'wp_template_part';
    2636
    2737        /**
     
    5363
    5464        /**
    55          * Create fake data before our tests run.
     65         * Template part post.
     66         *
     67         * @since 6.7.0
     68         *
     69         * @var WP_Post
     70         */
     71        private static $template_part_post;
     72
     73        /**
     74         * Create fake data before the tests run.
    5675         *
    5776         * @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
     
    7493                self::$template_post = $factory->post->create_and_get(
    7594                        array(
    76                                 'post_type'    => self::PARENT_POST_TYPE,
     95                                'post_type'    => self::TEMPLATE_POST_TYPE,
    7796                                'post_name'    => self::TEMPLATE_NAME,
    78                                 'post_title'   => 'My Template',
     97                                'post_title'   => 'My template',
    7998                                'post_content' => 'Content',
    8099                                'post_excerpt' => 'Description of my template',
     
    87106                );
    88107                wp_set_post_terms( self::$template_post->ID, self::TEST_THEME, 'wp_theme' );
     108
     109                // Set up template part post.
     110                self::$template_part_post = $factory->post->create_and_get(
     111                        array(
     112                                'post_type'    => self::TEMPLATE_PART_POST_TYPE,
     113                                'post_name'    => self::TEMPLATE_PART_NAME,
     114                                'post_title'   => 'My template part',
     115                                'post_content' => 'Content',
     116                                'post_excerpt' => 'Description of my template part',
     117                                'tax_input'    => array(
     118                                        'wp_theme'              => array(
     119                                                self::TEST_THEME,
     120                                        ),
     121                                        'wp_template_part_area' => array(
     122                                                WP_TEMPLATE_PART_AREA_HEADER,
     123                                        ),
     124                                ),
     125                        )
     126                );
     127                wp_set_post_terms( self::$template_part_post->ID, self::TEST_THEME, 'wp_theme' );
     128                wp_set_post_terms( self::$template_part_post->ID, WP_TEMPLATE_PART_AREA_HEADER, 'wp_template_part_area' );
    89129        }
    90130
     
    118158
    119159        /**
     160         * @coversNothing
     161         * @ticket 56922
     162         */
     163        public function test_context_param() {
     164                // A proper data provider cannot be used because this method's signature must match the parent method.
     165                // Therefore, actual tests are performed in the test_context_param_with_data_provider method.
     166                $this->assertTrue( true );
     167        }
     168
     169        /**
     170         * @dataProvider data_context_param_with_data_provider
    120171         * @covers WP_REST_Template_Autosaves_Controller::get_context_param
    121172         * @ticket 56922
    122          */
    123         public function test_context_param() {
     173         *
     174         * @param string $rest_base   Base part of the REST API endpoint to test.
     175         * @param string $template_id Template ID to use in the test.
     176         */
     177        public function test_context_param_with_data_provider( $rest_base, $template_id ) {
    124178                // Collection.
    125                 $request  = new WP_REST_Request( 'OPTIONS', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/autosaves' );
     179                $request  = new WP_REST_Request( 'OPTIONS', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves' );
    126180                $response = rest_get_server()->dispatch( $request );
    127181                $data     = $response->get_data();
     
    145199
    146200                // Single.
    147                 $request  = new WP_REST_Request( 'OPTIONS', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/autosaves/1' );
     201                $request  = new WP_REST_Request( 'OPTIONS', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves/1' );
    148202                $response = rest_get_server()->dispatch( $request );
    149203                $data     = $response->get_data();
     
    166220
    167221        /**
     222         * Data provider for test_context_param_with_data_provider.
     223         *
     224         * @return array
     225         */
     226        public function data_context_param_with_data_provider() {
     227                return array(
     228                        'templates'      => array( 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
     229                        'template parts' => array( 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
     230                );
     231        }
     232
     233        /**
     234         * @coversNothing
     235         * @ticket 56922
     236         */
     237        public function test_get_items() {
     238                // A proper data provider cannot be used because this method's signature must match the parent method.
     239                // Therefore, actual tests are performed in the test_get_items_with_data_provider method.
     240                $this->assertTrue( true );
     241        }
     242
     243        /**
     244         * @dataProvider data_get_items_with_data_provider
    168245         * @covers WP_REST_Template_Autosaves_Controller::get_items
    169246         * @ticket 56922
    170          */
    171         public function test_get_items() {
     247         *
     248         * @param string $parent_post_property_name  A class property name that contains the parent post object.
     249         * @param string $rest_base                  Base part of the REST API endpoint to test.
     250         * @param string $template_id                Template ID to use in the test.
     251         */
     252        public function test_get_items_with_data_provider( $parent_post_property_name, $rest_base, $template_id ) {
    172253                wp_set_current_user( self::$admin_id );
     254                // Cannot access this property in the data provider because it is not initialized at the time of execution.
     255                $parent_post      = self::$$parent_post_property_name;
    173256                $autosave_post_id = wp_create_post_autosave(
    174257                        array(
    175258                                'post_content' => 'Autosave content.',
    176                                 'post_ID'      => self::$template_post->ID,
    177                                 'post_type'    => self::PARENT_POST_TYPE,
     259                                'post_ID'      => $parent_post->ID,
     260                                'post_type'    => $parent_post->post_type,
    178261                        )
    179262                );
     
    181264                $request   = new WP_REST_Request(
    182265                        'GET',
    183                         '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/autosaves'
     266                        '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves'
    184267                );
    185268                $response  = rest_get_server()->dispatch( $request );
    186269                $autosaves = $response->get_data();
     270                $this->assertSame( WP_Http::OK, $response->get_status(), 'Response is expected to have a status code of 200.' );
    187271
    188272                $this->assertCount(
     
    198282                );
    199283                $this->assertSame(
    200                         self::$template_post->ID,
     284                        $parent_post->ID,
    201285                        $autosaves[0]['parent'],
    202286                        'Failed asserting that the parent ID of the autosave matches the template post ID.'
     
    210294
    211295        /**
     296         * Data provider for test_get_items_with_data_provider.
     297         *
     298         * @return array
     299         */
     300        public function data_get_items_with_data_provider() {
     301                return array(
     302                        'templates'      => array( 'template_post', 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
     303                        'template parts' => array( 'template_part_post', 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
     304                );
     305        }
     306
     307        /**
     308         * @dataProvider data_get_items_for_templates_based_on_theme_files_should_return_bad_response_status
     309         * @ticket 61970
     310         *
     311         * @param string $rest_base   Base part of the REST API endpoint to test.
     312         * @param string $template_id Template ID to use in the test.
     313         */
     314        public function test_get_items_for_templates_based_on_theme_files_should_return_bad_response_status( $rest_base, $template_id ) {
     315                wp_set_current_user( self::$admin_id );
     316                switch_theme( 'block-theme' );
     317
     318                $request = new WP_REST_Request( 'GET', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves' );
     319
     320                $response = rest_get_server()->dispatch( $request );
     321                $this->assertErrorResponse(
     322                        'rest_invalid_template',
     323                        $response,
     324                        WP_Http::BAD_REQUEST,
     325                        sprintf( 'Response is expected to have a status code of %d.', WP_Http::BAD_REQUEST )
     326                );
     327        }
     328
     329        /**
     330         * Data provider for test_get_items_for_templates_based_on_theme_files_should_return_bad_response_status.
     331         *
     332         * @return array
     333         */
     334        public function data_get_items_for_templates_based_on_theme_files_should_return_bad_response_status() {
     335                return array(
     336                        'templates'      => array( 'templates', self::TEST_THEME . '//page-home' ),
     337                        'template parts' => array( 'template-parts', self::TEST_THEME . '//small-header' ),
     338                );
     339        }
     340
     341        /**
     342         * @dataProvider data_get_item_for_templates_based_on_theme_files_should_return_bad_response_status
     343         * @ticket 56922
     344         *
     345         * @param string $rest_base   Base part of the REST API endpoint to test.
     346         * @param string $template_id Template ID to use in the test.
     347         */
     348        public function test_get_item_for_templates_based_on_theme_files_should_return_bad_response_status( $rest_base, $template_id ) {
     349                wp_set_current_user( self::$admin_id );
     350                switch_theme( 'block-theme' );
     351
     352                $request = new WP_REST_Request( 'GET', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves/1' );
     353
     354                $response = rest_get_server()->dispatch( $request );
     355                $this->assertErrorResponse(
     356                        'rest_invalid_template',
     357                        $response,
     358                        WP_Http::BAD_REQUEST,
     359                        sprintf( 'Response is expected to have a status code of %d.', WP_Http::BAD_REQUEST )
     360                );
     361        }
     362
     363        /**
     364         * Data provider for test_get_item_for_templates_based_on_theme_files_should_return_bad_response_status.
     365         *
     366         * @return array
     367         */
     368        public function data_get_item_for_templates_based_on_theme_files_should_return_bad_response_status() {
     369                return array(
     370                        'templates'      => array( 'templates', self::TEST_THEME . '//page-home' ),
     371                        'template parts' => array( 'template-parts', self::TEST_THEME . '//small-header' ),
     372                );
     373        }
     374
     375        /**
     376         * @coversNothing
     377         * @ticket 56922
     378         */
     379        public function test_get_item() {
     380                // A proper data provider cannot be used because this method's signature must match the parent method.
     381                // Therefore, actual tests are performed in the test_get_item_with_data_provider method.
     382                $this->assertTrue( true );
     383        }
     384
     385        /**
     386         * @dataProvider data_get_item_with_data_provider
    212387         * @covers WP_REST_Template_Autosaves_Controller::get_item
    213388         * @ticket 56922
    214          */
    215         public function test_get_item() {
     389         *
     390         * @param string  $parent_post_property_name  A class property name that contains the parent post object.
     391         * @param string  $rest_base                  Base part of the REST API endpoint to test.
     392         * @param string  $template_id                Template ID to use in the test.
     393         */
     394        public function test_get_item_with_data_provider( $parent_post_property_name, $rest_base, $template_id ) {
    216395                wp_set_current_user( self::$admin_id );
     396
     397                $parent_post = self::$$parent_post_property_name;
    217398
    218399                $autosave_post_id = wp_create_post_autosave(
    219400                        array(
    220401                                'post_content' => 'Autosave content.',
    221                                 'post_ID'      => self::$template_post->ID,
    222                                 'post_type'    => self::PARENT_POST_TYPE,
    223                         )
    224                 );
    225 
    226                 $request  = new WP_REST_Request( 'GET', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/autosaves/' . $autosave_post_id );
    227                 $response = rest_get_server()->dispatch( $request );
     402                                'post_ID'      => $parent_post->ID,
     403                                'post_type'    => $parent_post->post_type,
     404                        )
     405                );
     406
     407                $request  = new WP_REST_Request( 'GET', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves/' . $autosave_post_id );
     408                $response = rest_get_server()->dispatch( $request );
     409
     410                $this->assertSame( WP_Http::OK, $response->get_status(), 'Response is expected to have a status code of 200.' );
    228411                $autosave = $response->get_data();
    229412
     
    235418                );
    236419                $this->assertSame(
    237                         self::$template_post->ID,
     420                        $parent_post->ID,
    238421                        $autosave['parent'],
    239422                        sprintf(
    240423                                'Failed asserting that the parent id of the autosave is the same as %s.',
    241                                 self::$template_post->ID
    242                         )
    243                 );
    244         }
    245 
    246         /**
     424                                $parent_post->ID
     425                        )
     426                );
     427        }
     428
     429        /**
     430         * Data provider for test_get_item_with_data_provider.
     431         *
     432         * @return array
     433         */
     434        public function data_get_item_with_data_provider() {
     435                return array(
     436                        'templates'      => array( 'template_post', 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
     437                        'template parts' => array( 'template_part_post', 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
     438                );
     439        }
     440
     441        /**
     442         * @coversNothing
     443         * @ticket 56922
     444         */
     445        public function test_prepare_item() {
     446                // A proper data provider cannot be used because this method's signature must match the parent method.
     447                // Therefore, actual tests are performed in the test_prepare_item_with_data_provider method.
     448                $this->assertTrue( true );
     449        }
     450
     451        /**
     452         * @dataProvider data_prepare_item_with_data_provider
    247453         * @covers WP_REST_Template_Autosaves_Controller::prepare_item_for_response
    248454         * @ticket 56922
    249          */
    250         public function test_prepare_item() {
     455         *
     456         * @param string $parent_post_property_name A class property name that contains the parent post object.
     457         * @param string $rest_base                 Base part of the REST API endpoint to test.
     458         * @param string $template_id               Template ID to use in the test.
     459         */
     460        public function test_prepare_item_with_data_provider( $parent_post_property_name, $rest_base, $template_id ) {
    251461                wp_set_current_user( self::$admin_id );
     462                $parent_post      = self::$$parent_post_property_name;
    252463                $autosave_post_id = wp_create_post_autosave(
    253464                        array(
    254465                                'post_content' => 'Autosave content.',
    255                                 'post_ID'      => self::$template_post->ID,
    256                                 'post_type'    => self::PARENT_POST_TYPE,
     466                                'post_ID'      => $parent_post->ID,
     467                                'post_type'    => $parent_post->post_type,
    257468                        )
    258469                );
    259470                $autosave_db_post = get_post( $autosave_post_id );
    260                 $template_id      = self::TEST_THEME . '//' . self::TEMPLATE_NAME;
    261                 $request          = new WP_REST_Request( 'GET', '/wp/v2/templates/' . $template_id . '/autosaves/' . $autosave_db_post->ID );
    262                 $controller       = new WP_REST_Template_Autosaves_Controller( self::PARENT_POST_TYPE );
     471                $request          = new WP_REST_Request( 'GET', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves/' . $autosave_db_post->ID );
     472                $controller       = new WP_REST_Template_Autosaves_Controller( $parent_post->post_type );
    263473                $response         = $controller->prepare_item_for_response( $autosave_db_post, $request );
    264474                $this->assertInstanceOf(
     
    276486                );
    277487                $this->assertSame(
    278                         self::$template_post->ID,
     488                        $parent_post->ID,
    279489                        $autosave['parent'],
    280490                        sprintf(
    281491                                'Failed asserting that the parent id of the autosave is the same as %s.',
    282                                 self::$template_post->ID
     492                                $parent_post->ID
    283493                        )
    284494                );
     
    301511
    302512        /**
     513         * Data provider for test_prepare_item_with_data_provider.
     514         *
     515         * @return array
     516         */
     517        public function data_prepare_item_with_data_provider() {
     518                return array(
     519                        'templates'      => array( 'template_post', 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
     520                        'template parts' => array( 'template_part_post', 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
     521                );
     522        }
     523
     524        /**
     525         * @coversNothing
     526         * @ticket 56922
     527         */
     528        public function test_get_item_schema() {
     529                // A proper data provider cannot be used because this method's signature must match the parent method.
     530                // Therefore, actual tests are performed in the test_prepare_item_with_data_provider method.
     531                $this->assertTrue( true );
     532        }
     533
     534        /**
     535         * @dataProvider data_get_item_schema_with_data_provider
    303536         * @covers WP_REST_Template_Autosaves_Controller::get_item_schema
    304537         * @ticket 56922
    305          */
    306         public function test_get_item_schema() {
    307                 $request  = new WP_REST_Request( 'OPTIONS', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/autosaves' );
     538         *
     539         * @param string $rest_base             Base part of the REST API endpoint to test.
     540         * @param string $template_id           Template ID to use in the test.
     541         * @param int    $properties_count      Number of properties to check for in the schema.
     542         * @param array  $additional_properties Additional properties to check for in the schema.
     543         */
     544        public function test_get_item_schema_with_data_provider( $rest_base, $template_id, $properties_count, $additional_properties = array() ) {
     545                $request  = new WP_REST_Request( 'OPTIONS', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves' );
    308546                $response = rest_get_server()->dispatch( $request );
    309547                $data     = $response->get_data();
     
    311549                $properties = $data['schema']['properties'];
    312550
    313                 $this->assertCount( 19, $properties );
     551                $this->assertCount( $properties_count, $properties );
    314552                $this->assertArrayHasKey( 'id', $properties, 'ID key should exist in properties.' );
    315553                $this->assertArrayHasKey( 'slug', $properties, 'Slug key should exist in properties.' );
     
    325563                $this->assertArrayHasKey( 'author', $properties, 'author key should exist in properties.' );
    326564                $this->assertArrayHasKey( 'modified', $properties, 'modified key should exist in properties.' );
    327                 $this->assertArrayHasKey( 'is_custom', $properties, 'is_custom key should exist in properties.' );
    328565                $this->assertArrayHasKey( 'parent', $properties, 'Parent key should exist in properties.' );
    329566                $this->assertArrayHasKey( 'author_text', $properties, 'author_text key should exist in properties.' );
    330567                $this->assertArrayHasKey( 'original_source', $properties, 'original_source key should exist in properties.' );
    331                 $this->assertArrayHasKey( 'plugin', $properties, 'plugin key should exist in properties.' );
    332         }
    333 
    334         /**
     568                foreach ( $additional_properties as $additional_property ) {
     569                        $this->assertArrayHasKey( $additional_property, $properties, $additional_property . ' key should exist in properties.' );
     570                }
     571        }
     572
     573        /**
     574         * Data provider for test_get_item_schema_with_data_provider.
     575         *
     576         * @return array
     577         */
     578        public function data_get_item_schema_with_data_provider() {
     579                return array(
     580                        'templates'      => array(
     581                                'templates',
     582                                self::TEST_THEME . '//' . self::TEMPLATE_NAME,
     583                                19,
     584                                array( 'is_custom', 'plugin' ),
     585                        ),
     586                        'template parts' => array(
     587                                'template-parts',
     588                                self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME,
     589                                18,
     590                                array( 'area' ),
     591                        ),
     592                );
     593        }
     594
     595        /**
     596         * @coversNothing
     597         * @ticket 56922
     598         */
     599        public function test_create_item() {
     600                // A proper data provider cannot be used because this method's signature must match the parent method.
     601                // Therefore, actual tests are performed in the test_create_item_with_data_provider method.
     602                $this->assertTrue( true );
     603        }
     604
     605        /**
     606         * @dataProvider data_create_item_with_data_provider
    335607         * @covers WP_REST_Template_Autosaves_Controller::create_item
    336608         * @ticket 56922
    337          */
    338         public function test_create_item() {
     609         *
     610         * @param string $rest_base   Base part of the REST API endpoint to test.
     611         * @param string $template_id Template ID to use in the test.
     612         */
     613        public function test_create_item_with_data_provider( $rest_base, $template_id ) {
    339614                wp_set_current_user( self::$admin_id );
    340615
    341                 $template_id = self::TEST_THEME . '/' . self::TEMPLATE_NAME;
    342                 $request     = new WP_REST_Request( 'POST', '/wp/v2/templates/' . $template_id . '/autosaves' );
     616                $request = new WP_REST_Request( 'POST', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves' );
    343617                $request->add_header( 'Content-Type', 'application/x-www-form-urlencoded' );
    344618
     
    354628                $response = rest_get_server()->dispatch( $request );
    355629
    356                 $this->assertNotWPError( $response, 'The response from this request should not return a WP_Error object' );
     630                $this->assertNotWPError( $response, 'The response from this request should not return a WP_Error object.' );
    357631                $response = rest_ensure_response( $response );
    358632                $data     = $response->get_data();
    359633
    360                 $this->assertArrayHasKey( 'content', $data, 'Response should contain a key called content' );
    361                 $this->assertSame( $request_parameters['content'], $data['content']['raw'], 'Response data should match for field content' );
    362 
    363                 $this->assertArrayHasKey( 'title', $data, 'Response should contain a key called title' );
    364                 $this->assertSame( $request_parameters['title'], $data['title']['raw'], 'Response data should match for field title' );
    365         }
    366 
    367         /**
    368          * @covers WP_REST_Template_Autosaves_Controller::delete_item
    369          * @ticket 56922
    370          */
    371         public function test_create_item_incorrect_permission() {
     634                $this->assertArrayHasKey( 'content', $data, 'Response should contain a key called content.' );
     635                $this->assertSame( $request_parameters['content'], $data['content']['raw'], 'Response data should match for field content.' );
     636
     637                $this->assertArrayHasKey( 'title', $data, 'Response should contain a key called title.' );
     638                $this->assertSame( $request_parameters['title'], $data['title']['raw'], 'Response data should match for field title.' );
     639        }
     640
     641        /**
     642         * Data provider for test_create_item_with_data_provider.
     643         *
     644         * @return array
     645         */
     646        public function data_create_item_with_data_provider() {
     647                return array(
     648                        'templates'     => array( 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
     649                        'template part' => array( 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
     650                );
     651        }
     652
     653        /**
     654         * @dataProvider data_create_item_incorrect_permission
     655         * @covers WP_REST_Template_Autosaves_Controller::create_item_permissions_check
     656         * @ticket 56922
     657         *
     658         * @param string $rest_base   Base part of the REST API endpoint to test.
     659         * @param string $template_id Template ID to use in the test.
     660         */
     661        public function test_create_item_incorrect_permission( $rest_base, $template_id ) {
    372662                wp_set_current_user( self::$contributor_id );
    373                 $template_id = self::TEST_THEME . '/' . self::TEMPLATE_NAME;
    374                 $request     = new WP_REST_Request( 'POST', '/wp/v2/templates/' . $template_id . '/autosaves' );
    375                 $response    = rest_get_server()->dispatch( $request );
     663                $request  = new WP_REST_Request( 'POST', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves' );
     664                $response = rest_get_server()->dispatch( $request );
    376665                $this->assertErrorResponse( 'rest_cannot_manage_templates', $response, WP_Http::FORBIDDEN );
    377666        }
    378667
    379668        /**
    380          * @covers WP_REST_Template_Autosaves_Controller::delete_item
    381          * @ticket 56922
    382          */
    383         public function test_create_item_no_permission() {
     669         * Data provider for test_create_item_incorrect_permission.
     670         *
     671         * @return array
     672         */
     673        public function data_create_item_incorrect_permission() {
     674                return array(
     675                        'template'      => array( 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
     676                        'template part' => array( 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
     677                );
     678        }
     679
     680        /**
     681         * @dataProvider data_create_item_no_permission
     682         * @covers WP_REST_Template_Autosaves_Controller::create_item_permissions_check
     683         * @ticket 56922
     684         *
     685         * @param string $rest_base   Base part of the REST API endpoint to test.
     686         * @param string $template_id Template ID to use in the test.
     687         */
     688        public function test_create_item_no_permission( $rest_base, $template_id ) {
    384689                wp_set_current_user( 0 );
    385                 $template_id = self::TEST_THEME . '/' . self::TEMPLATE_NAME;
    386                 $request     = new WP_REST_Request( 'POST', '/wp/v2/templates/' . $template_id . '/autosaves' );
    387                 $response    = rest_get_server()->dispatch( $request );
     690                $request  = new WP_REST_Request( 'POST', '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves' );
     691                $response = rest_get_server()->dispatch( $request );
    388692                $this->assertErrorResponse( 'rest_cannot_manage_templates', $response, WP_Http::UNAUTHORIZED );
     693        }
     694
     695        /**
     696         * Data provider for test_create_item_no_permission.
     697         *
     698         * @return array
     699         */
     700        public function data_create_item_no_permission() {
     701                return array(
     702                        'template'      => array( 'templates', self::TEST_THEME . '//' . self::TEMPLATE_NAME ),
     703                        'template part' => array( 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
     704                );
    389705        }
    390706
Note: See TracChangeset for help on using the changeset viewer.