Skip to content

Commit bb3bf22

Browse files
Tests: Use more appropriate assertions in various tests.
This replaces instances of `assertFalse( stripos( ... ) )` with `assertStringNotContainsString()` or `assertStringNotContainsStringIgnoringCase()` to use native PHPUnit functionality. Going forward, these methods introduced in PHPUnit 7.5 should be used for similar assertions: * `assertStringContainsString()` * `assertStringContainsStringIgnoringCase()` * `assertStringNotContainsString()` * `assertStringNotContainsStringIgnoringCase()` As WordPress currently uses PHPUnit 5.7.x to run tests on PHP 5.6, polyfills for these methods are now added to the `WP_UnitTestCase` class for PHPUnit < 7.5. Follow-up to [51335], [51337], [51367], [51397], [51403], [51404], [51436], [51438], [51448], [51449], [51451], [51453], [51454]. See #53363. git-svn-id: https://develop.svn.wordpress.org/trunk@51461 602fd350-edb4-49c9-b593-d223f7449a82
1 parent fd872e5 commit bb3bf22

8 files changed

Lines changed: 48 additions & 16 deletions

tests/phpunit/includes/testcase.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,22 @@ public static function assertStringContainsString( $needle, $haystack, $message
389389
static::assertContains( $needle, $haystack, $message );
390390
}
391391

392+
/**
393+
* Asserts that a string haystack contains a needle (case-insensitive).
394+
*
395+
* This method has been backported from a more recent PHPUnit version,
396+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
397+
*
398+
* @since 5.9.0
399+
*
400+
* @param string $needle The string to search for.
401+
* @param string $haystack The string to treat as the haystack.
402+
* @param string $message Optional. Message to display when the assertion fails.
403+
*/
404+
public static function assertStringContainsStringIgnoringCase( $needle, $haystack, $message = '' ) {
405+
static::assertContains( $needle, $haystack, $message, true );
406+
}
407+
392408
/**
393409
* Asserts that a string haystack does not contain a needle.
394410
*
@@ -404,4 +420,20 @@ public static function assertStringContainsString( $needle, $haystack, $message
404420
public static function assertStringNotContainsString( $needle, $haystack, $message = '' ) {
405421
static::assertNotContains( $needle, $haystack, $message );
406422
}
423+
424+
/**
425+
* Asserts that a string haystack does not contain a needle (case-insensitive).
426+
*
427+
* This method has been backported from a more recent PHPUnit version,
428+
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
429+
*
430+
* @since 5.9.0
431+
*
432+
* @param string $needle The string to search for.
433+
* @param string $haystack The string to treat as the haystack.
434+
* @param string $message Optional. Message to display when the assertion fails.
435+
*/
436+
public static function assertStringNotContainsStringIgnoringCase( $needle, $haystack, $message = '' ) {
437+
static::assertNotContains( $needle, $haystack, $message, true );
438+
}
407439
}

tests/phpunit/tests/rest-api/rest-categories-controller.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ public function test_get_terms_pagination_headers() {
626626
),
627627
rest_url( 'wp/v2/categories' )
628628
);
629-
$this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
629+
$this->assertStringNotContainsString( 'rel="prev"', $headers['Link'] );
630630
$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
631631

632632
// 3rd page.
@@ -670,7 +670,7 @@ public function test_get_terms_pagination_headers() {
670670
rest_url( 'wp/v2/categories' )
671671
);
672672
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
673-
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
673+
$this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
674674

675675
// Out of bounds.
676676
$request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
@@ -687,7 +687,7 @@ public function test_get_terms_pagination_headers() {
687687
rest_url( 'wp/v2/categories' )
688688
);
689689
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
690-
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
690+
$this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
691691
}
692692

693693
public function test_get_items_per_page_exceeds_number_of_items() {

tests/phpunit/tests/rest-api/rest-comments-controller.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ public function test_get_comments_pagination_headers() {
821821
),
822822
rest_url( '/wp/v2/comments' )
823823
);
824-
$this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
824+
$this->assertStringNotContainsString( 'rel="prev"', $headers['Link'] );
825825
$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
826826

