Make WordPress Core


Ignore:
Timestamp:
10/16/2015 09:04:12 PM (10 years ago)
Author:
wonderboymusic
Message:

Unit Tests: one $factory to rule them all, and it shall be static.

Using more than one instance of WP_UnitTest_Factory causes all kinds of craziness, due to out-of-sync internal generator sequences. Since we want to use setUpBeforeClass, we were creating ad hoc instances. To avoid that, we were injecting one static instance via Dependency Injection in wpSetUpBeforeClass. All tests should really use the static instance, so we will remove the instance prop $factory.

Replace $this->factory with self::$factory over 2000 times.
Rewrite all of the tests that were hard-coding dynamic values.

#YOLOFriday

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/comment-submission.php

    r34801 r35225  
    3939        $this->assertSame( 0, did_action( $error ) );
    4040
    41         $post = $this->factory->post->create_and_get( array(
     41        $post = self::$factory->post->create_and_get( array(
    4242            'comment_status' => 'closed',
    4343        ) );
     
    5959        $this->assertSame( 0, did_action( $error ) );
    6060
    61         $post = $this->factory->post->create_and_get();
     61        $post = self::$factory->post->create_and_get();
    6262        wp_trash_post( $post );
    6363        $data = array(
     
    7878        $this->assertSame( 0, did_action( $error ) );
    7979
    80         $post = $this->factory->post->create_and_get( array(
     80        $post = self::$factory->post->create_and_get( array(
    8181            'post_status' => 'draft',
    8282        ) );
     
    9999        $this->assertSame( 0, did_action( $error ) );
    100100
    101         $post = $this->factory->post->create_and_get( array(
     101        $post = self::$factory->post->create_and_get( array(
    102102            'post_date' => date( 'Y-m-d H:i:s', strtotime( '+1 day' ) ),
    103103        ) );
     
    122122        $this->assertSame( 0, did_action( $error ) );
    123123
    124         $post = $this->factory->post->create_and_get( array(
     124        $post = self::$factory->post->create_and_get( array(
    125125            'post_password' => 'password',
    126126        ) );
     
    143143        $_COOKIE['wp-postpass_' . COOKIEHASH] = $hasher->HashPassword( $password );
    144144
    145         $post = $this->factory->post->create_and_get( array(
     145        $post = self::$factory->post->create_and_get( array(
    146146            'post_password' => $password,
    147147        ) );
     
    163163    public function test_submitting_valid_comment_as_logged_in_user_succeeds() {
    164164
    165         $user = $this->factory->user->create_and_get( array(
     165        $user = self::$factory->user->create_and_get( array(
    166166            'user_url' => 'http://user.example.org'
    167167        ) );
     
    169169        wp_set_current_user( $user->ID );
    170170
    171         $post = $this->factory->post->create_and_get();
     171        $post = self::$factory->post->create_and_get();
    172172        $data = array(
    173173            'comment_post_ID' => $post->ID,
     
    188188    public function test_submitting_valid_comment_anonymously_succeeds() {
    189189
    190         $post = $this->factory->post->create_and_get();
     190        $post = self::$factory->post->create_and_get();
    191191        $data = array(
    192192            'comment_post_ID' => $post->ID,
     
    215215    public function test_submitting_comment_handles_slashes_correctly_handles_slashes() {
    216216
    217         $post = $this->factory->post->create_and_get();
     217        $post = self::$factory->post->create_and_get();
    218218        $data = array(
    219219            'comment_post_ID' => $post->ID,
     
    237237        $error = 'not_logged_in';
    238238
    239         $post = $this->factory->post->create_and_get( array(
     239        $post = self::$factory->post->create_and_get( array(
    240240            'post_status' => 'private',
    241241        ) );
     
    253253    public function test_submitting_comment_to_own_private_post_succeeds() {
    254254
    255         $user = $this->factory->user->create_and_get();
    256 
    257         wp_set_current_user( $user->ID );
    258 
    259         $post = $this->factory->post->create_and_get( array(
     255        $user = self::$factory->user->create_and_get();
     256
     257        wp_set_current_user( $user->ID );
     258
     259        $post = self::$factory->post->create_and_get( array(
    260260            'post_status' => 'private',
    261261            'post_author' => $user->ID,
     
    275275    public function test_submitting_comment_to_accessible_private_post_succeeds() {
    276276
    277         $author = $this->factory->user->create_and_get( array(
     277        $author = self::$factory->user->create_and_get( array(
    278278            'role' => 'author',
    279279        ) );
    280         $user = $this->factory->user->create_and_get( array(
     280        $user = self::$factory->user->create_and_get( array(
    281281            'role' => 'editor',
    282282        ) );
     
    284284        wp_set_current_user( $user->ID );
    285285
    286         $post = $this->factory->post->create_and_get( array(
     286        $post = self::$factory->post->create_and_get( array(
    287287            'post_status' => 'private',
    288288            'post_author' => $author->ID,
     
    302302    public function test_anonymous_user_cannot_comment_unfiltered_html() {
    303303
    304         $post = $this->factory->post->create_and_get();
     304        $post = self::$factory->post->create_and_get();
    305305        $data = array(
    306306            'comment_post_ID' => $post->ID,
     
    319319    public function test_unprivileged_user_cannot_comment_unfiltered_html() {
    320320
    321         $user = $this->factory->user->create_and_get( array(
     321        $user = self::$factory->user->create_and_get( array(
    322322            'role' => 'author',
    323323        ) );
     
    326326        $this->assertFalse( current_user_can( 'unfiltered_html' ) );
    327327
    328         $post = $this->factory->post->create_and_get();
     328        $post = self::$factory->post->create_and_get();
    329329        $data = array(
    330330            'comment_post_ID' => $post->ID,
     
    341341    public function test_unprivileged_user_cannot_comment_unfiltered_html_even_with_valid_nonce() {
    342342
    343         $user = $this->factory->user->create_and_get( array(
     343        $user = self::$factory->user->create_and_get( array(
    344344            'role' => 'author',
    345345        ) );
     
    348348        $this->assertFalse( current_user_can( 'unfiltered_html' ) );
    349349
    350         $post   = $this->factory->post->create_and_get();
     350        $post   = self::$factory->post->create_and_get();
    351351        $action = 'unfiltered-html-comment_' . $post->ID;
    352352        $nonce  = wp_create_nonce( $action );
     
    371371        $this->assertFalse( defined( 'DISALLOW_UNFILTERED_HTML' ) );
    372372
    373         $user = $this->factory->user->create_and_get( array(
     373        $user = self::$factory->user->create_and_get( array(
    374374            'role' => 'editor',
    375375        ) );
     
    385385        $this->assertTrue( current_user_can( 'unfiltered_html' ) );
    386386
    387         $post   = $this->factory->post->create_and_get();
     387        $post   = self::$factory->post->create_and_get();
    388388        $action = 'unfiltered-html-comment_' . $post->ID;
    389389        $nonce  = wp_create_nonce( $action );
     
    406406    public function test_privileged_user_cannot_comment_unfiltered_html_without_valid_nonce() {
    407407
    408         $user = $this->factory->user->create_and_get( array(
     408        $user = self::$factory->user->create_and_get( array(
    409409            'role' => 'editor',
    410410        ) );
     
    420420        $this->assertTrue( current_user_can( 'unfiltered_html' ) );
    421421
    422         $post   = $this->factory->post->create_and_get();
     422        $post   = self::$factory->post->create_and_get();
    423423        $data = array(
    424424            'comment_post_ID' => $post->ID,
     
    440440        update_option( 'comment_registration', '1' );
    441441
    442         $post = $this->factory->post->create_and_get();
     442        $post = self::$factory->post->create_and_get();
    443443        $data = array(
    444444            'comment_post_ID' => $post->ID,
     
    460460        update_option( 'require_name_email', '1' );
    461461
    462         $post = $this->factory->post->create_and_get();
     462        $post = self::$factory->post->create_and_get();
    463463        $data = array(
    464464            'comment_post_ID' => $post->ID,
     
    482482        update_option( 'require_name_email', '1' );
    483483
    484         $post = $this->factory->post->create_and_get();
     484        $post = self::$factory->post->create_and_get();
    485485        $data = array(
    486486            'comment_post_ID' => $post->ID,
     
    504504        update_option( 'require_name_email', '1' );
    505505
    506         $post = $this->factory->post->create_and_get();
     506        $post = self::$factory->post->create_and_get();
    507507        $data = array(
    508508            'comment_post_ID' => $post->ID,
     
    524524        $error = 'require_valid_comment';
    525525
    526         $post = $this->factory->post->create_and_get();
     526        $post = self::$factory->post->create_and_get();
    527527        $data = array(
    528528            'comment_post_ID' => $post->ID,
Note: See TracChangeset for help on using the changeset viewer.