<!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>[849] trunk/wp-testcase/test_includes_canonical.php: Port over the canonical 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, #logmsg > ol { margin-left: 0; 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">
<dt>Revision</dt> <dd><a href="http://unit-tests.trac.wordpress.org/changeset/849">849</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2012-07-02 06:16:05 +0000 (Mon, 02 Jul 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Port over the canonical tests. This is exactly as fun as it sounds. fixes <a href="http://unit-tests.trac.wordpress.org/ticket/66">#66</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwptestcasetest_includes_canonicalphp">trunk/wp-testcase/test_includes_canonical.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwptestcasetest_includes_canonicalphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-testcase/test_includes_canonical.php (848 => 849)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-testcase/test_includes_canonical.php        2012-07-02 06:15:14 UTC (rev 848)
+++ trunk/wp-testcase/test_includes_canonical.php        2012-07-02 06:16:05 UTC (rev 849)
</span><span class="lines">@@ -7,43 +7,92 @@
</span><span class="cx">  *
</span><span class="cx">  */
</span><span class="cx"> 
</span><del>-class WP_Canonical extends _WPDataset2 {
</del><ins>+class WP_Test_Canonical extends WP_UnitTestCase {
</ins><span class="cx"> 
</span><span class="cx">         // 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
</span><span class="cx">         var $structure = '/%year%/%monthnum%/%day%/%postname%/';
</span><span class="cx"> 
</span><del>-        function SetUp() {
-                parent::SetUp();
</del><ins>+        var $old_current_user;
+        var $author_id;
+        var $post_ids;
+        var $term_ids;
</ins><span class="cx"> 
</span><del>-                update_option('permalink_structure', $this-&gt;structure);
-                update_option('comments_per_page', 5);
</del><ins>+        function setUp() {
+                parent::setUp();
</ins><span class="cx"> 
</span><del>-                global $wp_rewrite;
-                $wp_rewrite-&gt;set_permalink_structure($this-&gt;structure);
</del><ins>+                update_option( 'comments_per_page', 5 );
+                update_option( 'posts_per_page', 5 );
+
+                update_option( 'permalink_structure', $this-&gt;structure );
</ins><span class="cx">                 create_initial_taxonomies();
</span><ins>+                $GLOBALS['wp_rewrite']-&gt;init();
+                flush_rewrite_rules();
</ins><span class="cx"> 
</span><del>-                $wp_rewrite-&gt;flush_rules();
</del><ins>+                $this-&gt;old_current_user = get_current_user_id();
+                $this-&gt;author_id = $this-&gt;factory-&gt;user-&gt;create( array( 'user_login' =&gt; 'canonical-author' ) );
+                wp_set_current_user( $this-&gt;author_id );
+
+                // Already created by install defaults:
+                // $this-&gt;factory-&gt;term-&gt;create( array( 'taxonomy' =&gt; 'category', 'name' =&gt; 'uncategorized' ) );
+                
+                $this-&gt;term_ids = array();
+
+                $this-&gt;factory-&gt;post-&gt;create( array( 'import_id' =&gt; 587, 'post_title' =&gt; 'post-format-test-audio', 'post_date' =&gt; '2008-06-02' ) );
+                $post_id = $this-&gt;factory-&gt;post-&gt;create( array( 'post_title' =&gt; 'post-format-test-gallery', 'post_date' =&gt; '2008-06-10' ) );
+                $this-&gt;factory-&gt;post-&gt;create( array( 'import_id' =&gt; 611, 'post_type' =&gt; 'attachment', 'post_title' =&gt; 'canola2', 'post_parent' =&gt; $post_id ) );
+
+                $this-&gt;factory-&gt;post-&gt;create( array(
+                        'post_title' =&gt; 'images-test',
+                        'post_date' =&gt; '2008-09-03',
+                        'post_content' =&gt; 'Page 1 &lt;!--nextpage--&gt; Page 2 &lt;!--nextpage--&gt; Page 3'
+                ) );
+
+                $post_id = $this-&gt;factory-&gt;post-&gt;create( array( 'import_id' =&gt; 149, 'post_title' =&gt; 'comment-test', 'post_date' =&gt; '2008-03-03' ) );
+                $this-&gt;factory-&gt;comment-&gt;create_post_comments( $post_id, 15 );
+
+                $this-&gt;factory-&gt;post-&gt;create( array( 'post_date' =&gt; '2008-09-05' ) );
+                
+                $this-&gt;factory-&gt;post-&gt;create( array( 'import_id' =&gt; 123 ) );
+                $this-&gt;factory-&gt;post-&gt;create( array( 'import_id' =&gt; 1 ) );
+                $this-&gt;factory-&gt;post-&gt;create( array( 'import_id' =&gt; 358 ) );
+                
+                $this-&gt;factory-&gt;post-&gt;create( array( 'post_type' =&gt; 'page', 'post_title' =&gt; 'sample-page' ) );
+                $this-&gt;factory-&gt;post-&gt;create( array( 'post_type' =&gt; 'page', 'post_title' =&gt; 'about' ) );
+                $post_id = $this-&gt;factory-&gt;post-&gt;create( array( 'post_type' =&gt; 'page', 'post_title' =&gt; 'parent-page' ) );
+                $this-&gt;factory-&gt;post-&gt;create(
+                        array( 'import_id' =&gt; 144, 'post_type' =&gt; 'page', 'post_title' =&gt; 'child-page-1', 'post_parent' =&gt; $post_id,
+                ) );
+
+                $this-&gt;term_ids['/category/parent/'] = $this-&gt;factory-&gt;term-&gt;create( array( 'taxonomy' =&gt; 'category', 'name' =&gt; 'parent' ) );
+                $this-&gt;term_ids['/category/parent/child-1/'] = $this-&gt;factory-&gt;term-&gt;create( array(
+                        'taxonomy' =&gt; 'category', 'name' =&gt; 'child-1', 'parent' =&gt; $this-&gt;term_ids['/category/parent/'],
+                ) );
+                $this-&gt;term_ids['/category/parent/child-1/child-2/'] = $this-&gt;factory-&gt;term-&gt;create( array(
+                        'taxonomy' =&gt; 'category', 'name' =&gt; 'child-2', 'parent' =&gt; $this-&gt;term_ids['/category/parent/child-1/'],
+                ) );
+
+                $this-&gt;factory-&gt;term-&gt;create( array( 'taxonomy' =&gt; 'category', 'name' =&gt; 'cat-a' ) );
+                $this-&gt;factory-&gt;term-&gt;create( array( 'taxonomy' =&gt; 'category', 'name' =&gt; 'cat-b' ) );
+                
+                $this-&gt;factory-&gt;term-&gt;create( array( 'name' =&gt; 'post-formats' ) );
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         function tearDown() {
</span><span class="cx">                 parent::tearDown();
</span><ins>+                wp_set_current_user( $this-&gt;old_current_user );
</ins><span class="cx"> 
</span><del>-                delete_option('permalink_structure');
-
-                global $wp_rewrite;
-                $wp_rewrite-&gt;set_permalink_structure('');
-                $wp_rewrite-&gt;flush_rules();
-
-                $_GET = array();
</del><ins>+                $GLOBALS['wp_rewrite']-&gt;init();
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         // URL's are relative to the site &quot;front&quot;, ie. /category/uncategorized/ instead of http://site.../category..
</span><span class="cx">         // Return url's are full url's with the prepended home.
</span><span class="cx">         function get_canonical($test_url) {
</span><del>-                $can_url = redirect_canonical( get_option('home') . $test_url, false);
-                if ( empty($can_url) )
-                        return get_option('home') . $test_url; // No redirect will take place for this request
</del><ins>+                $test_url = home_url( $test_url );
</ins><span class="cx"> 
</span><ins>+                $can_url = redirect_canonical( $test_url, false );
+                if ( ! $can_url )
+                        return $test_url; // No redirect will take place for this request
+
</ins><span class="cx">                 return $can_url;
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -55,7 +104,7 @@
</span><span class="cx">                         $this-&gt;knownWPBug($ticket);
</span><span class="cx"> 
</span><span class="cx">                 $ticket_ref = ($ticket &gt; 0) ? 'Ticket #' . $ticket : null;
</span><del>-
</del><ins>+                
</ins><span class="cx">                 if ( is_string($expected) )
</span><span class="cx">                         $expected = array('url' =&gt; $expected);
</span><span class="cx">                 elseif ( is_array($expected) &amp;&amp; !isset($expected['url']) &amp;&amp; !isset($expected['qv']) )
</span><span class="lines">@@ -64,8 +113,15 @@
</span><span class="cx">                 if ( !isset($expected['url']) &amp;&amp; !isset($expected['qv']) )
</span><span class="cx">                         $this-&gt;markTestSkipped('No valid expected output was provided');
</span><span class="cx"> 
</span><del>-                $this-&gt;http( get_option('home') . $test_url );
</del><ins>+                if ( false !== strpos( $test_url, '%d' ) ) {
+                        if ( false !== strpos( $test_url, '/?author=%d' ) )
+                                $test_url = sprintf( $test_url, $this-&gt;author_id );
+                        if ( false !== strpos( $test_url, '?cat=%d' ) )
+                                $test_url = sprintf( $test_url, $this-&gt;term_ids[ $expected['url'] ] );
+                }
</ins><span class="cx"> 
</span><ins>+                $this-&gt;go_to( home_url( $test_url ) );
+
</ins><span class="cx">                 // Does the redirect match what's expected?
</span><span class="cx">                 $can_url = $this-&gt;get_canonical( $test_url );
</span><span class="cx">                 $parsed_can_url = parse_url($can_url);
</span><span class="lines">@@ -74,27 +130,26 @@
</span><span class="cx">                 if ( isset($expected['url']) )
</span><span class="cx">                         $this-&gt;assertEquals( $expected['url'], $parsed_can_url['path'] . (!empty($parsed_can_url['query']) ? '?' . $parsed_can_url['query'] : ''), $ticket_ref );
</span><span class="cx"> 
</span><del>-                if ( isset($expected['qv']) ) {
</del><ins>+                if ( ! isset($expected['qv']) )
+                        return;
</ins><span class="cx"> 
</span><del>-                        // &quot;make&quot; that the request and check the query is correct
-                        $this-&gt;http( $can_url );
</del><ins>+                // &quot;make&quot; that the request and check the query is correct
+                $this-&gt;go_to( $can_url );
</ins><span class="cx"> 
</span><del>-                        // Are all query vars accounted for, And correct?
-                        global $wp;
</del><ins>+                // Are all query vars accounted for, And correct?
+                global $wp;
</ins><span class="cx"> 
</span><del>-                        $query_vars = array_diff($wp-&gt;query_vars, $wp-&gt;extra_query_vars);
-                        if ( !empty($parsed_can_url['query']) ) {
-                                parse_str($parsed_can_url['query'], $_qv);
</del><ins>+                $query_vars = array_diff($wp-&gt;query_vars, $wp-&gt;extra_query_vars);
+                if ( !empty($parsed_can_url['query']) ) {
+                        parse_str($parsed_can_url['query'], $_qv);
</ins><span class="cx"> 
</span><del>-                                // $_qv should not contain any elements which are set in $query_vars already (ie. $_GET vars should not be present in the Rewrite)
-                                $this-&gt;assertEquals( array(), array_intersect( $query_vars, $_qv ), 'Query vars are duplicated from the Rewrite into $_GET; ' . $ticket_ref );
</del><ins>+                        // $_qv should not contain any elements which are set in $query_vars already (ie. $_GET vars should not be present in the Rewrite)
+                        $this-&gt;assertEquals( array(), array_intersect( $query_vars, $_qv ), 'Query vars are duplicated from the Rewrite into $_GET; ' . $ticket_ref );
</ins><span class="cx"> 
</span><del>-                                $query_vars = array_merge($query_vars, $_qv);
-                        }
</del><ins>+                        $query_vars = array_merge($query_vars, $_qv);
+                }
</ins><span class="cx"> 
</span><del>-                        $this-&gt;assertEquals( $expected['qv'], $query_vars );
-                } //isset $expected['qv']
-
</del><ins>+                $this-&gt;assertEquals( $expected['qv'], $query_vars );
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         function data() {
</span><span class="lines">@@ -110,9 +165,10 @@
</span><span class="cx">                 // Please Note: A few test cases are commented out below, Look at the test case following it, in most cases it's simple showing 2 options for the &quot;proper&quot; redirect.
</span><span class="cx">                 return array(
</span><span class="cx">                         // Categories
</span><del>-                        array( '?cat=32', '/category/parent/', 15256 ),
-                        array( '?cat=50', '/category/parent/child-1/', 15256 ),
-                        array( '?cat=51', '/category/parent/child-1/child-2/' ), // no children
</del><ins>+
+                        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
</ins><span class="cx">                         array( '/category/uncategorized/', array( 'url' =&gt; '/category/uncategorized/', 'qv' =&gt; array( 'category_name' =&gt; 'uncategorized' ) ) ),
</span><span class="cx">                         array( '/category/uncategorized/page/2/', array( 'url' =&gt; '/category/uncategorized/page/2/', 'qv' =&gt; array( 'category_name' =&gt; 'uncategorized', 'paged' =&gt; 2) ) ),
</span><span class="cx">                         array( '/category/uncategorized/?paged=2', array( 'url' =&gt; '/category/uncategorized/page/2/', 'qv' =&gt; array( 'category_name' =&gt; 'uncategorized', 'paged' =&gt; 2) ) ),
</span><span class="lines">@@ -183,11 +239,11 @@
</span><span class="cx">                         array( '/?year=2008', '/2008/'),
</span><span class="cx"> 
</span><span class="cx">                         // Authors
</span><del>-                        array( '/?author=3', '/author/chip-bennett/' ),
-//                        array( '/?author=3&amp;year=2008', '/2008/?author=3'),
-                        array( '/?author=3&amp;year=2008', '/author/chip-bennett/?year=2008', 17661 ),
-//                        array( '/author/chip-bennett/?year=2008', '/2008/?author=3'), //Either or, see previous testcase.
-                        array( '/author/chip-bennett/?year=2008', '/author/chip-bennett/?year=2008', 17661 ),
</del><ins>+                        array( '/?author=%d', '/author/canonical-author/' ),
+//                        array( '/?author=%d&amp;year=2008', '/2008/?author=3'),
+                        array( '/?author=%d&amp;year=2008', '/author/canonical-author/?year=2008', 17661 ),
+//                        array( '/author/canonical-author/?year=2008', '/2008/?author=3'), //Either or, see previous testcase.
+                        array( '/author/canonical-author/?year=2008', '/author/canonical-author/?year=2008', 17661 ),
</ins><span class="cx"> 
</span><span class="cx">                         // Feeds
</span><span class="cx">                         array( '/?feed=atom', '/feed/atom/' ),
</span><span class="lines">@@ -212,42 +268,21 @@
</span><span class="cx">                         array( '//2008////', '/2008/' ),
</span><span class="cx"> 
</span><span class="cx">                         // Todo: Endpoints (feeds, trackbacks, etc), More fuzzed mixed query variables, comment paging, Home page (Static)
</span><del>-
</del><span class="cx">                 );
</span><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-class WP_Canonical_PageOnFront extends WP_Canonical {
-        var $special_pages = array();
-
-        function SetUp() {
</del><ins>+class WP_Canonical_PageOnFront extends WP_Test_Canonical {
+        function setUp() {
+                parent::setUp();
</ins><span class="cx">                 global $wp_rewrite;
</span><del>-                parent::SetUp();
-                $this-&gt;special_pages['blog' ] = wp_insert_post( array( 'post_type' =&gt; 'page', 'post_status' =&gt; 'publish', 'post_title' =&gt; 'blog-page'  ) );
-                $this-&gt;special_pages['front'] = wp_insert_post( array( 'post_type' =&gt; 'page', 'post_status' =&gt; 'publish', 'post_title' =&gt; 'front-page' ) );
</del><span class="cx">                 update_option( 'show_on_front', 'page' );
</span><del>-                update_option( 'page_for_posts', $this-&gt;special_pages['blog'] );
-                update_option( 'page_on_front', $this-&gt;special_pages['front'] );
-                $wp_rewrite-&gt;flush_rules();
</del><ins>+                update_option( 'page_for_posts', $this-&gt;factory-&gt;post-&gt;create( array( 'post_title' =&gt; 'blog-page', 'post_type' =&gt; 'page' ) ) );
+                update_option( 'page_on_front', $this-&gt;factory-&gt;post-&gt;create( array( 'post_title' =&gt; 'front-page', 'post_type' =&gt; 'page' ) ) );
+                $wp_rewrite-&gt;init();
+                flush_rewrite_rules();
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        function tearDown() {
-                parent::tearDown();
-                update_option( 'show_on_front', 'posts' );
-                
-                delete_option( 'page_for_posts' );
-                delete_option( 'page_on_front' );
-                delete_option( 'permalink_structure' );
-
-                foreach ( $this-&gt;special_pages as $p )
-                        wp_delete_post( $p );
-                global $wp_rewrite;
-                $wp_rewrite-&gt;set_permalink_structure('');
-                $wp_rewrite-&gt;flush_rules();
-
-                $_GET = array();
-        }
-
</del><span class="cx">         function data() {
</span><span class="cx">                 /* Format:
</span><span class="cx">                  * [0]: $test_url,
</span><span class="lines">@@ -258,8 +293,8 @@
</span><span class="cx">                  * [3]: (optional) The ticket the test refers to, Can be skipped if unknown.
</span><span class="cx">                  */
</span><span class="cx">                  return array(
</span><del>-                         // Check against an odd redirect: #20385
-                         array( '/page/2/', '/page/2/' ),
</del><ins>+                         // Check against an odd redirect
+                         array( '/page/2/', '/page/2/', 20385 ),
</ins><span class="cx">                          // The page designated as the front page should redirect to the front of the site
</span><span class="cx">                          array( '/front-page/', '/' ),
</span><span class="cx">                          array( '/blog-page/?paged=2', '/blog-page/page/2/' ),
</span><span class="lines">@@ -267,10 +302,10 @@
</span><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-class WP_Canonical_CustomRules extends WP_Canonical {
-        function SetUp() {
</del><ins>+class WP_Canonical_CustomRules extends WP_Test_Canonical {
+        function setUp() {
+                parent::setUp();
</ins><span class="cx">                 global $wp_rewrite;
</span><del>-                parent::SetUp();
</del><span class="cx">                 // Add a custom Rewrite rule to test category redirections.
</span><span class="cx">                 $wp_rewrite-&gt;add_rule('ccr/(.+?)/sort/(asc|desc)', 'index.php?category_name=$matches[1]&amp;order=$matches[2]', 'top'); // ccr = Custom_Cat_Rule
</span><span class="cx">                 $wp_rewrite-&gt;flush_rules();
</span><span class="lines">@@ -294,7 +329,7 @@
</span><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-class WP_Canonical_NoRewrite extends WP_Canonical {
</del><ins>+class WP_Canonical_NoRewrite extends WP_Test_Canonical {
</ins><span class="cx"> 
</span><span class="cx">         var $structure = '';
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>