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/post.php

    r35224 r35225  
    546546                global $wpdb;
    547547
    548                 $attachment = $this->factory->post->create_and_get( array( 'post_title' => 'some-page', 'post_type' => 'attachment' ) );
    549                 $page       = $this->factory->post->create_and_get( array( 'post_title' => 'some-page', 'post_type' => 'page' ) );
    550                 $other_att  = $this->factory->post->create_and_get( array( 'post_title' => 'some-other-page', 'post_type' => 'attachment' ) );
     548                $attachment = self::$factory->post->create_and_get( array( 'post_title' => 'some-page', 'post_type' => 'attachment' ) );
     549                $page       = self::$factory->post->create_and_get( array( 'post_title' => 'some-page', 'post_type' => 'page' ) );
     550                $other_att  = self::$factory->post->create_and_get( array( 'post_title' => 'some-other-page', 'post_type' => 'attachment' ) );
    551551
    552552                $wpdb->update( $wpdb->posts, array( 'post_name' => 'some-page' ), array( 'ID' => $page->ID ) );
    … …  
    566566
    567567        function test_wp_publish_post() {
    568                 $draft_id = $this->factory->post->create( array( 'post_status' => 'draft' ) );
     568                $draft_id = self::$factory->post->create( array( 'post_status' => 'draft' ) );
    569569
    570570                $post = get_post( $draft_id );
    … …  
    582582        function test_wp_insert_post_and_wp_publish_post_with_future_date() {
    583583                $future_date = gmdate( 'Y-m-d H:i:s', time() + 10000000 );
    584                 $post_id = $this->factory->post->create( array(
     584                $post_id = self::$factory->post->create( array(
    585585                        'post_status' => 'publish',
    586586                        'post_date' => $future_date,
    … …  
    642642         */
    643643        function test_get_page_uri_with_stdclass_post_object() {
    644                 $post_id    = $this->factory->post->create( array( 'post_name' => 'get-page-uri-post-name' ) );
     644                $post_id    = self::$factory->post->create( array( 'post_name' => 'get-page-uri-post-name' ) );
    645645
    646646                // Mimick an old stdClass post object, missing the ancestors field.
    … …  
    665665         */
    666666        function test_get_post_uri_check_orphan() {
    667                 $parent_id = $this->factory->post->create( array( 'post_name' => 'parent' ) );
    668                 $child_id = $this->factory->post->create( array( 'post_name' => 'child', 'post_parent' => $parent_id ) );
     667                $parent_id = self::$factory->post->create( array( 'post_name' => 'parent' ) );
     668                $child_id = self::$factory->post->create( array( 'post_name' => 'child', 'post_parent' => $parent_id ) );
    669669
    670670                // check the parent for good measure
    … …  
    684684        function test_get_post_ancestors_within_loop() {
    685685                global $post;
    686                 $parent_id = $this->factory->post->create();
    687                 $post = $this->factory->post->create_and_get( array( 'post_parent' => $parent_id ) );
     686                $parent_id = self::$factory->post->create();
     687                $post = self::$factory->post->create_and_get( array( 'post_parent' => $parent_id ) );
    688688                $this->assertEquals( array( $parent_id ), get_post_ancestors( 0 ) );
    689689        }
    … …  
    693693         */
    694694        function test_update_invalid_post_id() {
    695                 $post_id = $this->factory->post->create( array( 'post_name' => 'get-page-uri-post-name' ) );
     695                $post_id = self::$factory->post->create( array( 'post_name' => 'get-page-uri-post-name' ) );
    696696                $post = get_post( $post_id, ARRAY_A );
    697697
    … …  
    708708        function test_parse_post_content_single_page() {
    709709                global $multipage, $pages, $numpages;
    710                 $post_id = $this->factory->post->create( array( 'post_content' => 'Page 0' ) );
     710                $post_id = self::$factory->post->create( array( 'post_content' => 'Page 0' ) );
    711711                $post = get_post( $post_id );
    712712                setup_postdata( $post );
    … …  
    719719        function test_parse_post_content_multi_page() {
    720720                global $multipage, $pages, $numpages;
    721                 $post_id = $this->factory->post->create( array( 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
     721                $post_id = self::$factory->post->create( array( 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
    722722                $post = get_post( $post_id );
    723723                setup_postdata( $post );
    … …  
    730730        function test_parse_post_content_remaining_single_page() {
    731731                global $multipage, $pages, $numpages;
    732                 $post_id = $this->factory->post->create( array( 'post_content' => 'Page 0' ) );
     732                $post_id = self::$factory->post->create( array( 'post_content' => 'Page 0' ) );
    733733                $post = get_post( $post_id );
    734734                setup_postdata( $post );
    … …  
    741741        function test_parse_post_content_remaining_multi_page() {
    742742                global $multipage, $pages, $numpages;
    743                 $post_id = $this->factory->post->create( array( 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
     743                $post_id = self::$factory->post->create( array( 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
    744744                $post = get_post( $post_id );
    745745                setup_postdata( $post );
    … …  
    755755        function test_parse_post_content_starting_with_nextpage() {
    756756                global $multipage, $pages, $numpages;
    757                 $post_id = $this->factory->post->create( array( 'post_content' => '<!--nextpage-->Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
     757                $post_id = self::$factory->post->create( array( 'post_content' => '<!--nextpage-->Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
    758758                $post = get_post( $post_id );
    759759                setup_postdata( $post );
    … …  
    769769        function test_parse_post_content_starting_with_nextpage_multi() {
    770770                global $multipage, $pages, $numpages;
    771                 $post_id = $this->factory->post->create( array( 'post_content' => '<!--nextpage-->Page 0' ) );
     771                $post_id = self::$factory->post->create( array( 'post_content' => '<!--nextpage-->Page 0' ) );
    772772                $post = get_post( $post_id );
    773773                setup_postdata( $post );
    … …  
    810810                $post_type = rand_str(20);
    811811                register_post_type( $post_type );
    812                 $this->factory->post->create( array(
     812                self::$factory->post->create( array(
    813813                        'post_type' => $post_type,
    814814                        'post_author' => self::$editor_id
    … …  
    823823                $post_type = rand_str(20);
    824824                register_post_type( $post_type );
    825                 $this->factory->post->create_many( 3, array(
     825                self::$factory->post->create_many( 3, array(
    826826                        'post_type' => $post_type,
    827827                        'post_author' => self::$editor_id
    … …  
    843843
    844844        function test_wp_count_posts_insert_invalidation() {
    845                 $post_ids = $this->factory->post->create_many( 3 );
     845                $post_ids = self::$factory->post->create_many( 3 );
    846846                $initial_counts = wp_count_posts();
    847847
    … …  
    861861
    862862        function test_wp_count_posts_trash_invalidation() {
    863                 $post_ids = $this->factory->post->create_many( 3 );
     863                $post_ids = self::$factory->post->create_many( 3 );
    864864                $initial_counts = wp_count_posts();
    865865
    … …  
    882882         */
    883883        function test_get_the_date_with_id_returns_correct_time() {
    884                 $post_id = $this->factory->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) );
     884                $post_id = self::$factory->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) );
    885885                $this->assertEquals( 'March 1, 2014', get_the_date( 'F j, Y', $post_id ) );
    886886        }
    … …  
    900900         */
    901901        function test_get_the_time_with_id_returns_correct_time() {
    902                 $post_id = $this->factory->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) );
     902                $post_id = self::$factory->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) );
    903903                $this->assertEquals( '16:35:00', get_the_time( 'H:i:s', $post_id ) );
    904904        }
    … …  
    918918         */
    919919        function test_get_post_time_with_id_returns_correct_time() {
    920                 $post_id = $this->factory->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) );
     920                $post_id = self::$factory->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) );
    921921                $this->assertEquals( '16:35:00', get_post_time( 'H:i:s', false, $post_id ) );
    922922        }
    … …  
    936936         */
    937937        function test_get_post_modified_time_with_id_returns_correct_time() {
    938                 $post_id = $this->factory->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) );
     938                $post_id = self::$factory->post->create( array( 'post_date' => '2014-03-01 16:35:00' ) );
    939939                $this->assertEquals( '16:35:00', get_post_modified_time( 'H:i:s', false, $post_id ) );
    940940        }
    … …  
    974974                register_taxonomy( $tax, $post_type );
    975975
    976                 $post = $this->factory->post->create( array( 'post_type' => $post_type ) );
     976                $post = self::$factory->post->create( array( 'post_type' => $post_type ) );
    977977                wp_set_object_terms( $post, rand_str(), $tax );
    978978
    … …  
    10161016                require_once( ABSPATH . '/wp-admin/includes/post.php' );
    10171017
    1018                 $post_id = $this->factory->post->create();
     1018                $post_id = self::$factory->post->create();
    10191019
    10201020                $data = array(
    … …  
    10441044         */
    10451045        function test_wp_insert_post_default_comment_ping_status_open() {
    1046                 $post_id = $this->factory->post->create( array(
     1046                $post_id = self::$factory->post->create( array(
    10471047                        'post_author' => self::$editor_id,
    10481048                        'post_status' => 'public',
    … …  
    10601060         */
    10611061        function test_wp_insert_post_page_default_comment_ping_status_closed() {
    1062                 $post_id = $this->factory->post->create( array(
     1062                $post_id = self::$factory->post->create( array(
    10631063                        'post_author' => self::$editor_id,
    10641064                        'post_status' => 'public',
    … …  
    10791079                $post_type = rand_str(20);
    10801080                register_post_type( $post_type, array( 'supports' => array( 'comments', 'trackbacks' ) ) );
    1081                 $post_id = $this->factory->post->create( array(
     1081                $post_id = self::$factory->post->create( array(
    10821082                        'post_author' => self::$editor_id,
    10831083                        'post_status' => 'public',
    … …  
    10991099                $post_type = rand_str(20);
    11001100                register_post_type( $post_type );
    1101                 $post_id = $this->factory->post->create( array(
     1101                $post_id = self::$factory->post->create( array(
    11021102                        'post_author' => self::$editor_id,
    11031103                        'post_status' => 'public',
    … …  
    11281128
    11291129                // Create a sticky post.
    1130                 $post = $this->factory->post->create_and_get( array(
     1130                $post = self::$factory->post->create_and_get( array(
    11311131                        'post_title'   => 'Will be changed',
    11321132                        'post_content' => 'Will be changed',
    … …  
    11571157        function test_user_without_publish_cannot_affect_sticky_with_edit_post() {
    11581158                // Create a sticky post.
    1159                 $post = $this->factory->post->create_and_get( array(
     1159                $post = self::$factory->post->create_and_get( array(
    11601160                        'post_title'   => 'Will be changed',
    11611161                        'post_content' => 'Will be changed',
    … …  
    11921192         */
    11931193        public function test_wp_insert_post_author_zero() {
    1194                 $post_id = $this->factory->post->create( array( 'post_author' => 0 ) );
     1194                $post_id = self::$factory->post->create( array( 'post_author' => 0 ) );
    11951195
    11961196                $this->assertEquals( 0, get_post( $post_id )->post_author );
    … …  
    12011201         */
    12021202        public function test_wp_insert_post_author_null() {
    1203                 $post_id = $this->factory->post->create( array( 'post_author' => null ) );
     1203                $post_id = self::$factory->post->create( array( 'post_author' => null ) );
    12041204
    12051205                $this->assertEquals( self::$editor_id, get_post( $post_id )->post_author );
Note: See TracChangeset for help on using the changeset viewer.