Skip to content

Commit 0ea2acb

Browse files
committed
Tests: Permalink Structures Phase II: DRY up logic for setting permalink structures in test methods.
Renames `reset_permalinks()` to `set_permalink_structure()` (mimicking `$wp_rewrite->set_permalink_structure()`) and allows it to accept an optional permalink structure. In this way, we can double dip using it to both set and reset the permalink structure from anywhere. Removes alot of duplicated code from tests. See #33968. git-svn-id: https://develop.svn.wordpress.org/trunk@34810 602fd350-edb4-49c9-b593-d223f7449a82
1 parent eebe549 commit 0ea2acb

15 files changed

Lines changed: 82 additions & 264 deletions

tests/phpunit/includes/testcase.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function setUp() {
5050
$this->reset_post_types();
5151
$this->reset_taxonomies();
5252
$this->reset_post_statuses();
53-
$this->reset_permalinks();
53+
$this->set_permalink_structure();
5454
}
5555

5656
$this->start_transaction();
@@ -653,12 +653,14 @@ public function db_version() {
653653
* @since 4.4.0
654654
*
655655
* @global WP_Rewrite $wp_rewrite
656+
*
657+
* @param string $structure Optional. Permalink structure to set. Default empty.
656658
*/
657-
public function reset_permalinks() {
659+
public function set_permalink_structure( $structure = '' ) {
658660
global $wp_rewrite;
659661

660662
$wp_rewrite->init();
661-
$wp_rewrite->set_permalink_structure( '' );
663+
$wp_rewrite->set_permalink_structure( $structure );
662664
$wp_rewrite->flush_rules();
663665
}
664666
}

tests/phpunit/tests/admin/includesPost.php

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,8 @@ function test_bulk_edit_posts_stomping() {
251251
* @ticket 30910
252252
*/
253253
public function test_get_sample_permalink_should_return_pretty_permalink_for_posts_with_post_status_future() {
254-
global $wp_rewrite;
255-
256254
$permalink_structure = '%postname%';
257-
$wp_rewrite->set_permalink_structure( "/$permalink_structure/" );
258-
flush_rewrite_rules();
255+
$this->set_permalink_structure( "/$permalink_structure/" );
259256

260257
$future_date = date( 'Y-m-d H:i:s', time() + 100 );
261258
$p = $this->factory->post->create( array( 'post_status' => 'future', 'post_name' => 'foo', 'post_date' => $future_date ) );
@@ -285,10 +282,7 @@ public function test_get_sample_permalink_html_should_use_default_permalink_for_
285282
* @ticket 18306
286283
*/
287284
public function test_get_sample_permalink_html_should_use_pretty_permalink_for_view_post_link_when_pretty_permalinks_are_enabled() {
288-
global $wp_rewrite;
289-
$permalink_structure = '%postname%';
290-
$wp_rewrite->set_permalink_structure( "/$permalink_structure/" );
291-
flush_rewrite_rules();
285+
$this->set_permalink_structure( '/%postname%/' );
292286

293287
wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
294288

@@ -305,10 +299,7 @@ public function test_get_sample_permalink_html_should_use_pretty_permalink_for_v
305299
* @ticket 18306
306300
*/
307301
public function test_get_sample_permalink_html_should_use_correct_permalink_for_view_post_link_when_changing_slug() {
308-
global $wp_rewrite;
309-
$permalink_structure = '%postname%';
310-
$wp_rewrite->set_permalink_structure( "/$permalink_structure/" );
311-
flush_rewrite_rules();
302+
$this->set_permalink_structure( '/%postname%/' );
312303

313304
wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
314305

@@ -346,10 +337,7 @@ public function test_get_sample_permalink_html_should_use_correct_permalink_for_
346337
* @ticket 5305
347338
*/
348339
public function test_get_sample_permalink_should_avoid_slugs_that_would_create_clashes_with_year_archives() {
349-
global $wp_rewrite;
350-
$wp_rewrite->init();
351-
$wp_rewrite->set_permalink_structure( '/%postname%/' );
352-
$wp_rewrite->flush_rules();
340+
$this->set_permalink_structure( '/%postname%/' );
353341

354342
$p = $this->factory->post->create( array(
355343
'post_name' => '2015',
@@ -363,10 +351,7 @@ public function test_get_sample_permalink_should_avoid_slugs_that_would_create_c
363351
* @ticket 5305
364352
*/
365353
public function test_get_sample_permalink_should_allow_yearlike_slugs_if_permastruct_does_not_cause_an_archive_conflict() {
366-
global $wp_rewrite;
367-
$wp_rewrite->init();
368-
$wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
369-
$wp_rewrite->flush_rules();
354+
$this->set_permalink_structure( '/%year%/%postname%/' );
370355

371356
$p = $this->factory->post->create( array(
372357
'post_name' => '2015',
@@ -380,10 +365,7 @@ public function test_get_sample_permalink_should_allow_yearlike_slugs_if_permast
380365
* @ticket 5305
381366
*/
382367
public function test_get_sample_permalink_should_avoid_slugs_that_would_create_clashes_with_month_archives() {
383-
global $wp_rewrite;
384-
$wp_rewrite->init();
385-
$wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
386-
$wp_rewrite->flush_rules();
368+
$this->set_permalink_structure( '/%year%/%postname%/' );
387369

388370
$p = $this->factory->post->create( array(
389371
'post_name' => '11',
@@ -397,10 +379,7 @@ public function test_get_sample_permalink_should_avoid_slugs_that_would_create_c
397379
* @ticket 5305
398380
*/
399381
public function test_get_sample_permalink_should_ignore_potential_month_conflicts_for_invalid_monthnum() {
400-
global $wp_rewrite;
401-
$wp_rewrite->init();
402-
$wp_rewrite->set_permalink_structure( '/%year%/%postname%/' );
403-
$wp_rewrite->flush_rules();
382+
$this->set_permalink_structure( '/%year%/%postname%/' );
404383

405384
$p = $this->factory->post->create( array(
406385
'post_name' => '13',
@@ -414,10 +393,7 @@ public function test_get_sample_permalink_should_ignore_potential_month_conflict
414393
* @ticket 5305
415394
*/
416395
public function test_get_sample_permalink_should_avoid_slugs_that_would_create_clashes_with_day_archives() {
417-
global $wp_rewrite;
418-
$wp_rewrite->init();
419-
$wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
420-
$wp_rewrite->flush_rules();
396+
$this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
421397

422398
$p = $this->factory->post->create( array(
423399
'post_name' => '30',
@@ -431,10 +407,7 @@ public function test_get_sample_permalink_should_avoid_slugs_that_would_create_c
431407
* @ticket 5305
432408
*/
433409
public function test_get_sample_permalink_should_iterate_slug_suffix_when_a_date_conflict_is_found() {
434-
global $wp_rewrite;
435-
$wp_rewrite->init();
436-
$wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
437-
$wp_rewrite->flush_rules();
410+
$this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
438411

439412
$this->factory->post->create( array(
440413
'post_name' => '30-2',
@@ -452,10 +425,7 @@ public function test_get_sample_permalink_should_iterate_slug_suffix_when_a_date
452425
* @ticket 5305
453426
*/
454427
public function test_get_sample_permalink_should_ignore_potential_day_conflicts_for_invalid_day() {
455-
global $wp_rewrite;
456-
$wp_rewrite->init();
457-
$wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
458-
$wp_rewrite->flush_rules();
428+
$this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
459429

460430
$p = $this->factory->post->create( array(
461431
'post_name' => '32',
@@ -469,10 +439,7 @@ public function test_get_sample_permalink_should_ignore_potential_day_conflicts_
469439
* @ticket 5305
470440
*/
471441
public function test_get_sample_permalink_should_allow_daylike_slugs_if_permastruct_does_not_cause_an_archive_conflict() {
472-
global $wp_rewrite;
473-
$wp_rewrite->init();
474-
$wp_rewrite->set_permalink_structure( '/%year%/%month%/%day%/%postname%/' );
475-
$wp_rewrite->flush_rules();
442+
$this->set_permalink_structure( '/%year%/%month%/%day%/%postname%/' );
476443

477444
$p = $this->factory->post->create( array(
478445
'post_name' => '30',

tests/phpunit/tests/link.php

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ function _get_pagenum_link_cb( $url ) {
2020
function test_get_pagenum_link_case_insensitivity() {
2121
$old_req_uri = $_SERVER['REQUEST_URI'];
2222

23-
global $wp_rewrite;
24-
$wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/');
25-
$wp_rewrite->flush_rules();
23+
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
2624

2725
add_filter( 'home_url', array( $this, '_get_pagenum_link_cb' ) );
2826
$_SERVER['REQUEST_URI'] = '/woohoo';
@@ -35,8 +33,6 @@ function test_get_pagenum_link_case_insensitivity() {
3533
}
3634

3735
function test_wp_get_shortlink() {
38-
global $wp_rewrite;
39-
4036
$post_id = $this->factory->post->create();
4137
$post_id2 = $this->factory->post->create();
4238

@@ -67,8 +63,7 @@ function test_wp_get_shortlink() {
6763
$this->assertEquals( '', wp_get_shortlink( 0 ) );
6864
$this->assertEquals( '', wp_get_shortlink() );
6965

70-
$wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
71-
$wp_rewrite->flush_rules();
66+
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
7267

7368
// With a permalink structure set, get_permalink() will no longer match.
7469
$this->assertNotEquals( get_permalink( $post_id ), wp_get_shortlink( $post_id, 'post' ) );
@@ -88,9 +83,7 @@ function test_wp_get_shortlink_with_page() {
8883
// Don't test against get_permalink() since it uses ?page_id= for pages.
8984
$this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) );
9085

91-
global $wp_rewrite;
92-
$wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
93-
$wp_rewrite->flush_rules();
86+
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
9487

9588
$this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) );
9689
}
@@ -105,10 +98,7 @@ function test_wp_get_shortlink_with_home_page() {
10598

10699
$this->assertEquals( home_url( '/' ), wp_get_shortlink( $post_id, 'post' ) );
107100

108-
global $wp_rewrite;
109-
$wp_rewrite->permalink_structure = '';
110-
$wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
111-
$wp_rewrite->flush_rules();
101+
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
112102

113103
$this->assertEquals( home_url( '/' ), wp_get_shortlink( $post_id, 'post' ) );
114104
}
@@ -396,9 +386,7 @@ public function test_get_permalink_should_not_reveal_post_name_for_cpt_with_post
396386
* @ticket 1914
397387
*/
398388
public function test_unattached_attachment_has_a_pretty_permalink() {
399-
global $wp_rewrite;
400-
$wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/');
401-
$wp_rewrite->flush_rules();
389+
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
402390

403391
$attachment_id = $this->factory->attachment->create_object( 'image.jpg', 0, array(
404392
'post_mime_type' => 'image/jpeg',
@@ -416,12 +404,13 @@ public function test_unattached_attachment_has_a_pretty_permalink() {
416404
* @ticket 1914
417405
*/
418406
public function test_attachment_attached_to_non_existent_post_type_has_a_pretty_permalink() {
419-
global $wp_rewrite, $wp_post_types;
420-
$wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/');
407+
global $wp_post_types;
408+
409+
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
421410

422411
register_post_type( 'not_a_post_type', array( 'public' => true ) );
423412

424-
$wp_rewrite->flush_rules();
413+
flush_rewrite_rules();
425414

426415
$post_id = $this->factory->post->create( array( 'post_type' => 'not_a_post_type' ) );
427416

tests/phpunit/tests/link/getPostCommentsFeedLink.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ public function test_post_link() {
1717
}
1818

1919
public function test_post_pretty_link() {
20-
global $wp_rewrite;
21-
$wp_rewrite->init();
22-
$wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
23-
$wp_rewrite->flush_rules();
20+
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
2421

2522
$post_id = $this->factory->post->create();
2623

@@ -47,10 +44,7 @@ public function test_attachment_link() {
4744
}
4845

4946
public function test_attachment_pretty_link() {
50-
global $wp_rewrite;
51-
$wp_rewrite->init();
52-
$wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
53-
$wp_rewrite->flush_rules();
47+
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
5448

5549
$post_id = $this->factory->post->create( array(
5650
'post_status' => 'publish'
@@ -70,10 +64,7 @@ public function test_attachment_pretty_link() {
7064
}
7165

7266
public function test_attachment_no_name_pretty_link() {
73-
global $wp_rewrite;
74-
$wp_rewrite->init();
75-
$wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
76-
$wp_rewrite->flush_rules();
67+
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
7768

7869
$post_id = $this->factory->post->create();
7970
$attachment_id = $this->factory->attachment->create_object( 'image.jpg', $post_id, array(
@@ -103,10 +94,7 @@ public function test_unattached_link() {
10394
}
10495

10596
public function test_unattached_pretty_link() {
106-
global $wp_rewrite;
107-
$wp_rewrite->init();
108-
$wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
109-
$wp_rewrite->flush_rules();
97+
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
11098

11199
$attachment_id = $this->factory->attachment->create_object( 'image.jpg', 0, array(
112100
'post_mime_type' => 'image/jpeg',

tests/phpunit/tests/post.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,7 @@ function test_vb_insert_future_change_to_private() {
415415
* @ticket 5305
416416
*/
417417
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() {
418-
global $wp_rewrite;
419-
$wp_rewrite->init();
420-
$wp_rewrite->set_permalink_structure( '/%postname%/' );
421-
$wp_rewrite->flush_rules();
418+
$this->set_permalink_structure( '/%postname%/' );
422419

423420
$p = wp_insert_post( array(
424421
'post_title' => '',
@@ -429,7 +426,7 @@ public function test_wp_insert_post_should_not_allow_a_bare_numeric_slug_that_mi
429426

430427
$post = get_post( $p );
431428

432-
$this->reset_permalinks();
429+
$this->set_permalink_structure();
433430

434431
$this->assertEquals( "$p-2", $post->post_name );
435432
}
@@ -505,10 +502,7 @@ function test_permalink_without_title() {
505502
// bug: permalink doesn't work if post title is empty
506503
// might only fail if the post ID is greater than four characters
507504

508-
global $wp_rewrite;
509-
$wp_rewrite->init();
510-
$wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/');
511-
$wp_rewrite->flush_rules();
505+
$this->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/');
512506

513507
$post = array(
514508
'post_author' => $this->author_id,

0 commit comments

Comments
 (0)