827827
// 3rd page.
@@ -867,7 +867,7 @@ public function test_get_comments_pagination_headers() {
867867
rest_url( '/wp/v2/comments' )
868868
);
869869
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
870-
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
870+
$this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
871871

872872
// Out of bounds.
873873
$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
@@ -883,7 +883,7 @@ public function test_get_comments_pagination_headers() {
883883
rest_url( '/wp/v2/comments' )
884884
);
885885
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
886-
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
886+
$this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
887887
}
888888

889889
public function test_get_comments_invalid_date() {

tests/phpunit/tests/rest-api/rest-pattern-directory-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public function test_get_items_search() {
176176
foreach ( $patterns as $pattern ) {
177177
$search_field_values = $pattern['title'] . ' ' . $pattern['description'];
178178

179-
$this->assertNotFalse( stripos( $search_field_values, $search_term ) );
179+
$this->assertStringContainsStringIgnoringCase( $search_term, $search_field_values );
180180
}
181181
}
182182

tests/phpunit/tests/rest-api/rest-posts-controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,7 @@ public function test_get_items_pagination_headers() {
15311531
),
15321532
rest_url( '/wp/v2/posts' )
15331533
);
1534-
$this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
1534+
$this->assertStringNotContainsString( 'rel="prev"', $headers['Link'] );
15351535
$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
15361536

15371537
// 3rd page.
@@ -1573,7 +1573,7 @@ public function test_get_items_pagination_headers() {
15731573
rest_url( '/wp/v2/posts' )
15741574
);
15751575
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
1576-
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
1576+
$this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
15771577

15781578
// Out of bounds.
15791579
$request = new WP_REST_Request( 'GET', '/wp/v2/posts' );

tests/phpunit/tests/rest-api/rest-revisions-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ public function test_get_items_pagination_header_of_the_first_page() {
482482
),
483483
rest_url( $rest_route )
484484
);
485-
$this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
485+
$this->assertStringNotContainsString( 'rel="prev"', $headers['Link'] );
486486
$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
487487
}
488488

tests/phpunit/tests/rest-api/rest-tags-controller.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ public function test_get_terms_pagination_headers() {
590590
),
591591
rest_url( 'wp/v2/tags' )
592592
);
593-
$this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
593+
$this->assertStringNotContainsString( 'rel="prev"', $headers['Link'] );
594594
$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
595595

596596
// 3rd page.
@@ -632,7 +632,7 @@ public function test_get_terms_pagination_headers() {
632632
rest_url( 'wp/v2/tags' )
633633
);
634634
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
635-
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
635+
$this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
636636

637637
// Out of bounds.
638638
$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
@@ -648,7 +648,7 @@ public function test_get_terms_pagination_headers() {
648648
rest_url( 'wp/v2/tags' )
649649
);
650650
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
651-
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
651+
$this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
652652
}
653653

654654
public function test_get_items_invalid_context() {

tests/phpunit/tests/rest-api/rest-users-controller.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ public function test_get_items_pagination_headers() {
321321
),
322322
rest_url( 'wp/v2/users' )
323323
);
324-
$this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
324+
$this->assertStringNotContainsString( 'rel="prev"', $headers['Link'] );
325325
$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
326326

327327
// 3rd page.
@@ -363,7 +363,7 @@ public function test_get_items_pagination_headers() {
363363
rest_url( 'wp/v2/users' )
364364
);
365365
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
366-
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
366+
$this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
367367

368368
// Out of bounds.
369369
$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
@@ -379,7 +379,7 @@ public function test_get_items_pagination_headers() {
379379
rest_url( 'wp/v2/users' )
380380
);
381381
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
382-
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
382+
$this->assertStringNotContainsString( 'rel="next"', $headers['Link'] );
383383
}
384384

385385
public function test_get_items_per_page() {

0 commit comments

Comments
 (0)