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

    r38942 r38975  
    1212 */
    1313class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcase {
    14 
    15     public function setUp() {
    16         parent::setUp();
    17         $this->administrator = $this->factory->user->create( array(
     14    protected static $administrator;
     15    protected static $subscriber;
     16
     17    public static function wpSetUpBeforeClass( $factory ) {
     18        self::$administrator = $factory->user->create( array(
    1819            'role' => 'administrator',
    1920        ) );
    20         $this->subscriber = $this->factory->user->create( array(
     21        self::$subscriber = $factory->user->create( array(
    2122            'role' => 'subscriber',
    2223        ) );
     24    }
     25
     26    public static function wpTearDownAfterClass() {
     27        self::delete_user( self::$administrator );
     28        self::delete_user( self::$subscriber );
    2329    }
    2430
     
    573579
    574580    public function test_create_item() {
    575         wp_set_current_user( $this->administrator );
     581        wp_set_current_user( self::$administrator );
    576582        $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
    577583        $request->set_param( 'name', 'My Awesome Term' );
     
    589595
    590596    public function test_create_item_invalid_taxonomy() {
    591         wp_set_current_user( $this->administrator );
     597        wp_set_current_user( self::$administrator );
    592598        $request = new WP_REST_Request( 'POST', '/wp/v2/invalid-taxonomy' );
    593599        $request->set_param( 'name', 'Invalid Taxonomy' );
     
    597603
    598604    public function test_create_item_incorrect_permissions() {
    599         wp_set_current_user( $this->subscriber );
     605        wp_set_current_user( self::$subscriber );
    600606        $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
    601607        $request->set_param( 'name', 'Incorrect permissions' );
     
    605611
    606612    public function test_create_item_missing_arguments() {
    607         wp_set_current_user( $this->administrator );
     613        wp_set_current_user( self::$administrator );
    608614        $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
    609615        $response = $this->server->dispatch( $request );
     
    612618
    613619    public function test_create_item_with_parent() {
    614         wp_set_current_user( $this->administrator );
     620        wp_set_current_user( self::$administrator );
    615621        $parent = wp_insert_term( 'test-category', 'category' );
    616622        $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
     
    624630
    625631    public function test_create_item_invalid_parent() {
    626         wp_set_current_user( $this->administrator );
     632        wp_set_current_user( self::$administrator );
    627633        $term = get_term_by( 'id', $this->factory->category->create(), 'category' );
    628634
     
    635641
    636642    public function test_update_item() {
    637         wp_set_current_user( $this->administrator );
     643        wp_set_current_user( self::$administrator );
    638644        $orig_args = array(
    639645            'name'        => 'Original Name',
     
    655661
    656662    public function test_update_item_invalid_taxonomy() {
    657         wp_set_current_user( $this->administrator );
     663        wp_set_current_user( self::$administrator );
    658664        $request = new WP_REST_Request( 'POST', '/wp/v2/invalid-taxonomy/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    659665        $request->set_param( 'name', 'Invalid Taxonomy' );
     
    663669
    664670    public function test_update_item_invalid_term() {
    665         wp_set_current_user( $this->administrator );
     671        wp_set_current_user( self::$administrator );
    666672        $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    667673        $request->set_param( 'name', 'Invalid Term' );
     
    671677
    672678    public function test_update_item_incorrect_permissions() {
    673         wp_set_current_user( $this->subscriber );
     679        wp_set_current_user( self::$subscriber );
    674680        $term = get_term_by( 'id', $this->factory->category->create(), 'category' );
    675681        $request = new WP_REST_Request( 'POST', '/wp/v2/categories/' . $term->term_id );
     
    680686
    681687    public function test_update_item_parent() {
    682         wp_set_current_user( $this->administrator );
     688        wp_set_current_user( self::$administrator );
    683689        $parent = get_term_by( 'id', $this->factory->category->create(), 'category' );
    684690        $term = get_term_by( 'id', $this->factory->category->create(), 'category' );
     
    694700
    695701    public function test_update_item_invalid_parent() {
    696         wp_set_current_user( $this->administrator );
     702        wp_set_current_user( self::$administrator );
    697703        $term = get_term_by( 'id', $this->factory->category->create(), 'category' );
    698704
     
    704710
    705711    public function test_delete_item() {
    706         wp_set_current_user( $this->administrator );
     712        wp_set_current_user( self::$administrator );
    707713        $term = get_term_by( 'id', $this->factory->category->create( array( 'name' => 'Deleted Category' ) ), 'category' );
    708714        $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id );
     
    715721
    716722    public function test_delete_item_force_false() {
    717         wp_set_current_user( $this->administrator );
     723        wp_set_current_user( self::$administrator );
    718724        $term = get_term_by( 'id', $this->factory->category->create( array( 'name' => 'Deleted Category' ) ), 'category' );
    719725        $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id );
     
    724730
    725731    public function test_delete_item_invalid_taxonomy() {
    726         wp_set_current_user( $this->administrator );
     732        wp_set_current_user( self::$administrator );
    727733        $request = new WP_REST_Request( 'DELETE', '/wp/v2/invalid-taxonomy/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    728734        $response = $this->server->dispatch( $request );
     
    731737
    732738    public function test_delete_item_invalid_term() {
    733         wp_set_current_user( $this->administrator );
     739        wp_set_current_user( self::$administrator );
    734740        $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
    735741        $response = $this->server->dispatch( $request );
     
    738744
    739745    public function test_delete_item_incorrect_permissions() {
    740         wp_set_current_user( $this->subscriber );
     746        wp_set_current_user( self::$subscriber );
    741747        $term = get_term_by( 'id', $this->factory->category->create(), 'category' );
    742748        $request = new WP_REST_Request( 'DELETE', '/wp/v2/categories/' . $term->term_id );
Note: See TracChangeset for help on using the changeset viewer.