<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[30277] trunk/tests/phpunit: Share fixtures across 'canonical' automated tests.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/30277">30277</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/30277","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2014-11-08 19:28:12 +0000 (Sat, 08 Nov 2014)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/30017">#30017</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunktestsphpunitincludesbootstrapphp">trunk/tests/phpunit/includes/bootstrap.php</a></li>
<li><a href="#trunktestsphpunittestscanonicalcustomRulesphp">trunk/tests/phpunit/tests/canonical/customRules.php</a></li>
<li><a href="#trunktestsphpunittestscanonicalhttpsphp">trunk/tests/phpunit/tests/canonical/https.php</a></li>
<li><a href="#trunktestsphpunittestscanonicalnoRewritephp">trunk/tests/phpunit/tests/canonical/noRewrite.php</a></li>
<li><a href="#trunktestsphpunittestscanonicalpageOnFrontphp">trunk/tests/phpunit/tests/canonical/pageOnFront.php</a></li>
<li><a href="#trunktestsphpunittestscanonicalphp">trunk/tests/phpunit/tests/canonical.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunitincludestestcasecanonicalphp">trunk/tests/phpunit/includes/testcase-canonical.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunktestsphpunitincludesbootstrapphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/includes/bootstrap.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/bootstrap.php        2014-11-08 15:29:31 UTC (rev 30276)
+++ trunk/tests/phpunit/includes/bootstrap.php  2014-11-08 19:28:12 UTC (rev 30277)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -90,6 +90,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require dirname( __FILE__ ) . '/testcase.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require dirname( __FILE__ ) . '/testcase-xmlrpc.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require dirname( __FILE__ ) . '/testcase-ajax.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require dirname( __FILE__ ) . '/testcase-canonical.php';
</ins><span class="cx" style="display: block; padding: 0 10px"> require dirname( __FILE__ ) . '/exceptions.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require dirname( __FILE__ ) . '/utils.php';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunktestsphpunitincludestestcasecanonicalphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/includes/testcase-canonical.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/testcase-canonical.php                               (rev 0)
+++ trunk/tests/phpunit/includes/testcase-canonical.php 2014-11-08 19:28:12 UTC (rev 30277)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,219 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?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;
+       }
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestscanonicalcustomRulesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/canonical/customRules.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/canonical/customRules.php       2014-11-08 15:29:31 UTC (rev 30276)
+++ trunk/tests/phpunit/tests/canonical/customRules.php 2014-11-08 19:28:12 UTC (rev 30277)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,13 +1,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-require_once dirname( dirname( __FILE__ ) ) . '/canonical.php';
-
</del><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px">  * @group canonical
</span><span class="cx" style="display: block; padding: 0 10px">  * @group rewrite
</span><span class="cx" style="display: block; padding: 0 10px">  * @group query
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-class Tests_Canonical_CustomRules extends Tests_Canonical {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+class Tests_Canonical_CustomRules extends WP_Canonical_UnitTestCase {
+
</ins><span class="cx" style="display: block; padding: 0 10px">         function setUp() {
</span><span class="cx" style="display: block; padding: 0 10px">                parent::setUp();
</span><span class="cx" style="display: block; padding: 0 10px">                global $wp_rewrite;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -16,6 +15,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $wp_rewrite->flush_rules();
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * @dataProvider data
+        */
+       function test( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
+               $this->assertCanonical( $test_url, $expected, $ticket, $expected_doing_it_wrong );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         function data() {
</span><span class="cx" style="display: block; padding: 0 10px">                /* Format:
</span><span class="cx" style="display: block; padding: 0 10px">                 * [0]: $test_url,
</span></span></pre></div>
<a id="trunktestsphpunittestscanonicalhttpsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/canonical/https.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/canonical/https.php     2014-11-08 15:29:31 UTC (rev 30276)
+++ trunk/tests/phpunit/tests/canonical/https.php       2014-11-08 19:28:12 UTC (rev 30277)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,16 +1,21 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-require_once dirname( dirname( __FILE__ ) ) . '/canonical.php';
-
</del><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px">  * @group canonical
</span><span class="cx" style="display: block; padding: 0 10px">  * @group rewrite
</span><span class="cx" style="display: block; padding: 0 10px">  * @group query
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-class Tests_Canonical_HTTPS extends Tests_Canonical {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+class Tests_Canonical_HTTPS extends WP_Canonical_UnitTestCase {
+       function setUp() {
+               global $wp_rewrite;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function setUp() {
</del><span class="cx" style="display: block; padding: 0 10px">                 parent::setUp();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
+               create_initial_taxonomies();
+               $wp_rewrite->flush_rules();
+               $wp_rewrite->init();
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->http  = set_url_scheme( home_url( 'sample-page/' ), 'http' );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->https = set_url_scheme( home_url( 'sample-page/' ), 'https' );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span></span></pre></div>
<a id="trunktestsphpunittestscanonicalnoRewritephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/canonical/noRewrite.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/canonical/noRewrite.php 2014-11-08 15:29:31 UTC (rev 30276)
+++ trunk/tests/phpunit/tests/canonical/noRewrite.php   2014-11-08 19:28:12 UTC (rev 30277)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7,12 +7,35 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @group rewrite
</span><span class="cx" style="display: block; padding: 0 10px">  * @group query
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-class Tests_Canonical_NoRewrite extends Tests_Canonical {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+class Tests_Canonical_NoRewrite extends WP_Canonical_UnitTestCase {
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        var $structure = '';
-
</del><span class="cx" style="display: block; padding: 0 10px">         // These test cases are run against the test handler in WP_Canonical
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        public static function setUpBeforeClass() {
+               self::generate_shared_fixtures();
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        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 );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         function data() {
</span><span class="cx" style="display: block; padding: 0 10px">                /* Format:
</span><span class="cx" style="display: block; padding: 0 10px">                 * [0]: $test_url,
</span></span></pre></div>
<a id="trunktestsphpunittestscanonicalpageOnFrontphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/canonical/pageOnFront.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/canonical/pageOnFront.php       2014-11-08 15:29:31 UTC (rev 30276)
+++ trunk/tests/phpunit/tests/canonical/pageOnFront.php 2014-11-08 19:28:12 UTC (rev 30277)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,13 +1,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-require_once dirname( dirname( __FILE__ ) ) . '/canonical.php';
-
</del><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px">  * @group canonical
</span><span class="cx" style="display: block; padding: 0 10px">  * @group rewrite
</span><span class="cx" style="display: block; padding: 0 10px">  * @group query
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-class Tests_Canonical_PageOnFront extends Tests_Canonical {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+class Tests_Canonical_PageOnFront extends WP_Canonical_UnitTestCase {
+       public static function setUpBeforeClass() {
+               self::generate_shared_fixtures();
+       }
+
+       public static function tearDownAfterClass() {
+               self::delete_shared_fixtures();
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         function setUp() {
</span><span class="cx" style="display: block; padding: 0 10px">                parent::setUp();
</span><span class="cx" style="display: block; padding: 0 10px">                global $wp_rewrite;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -24,6 +30,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $wp_rewrite->init();
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * @dataProvider data
+        */
+       function test( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
+               $this->assertCanonical( $test_url, $expected, $ticket, $expected_doing_it_wrong );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         function data() {
</span><span class="cx" style="display: block; padding: 0 10px">                /* Format:
</span><span class="cx" style="display: block; padding: 0 10px">                 * [0]: $test_url,
</span></span></pre></div>
<a id="trunktestsphpunittestscanonicalphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/canonical.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/canonical.php   2014-11-08 15:29:31 UTC (rev 30276)
+++ trunk/tests/phpunit/tests/canonical.php     2014-11-08 19:28:12 UTC (rev 30277)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -8,157 +8,38 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @group rewrite
</span><span class="cx" style="display: block; padding: 0 10px">  * @group query
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-class Tests_Canonical extends WP_UnitTestCase {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+class Tests_Canonical extends WP_Canonical_UnitTestCase {
+       public static function setUpBeforeClass() {
+               self::generate_shared_fixtures();
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        // 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%/';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public static function tearDownAfterClass() {
+               self::delete_shared_fixtures();
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        var $old_current_user;
-       var $author_id;
-       var $post_ids;
-       var $term_ids;
-
-       function setUp() {
-               global $wp_rewrite;
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function setUp() {
</ins><span class="cx" style="display: block; padding: 0 10px">                 parent::setUp();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-               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' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         wp_set_current_user( self::$author_id );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        function tearDown() {
-               global $wp_rewrite;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function tearDown() {
</ins><span class="cx" style="display: block; padding: 0 10px">                 parent::tearDown();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                wp_set_current_user( $this->old_current_user );
-
-               $wp_rewrite->init();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         wp_set_current_user( self::$old_current_user );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        // 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;
-       }
-
</del><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @dataProvider data
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        function test( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, (array) $expected_doing_it_wrong );
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                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');
-
</del><span class="cx" style="display: block; padding: 0 10px">                 if ( false !== strpos( $test_url, '%d' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( false !== strpos( $test_url, '/?author=%d' ) )
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $test_url = sprintf( $test_url, $this->author_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $test_url = sprintf( $test_url, self::$author_id );
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( false !== strpos( $test_url, '?cat=%d' ) )
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $test_url = sprintf( $test_url, $this->term_ids[ $expected['url'] ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $test_url = sprintf( $test_url, self::$terms[ $expected['url'] ] );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $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 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertCanonical( $test_url, $expected, $ticket, $expected_doing_it_wrong );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        function data() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -175,9 +56,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return array(
</span><span class="cx" style="display: block; padding: 0 10px">                        // Categories
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        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
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 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
</ins><span class="cx" style="display: block; padding: 0 10px">                         array( '/category/uncategorized/', array( 'url' => '/category/uncategorized/', 'qv' => array( 'category_name' => 'uncategorized' ) ) ),
</span><span class="cx" style="display: block; padding: 0 10px">                        array( '/category/uncategorized/page/2/', array( 'url' => '/category/uncategorized/page/2/', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2) ) ),
</span><span class="cx" style="display: block; padding: 0 10px">                        array( '/category/uncategorized/?paged=2', array( 'url' => '/category/uncategorized/page/2/', 'qv' => array( 'category_name' => 'uncategorized', 'paged' => 2) ) ),
</span></span></pre>
</div>
</div>

</body>
</html>