WordPress.org

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-settings-controller.php

    r38971 r38975  
    1111 */
    1212class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase {
     13    protected static $administrator;
     14
     15    public static function wpSetUpBeforeClass( $factory ) {
     16        self::$administrator = $factory->user->create( array(
     17            'role' => 'administrator',
     18        ) );
     19    }
     20
     21    public static function wpTearDownAfterClass() {
     22        self::delete_user( self::$administrator );
     23    }
    1324
    1425    public function setUp() {
    1526        parent::setUp();
    16         $this->administrator = $this->factory->user->create( array(
    17             'role' => 'administrator',
    18         ) );
    1927        $this->endpoint = new WP_REST_Settings_Controller();
    2028    }
     
    3846
    3947    public function test_get_item() {
    40         wp_set_current_user( $this->administrator );
     48        wp_set_current_user( self::$administrator );
    4149        $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
    4250        $response = $this->server->dispatch( $request );
     
    6472
    6573    public function test_get_item_value_is_cast_to_type() {
    66         wp_set_current_user( $this->administrator );
     74        wp_set_current_user( self::$administrator );
    6775        update_option( 'posts_per_page', 'invalid_number' ); // this is cast to (int) 1
    6876        $request = new WP_REST_Request( 'GET', '/wp/v2/settings' );
     
    7583
    7684    public function test_get_item_with_custom_setting() {
    77         wp_set_current_user( $this->administrator );
     85        wp_set_current_user( self::$administrator );
    7886
    7987        register_setting( 'somegroup', 'mycustomsetting', array(
     
    115123
    116124    public function test_get_item_with_filter() {
    117         wp_set_current_user( $this->administrator );
     125        wp_set_current_user( self::$administrator );
    118126
    119127        add_filter( 'rest_pre_get_setting', array( $this, 'get_setting_custom_callback' ), 10, 3 );
     
    156164
    157165    public function test_update_item() {
    158         wp_set_current_user( $this->administrator );
     166        wp_set_current_user( self::$administrator );
    159167        $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
    160168        $request->set_param( 'title', 'The new title!' );
     
    177185
    178186    public function test_update_item_with_filter() {
    179         wp_set_current_user( $this->administrator );
     187        wp_set_current_user( self::$administrator );
    180188
    181189        $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
     
    208216
    209217    public function test_update_item_with_invalid_type() {
    210         wp_set_current_user( $this->administrator );
     218        wp_set_current_user( self::$administrator );
    211219        $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
    212220        $request->set_param( 'title', array( 'rendered' => 'This should fail.' ) );
     
    221229        update_option( 'posts_per_page', 9 );
    222230
    223         wp_set_current_user( $this->administrator );
     231        wp_set_current_user( self::$administrator );
    224232        $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
    225233        $request->set_param( 'posts_per_page', null );
     
    234242        update_option( 'posts_per_page', 9 );
    235243
    236         wp_set_current_user( $this->administrator );
     244        wp_set_current_user( self::$administrator );
    237245        $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
    238246        $request->set_param( 'default_ping_status', 'open&closed' );
Note: See TracChangeset for help on using the changeset viewer.