From f23a5ef832bd583f248e6a9b5a39f8d1df163da4 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 3 Jul 2014 01:42:57 +0000 Subject: [PATCH] Setup rewrite changes in tests in a predictable fashion. Don't mix method calls with wrapper function calls. See #28706. git-svn-id: https://develop.svn.wordpress.org/trunk@28966 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/canonical.php | 3 ++- tests/phpunit/tests/canonical/pageOnFront.php | 8 +++++- tests/phpunit/tests/link.php | 17 +++++-------- tests/phpunit/tests/query/conditionals.php | 12 ++++++--- tests/phpunit/tests/query/taxQuery.php | 12 ++++++--- .../tests/query/verboseRewriteRules.php | 12 ++++++--- tests/phpunit/tests/rewrite.php | 25 +++++++++++-------- 7 files changed, 53 insertions(+), 36 deletions(-) diff --git a/tests/phpunit/tests/canonical.php b/tests/phpunit/tests/canonical.php index a673cc7566..fe6e22fb93 100644 --- a/tests/phpunit/tests/canonical.php +++ b/tests/phpunit/tests/canonical.php @@ -84,10 +84,11 @@ class Tests_Canonical extends WP_UnitTestCase { } function tearDown() { + global $wp_rewrite; parent::tearDown(); wp_set_current_user( $this->old_current_user ); - $GLOBALS['wp_rewrite']->init(); + $wp_rewrite->init(); } // URL's are relative to the site "front", ie. /category/uncategorized/ instead of http://site.../category.. diff --git a/tests/phpunit/tests/canonical/pageOnFront.php b/tests/phpunit/tests/canonical/pageOnFront.php index 6c0cf6649c..ca8ac20899 100644 --- a/tests/phpunit/tests/canonical/pageOnFront.php +++ b/tests/phpunit/tests/canonical/pageOnFront.php @@ -15,7 +15,13 @@ class Tests_Canonical_PageOnFront extends Tests_Canonical { update_option( 'page_for_posts', $this->factory->post->create( array( 'post_title' => 'blog-page', 'post_type' => 'page' ) ) ); update_option( 'page_on_front', $this->factory->post->create( array( 'post_title' => 'front-page', 'post_type' => 'page' ) ) ); $wp_rewrite->init(); - flush_rewrite_rules(); + $wp_rewrite->flush_rules(); + } + + function tearDown() { + global $wp_rewrite; + parent::tearDown(); + $wp_rewrite->init(); } function data() { diff --git a/tests/phpunit/tests/link.php b/tests/phpunit/tests/link.php index 3b363e1659..96f4ecda51 100644 --- a/tests/phpunit/tests/link.php +++ b/tests/phpunit/tests/link.php @@ -4,6 +4,12 @@ */ class Tests_Link extends WP_UnitTestCase { + function tearDown() { + global $wp_rewrite; + parent::tearDown(); + $wp_rewrite->init(); + } + function _get_pagenum_link_cb( $url ) { return $url . '/WooHoo'; } @@ -65,7 +71,6 @@ class Tests_Link extends WP_UnitTestCase { $this->assertEquals( '', wp_get_shortlink( 0 ) ); $this->assertEquals( '', wp_get_shortlink() ); - $wp_rewrite->permalink_structure = ''; $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); $wp_rewrite->flush_rules(); @@ -78,9 +83,6 @@ class Tests_Link extends WP_UnitTestCase { $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( 0, 'post' ) ); $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( 0 ) ); $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink() ); - - $wp_rewrite->set_permalink_structure( '' ); - $wp_rewrite->flush_rules(); } function test_wp_get_shortlink_with_page() { @@ -91,14 +93,10 @@ class Tests_Link extends WP_UnitTestCase { $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) ); global $wp_rewrite; - $wp_rewrite->permalink_structure = ''; $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); $wp_rewrite->flush_rules(); $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) ); - - $wp_rewrite->set_permalink_structure( '' ); - $wp_rewrite->flush_rules(); } /** @@ -117,9 +115,6 @@ class Tests_Link extends WP_UnitTestCase { $wp_rewrite->flush_rules(); $this->assertEquals( home_url( '/' ), wp_get_shortlink( $post_id, 'post' ) ); - - $wp_rewrite->set_permalink_structure( '' ); - $wp_rewrite->flush_rules(); } /** diff --git a/tests/phpunit/tests/query/conditionals.php b/tests/phpunit/tests/query/conditionals.php index 0a2004b28c..1af50ac7aa 100644 --- a/tests/phpunit/tests/query/conditionals.php +++ b/tests/phpunit/tests/query/conditionals.php @@ -23,15 +23,19 @@ class Tests_Query_Conditionals extends WP_UnitTestCase { update_option( 'posts_per_page', 5 ); global $wp_rewrite; - update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' ); + + $wp_rewrite->init(); + $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); + create_initial_taxonomies(); - $GLOBALS['wp_rewrite']->init(); - flush_rewrite_rules(); + + $wp_rewrite->flush_rules(); } function tearDown() { + global $wp_rewrite; parent::tearDown(); - $GLOBALS['wp_rewrite']->init(); + $wp_rewrite->init(); } function test_home() { diff --git a/tests/phpunit/tests/query/taxQuery.php b/tests/phpunit/tests/query/taxQuery.php index 096f5151c9..fe6540f808 100644 --- a/tests/phpunit/tests/query/taxQuery.php +++ b/tests/phpunit/tests/query/taxQuery.php @@ -22,20 +22,21 @@ class Tests_Query_TaxQuery extends WP_UnitTestCase { protected $tax; function setUp() { + global $wp_rewrite; parent::setUp(); set_current_screen( 'front' ); - update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' ); - $GLOBALS['wp_the_query'] = new WP_Query(); $GLOBALS['wp_query'] = $GLOBALS['wp_the_query']; + $wp_rewrite->init(); + $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); + create_initial_taxonomies(); register_taxonomy( 'testtax', 'post', array( 'public' => true ) ); - $GLOBALS['wp_rewrite']->init(); - flush_rewrite_rules(); + $wp_rewrite->flush_rules(); $this->tag_id = $this->factory->tag->create( array( 'slug' => 'tag-slug' ) ); $this->cat_id = $this->factory->category->create( array( 'slug' => 'cat-slug' ) ); @@ -56,10 +57,13 @@ class Tests_Query_TaxQuery extends WP_UnitTestCase { } function tearDown() { + global $wp_rewrite; parent::tearDown(); _unregister_taxonomy( 'testtax' ); + $wp_rewrite->init(); + remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); } diff --git a/tests/phpunit/tests/query/verboseRewriteRules.php b/tests/phpunit/tests/query/verboseRewriteRules.php index 4918e751cb..b6c05c42b5 100644 --- a/tests/phpunit/tests/query/verboseRewriteRules.php +++ b/tests/phpunit/tests/query/verboseRewriteRules.php @@ -8,11 +8,15 @@ require_once dirname( dirname( __FILE__ ) ) . '/query.php'; */ class Tests_Query_VerbosePageRules extends Tests_Query_Conditionals { function setUp() { - parent::setUp(); global $wp_rewrite; - update_option( 'permalink_structure', '/%category%/%year%/%postname%/' ); + + parent::setUp(); + + $wp_rewrite->init(); + $wp_rewrite->set_permalink_structure( '/%category%/%year%/%postname%/' ); + create_initial_taxonomies(); - $GLOBALS['wp_rewrite']->init(); - flush_rewrite_rules(); + + $wp_rewrite->flush_rules(); } } diff --git a/tests/phpunit/tests/rewrite.php b/tests/phpunit/tests/rewrite.php index a6c5417658..1e3606eabb 100644 --- a/tests/phpunit/tests/rewrite.php +++ b/tests/phpunit/tests/rewrite.php @@ -8,19 +8,22 @@ class Tests_Rewrite extends WP_UnitTestCase { function setUp() { + global $wp_rewrite; parent::setUp(); // Need rewrite rules in place to use url_to_postid - global $wp_rewrite; - update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' ); + $wp_rewrite->init(); + $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); + create_initial_taxonomies(); - $GLOBALS['wp_rewrite']->init(); - flush_rewrite_rules(); + + $wp_rewrite->flush_rules(); } function tearDown() { + global $wp_rewrite; parent::tearDown(); - $GLOBALS['wp_rewrite']->init(); + $wp_rewrite->init(); } function test_url_to_postid() { @@ -34,16 +37,16 @@ class Tests_Rewrite extends WP_UnitTestCase { function test_url_to_postid_custom_post_type() { delete_option( 'rewrite_rules' ); - + $post_type = rand_str( 12 ); register_post_type( $post_type, array( 'public' => true ) ); - + $id = $this->factory->post->create( array( 'post_type' => $post_type ) ); - $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); - - _unregister_post_type( $post_type ); + $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); + + _unregister_post_type( $post_type ); } - + function test_url_to_postid_hierarchical() { $parent_id = $this->factory->post->create( array( 'post_title' => 'Parent', 'post_type' => 'page' ) );