2013-08-07 08:38:38 +02:00
< ? php
/**
* Tests Canonical redirections .
*
* In the process of doing so , it also tests WP , WP_Rewrite and WP_Query , A fail here may show a bug in any one of these areas .
*
* @ group canonical
* @ group rewrite
* @ group query
*/
2014-11-08 20:28:12 +01:00
class Tests_Canonical extends WP_Canonical_UnitTestCase {
2014-07-03 02:49:32 +02:00
2014-11-08 20:28:12 +01:00
public function setUp () {
2013-08-07 08:38:38 +02:00
parent :: setUp ();
2014-11-08 20:28:12 +01:00
wp_set_current_user ( self :: $author_id );
2013-08-07 08:38:38 +02:00
}
/**
2016-02-25 22:55:57 +01:00
* @ dataProvider data_canonical
2013-08-07 08:38:38 +02:00
*/
2016-02-25 22:55:57 +01:00
function test_canonical ( $test_url , $expected , $ticket = 0 , $expected_doing_it_wrong = array () ) {
2013-08-07 08:38:38 +02:00
if ( false !== strpos ( $test_url , '%d' ) ) {
2017-12-01 00:09:33 +01:00
if ( false !== strpos ( $test_url , '/?author=%d' ) ) {
2014-11-08 20:28:12 +01:00
$test_url = sprintf ( $test_url , self :: $author_id );
2017-12-01 00:09:33 +01:00
}
if ( false !== strpos ( $test_url , '?cat=%d' ) ) {
2014-11-08 20:28:12 +01:00
$test_url = sprintf ( $test_url , self :: $terms [ $expected [ 'url' ] ] );
2017-12-01 00:09:33 +01:00
}
2013-08-07 08:38:38 +02:00
}
2014-11-08 20:28:12 +01:00
$this -> assertCanonical ( $test_url , $expected , $ticket , $expected_doing_it_wrong );
2013-08-07 08:38:38 +02:00
}
2016-02-25 22:55:57 +01:00
function data_canonical () {
2013-08-07 08:38:38 +02:00
/* Data format :
2016-02-25 22:55:57 +01:00
* [ 0 ] : Test URL .
2013-08-07 08:38:38 +02:00
* [ 1 ] : expected results : Any of the following can be used
* array ( 'url' : expected redirection location , 'qv' : expected query vars to be set via the rewrite AND $_GET );
* array ( expected query vars to be set , same as 'qv' above )
* ( string ) expected redirect location
* [ 2 ] : ( optional ) The ticket the test refers to , Can be skipped if unknown .
2016-02-25 22:55:57 +01:00
* [ 3 ] : ( optional ) Array of class / function names expected to throw `_doing_it_wrong()` notices .
2013-08-07 08:38:38 +02:00
*/
2015-09-16 14:45:51 +02:00
// Please Note: A few test cases are commented out below, Look at the test case following it, in most cases it's simply showing 2 options for the "proper" redirect.
2013-08-07 08:38:38 +02:00
return array (
// Categories
2014-11-08 20:28:12 +01:00
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
2017-12-01 00:09:33 +01:00
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 ,
),
),
),
array (
'/category/uncategorized/?paged=2&category_name=uncategorized' ,
array (
'url' => '/category/uncategorized/page/2/' ,
'qv' => array (
'category_name' => 'uncategorized' ,
'paged' => 2 ,
),
),
17174 ,
),
2013-08-07 08:38:38 +02:00
// Categories & Intersections with other vars
2017-12-01 00:09:33 +01:00
array (
'/category/uncategorized/?tag=post-formats' ,
array (
'url' => '/category/uncategorized/?tag=post-formats' ,
'qv' => array (
'category_name' => 'uncategorized' ,
'tag' => 'post-formats' ,
),
),
),
array (
'/?category_name=cat-a,cat-b' ,
array (
'url' => '/?category_name=cat-a,cat-b' ,
'qv' => array ( 'category_name' => 'cat-a,cat-b' ),
),
),
2013-08-07 08:38:38 +02:00
// Taxonomies with extra Query Vars
2017-12-01 00:09:33 +01:00
array ( '/category/cat-a/page/1/?test=one%20two' , '/category/cat-a/?test=one%20two' , 18086 ), // Extra query vars should stay encoded
2013-08-07 08:38:38 +02:00
// Categories with Dates
2017-12-01 00:09:33 +01:00
array (
'/2008/04/?cat=1' ,
array (
'url' => '/2008/04/?cat=1' ,
'qv' => array (
'cat' => '1' ,
'year' => '2008' ,
'monthnum' => '04' ,
),
),
17661 ,
),
// array( '/2008/?category_name=cat-a', array( 'url' => '/2008/?category_name=cat-a', 'qv' => array('category_name' => 'cat-a', 'year' => '2008' ) ) ),
// Pages
array ( '/child-page-1/' , '/parent-page/child-page-1/' ),
array ( '/?page_id=144' , '/parent-page/child-page-1/' ),
2013-08-07 08:38:38 +02:00
array ( '/abo' , '/about/' ),
2015-06-24 02:40:45 +02:00
array ( '/parent/child1/grandchild/' , '/parent/child1/grandchild/' ),
array ( '/parent/child2/grandchild/' , '/parent/child2/grandchild/' ),
2013-08-07 08:38:38 +02:00
// Posts
2017-12-01 00:09:33 +01:00
array ( '?p=587' , '/2008/06/02/post-format-test-audio/' ),
array ( '/?name=images-test' , '/2008/09/03/images-test/' ),
2013-08-07 08:38:38 +02:00
// Incomplete slug should resolve and remove the ?name= parameter
2017-12-01 00:09:33 +01:00
array ( '/?name=images-te' , '/2008/09/03/images-test/' , 20374 ),
2013-08-07 08:38:38 +02:00
// Page slug should resolve to post slug and remove the ?pagename= parameter
2017-12-01 00:09:33 +01:00
array ( '/?pagename=images-test' , '/2008/09/03/images-test/' , 20374 ),
array ( '/2008/06/02/post-format-test-au/' , '/2008/06/02/post-format-test-audio/' ),
array ( '/2008/06/post-format-test-au/' , '/2008/06/02/post-format-test-audio/' ),
array ( '/2008/post-format-test-au/' , '/2008/06/02/post-format-test-audio/' ),
array ( '/2010/post-format-test-au/' , '/2008/06/02/post-format-test-audio/' ), // A Year the post is not in
array ( '/post-format-test-au/' , '/2008/06/02/post-format-test-audio/' ),
array (
'/2008/09/03/images-test/3/' ,
array (
'url' => '/2008/09/03/images-test/3/' ,
'qv' => array (
'name' => 'images-test' ,
'year' => '2008' ,
'monthnum' => '09' ,
'day' => '03' ,
'page' => '3' ,
),
),
),
2013-08-07 08:38:38 +02:00
array ( '/2008/09/03/images-test/?page=3' , '/2008/09/03/images-test/3/' ),
array ( '/2008/09/03/images-te?page=3' , '/2008/09/03/images-test/3/' ),
// Comments
2013-11-11 19:57:11 +01:00
array ( '/2008/03/03/comment-test/?cpage=2' , '/2008/03/03/comment-test/comment-page-2/' ),
2013-08-07 08:38:38 +02:00
// Attachments
array ( '/?attachment_id=611' , '/2008/06/10/post-format-test-gallery/canola2/' ),
array ( '/2008/06/10/post-format-test-gallery/?attachment_id=611' , '/2008/06/10/post-format-test-gallery/canola2/' ),
// Dates
array ( '/?m=2008' , '/2008/' ),
2017-12-01 00:09:33 +01:00
array ( '/?m=200809' , '/2008/09/' ),
array ( '/?m=20080905' , '/2008/09/05/' ),
2013-08-07 08:38:38 +02:00
2017-12-01 00:09:33 +01:00
array ( '/2008/?day=05' , '/2008/?day=05' ), // no redirect
array ( '/2008/09/?day=05' , '/2008/09/05/' ),
array ( '/2008/?monthnum=9' , '/2008/09/' ),
2013-08-07 08:38:38 +02:00
2017-12-01 00:09:33 +01:00
array ( '/?year=2008' , '/2008/' ),
2013-08-07 08:38:38 +02:00
2017-12-01 00:09:33 +01:00
array ( '/2012/13/' , '/2012/' ),
2014-10-17 02:40:06 +02:00
array ( '/2012/11/51/' , '/2012/11/' , 0 , array ( 'WP_Date_Query' ) ),
2013-09-06 18:35:22 +02:00
2013-08-07 08:38:38 +02:00
// Authors
array ( '/?author=%d' , '/author/canonical-author/' ),
2017-12-01 00:09:33 +01:00
// array( '/?author=%d&year=2008', '/2008/?author=3'),
// array( '/author/canonical-author/?year=2008', '/2008/?author=3'), //Either or, see previous testcase.
2013-08-07 08:38:38 +02:00
2017-12-01 00:09:33 +01:00
// Feeds
array ( '/?feed=atom' , '/feed/atom/' ),
2013-08-07 08:38:38 +02:00
array ( '/?feed=rss2' , '/feed/' ),
2017-12-01 00:09:33 +01:00
array ( '/?feed=comments-rss2' , '/comments/feed/' ),
array ( '/?feed=comments-atom' , '/comments/feed/atom/' ),
2013-08-07 08:38:38 +02:00
// Feeds (per-post)
2017-12-01 00:09:33 +01:00
array ( '/2008/03/03/comment-test/?feed=comments-atom' , '/2008/03/03/comment-test/feed/atom/' ),
array ( '/?p=149&feed=comments-atom' , '/2008/03/03/comment-test/feed/atom/' ),
2013-08-07 08:38:38 +02:00
// Index
array ( '/?paged=1' , '/' ),
array ( '/page/1/' , '/' ),
array ( '/page1/' , '/' ),
array ( '/?paged=2' , '/page/2/' ),
array ( '/page2/' , '/page/2/' ),
// Misc
array ( '/2008%20' , '/2008' ),
array ( '//2008////' , '/2008/' ),
// Todo: Endpoints (feeds, trackbacks, etc), More fuzzed mixed query variables, comment paging, Home page (Static)
);
}
}