diff --git a/tests/phpunit/includes/bootstrap.php b/tests/phpunit/includes/bootstrap.php index 830ff8e5b2..0fde392d1d 100644 --- a/tests/phpunit/includes/bootstrap.php +++ b/tests/phpunit/includes/bootstrap.php @@ -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'; diff --git a/tests/phpunit/includes/testcase-canonical.php b/tests/phpunit/includes/testcase-canonical.php new file mode 100644 index 0000000000..f36e2bf6ee --- /dev/null +++ b/tests/phpunit/includes/testcase-canonical.php @@ -0,0 +1,219 @@ +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 Page 2 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; + } +} diff --git a/tests/phpunit/tests/canonical.php b/tests/phpunit/tests/canonical.php index b7aa49c1cc..c91b730bd2 100644 --- a/tests/phpunit/tests/canonical.php +++ b/tests/phpunit/tests/canonical.php @@ -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 Page 2 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) ) ), diff --git a/tests/phpunit/tests/canonical/customRules.php b/tests/phpunit/tests/canonical/customRules.php index dff15876a3..2873e537b7 100644 --- a/tests/phpunit/tests/canonical/customRules.php +++ b/tests/phpunit/tests/canonical/customRules.php @@ -1,13 +1,12 @@ 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, diff --git a/tests/phpunit/tests/canonical/https.php b/tests/phpunit/tests/canonical/https.php index 84a85f6577..88fa7f4ec7 100644 --- a/tests/phpunit/tests/canonical/https.php +++ b/tests/phpunit/tests/canonical/https.php @@ -1,16 +1,21 @@ 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' ); } diff --git a/tests/phpunit/tests/canonical/noRewrite.php b/tests/phpunit/tests/canonical/noRewrite.php index b83340837b..673cafe2c0 100644 --- a/tests/phpunit/tests/canonical/noRewrite.php +++ b/tests/phpunit/tests/canonical/noRewrite.php @@ -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: diff --git a/tests/phpunit/tests/canonical/pageOnFront.php b/tests/phpunit/tests/canonical/pageOnFront.php index ca8ac20899..26d360314f 100644 --- a/tests/phpunit/tests/canonical/pageOnFront.php +++ b/tests/phpunit/tests/canonical/pageOnFront.php @@ -1,13 +1,19 @@ 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,