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.

git-svn-id: https://develop.svn.wordpress.org/trunk@30277 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2014-11-08 19:28:12 +00:00
parent fe726039bd
commit d63ceb08a3
7 changed files with 297 additions and 149 deletions

View File

@ -90,6 +90,7 @@ _delete_all_posts();
require dirname( __FILE__ ) . '/testcase.php';
require dirname( __FILE__ ) . '/testcase-xmlrpc.php';
require dirname( __FILE__ ) . '/testcase-ajax.php';
require dirname( __FILE__ ) . '/testcase-canonical.php';
require dirname( __FILE__ ) . '/exceptions.php';
require dirname( __FILE__ ) . '/utils.php';

View File

@ -0,0 +1,219 @@
<?php
class WP_Canonical_UnitTestCase extends WP_UnitTestCase {
static $old_current_user;
static $author_id;
static $post_ids = array();
static $comment_ids = array();
static $term_ids = array();
static $terms = array();
static $old_options = array();
/**
* This can be defined in a subclass of this class which contains its own data() method.
* Those tests will be run against the specified permastruct.
*/
public $structure = '/%year%/%monthnum%/%day%/%postname%/';
public function setUp() {
global $wp_rewrite;
parent::setUp();
update_option( 'page_comments', true );
update_option( 'comments_per_page', 5 );
update_option( 'posts_per_page', 5 );
$wp_rewrite->init();
$wp_rewrite->set_permalink_structure( $this->structure );
create_initial_taxonomies();
$wp_rewrite->flush_rules();
}
/**
* Generate fixtures to be shared between canonical tests.
*
* Abstracted here because it's invoked by setUpBeforeClass() in more than one class.
*
* @since 4.1.0
*/
public static function generate_shared_fixtures() {
global $wp_rewrite;
$factory = new WP_UnitTest_Factory;
self::$old_current_user = get_current_user_id();
self::$author_id = $factory->user->create( array( 'user_login' => 'canonical-author' ) );
/*
* Also set in self::setUp(), but we must configure here to make sure that
* post authorship is properly attributed for fixtures.
*/
wp_set_current_user( self::$author_id );
// Already created by install defaults:
// $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'uncategorized' ) );
self::$post_ids[] = $factory->post->create( array( 'import_id' => 587, 'post_title' => 'post-format-test-audio', 'post_date' => '2008-06-02 00:00:00' ) );
self::$post_ids[] = $post_id = $factory->post->create( array( 'post_title' => 'post-format-test-gallery', 'post_date' => '2008-06-10 00:00:00' ) );
self::$post_ids[] = $factory->post->create( array( 'import_id' => 611, 'post_type' => 'attachment', 'post_title' => 'canola2', 'post_parent' => $post_id ) );
self::$post_ids[] = $factory->post->create( array(
'post_title' => 'images-test',
'post_date' => '2008-09-03 00:00:00',
'post_content' => 'Page 1 <!--nextpage--> Page 2 <!--nextpage--> Page 3'
) );
self::$post_ids[] = $post_id = $factory->post->create( array( 'import_id' => 149, 'post_title' => 'comment-test', 'post_date' => '2008-03-03 00:00:00' ) );
self::$comment_ids = $factory->comment->create_post_comments( $post_id, 15 );
self::$post_ids[] = $factory->post->create( array( 'post_date' => '2008-09-05 00:00:00' ) );
self::$post_ids[] = $factory->post->create( array( 'import_id' => 123 ) );
self::$post_ids[] = $factory->post->create( array( 'import_id' => 1 ) );
self::$post_ids[] = $factory->post->create( array( 'import_id' => 358 ) );
self::$post_ids[] = $factory->post->create( array( 'post_type' => 'page', 'post_title' => 'sample-page' ) );
self::$post_ids[] = $factory->post->create( array( 'post_type' => 'page', 'post_title' => 'about' ) );
self::$post_ids[] = $post_id = $factory->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) );
self::$post_ids[] = $factory->post->create(
array( 'import_id' => 144, 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $post_id,
) );
$cat1 = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'parent' ) );
self::$terms['/category/parent/'] = $cat1;
self::$term_ids[ $cat1 ] = 'category';
$cat2 = $factory->term->create( array(
'taxonomy' => 'category', 'name' => 'child-1', 'parent' => self::$terms['/category/parent/'],
) );
self::$terms['/category/parent/child-1/'] = $cat2;
self::$term_ids[ $cat2 ] = 'category';
$cat3 = $factory->term->create( array(
'taxonomy' => 'category', 'name' => 'child-2', 'parent' => self::$terms['/category/parent/child-1/'],
) );
self::$terms['/category/parent/child-1/child-2/'] = $cat3;
self::$term_ids[ $cat3 ] = 'category';
$cat4 = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-a' ) );
self::$term_ids[ $cat4 ] = 'category';
$cat5 = $factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-b' ) );
self::$term_ids[ $cat5 ] = 'category';
$tag1 = $factory->term->create( array( 'name' => 'post-formats' ) );
self::$term_ids[ $tag1 ] = 'post_tag';
self::commit_transaction();
}
/**
* Clean up shared fixtures.
*
* @since 4.1.0
*/
public static function delete_shared_fixtures() {
global $wp_rewrite;
wp_delete_user( self::$author_id );
foreach ( self::$post_ids as $pid ) {
wp_delete_post( $pid, true );
}
foreach ( self::$comment_ids as $cid ) {
wp_delete_comment( $cid, true );
}
foreach ( self::$term_ids as $tid => $tax ) {
wp_delete_term( $tid, $tax );
}
self::$author_id = null;
self::$post_ids = array();
self::$comment_ids = array();
self::$term_ids = array();
self::$terms = array();
self::commit_transaction();
}
/**
* Assert that a given URL is the same a the canonical URL generated by WP.
*
* @since 4.1.0
*
* @param string $test_url Raw URL that will be run through redirect_canonical().
* @param string $expected Expected string.
* @param int $ticket Optional. Trac ticket number.
* @param array $expected_doing_it_wrong Array of class/function names expected to throw _doing_it_wrong() notices.
*/
public function assertCanonical( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
$this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, (array) $expected_doing_it_wrong );
if ( $ticket )
$this->knownWPBug( $ticket );
$ticket_ref = ($ticket > 0) ? 'Ticket #' . $ticket : null;
global $wpdb;
//print_r( $wpdb->get_results( "SELECT * FROM $wpdb->terms" ) );
if ( is_string($expected) )
$expected = array('url' => $expected);
elseif ( is_array($expected) && !isset($expected['url']) && !isset($expected['qv']) )
$expected = array( 'qv' => $expected );
if ( !isset($expected['url']) && !isset($expected['qv']) )
$this->markTestSkipped('No valid expected output was provided');
$this->go_to( home_url( $test_url ) );
// Does the redirect match what's expected?
$can_url = $this->get_canonical( $test_url );
$parsed_can_url = parse_url($can_url);
// Just test the Path and Query if present
if ( isset($expected['url']) ) {
$this->assertEquals( $expected['url'], $parsed_can_url['path'] . (!empty($parsed_can_url['query']) ? '?' . $parsed_can_url['query'] : ''), $ticket_ref );
}
if ( ! isset($expected['qv']) )
return;
// "make" that the request and check the query is correct
$this->go_to( $can_url );
// Are all query vars accounted for, And correct?
global $wp;
$query_vars = array_diff($wp->query_vars, $wp->extra_query_vars);
if ( !empty($parsed_can_url['query']) ) {
parse_str($parsed_can_url['query'], $_qv);
// $_qv should not contain any elements which are set in $query_vars already (ie. $_GET vars should not be present in the Rewrite)
$this->assertEquals( array(), array_intersect( $query_vars, $_qv ), 'Query vars are duplicated from the Rewrite into $_GET; ' . $ticket_ref );
$query_vars = array_merge($query_vars, $_qv);
}
$this->assertEquals( $expected['qv'], $query_vars );
}
/**
* Get the canonical URL given a raw URL.
*
* @param string $test_url Should be relative to the site "front", ie /category/uncategorized/
* as opposed to http://example.com/category/uncategorized/
* @return $can_url Returns the original $test_url if no canonical can be generated, otherwise returns
* the fully-qualified URL as generated by redirect_canonical().
*/
public function get_canonical( $test_url ) {
$test_url = home_url( $test_url );
$can_url = redirect_canonical( $test_url, false );
if ( ! $can_url )
return $test_url; // No redirect will take place for this request
return $can_url;
}
}

