Make WordPress Core


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

    r35195 r35225  
    8686    function test_url_to_postid() {
    8787
    88         $id = $this->factory->post->create();
    89         $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
    90 
    91         $id = $this->factory->post->create( array( 'post_type' => 'page' ) );
     88        $id = self::$factory->post->create();
     89        $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
     90
     91        $id = self::$factory->post->create( array( 'post_type' => 'page' ) );
    9292        $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
    9393    }
    9494
    9595    function test_url_to_postid_set_url_scheme_https_to_http() {
    96         $post_id = $this->factory->post->create();
     96        $post_id = self::$factory->post->create();
    9797        $permalink = get_permalink( $post_id );
    9898        $this->assertEquals( $post_id, url_to_postid( set_url_scheme( $permalink, 'https' ) ) );
    9999
    100         $post_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
     100        $post_id = self::$factory->post->create( array( 'post_type' => 'page' ) );
    101101        $permalink = get_permalink( $post_id );
    102102        $this->assertEquals( $post_id, url_to_postid( set_url_scheme( $permalink, 'https' ) ) );
     
    110110        $_SERVER['HTTPS'] = 'on';
    111111
    112         $post_id = $this->factory->post->create();
     112        $post_id = self::$factory->post->create();
    113113        $permalink = get_permalink( $post_id );
    114114        $this->assertEquals( $post_id, url_to_postid( set_url_scheme( $permalink, 'http' ) ) );
    115115
    116         $post_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
     116        $post_id = self::$factory->post->create( array( 'post_type' => 'page' ) );
    117117        $permalink = get_permalink( $post_id );
    118118        $this->assertEquals( $post_id, url_to_postid( set_url_scheme( $permalink, 'http' ) ) );
     
    129129        register_post_type( $post_type, array( 'public' => true ) );
    130130
    131         $id = $this->factory->post->create( array( 'post_type' => $post_type ) );
     131        $id = self::$factory->post->create( array( 'post_type' => $post_type ) );
    132132        $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
    133133
     
    137137    function test_url_to_postid_hierarchical() {
    138138
    139         $parent_id = $this->factory->post->create( array( 'post_title' => 'Parent', 'post_type' => 'page' ) );
    140         $child_id = $this->factory->post->create( array( 'post_title' => 'Child', 'post_type' => 'page', 'post_parent' => $parent_id ) );
     139        $parent_id = self::$factory->post->create( array( 'post_title' => 'Parent', 'post_type' => 'page' ) );
     140        $child_id = self::$factory->post->create( array( 'post_title' => 'Child', 'post_type' => 'page', 'post_parent' => $parent_id ) );
    141141
    142142        $this->assertEquals( $parent_id, url_to_postid( get_permalink( $parent_id ) ) );
     
    146146    function test_url_to_postid_hierarchical_with_matching_leaves() {
    147147
    148         $parent_id = $this->factory->post->create( array(
     148        $parent_id = self::$factory->post->create( array(
    149149            'post_name' => 'parent',
    150150            'post_type' => 'page',
    151151        ) );
    152         $child_id_1 = $this->factory->post->create( array(
     152        $child_id_1 = self::$factory->post->create( array(
    153153            'post_name'   => 'child1',
    154154            'post_type'   => 'page',
    155155            'post_parent' => $parent_id,
    156156        ) );
    157         $child_id_2 = $this->factory->post->create( array(
     157        $child_id_2 = self::$factory->post->create( array(
    158158            'post_name'   => 'child2',
    159159            'post_type'   => 'page',
    160160            'post_parent' => $parent_id,
    161161        ) );
    162         $grandchild_id_1 = $this->factory->post->create( array(
     162        $grandchild_id_1 = self::$factory->post->create( array(
    163163            'post_name'   => 'grandchild',
    164164            'post_type'   => 'page',
    165165            'post_parent' => $child_id_1,
    166166        ) );
    167         $grandchild_id_2 = $this->factory->post->create( array(
     167        $grandchild_id_2 = self::$factory->post->create( array(
    168168            'post_name'   => 'grandchild',
    169169            'post_type'   => 'page',
     
    181181        update_option( 'home', home_url( '/example/' ) );
    182182
    183         $id = $this->factory->post->create( array( 'post_title' => 'Hi', 'post_type' => 'page', 'post_name' => 'examp' ) );
     183        $id = self::$factory->post->create( array( 'post_title' => 'Hi', 'post_type' => 'page', 'post_name' => 'examp' ) );
    184184        $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
    185185        $this->assertEquals( $id, url_to_postid( site_url('/example/examp' ) ) );
     
    245245        update_option( 'home', home_url('/example/') );
    246246
    247         $id = $this->factory->post->create( array( 'post_title' => 'Hi', 'post_type' => 'page', 'post_name' => 'example' ) );
     247        $id = self::$factory->post->create( array( 'post_title' => 'Hi', 'post_type' => 'page', 'post_name' => 'example' ) );
    248248
    249249        $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
     
    259259        update_option( 'home', home_url( '/example' ) );
    260260
    261         $this->factory->post->create( array( 'post_title' => 'Collision', 'post_type' => 'page', 'post_name' => 'collision' ) );
     261        self::$factory->post->create( array( 'post_title' => 'Collision', 'post_type' => 'page', 'post_name' => 'collision' ) );
    262262
    263263        // This url should NOT return a post ID
     
    278278        }
    279279
    280         $blog_id = $this->factory->blog->create( array( 'path' => '/example' ) );
     280        $blog_id = self::$factory->blog->create( array( 'path' => '/example' ) );
    281281        switch_to_blog( $blog_id );
    282282
    283         $this->factory->post->create( array( 'post_title' => 'Collision ', 'post_type' => 'page' ) );
     283        self::$factory->post->create( array( 'post_title' => 'Collision ', 'post_type' => 'page' ) );
    284284
    285285        // This url should NOT return a post ID
     
    295295    public function test_is_home_should_be_false_when_visiting_custom_endpoint_without_a_registered_query_var_and_page_on_front_is_set() {
    296296
    297         $page_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
     297        $page_id = self::$factory->post->create( array( 'post_type' => 'page' ) );
    298298        update_option( 'show_on_front', 'page' );
    299299        update_option( 'page_on_front', $page_id );
     
    314314        $this->set_permalink_structure( '/%postname%/' );
    315315
    316         $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_status' => 'trash' ) );
    317         $post_id = $this->factory->post->create( array( 'post_title' => get_post( $page_id )->post_title ) );
    318        
     316        $page_id = self::$factory->post->create( array( 'post_type' => 'page', 'post_status' => 'trash' ) );
     317        $post_id = self::$factory->post->create( array( 'post_title' => get_post( $page_id )->post_title ) );
     318
    319319        $this->assertEquals( $post_id, url_to_postid( get_permalink( $post_id ) ) );
    320320
     
    328328        $this->set_permalink_structure( '/%postname%/' );
    329329
    330         $page_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_status' => 'trash' ) );
    331         $post_id = $this->factory->post->create( array( 'post_title' => get_post( $page_id )->post_title ) );
    332        
     330        $page_id = self::$factory->post->create( array( 'post_type' => 'page', 'post_status' => 'trash' ) );
     331        $post_id = self::$factory->post->create( array( 'post_title' => get_post( $page_id )->post_title ) );
     332
    333333        $this->go_to( get_permalink( $post_id ) );
    334334
Note: See TracChangeset for help on using the changeset viewer.