diff --git a/tests/phpunit/includes/testcase.php b/tests/phpunit/includes/testcase.php index 17631bbb2f..8ced19744f 100644 --- a/tests/phpunit/includes/testcase.php +++ b/tests/phpunit/includes/testcase.php @@ -50,7 +50,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { $this->reset_post_types(); $this->reset_taxonomies(); $this->reset_post_statuses(); - $this->reset_permalinks(); + $this->set_permalink_structure(); } $this->start_transaction(); @@ -653,12 +653,14 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { * @since 4.4.0 * * @global WP_Rewrite $wp_rewrite + * + * @param string $structure Optional. Permalink structure to set. Default empty. */ - public function reset_permalinks() { + public function set_permalink_structure( $structure = '' ) { global $wp_rewrite; $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '' ); + $wp_rewrite->set_permalink_structure( $structure ); $wp_rewrite->flush_rules(); } } diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index 56156acee9..aabc69fc55 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -251,11 +251,8 @@ class Tests_Admin_includesPost extends WP_UnitTestCase { * @ticket 30910 */ public function test_get_sample_permalink_should_return_pretty_permalink_for_posts_with_post_status_future() { - global $wp_rewrite; - $permalink_structure = '%postname%'; - $wp_rewrite->set_permalink_structure( "/$permalink_structure/" ); - flush_rewrite_rules(); + $this->set_permalink_structure( "/$permalink_structure/" ); $future_date = date( 'Y-m-d H:i:s', time() + 100 ); $p = $this->factory->post->create( array( 'post_status' => 'future', 'post_name' => 'foo', 'post_date' => $future_date ) ); @@ -285,10 +282,7 @@ class Tests_Admin_includesPost extends WP_UnitTestCase { * @ticket 18306 */ public function test_get_sample_permalink_html_should_use_pretty_permalink_for_view_post_link_when_pretty_permalinks_are_enabled() { - global $wp_rewrite; - $permalink_structure = '%postname%'; - $wp_rewrite->set_permalink_structure( "/$permalink_structure/" ); - flush_rewrite_rules(); + $this->set_permalink_structure( '/%postname%/' ); wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) ); @@ -305,10 +299,7 @@ class Tests_Admin_includesPost extends WP_UnitTestCase { * @ticket 18306 */ public function test_get_sample_permalink_html_should_use_correct_permalink_for_view_post_link_when_changing_slug() { - global $wp_rewrite; - $permalink_structure = '%postname%'; - $wp_rewrite->set_permalink_structure( "/$permalink_structure/" ); - flush_rewrite_rules(); + $this->set_permalink_structure( '/%postname%/' ); wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) ); @@ -346,10 +337,7 @@ class Tests_Admin_includesPost extends WP_UnitTestCase { * @ticket 5305 */ public function test_get_sample_permalink_should_avoid_slugs_that_would_create_clashes_with_year_archives() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%postname%/' ); $p = $this->factory->post->create( array( 'post_name' => '2015', @@ -363,10 +351,7 @@ class Tests_Admin_includesPost extends WP_UnitTestCase { * @ticket 5305 */ public function test_get_sample_permalink_should_allow_yearlike_slugs_if_permastruct_does_not_cause_an_archive_conflict() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%postname%/' ); $p = $this->factory->post->create( array( 'post_name' => '2015', @@ -380,10 +365,7 @@ class Tests_Admin_includesPost extends WP_UnitTestCase { * @ticket 5305 */ public function test_get_sample_permalink_should_avoid_slugs_that_would_create_clashes_with_month_archives() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%postname%/' ); $p = $this->factory->post->create( array( 'post_name' => '11', @@ -397,10 +379,7 @@ class Tests_Admin_includesPost extends WP_UnitTestCase { * @ticket 5305 */ public function test_get_sample_permalink_should_ignore_potential_month_conflicts_for_invalid_monthnum() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%postname%/' ); $p = $this->factory->post->create( array( 'post_name' => '13', @@ -414,10 +393,7 @@ class Tests_Admin_includesPost extends WP_UnitTestCase { * @ticket 5305 */ public function test_get_sample_permalink_should_avoid_slugs_that_would_create_clashes_with_day_archives() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); $p = $this->factory->post->create( array( 'post_name' => '30', @@ -431,10 +407,7 @@ class Tests_Admin_includesPost extends WP_UnitTestCase { * @ticket 5305 */ public function test_get_sample_permalink_should_iterate_slug_suffix_when_a_date_conflict_is_found() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); $this->factory->post->create( array( 'post_name' => '30-2', @@ -452,10 +425,7 @@ class Tests_Admin_includesPost extends WP_UnitTestCase { * @ticket 5305 */ public function test_get_sample_permalink_should_ignore_potential_day_conflicts_for_invalid_day() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); $p = $this->factory->post->create( array( 'post_name' => '32', @@ -469,10 +439,7 @@ class Tests_Admin_includesPost extends WP_UnitTestCase { * @ticket 5305 */ public function test_get_sample_permalink_should_allow_daylike_slugs_if_permastruct_does_not_cause_an_archive_conflict() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%month%/%day%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%month%/%day%/%postname%/' ); $p = $this->factory->post->create( array( 'post_name' => '30', diff --git a/tests/phpunit/tests/link.php b/tests/phpunit/tests/link.php index 436fddd89d..777347b702 100644 --- a/tests/phpunit/tests/link.php +++ b/tests/phpunit/tests/link.php @@ -20,9 +20,7 @@ class Tests_Link extends WP_UnitTestCase { function test_get_pagenum_link_case_insensitivity() { $old_req_uri = $_SERVER['REQUEST_URI']; - global $wp_rewrite; - $wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/'); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); add_filter( 'home_url', array( $this, '_get_pagenum_link_cb' ) ); $_SERVER['REQUEST_URI'] = '/woohoo'; @@ -35,8 +33,6 @@ class Tests_Link extends WP_UnitTestCase { } function test_wp_get_shortlink() { - global $wp_rewrite; - $post_id = $this->factory->post->create(); $post_id2 = $this->factory->post->create(); @@ -67,8 +63,7 @@ class Tests_Link extends WP_UnitTestCase { $this->assertEquals( '', wp_get_shortlink( 0 ) ); $this->assertEquals( '', wp_get_shortlink() ); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); // With a permalink structure set, get_permalink() will no longer match. $this->assertNotEquals( get_permalink( $post_id ), wp_get_shortlink( $post_id, 'post' ) ); @@ -88,9 +83,7 @@ class Tests_Link extends WP_UnitTestCase { // Don't test against get_permalink() since it uses ?page_id= for pages. $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) ); - global $wp_rewrite; - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) ); } @@ -105,10 +98,7 @@ class Tests_Link extends WP_UnitTestCase { $this->assertEquals( home_url( '/' ), 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->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); $this->assertEquals( home_url( '/' ), wp_get_shortlink( $post_id, 'post' ) ); } @@ -396,9 +386,7 @@ class Tests_Link extends WP_UnitTestCase { * @ticket 1914 */ public function test_unattached_attachment_has_a_pretty_permalink() { - global $wp_rewrite; - $wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/'); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); $attachment_id = $this->factory->attachment->create_object( 'image.jpg', 0, array( 'post_mime_type' => 'image/jpeg', @@ -416,12 +404,13 @@ class Tests_Link extends WP_UnitTestCase { * @ticket 1914 */ public function test_attachment_attached_to_non_existent_post_type_has_a_pretty_permalink() { - global $wp_rewrite, $wp_post_types; - $wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/'); + global $wp_post_types; + + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); register_post_type( 'not_a_post_type', array( 'public' => true ) ); - $wp_rewrite->flush_rules(); + flush_rewrite_rules(); $post_id = $this->factory->post->create( array( 'post_type' => 'not_a_post_type' ) ); diff --git a/tests/phpunit/tests/link/getPostCommentsFeedLink.php b/tests/phpunit/tests/link/getPostCommentsFeedLink.php index 094d70787d..b2474d8e00 100644 --- a/tests/phpunit/tests/link/getPostCommentsFeedLink.php +++ b/tests/phpunit/tests/link/getPostCommentsFeedLink.php @@ -17,10 +17,7 @@ class Tests_Link_GetPostCommentsFeedLink extends WP_UnitTestCase { } public function test_post_pretty_link() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); $post_id = $this->factory->post->create(); @@ -47,10 +44,7 @@ class Tests_Link_GetPostCommentsFeedLink extends WP_UnitTestCase { } public function test_attachment_pretty_link() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); $post_id = $this->factory->post->create( array( 'post_status' => 'publish' @@ -70,10 +64,7 @@ class Tests_Link_GetPostCommentsFeedLink extends WP_UnitTestCase { } public function test_attachment_no_name_pretty_link() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); $post_id = $this->factory->post->create(); $attachment_id = $this->factory->attachment->create_object( 'image.jpg', $post_id, array( @@ -103,10 +94,7 @@ class Tests_Link_GetPostCommentsFeedLink extends WP_UnitTestCase { } public function test_unattached_pretty_link() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); $attachment_id = $this->factory->attachment->create_object( 'image.jpg', 0, array( 'post_mime_type' => 'image/jpeg', diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index 614f30586c..98fea84ec5 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -415,10 +415,7 @@ class Tests_Post extends WP_UnitTestCase { * @ticket 5305 */ public function test_wp_insert_post_should_not_allow_a_bare_numeric_slug_that_might_conflict_with_a_date_archive_when_generating_from_an_empty_post_title() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%postname%/' ); $p = wp_insert_post( array( 'post_title' => '', @@ -429,7 +426,7 @@ class Tests_Post extends WP_UnitTestCase { $post = get_post( $p ); - $this->reset_permalinks(); + $this->set_permalink_structure(); $this->assertEquals( "$p-2", $post->post_name ); } @@ -505,10 +502,7 @@ class Tests_Post extends WP_UnitTestCase { // bug: permalink doesn't work if post title is empty // might only fail if the post ID is greater than four characters - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/'); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/'); $post = array( 'post_author' => $this->author_id, diff --git a/tests/phpunit/tests/post/wpUniquePostSlug.php b/tests/phpunit/tests/post/wpUniquePostSlug.php index 7d7755b346..2aaf1ba1d2 100644 --- a/tests/phpunit/tests/post/wpUniquePostSlug.php +++ b/tests/phpunit/tests/post/wpUniquePostSlug.php @@ -169,10 +169,7 @@ class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { * @ticket 5305 */ public function test_slugs_resulting_in_permalinks_that_resemble_year_archives_should_be_suffixed() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%postname%/' ); $p = $this->factory->post->create( array( 'post_type' => 'post', @@ -187,10 +184,7 @@ class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { * @ticket 5305 */ public function test_slugs_resulting_in_permalinks_that_resemble_year_archives_should_not_be_suffixed_for_already_published_posts() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%postname%/' ); $p = $this->factory->post->create( array( 'post_type' => 'post', @@ -206,10 +200,7 @@ class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { * @ticket 5305 */ public function test_yearlike_slugs_should_not_be_suffixed_if_permalink_structure_does_not_result_in_a_clash_with_year_archives() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%postname%/' ); $p = $this->factory->post->create( array( 'post_type' => 'post', @@ -224,10 +215,7 @@ class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { * @ticket 5305 */ public function test_slugs_resulting_in_permalinks_that_resemble_month_archives_should_be_suffixed() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%postname%/' ); $p = $this->factory->post->create( array( 'post_type' => 'post', @@ -242,10 +230,7 @@ class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { * @ticket 5305 */ public function test_monthlike_slugs_should_not_be_suffixed_if_permalink_structure_does_not_result_in_a_clash_with_month_archives() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/foo/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/foo/%postname%/' ); $p = $this->factory->post->create( array( 'post_type' => 'post', @@ -260,10 +245,7 @@ class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { * @ticket 5305 */ public function test_monthlike_slugs_should_not_be_suffixed_for_invalid_month_numbers() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%postname%/' ); $p = $this->factory->post->create( array( 'post_type' => 'post', @@ -278,10 +260,7 @@ class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { * @ticket 5305 */ public function test_slugs_resulting_in_permalinks_that_resemble_day_archives_should_be_suffixed() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); $p = $this->factory->post->create( array( 'post_type' => 'post', @@ -296,10 +275,7 @@ class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { * @ticket 5305 */ public function test_daylike_slugs_should_not_be_suffixed_if_permalink_structure_does_not_result_in_a_clash_with_day_archives() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); $p = $this->factory->post->create( array( 'post_type' => 'post', @@ -314,10 +290,7 @@ class Tests_Post_WpUniquePostSlug extends WP_UnitTestCase { * @ticket 5305 */ public function test_daylike_slugs_should_not_be_suffixed_for_invalid_day_numbers() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); $p = $this->factory->post->create( array( 'post_type' => 'post', diff --git a/tests/phpunit/tests/query.php b/tests/phpunit/tests/query.php index a7c20a9d2b..ed2b459448 100644 --- a/tests/phpunit/tests/query.php +++ b/tests/phpunit/tests/query.php @@ -3,15 +3,10 @@ class Tests_Query extends WP_UnitTestCase { function setUp() { - global $wp_rewrite; parent::setUp(); - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); - create_initial_taxonomies(); - - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); } /** diff --git a/tests/phpunit/tests/query/conditionals.php b/tests/phpunit/tests/query/conditionals.php index 3c273884c0..4c91ecf466 100644 --- a/tests/phpunit/tests/query/conditionals.php +++ b/tests/phpunit/tests/query/conditionals.php @@ -22,14 +22,9 @@ class Tests_Query_Conditionals extends WP_UnitTestCase { update_option( 'comments_per_page', 5 ); update_option( 'posts_per_page', 5 ); - global $wp_rewrite; - - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); - create_initial_taxonomies(); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); } function tearDown() { diff --git a/tests/phpunit/tests/query/isTerm.php b/tests/phpunit/tests/query/isTerm.php index ec93ce6f49..99055fa351 100644 --- a/tests/phpunit/tests/query/isTerm.php +++ b/tests/phpunit/tests/query/isTerm.php @@ -23,7 +23,6 @@ class Tests_Query_IsTerm extends WP_UnitTestCase { protected $tax; function setUp() { - global $wp_rewrite; parent::setUp(); set_current_screen( 'front' ); @@ -31,13 +30,12 @@ class Tests_Query_IsTerm extends WP_UnitTestCase { $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%/' ); + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); create_initial_taxonomies(); register_taxonomy( 'testtax', 'post', array( 'public' => true ) ); - $wp_rewrite->flush_rules(); + flush_rewrite_rules(); $this->tag_id = $this->factory->tag->create( array( 'slug' => 'tag-slug' ) ); $this->cat_id = $this->factory->category->create( array( 'slug' => 'cat-slug' ) ); diff --git a/tests/phpunit/tests/query/verboseRewriteRules.php b/tests/phpunit/tests/query/verboseRewriteRules.php index ae71dfa1ca..90d29b253e 100644 --- a/tests/phpunit/tests/query/verboseRewriteRules.php +++ b/tests/phpunit/tests/query/verboseRewriteRules.php @@ -8,15 +8,10 @@ require_once dirname( __FILE__ ) . '/conditionals.php'; */ class Tests_Query_VerbosePageRules extends Tests_Query_Conditionals { function setUp() { - global $wp_rewrite; - parent::setUp(); - - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%category%/%year%/%postname%/' ); create_initial_taxonomies(); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%category%/%year%/%postname%/' ); } } diff --git a/tests/phpunit/tests/rewrite.php b/tests/phpunit/tests/rewrite.php index 0813c3d29a..80f2e9a986 100644 --- a/tests/phpunit/tests/rewrite.php +++ b/tests/phpunit/tests/rewrite.php @@ -9,16 +9,11 @@ class Tests_Rewrite extends WP_UnitTestCase { private $home_url; function setUp() { - global $wp_rewrite; parent::setUp(); - // Need rewrite rules in place to use url_to_postid - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); - create_initial_taxonomies(); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); $this->home_url = get_option( 'home' ); } diff --git a/tests/phpunit/tests/rewrite/numericSlugs.php b/tests/phpunit/tests/rewrite/numericSlugs.php index 5d8745904d..c8f3b28d23 100644 --- a/tests/phpunit/tests/rewrite/numericSlugs.php +++ b/tests/phpunit/tests/rewrite/numericSlugs.php @@ -20,10 +20,8 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_go_to_year_segment_collision_without_title() { - global $wp_rewrite, $wpdb; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%postname%/' ); - $wp_rewrite->flush_rules(); + global $wpdb; + $this->set_permalink_structure( '/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -50,10 +48,8 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_url_to_postid_year_segment_collision_without_title() { - global $wp_rewrite, $wpdb; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%postname%/' ); - $wp_rewrite->flush_rules(); + global $wpdb; + $this->set_permalink_structure( '/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -78,10 +74,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_go_to_year_segment_collision_with_title() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -97,10 +90,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_url_to_postid_year_segment_collision_with_title() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -114,10 +104,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_go_to_month_segment_collision_without_title() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -134,10 +121,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_url_to_postid_month_segment_collision_without_title() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -152,10 +136,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_go_to_month_segment_collision_without_title_no_leading_zero() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -172,10 +153,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_url_to_postid_month_segment_collision_without_title_no_leading_zero() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -190,10 +168,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_go_to_month_segment_collision_with_title() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -209,10 +184,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_url_to_postid_month_segment_collision_with_title() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -226,10 +198,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_go_to_month_segment_collision_with_title_no_leading_zero() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -245,10 +214,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_url_to_postid_month_segment_collision_with_title_no_leading_zero() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -262,10 +228,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_go_to_day_segment_collision_without_title() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -282,10 +245,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_url_to_postid_day_segment_collision_without_title() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -300,10 +260,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_go_to_day_segment_collision_with_title() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -319,10 +276,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_url_to_postid_day_segment_collision_with_title() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -336,10 +290,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_numeric_slug_permalink_conflicts_should_only_be_resolved_for_the_main_query() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -360,10 +311,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_month_slug_collision_should_resolve_to_date_archive_when_year_does_not_match_post_year() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%postname%/' ); // Make sure a post is published in 2013/02, to avoid 404s. $this->factory->post->create( array( @@ -391,10 +339,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_day_slug_collision_should_resolve_to_date_archive_when_monthnum_does_not_match_post_month() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' ); // Make sure a post is published on 2015/01/01, to avoid 404s. $this->factory->post->create( array( @@ -422,10 +367,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_date_slug_collision_should_distinguish_valid_pagination_from_date() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -441,10 +383,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_date_slug_collision_should_distinguish_too_high_pagination_from_date() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -460,10 +399,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_date_slug_collision_should_not_require_pagination_query_var() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, @@ -480,10 +416,7 @@ class Tests_Rewrite_NumericSlugs extends WP_UnitTestCase { } public function test_date_slug_collision_should_be_ignored_when_pagination_var_is_present_but_post_does_not_have_multiple_pages() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%postname%/' ); $id = $this->factory->post->create( array( 'post_author' => $this->author_id, diff --git a/tests/phpunit/tests/rewrite/oldSlugRedirect.php b/tests/phpunit/tests/rewrite/oldSlugRedirect.php index 8497c90c49..02323dc69e 100644 --- a/tests/phpunit/tests/rewrite/oldSlugRedirect.php +++ b/tests/phpunit/tests/rewrite/oldSlugRedirect.php @@ -19,13 +19,12 @@ class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase { add_filter( 'old_slug_redirect_url', array( $this, 'filter_old_slug_redirect_url' ), 10, 1 ); - global $wp_rewrite; + $this->set_permalink_structure( '/%postname%/' ); - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%postname%/' ); add_rewrite_endpoint( 'custom-endpoint', EP_PERMALINK ); add_rewrite_endpoint( 'second-endpoint', EP_PERMALINK, 'custom' ); - $wp_rewrite->flush_rules(); + + flush_rewrite_rules(); } public function tearDown() { diff --git a/tests/phpunit/tests/term/getTermLink.php b/tests/phpunit/tests/term/getTermLink.php index 932c638e59..7ba2488bfb 100644 --- a/tests/phpunit/tests/term/getTermLink.php +++ b/tests/phpunit/tests/term/getTermLink.php @@ -85,10 +85,7 @@ class Tests_Term_GetTermLink extends WP_UnitTestCase { } public function test_taxonomy_permastruct_with_hierarchical_rewrite_should_put_term_ancestors_in_link() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); register_taxonomy( 'wptests_tax2', 'post', array( 'hierarchical' => true, @@ -98,6 +95,8 @@ class Tests_Term_GetTermLink extends WP_UnitTestCase { ), ) ); + flush_rewrite_rules(); + $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2', 'slug' => 'term1', @@ -115,10 +114,7 @@ class Tests_Term_GetTermLink extends WP_UnitTestCase { } public function test_taxonomy_permastruct_with_nonhierarchical_rewrite_should_not_put_term_ancestors_in_link() { - global $wp_rewrite; - $permalink_structure = get_option( 'permalink_structure' ); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); register_taxonomy( 'wptests_tax2', 'post', array( 'hierarchical' => true, @@ -128,6 +124,8 @@ class Tests_Term_GetTermLink extends WP_UnitTestCase { ), ) ); + flush_rewrite_rules(); + $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2', 'slug' => 'term1', diff --git a/tests/phpunit/tests/user/author.php b/tests/phpunit/tests/user/author.php index 09dadf5743..8b83ada7a1 100644 --- a/tests/phpunit/tests/user/author.php +++ b/tests/phpunit/tests/user/author.php @@ -127,10 +127,7 @@ class Tests_User_Author_Template extends WP_UnitTestCase { * @ticket 30355 */ public function test_get_the_author_posts_link_with_permalinks() { - global $wp_rewrite; - $wp_rewrite->init(); - $wp_rewrite->set_permalink_structure( '/%postname%/' ); - $wp_rewrite->flush_rules(); + $this->set_permalink_structure( '/%postname%/' ); $author = $this->factory->user->create_and_get( array( 'display_name' => 'Foo',