Make WordPress Core


Ignore:
Timestamp:
10/16/2015 09:04:12 PM (11 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/url.php

    r34703 r35225  
    273273    public function test_get_adjacent_post() {
    274274        $now = time();
    275         $post_id = $this->factory->post->create( array( 'post_date' => date( 'Y-m-d H:i:s', $now - 1 ) ) );
    276         $post_id2 = $this->factory->post->create( array( 'post_date' => date( 'Y-m-d H:i:s', $now ) ) );
     275        $post_id = self::$factory->post->create( array( 'post_date' => date( 'Y-m-d H:i:s', $now - 1 ) ) );
     276        $post_id2 = self::$factory->post->create( array( 'post_date' => date( 'Y-m-d H:i:s', $now ) ) );
    277277
    278278        if ( ! isset( $GLOBALS['post'] ) )
     
    306306     */
    307307    public function test_get_adjacent_post_should_return_private_posts_belonging_to_the_current_user() {
    308         $u = $this->factory->user->create( array( 'role' => 'author' ) );
     308        $u = self::$factory->user->create( array( 'role' => 'author' ) );
    309309        $old_uid = get_current_user_id();
    310310        wp_set_current_user( $u );
    311311
    312312        $now = time();
    313         $p1 = $this->factory->post->create( array( 'post_author' => $u, 'post_status' => 'private', 'post_date' => date( 'Y-m-d H:i:s', $now - 1 ) ) );
    314         $p2 = $this->factory->post->create( array( 'post_author' => $u, 'post_date' => date( 'Y-m-d H:i:s', $now ) ) );
     313        $p1 = self::$factory->post->create( array( 'post_author' => $u, 'post_status' => 'private', 'post_date' => date( 'Y-m-d H:i:s', $now - 1 ) ) );
     314        $p2 = self::$factory->post->create( array( 'post_author' => $u, 'post_date' => date( 'Y-m-d H:i:s', $now ) ) );
    315315
    316316        if ( ! isset( $GLOBALS['post'] ) ) {
     
    332332     */
    333333    public function test_get_adjacent_post_should_return_private_posts_belonging_to_other_users_if_the_current_user_can_read_private_posts() {
    334         $u1 = $this->factory->user->create( array( 'role' => 'author' ) );
    335         $u2 = $this->factory->user->create( array( 'role' => 'administrator' ) );
     334        $u1 = self::$factory->user->create( array( 'role' => 'author' ) );
     335        $u2 = self::$factory->user->create( array( 'role' => 'administrator' ) );
    336336        $old_uid = get_current_user_id();
    337337        wp_set_current_user( $u2 );
    338338
    339339        $now = time();
    340         $p1 = $this->factory->post->create( array( 'post_author' => $u1, 'post_status' => 'private', 'post_date' => date( 'Y-m-d H:i:s', $now - 1 ) ) );
    341         $p2 = $this->factory->post->create( array( 'post_author' => $u1, 'post_date' => date( 'Y-m-d H:i:s', $now ) ) );
     340        $p1 = self::$factory->post->create( array( 'post_author' => $u1, 'post_status' => 'private', 'post_date' => date( 'Y-m-d H:i:s', $now - 1 ) ) );
     341        $p2 = self::$factory->post->create( array( 'post_author' => $u1, 'post_date' => date( 'Y-m-d H:i:s', $now ) ) );
    342342
    343343        if ( ! isset( $GLOBALS['post'] ) ) {
     
    359359     */
    360360    public function test_get_adjacent_post_should_not_return_private_posts_belonging_to_other_users_if_the_current_user_cannot_read_private_posts() {
    361         $u1 = $this->factory->user->create( array( 'role' => 'author' ) );
    362         $u2 = $this->factory->user->create( array( 'role' => 'author' ) );
     361        $u1 = self::$factory->user->create( array( 'role' => 'author' ) );
     362        $u2 = self::$factory->user->create( array( 'role' => 'author' ) );
    363363        $old_uid = get_current_user_id();
    364364        wp_set_current_user( $u2 );
    365365
    366366        $now = time();
    367         $p1 = $this->factory->post->create( array( 'post_author' => $u1, 'post_date' => date( 'Y-m-d H:i:s', $now - 2 ) ) );
    368         $p2 = $this->factory->post->create( array( 'post_author' => $u1, 'post_status' => 'private', 'post_date' => date( 'Y-m-d H:i:s', $now - 1 ) ) );
    369         $p3 = $this->factory->post->create( array( 'post_author' => $u1, 'post_date' => date( 'Y-m-d H:i:s', $now ) ) );
     367        $p1 = self::$factory->post->create( array( 'post_author' => $u1, 'post_date' => date( 'Y-m-d H:i:s', $now - 2 ) ) );
     368        $p2 = self::$factory->post->create( array( 'post_author' => $u1, 'post_status' => 'private', 'post_date' => date( 'Y-m-d H:i:s', $now - 1 ) ) );
     369        $p3 = self::$factory->post->create( array( 'post_author' => $u1, 'post_date' => date( 'Y-m-d H:i:s', $now ) ) );
    370370
    371371        if ( ! isset( $GLOBALS['post'] ) ) {
Note: See TracChangeset for help on using the changeset viewer.