View File

@ -8,157 +8,38 @@
* @group rewrite
* @group query
*/
class Tests_Canonical extends WP_UnitTestCase {
class Tests_Canonical extends WP_Canonical_UnitTestCase {
public static function setUpBeforeClass() {
self::generate_shared_fixtures();
}
// 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
var $structure = '/%year%/%monthnum%/%day%/%postname%/';
var $old_current_user;
var $author_id;
var $post_ids;
var $term_ids;
function setUp() {
global $wp_rewrite;
public static function tearDownAfterClass() {
self::delete_shared_fixtures();
}
public function setUp() {
parent::setUp();
update_option( 'page_comments', true );
update_option( 'comments_per_page', 5 );
update_option( 'posts_per_page', 5 );
$wp_rewrite->init();
$wp_rewrite->set_permalink_structure( $this->structure );
create_initial_taxonomies();
$wp_rewrite->flush_rules();
$this->old_current_user = get_current_user_id();
$this->author_id = $this->factory->user->create( array( 'user_login' => 'canonical-author' ) );
wp_set_current_user( $this->author_id );
// Already created by install defaults:
// $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'uncategorized' ) );
$this->term_ids = array();
$this->factory->post->create( array( 'import_id' => 587, 'post_title' => 'post-format-test-audio', 'post_date' => '2008-06-02 00:00:00' ) );
$post_id = $this->factory->post->create( array( 'post_title' => 'post-format-test-gallery', 'post_date' => '2008-06-10 00:00:00' ) );
$this->factory->post->create( array( 'import_id' => 611, 'post_type' => 'attachment', 'post_title' => 'canola2', 'post_parent' => $post_id ) );
$this->factory->post->create( array(
'post_title' => 'images-test',
'post_date' => '2008-09-03 00:00:00',
'post_content' => 'Page 1 <!--nextpage--> Page 2 <!--nextpage--> Page 3'
) );
$post_id = $this->factory->post->create( array( 'import_id' => 149, 'post_title' => 'comment-test', 'post_date' => '2008-03-03 00:00:00' ) );
$this->factory->comment->create_post_comments( $post_id, 15 );
$this->factory->post->create( array( 'post_date' => '2008-09-05 00:00:00' ) );
$this->factory->post->create( array( 'import_id' => 123 ) );
$this->factory->post->create( array( 'import_id' => 1 ) );
$this->factory->post->create( array( 'import_id' => 358 ) );
$this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'sample-page' ) );
$this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'about' ) );
$post_id = $this->factory->post->create( array( 'post_type' => 'page', 'post_title' => 'parent-page' ) );
$this->factory->post->create(
array( 'import_id' => 144, 'post_type' => 'page', 'post_title' => 'child-page-1', 'post_parent' => $post_id,
) );
$this->term_ids['/category/parent/'] = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'parent' ) );
$this->term_ids['/category/parent/child-1/'] = $this->factory->term->create( array(
'taxonomy' => 'category', 'name' => 'child-1', 'parent' => $this->term_ids['/category/parent/'],
) );
$this->term_ids['/category/parent/child-1/child-2/'] = $this->factory->term->create( array(
'taxonomy' => 'category', 'name' => 'child-2', 'parent' => $this->term_ids['/category/parent/child-1/'],
) );
$this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-a' ) );
$this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-b' ) );
$this->factory->term->create( array( 'name' => 'post-formats' ) );
wp_set_current_user( self::$author_id );
}
function tearDown() {
global $wp_rewrite;
public function tearDown() {
parent::tearDown();
wp_set_current_user( $this->old_current_user );
$wp_rewrite->init();
}
// URL's are relative to the site "front", ie. /category/uncategorized/ instead of http://site.../category..
// Return url's are full url's with the prepended home.
function get_canonical($test_url) {
$test_url = home_url( $test_url );
$can_url = redirect_canonical( $test_url, false );
if ( ! $can_url )
return $test_url; // No redirect will take place for this request
return $can_url;
wp_set_current_user( self::$old_current_user );
}
/**
* @dataProvider data
*/
function test( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
$this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, (array) $expected_doing_it_wrong );
if ( $ticket )
$this->knownWPBug( $ticket );
$ticket_ref = ($ticket > 0) ? 'Ticket #' . $ticket : null;
if ( is_string($expected) )
$expected = array('url' => $expected);
elseif ( is_array($expected) && !isset($expected['url']) && !isset($expected['qv']) )
$expected = array( 'qv' => $expected );
if ( !isset($expected['url']) && !isset($expected['qv']) )
$this->markTestSkipped('No valid expected output was provided');
if ( false !== strpos( $test_url, '%d' ) ) {
if ( false !== strpos( $test_url, '/?author=%d' ) )
$test_url = sprintf( $test_url, $this->author_id );
$test_url = sprintf( $test_url, self::$author_id );
if ( false !== strpos( $test_url, '?cat=%d' ) )
$test_url = sprintf( $test_url, $this->term_ids[ $expected['url'] ] );
$test_url = sprintf( $test_url, self::$terms[ $expected['url'] ] );
}
$this->go_to( home_url( $test_url ) );
// Does the redirect match what's expected?
$can_url = $this->get_canonical( $test_url );
$parsed_can_url = parse_url($can_url);
// Just test the Path and Query if present
if ( isset($expected['url']) )
$this->assertEquals( $expected['url'], $parsed_can_url['path'] . (!empty($parsed_can_url['query']) ? '?' . $parsed_can_url['query'] : ''), $ticket_ref );
if ( ! isset($expected['qv']) )
return;
// "make" that the request and check the query is correct
$this->go_to( $can_url );
// Are all query vars accounted for, And correct?
global $wp;
$query_vars = array_diff($wp->query_vars, $wp->extra_query_vars);
if ( !empty($parsed_can_url['query']) ) {
parse_str($parsed_can_url['query'], $_qv);
// $_qv should not contain any elements which are set in $query_vars already (ie. $_GET vars should not be present in the Rewrite)
$this->assertEquals( array(), array_intersect( $query_vars, $_qv ), 'Query vars are duplicated from the Rewrite into $_GET; ' . $ticket_ref );
$query_vars = array_merge($query_vars, $_qv);
}
$this->assertEquals( $expected['qv'], $query_vars );
$this->assertCanonical( $test_url, $expected, $ticket, $expected_doing_it_wrong );
}
function data() {
@ -175,9 +56,9 @@ class Tests_Canonical extends WP_UnitTestCase {
return array(
// Categories
array( '?cat=%d', '/category/parent/', 15256 ),
array( '?cat=%d', '/category/parent/child-1/', 15256 ),
array( '?cat=%d', '/category/parent/child-1/child-2/' ), // no children
array( '?cat=%d', array( 'url' => '/category/parent/' ), 15256 ),
array( '?cat=%d', array( 'url' => '/category/parent/child-1/' ), 15256 ),
array( '?cat=%d', array( 'url' => '/category/parent/child-1/child-2/' ) ), // no children
array( '/category/uncategorized/', array( 'url' => '/category/uncategorized/', 'qv' => array( 'category_name' => 'uncategorized' ) ) ),
array( '/category/uncategorized/page/2/', array( 'url' => '/category/uncategorized/page/2/', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2) ) ),
array( '/category/uncategorized/?paged=2', array( 'url' => '/category/uncategorized/page/2/', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2) ) ),

View File

@ -1,13 +1,12 @@
<?php
require_once dirname( dirname( __FILE__ ) ) . '/canonical.php';
/**
* @group canonical
* @group rewrite
* @group query
*/
class Tests_Canonical_CustomRules extends Tests_Canonical {
class Tests_Canonical_CustomRules extends WP_Canonical_UnitTestCase {
function setUp() {
parent::setUp();
global $wp_rewrite;
@ -16,6 +15,13 @@ class Tests_Canonical_CustomRules extends Tests_Canonical {
$wp_rewrite->flush_rules();
}
/**
* @dataProvider data
*/
function test( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
$this->assertCanonical( $test_url, $expected, $ticket, $expected_doing_it_wrong );
}
function data() {
/* Format:
* [0]: $test_url,

View File

@ -1,16 +1,21 @@
<?php
require_once dirname( dirname( __FILE__ ) ) . '/canonical.php';
/**
* @group canonical
* @group rewrite
* @group query
*/
class Tests_Canonical_HTTPS extends Tests_Canonical {
class Tests_Canonical_HTTPS extends WP_Canonical_UnitTestCase {
function setUp() {
global $wp_rewrite;
parent::setUp();
$wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
create_initial_taxonomies();
$wp_rewrite->flush_rules();
$wp_rewrite->init();
$this->http = set_url_scheme( home_url( 'sample-page/' ), 'http' );
$this->https = set_url_scheme( home_url( 'sample-page/' ), 'https' );
}

View File

@ -7,11 +7,34 @@ require_once dirname( dirname( __FILE__ ) ) . '/canonical.php';
* @group rewrite
* @group query
*/
class Tests_Canonical_NoRewrite extends Tests_Canonical {
var $structure = '';
class Tests_Canonical_NoRewrite extends WP_Canonical_UnitTestCase {
// These test cases are run against the test handler in WP_Canonical
public static function setUpBeforeClass() {
self::generate_shared_fixtures();
}
public static function tearDownAfterClass() {
self::delete_shared_fixtures();
}
public function setUp() {
global $wp_rewrite;
parent::setUp();
$wp_rewrite->init();
$wp_rewrite->set_permalink_structure( '' );
$wp_rewrite->flush_rules();
$wp_rewrite->init();
}
/**
* @dataProvider data
*/
function test( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
$this->assertCanonical( $test_url, $expected, $ticket, $expected_doing_it_wrong );
}
function data() {
/* Format:

View File

@ -1,13 +1,19 @@
<?php
require_once dirname( dirname( __FILE__ ) ) . '/canonical.php';
/**
* @group canonical
* @group rewrite
* @group query
*/
class Tests_Canonical_PageOnFront extends Tests_Canonical {
class Tests_Canonical_PageOnFront extends WP_Canonical_UnitTestCase {
public static function setUpBeforeClass() {
self::generate_shared_fixtures();
}
public static function tearDownAfterClass() {
self::delete_shared_fixtures();
}
function setUp() {
parent::setUp();
global $wp_rewrite;
@ -24,6 +30,13 @@ class Tests_Canonical_PageOnFront extends Tests_Canonical {
$wp_rewrite->init();
}
/**
* @dataProvider data
*/
function test( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
$this->assertCanonical( $test_url, $expected, $ticket, $expected_doing_it_wrong );
}
function data() {
/* Format:
* [0]: $test_url,