Make WordPress Core

Changeset 30277


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.

Location:
trunk/tests/phpunit
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/bootstrap.php

    r28943 r30277  
    9191require dirname( __FILE__ ) . '/testcase-xmlrpc.php';
    9292require dirname( __FILE__ ) . '/testcase-ajax.php';
     93require dirname( __FILE__ ) . '/testcase-canonical.php';
    9394require dirname( __FILE__ ) . '/exceptions.php';
    9495require dirname( __FILE__ ) . '/utils.php';
  • 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) ) ),
  • trunk/tests/phpunit/tests/canonical/customRules.php

    r25002 r30277  
    11<?php
    2 
    3 require_once dirname( dirname( __FILE__ ) ) . '/canonical.php';
    42
    53/**
     
    86 * @group query
    97 */
    10 class Tests_Canonical_CustomRules extends Tests_Canonical {
     8class Tests_Canonical_CustomRules extends WP_Canonical_UnitTestCase {
     9
    1110    function setUp() {
    1211        parent::setUp();
     
    1514        $wp_rewrite->add_rule('ccr/(.+?)/sort/(asc|desc)', 'index.php?category_name=$matches[1]&order=$matches[2]', 'top'); // ccr = Custom_Cat_Rule
    1615        $wp_rewrite->flush_rules();
     16    }
     17
     18    /**
     19     * @dataProvider data
     20     */
     21    function test( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
     22        $this->assertCanonical( $test_url, $expected, $ticket, $expected_doing_it_wrong );
    1723    }
    1824
  • trunk/tests/phpunit/tests/canonical/https.php

    r30160 r30277  
    11<?php
    2 
    3 require_once dirname( dirname( __FILE__ ) ) . '/canonical.php';
    42
    53/**
     
    86 * @group query
    97 */
    10 class Tests_Canonical_HTTPS extends Tests_Canonical {
     8class Tests_Canonical_HTTPS extends WP_Canonical_UnitTestCase {
     9    function setUp() {
     10        global $wp_rewrite;
    1111
    12     function setUp() {
    1312        parent::setUp();
     13
     14        $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
     15        create_initial_taxonomies();
     16        $wp_rewrite->flush_rules();
     17        $wp_rewrite->init();
     18
    1419        $this->http  = set_url_scheme( home_url( 'sample-page/' ), 'http' );
    1520        $this->https = set_url_scheme( home_url( 'sample-page/' ), 'https' );
  • trunk/tests/phpunit/tests/canonical/noRewrite.php

    r25002 r30277  
    88 * @group query
    99 */
    10 class Tests_Canonical_NoRewrite extends Tests_Canonical {
    11 
    12     var $structure = '';
     10class Tests_Canonical_NoRewrite extends WP_Canonical_UnitTestCase {
    1311
    1412    // These test cases are run against the test handler in WP_Canonical
     13    public static function setUpBeforeClass() {
     14        self::generate_shared_fixtures();
     15    }
     16
     17    public static function tearDownAfterClass() {
     18        self::delete_shared_fixtures();
     19    }
     20
     21    public function setUp() {
     22        global $wp_rewrite;
     23
     24        parent::setUp();
     25
     26        $wp_rewrite->init();
     27        $wp_rewrite->set_permalink_structure( '' );
     28        $wp_rewrite->flush_rules();
     29        $wp_rewrite->init();
     30    }
     31
     32    /**
     33     * @dataProvider data
     34     */
     35    function test( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
     36        $this->assertCanonical( $test_url, $expected, $ticket, $expected_doing_it_wrong );
     37    }
    1538
    1639    function data() {
  • trunk/tests/phpunit/tests/canonical/pageOnFront.php

    r28966 r30277  
    11<?php
    2 
    3 require_once dirname( dirname( __FILE__ ) ) . '/canonical.php';
    42
    53/**
     
    86 * @group query
    97 */
    10 class Tests_Canonical_PageOnFront extends Tests_Canonical {
     8class Tests_Canonical_PageOnFront extends WP_Canonical_UnitTestCase {
     9    public static function setUpBeforeClass() {
     10        self::generate_shared_fixtures();
     11    }
     12
     13    public static function tearDownAfterClass() {
     14        self::delete_shared_fixtures();
     15    }
     16
    1117    function setUp() {
    1218        parent::setUp();
     
    2329        parent::tearDown();
    2430        $wp_rewrite->init();
     31    }
     32
     33    /**
     34     * @dataProvider data
     35     */
     36    function test( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
     37        $this->assertCanonical( $test_url, $expected, $ticket, $expected_doing_it_wrong );
    2538    }
    2639
Note: See TracChangeset for help on using the changeset viewer.