Make WordPress Core


Ignore:
Timestamp:
11/08/2014 07:28:12 PM (10 years ago)
Author:
boonebgorges
Message:

Share fixtures across 'canonical' automated tests.

Sharing these fixtures results in a speed improvement of almost one minute per
run of the test suite.

My hope is that future WordPress developers will spend this extra minute with
their loved ones, for life on this earth is short, my friends, and the moments
you spend watching WP generate test data can never again be reclaimed from the
grizzled clutches of Time, and none of us are really getting younger, I mean,
geez, have you looked in the mirror lately, Gandalf?

See #30017.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/canonical.php

    r29932 r30277  
    99 * @group query
    1010 */
    11 class Tests_Canonical extends WP_UnitTestCase {
    12 
    13     // This can be defined in a subclass of this class which contains it's own data() method, those tests will be run against the specified permastruct
    14     var $structure = '/%year%/%monthnum%/%day%/%postname%/';
    15 
    16     var $old_current_user;
    17     var $author_id;
    18     var $post_ids;
    19     var $term_ids;
    20 
    21     function setUp() {
    22         global $wp_rewrite;
    23 
    24         parent::setUp();
    25 
    26         update_option( 'page_comments', true );
    27         update_option( 'comments_per_page', 5 );
    28         update_option( 'posts_per_page', 5 );
    29 
    30         $wp_rewrite->init();
    31         $wp_rewrite->set_permalink_structure( $this->structure );
    32 
    33         create_initial_taxonomies();
    34 
    35         $wp_rewrite->flush_rules();
    36 
    37         $this->old_current_user = get_current_user_id();
    38         $this->author_id = $this->factory->user->create( array( 'user_login' => 'canonical-author' ) );
    39         wp_set_current_user( $this->author_id );
    40 
    41         // Already created by install defaults:
    42         // $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'uncategorized' ) );
    43 
    44         $this->term_ids = array();
    45 
    46         $this->factory->post->create( array( 'import_id' => 587, 'post_title' => 'post-format-test-audio', 'post_date' => '2008-06-02 00:00:00' ) );
    47         $post_id = $this->factory->post->create( array( 'post_title' => 'post-format-test-gallery', 'post_date' => '2008-06-10 00:00:00' ) );
    48         $this->factory->post->create( array( 'import_id' => 611, 'post_type' => 'attachment', 'post_title' => 'canola2', 'post_parent' => $post_id ) );
    49 
    50         $this->factory->post->create( array(
    51             'post_title' => 'images-test',
    52             'post_date' => '2008-09-03 00:00:00',
    53             'post_content' => 'Page 1 <!--nextpage--> Page 2 <!--nextpage--> Page 3'
    54         ) );
    55 
    56         $post_id = $this->factory->post->create( array( 'import_id' => 149, 'post_title' => 'comment-test', 'post_date' => '2008-03-03 00:00:00' ) );
    57         $this->factory->comment->create_post_comments( $post_id, 15 );
    58 
    59         $this->factory->post->create( array( 'post_date' => '2008-09-05 00:00:00' ) );
    60 
    61         $this->factory->post->create( array( 'import_id' => 123 ) );
    62         $this->factory->post->create( array( 'import_id' => 1 ) );
    63         $this->factory->post->create( array( 'import_id' => 358 ) );
    64 
    65         $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'sample-page' ) );
    66         $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'about' ) );
    67         $post_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) );
    68         $this->factory->post->create(
    69             array( 'import_id' => 144, 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $post_id,
    70         ) );
    71 
    72         $this->term_ids['/category/parent/'] = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'parent' ) );
    73         $this->term_ids['/category/parent/child-1/'] = $this->factory->term->create( array(
    74             'taxonomy' => 'category', 'name' => 'child-1', 'parent' => $this->term_ids['/category/parent/'],
    75         ) );
    76         $this->term_ids['/category/parent/child-1/child-2/'] = $this->factory->term->create( array(
    77             'taxonomy' => 'category', 'name' => 'child-2', 'parent' => $this->term_ids['/category/parent/child-1/'],
    78         ) );
    79 
    80         $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-a' ) );
    81         $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-b' ) );
    82 
    83         $this->factory->term->create( array( 'name' => 'post-formats' ) );
     11class Tests_Canonical extends WP_Canonical_UnitTestCase {
     12    public static function setUpBeforeClass() {
     13        self::generate_shared_fixtures();
    8414    }
    8515
    86     function tearDown() {
    87         global $wp_rewrite;
    88         parent::tearDown();
    89         wp_set_current_user( $this->old_current_user );
    90 
    91         $wp_rewrite->init();
     16    public static function tearDownAfterClass() {
     17        self::delete_shared_fixtures();
    9218    }
    9319
    94     // URL's are relative to the site "front", ie. /category/uncategorized/ instead of http://site.../category..
    95     // Return url's are full url's with the prepended home.
    96     function get_canonical($test_url) {
    97         $test_url = home_url( $test_url );
     20    public function setUp() {
     21        parent::setUp();
     22        wp_set_current_user( self::$author_id );
     23    }
    9824
    99         $can_url = redirect_canonical( $test_url, false );
    100         if ( ! $can_url )
    101             return $test_url; // No redirect will take place for this request
    102 
    103         return $can_url;
     25    public function tearDown() {
     26        parent::tearDown();
     27        wp_set_current_user( self::$old_current_user );
    10428    }
    10529
     
    10832     */
    10933    function test( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
    110         $this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, (array) $expected_doing_it_wrong );
    111 
    112         if ( $ticket )
    113             $this->knownWPBug( $ticket );
    114 
    115         $ticket_ref = ($ticket > 0) ? 'Ticket #' . $ticket : null;
    116 
    117         if ( is_string($expected) )
    118             $expected = array('url' => $expected);
    119         elseif ( is_array($expected) && !isset($expected['url']) && !isset($expected['qv']) )
    120             $expected = array( 'qv' => $expected );
    121 
    122         if ( !isset($expected['url']) && !isset($expected['qv']) )
    123             $this->markTestSkipped('No valid expected output was provided');
    12434
    12535        if ( false !== strpos( $test_url, '%d' ) ) {
    12636            if ( false !== strpos( $test_url, '/?author=%d' ) )
    127                 $test_url = sprintf( $test_url, $this->author_id );
     37                $test_url = sprintf( $test_url, self::$author_id );
    12838            if ( false !== strpos( $test_url, '?cat=%d' ) )
    129                 $test_url = sprintf( $test_url, $this->term_ids[ $expected['url'] ] );
     39                $test_url = sprintf( $test_url, self::$terms[ $expected['url'] ] );
    13040        }
    13141
    132         $this->go_to( home_url( $test_url ) );
    133 
    134         // Does the redirect match what's expected?
    135         $can_url = $this->get_canonical( $test_url );
    136         $parsed_can_url = parse_url($can_url);
    137 
    138         // Just test the Path and Query if present
    139         if ( isset($expected['url']) )
    140             $this->assertEquals( $expected['url'], $parsed_can_url['path'] . (!empty($parsed_can_url['query']) ? '?' . $parsed_can_url['query'] : ''), $ticket_ref );
    141 
    142         if ( ! isset($expected['qv']) )
    143             return;
    144 
    145         // "make" that the request and check the query is correct
    146         $this->go_to( $can_url );
    147 
    148         // Are all query vars accounted for, And correct?
    149         global $wp;
    150 
    151         $query_vars = array_diff($wp->query_vars, $wp->extra_query_vars);
    152         if ( !empty($parsed_can_url['query']) ) {
    153             parse_str($parsed_can_url['query'], $_qv);
    154 
    155             // $_qv should not contain any elements which are set in $query_vars already (ie. $_GET vars should not be present in the Rewrite)
    156             $this->assertEquals( array(), array_intersect( $query_vars, $_qv ), 'Query vars are duplicated from the Rewrite into $_GET; ' . $ticket_ref );
    157 
    158             $query_vars = array_merge($query_vars, $_qv);
    159         }
    160 
    161         $this->assertEquals( $expected['qv'], $query_vars );
     42        $this->assertCanonical( $test_url, $expected, $ticket, $expected_doing_it_wrong );
    16243    }
    16344
     
    17657            // Categories
    17758
    178             array( '?cat=%d', '/category/parent/', 15256 ),
    179             array( '?cat=%d', '/category/parent/child-1/', 15256 ),
    180             array( '?cat=%d', '/category/parent/child-1/child-2/' ), // no children
     59            array( '?cat=%d', array( 'url' => '/category/parent/' ), 15256 ),
     60            array( '?cat=%d', array( 'url' => '/category/parent/child-1/' ), 15256 ),
     61            array( '?cat=%d', array( 'url' => '/category/parent/child-1/child-2/' ) ), // no children
    18162            array( '/category/uncategorized/', array( 'url' => '/category/uncategorized/', 'qv' => array( 'category_name' => 'uncategorized' ) ) ),
    18263            array( '/category/uncategorized/page/2/', array( 'url' => '/category/uncategorized/page/2/', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2) ) ),
Note: See TracChangeset for help on using the changeset viewer.