Make WordPress Core


Ignore:
Timestamp:
10/27/2016 02:56:28 AM (5 years ago)
Author:
boonebgorges
Message:

Share fixtures in REST API endpoint tests.

As sparrows' tears shed steadily
Make widest rivers filled,

setUp() routines run prodig'ly
Add minutes to a build.

So cull ye fixtures profligate!
Direct thine frugal gaze!

Our savings here - a half-minute -
When multiplied: Amaze!

See #30017.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-revisions-controller.php

    r38832 r38975  
    1111  */
    1212class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase {
     13    protected static $post_id;
     14    protected static $page_id;
     15
     16    protected static $editor_id;
     17    protected static $contributor_id;
     18
     19    public static function wpSetUpBeforeClass( $factory ) {
     20        self::$post_id = $factory->post->create();
     21        self::$page_id = $factory->post->create( array( 'post_type' => 'page' ) );
     22
     23        self::$editor_id = $factory->user->create( array(
     24            'role' => 'editor',
     25        ) );
     26        self::$contributor_id = $factory->user->create( array(
     27            'role' => 'contributor',
     28        ) );
     29
     30        wp_update_post( array( 'post_content' => 'This content is better.', 'ID' => self::$post_id ) );
     31        wp_update_post( array( 'post_content' => 'This content is marvelous.', 'ID' => self::$post_id ) );
     32    }
     33
     34    public static function wpTearDownAfterClass() {
     35        // Also deletes revisions.
     36        wp_delete_post( self::$post_id, true );
     37        wp_delete_post( self::$page_id, true );
     38
     39        self::delete_user( self::$editor_id );
     40        self::delete_user( self::$contributor_id );
     41    }
    1342
    1443    public function setUp() {
    1544        parent::setUp();
    16         $this->post_id = $this->factory->post->create();
    17         $this->page_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
    18 
    19         $this->editor_id = $this->factory->user->create( array(
    20             'role' => 'editor',
    21         ) );
    22         $this->contributor_id = $this->factory->user->create( array(
    23             'role' => 'contributor',
    24         ) );
    25 
    26         wp_update_post( array( 'post_content' => 'This content is better.', 'ID' => $this->post_id ) );
    27         wp_update_post( array( 'post_content' => 'This content is marvelous.', 'ID' => $this->post_id ) );
    28         $revisions = wp_get_post_revisions( $this->post_id );
     45
     46        $revisions = wp_get_post_revisions( self::$post_id );
    2947        $this->revision_1 = array_pop( $revisions );
    3048        $this->revision_id1 = $this->revision_1->ID;
     
    4361    public function test_context_param() {
    4462        // Collection
    45         $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . $this->post_id . '/revisions' );
     63        $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' );
    4664        $response = $this->server->dispatch( $request );
    4765        $data = $response->get_data();
     
    4967        $this->assertEqualSets( array( 'view', 'edit', 'embed' ), $data['endpoints'][0]['args']['context']['enum'] );
    5068        // Single
    51         $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_1->ID );
     69        $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_1->ID );
    5270        $response = $this->server->dispatch( $request );
    5371        $data = $response->get_data();
     
    5775
    5876    public function test_get_items() {
    59         wp_set_current_user( $this->editor_id );
    60         $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions' );
     77        wp_set_current_user( self::$editor_id );
     78        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
    6179        $response = $this->server->dispatch( $request );
    6280        $data = $response->get_data();
     
    7492    public function test_get_items_no_permission() {
    7593        wp_set_current_user( 0 );
    76         $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions' );
     94        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions' );
    7795        $response = $this->server->dispatch( $request );
    7896
    7997        $this->assertErrorResponse( 'rest_cannot_read', $response, 401 );
    80         wp_set_current_user( $this->contributor_id );
     98        wp_set_current_user( self::$contributor_id );
    8199        $response = $this->server->dispatch( $request );
    82100        $this->assertErrorResponse( 'rest_cannot_read', $response, 403 );
     
    84102
    85103    public function test_get_items_missing_parent() {
    86         wp_set_current_user( $this->editor_id );
     104        wp_set_current_user( self::$editor_id );
    87105        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions' );
    88106        $response = $this->server->dispatch( $request );
     
    91109
    92110    public function test_get_items_invalid_parent_post_type() {
    93         wp_set_current_user( $this->editor_id );
    94         $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->page_id . '/revisions' );
     111        wp_set_current_user( self::$editor_id );
     112        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$page_id . '/revisions' );
    95113        $response = $this->server->dispatch( $request );
    96114        $this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 );
     
    98116
    99117    public function test_get_item() {
    100         wp_set_current_user( $this->editor_id );
    101         $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 );
     118        wp_set_current_user( self::$editor_id );
     119        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
    102120        $response = $this->server->dispatch( $request );
    103121        $this->assertEquals( 200, $response->get_status() );
     
    122140
    123141    public function test_get_item_embed_context() {
    124         wp_set_current_user( $this->editor_id );
    125         $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 );
     142        wp_set_current_user( self::$editor_id );
     143        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
    126144        $request->set_param( 'context', 'embed' );
    127145        $response = $this->server->dispatch( $request );
     
    141159    public function test_get_item_no_permission() {
    142160        wp_set_current_user( 0 );
    143         $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 );
     161        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
    144162
    145163        $response = $this->server->dispatch( $request );
    146164        $this->assertErrorResponse( 'rest_cannot_read', $response, 401 );
    147         wp_set_current_user( $this->contributor_id );
     165        wp_set_current_user( self::$contributor_id );
    148166        $response = $this->server->dispatch( $request );
    149167        $this->assertErrorResponse( 'rest_cannot_read', $response, 403 );
     
    151169
    152170    public function test_get_item_missing_parent() {
    153         wp_set_current_user( $this->editor_id );
     171        wp_set_current_user( self::$editor_id );
    154172        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions/' . $this->revision_id1 );
    155173        $response = $this->server->dispatch( $request );
     
    158176
    159177    public function test_get_item_invalid_parent_post_type() {
    160         wp_set_current_user( $this->editor_id );
    161         $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->page_id . '/revisions/' . $this->revision_id1 );
     178        wp_set_current_user( self::$editor_id );
     179        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$page_id . '/revisions/' . $this->revision_id1 );
    162180        $response = $this->server->dispatch( $request );
    163181        $this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 );
     
    165183
    166184    public function test_delete_item() {
    167         wp_set_current_user( $this->editor_id );
    168         $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 );
     185        wp_set_current_user( self::$editor_id );
     186        $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
    169187        $response = $this->server->dispatch( $request );
    170188        $this->assertEquals( 200, $response->get_status() );
     
    173191
    174192    public function test_delete_item_no_permission() {
    175         wp_set_current_user( $this->contributor_id );
    176         $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 );
     193        wp_set_current_user( self::$contributor_id );
     194        $request = new WP_REST_Request( 'DELETE', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
    177195        $response = $this->server->dispatch( $request );
    178196        $this->assertErrorResponse( 'rest_cannot_read', $response, 403 );
     
    180198
    181199    public function test_prepare_item() {
    182         wp_set_current_user( $this->editor_id );
    183         $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 );
     200        wp_set_current_user( self::$editor_id );
     201        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
    184202        $response = $this->server->dispatch( $request );
    185203        $this->assertEquals( 200, $response->get_status() );
     
    188206
    189207    public function test_get_item_schema() {
    190         $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . $this->post_id . '/revisions' );
     208        $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' );
    191209        $response = $this->server->dispatch( $request );
    192210        $data = $response->get_data();
     
    208226
    209227    public function test_create_item() {
    210         $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $this->post_id . '/revisions' );
     228        $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . self::$post_id . '/revisions' );
    211229        $response = $this->server->dispatch( $request );
    212230        $this->assertErrorResponse( 'rest_no_route', $response, 404 );
     
    214232
    215233    public function test_update_item() {
    216         $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 );
     234        $request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
    217235        $response = $this->server->dispatch( $request );
    218236        $this->assertErrorResponse( 'rest_no_route', $response, 404 );
     
    234252        ) );
    235253
    236         $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . $this->post_id . '/revisions' );
     254        $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' );
    237255
    238256        $response = $this->server->dispatch( $request );
     
    244262        wp_set_current_user( 1 );
    245263
    246         $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $this->post_id . '/revisions/' . $this->revision_id1 );
     264        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
    247265
    248266        $response = $this->server->dispatch( $request );
Note: See TracChangeset for help on using the changeset viewer.