Skip to content

Commit 8e88f4c

Browse files
committed
Build/Test Tools: Add assertions that test the tests.
Several tests perform assertions conditionally or iterate dynamic arrays without ensuring they're populated. If the test is faulty and the condition never evaluates to true, or the array being iterated is unexpectedly empty, this will now correctly cause the test to fail. Props johnbillion, jrf. See #63167 git-svn-id: https://develop.svn.wordpress.org/trunk@60251 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 9994c95 commit 8e88f4c

40 files changed

Lines changed: 216 additions & 86 deletions

tests/phpunit/tests/comment.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,8 @@ public function test_wp_get_comment_fields_max_lengths() {
15661566

15671567
$lengths = wp_get_comment_fields_max_lengths();
15681568

1569+
$this->assertNotEmpty( $lengths );
1570+
15691571
foreach ( $lengths as $field => $length ) {
15701572
$this->assertSame( $expected[ $field ], $length );
15711573
}

tests/phpunit/tests/comment/commentsTemplate.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,9 @@ public function test_comment_permalinks_should_be_correct_when_using_default_dis
566566
// Find the comment permalinks.
567567
preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p1, $matches );
568568

569+
$this->assertNotEmpty( $matches );
570+
$this->assertNotEmpty( $matches[1] );
571+
569572
// This is the main post page, so we don't expect any cpage param.
570573
foreach ( $matches[1] as $m ) {
571574
$this->assertStringNotContainsString( 'cpage', $m );
@@ -586,6 +589,9 @@ public function test_comment_permalinks_should_be_correct_when_using_default_dis
586589
// Find the comment permalinks.
587590
preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p2, $matches );
588591

592+
$this->assertNotEmpty( $matches );
593+
$this->assertNotEmpty( $matches[1] );
594+
589595
// They should all be on page 2.
590596
foreach ( $matches[1] as $m ) {
591597
$this->assertStringContainsString( 'cpage=2', $m );
@@ -659,6 +665,9 @@ public function test_comment_permalinks_should_be_correct_when_using_default_dis
659665
// Find the comment permalinks.
660666
preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p0, $matches );
661667

668+
$this->assertNotEmpty( $matches );
669+
$this->assertNotEmpty( $matches[1] );
670+
662671
foreach ( $matches[1] as $m ) {
663672
$this->assertStringContainsString( 'cpage=3', $m );
664673
}
@@ -678,6 +687,9 @@ public function test_comment_permalinks_should_be_correct_when_using_default_dis
678687
// Find the comment permalinks.
679688
preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p2, $matches );
680689

690+
$this->assertNotEmpty( $matches );
691+
$this->assertNotEmpty( $matches[1] );
692+
681693
// They should all be on page 2.
682694
foreach ( $matches[1] as $m ) {
683695
$this->assertStringContainsString( 'cpage=2', $m );
@@ -699,6 +711,9 @@ public function test_comment_permalinks_should_be_correct_when_using_default_dis
699711
// Find the comment permalinks.
700712
preg_match_all( '|href="(.*?#comment-([0-9]+))|', $found_p1, $matches );
701713

714+
$this->assertNotEmpty( $matches );
715+
$this->assertNotEmpty( $matches[1] );
716+
702717
// They should all be on page 2.
703718
foreach ( $matches[1] as $m ) {
704719
$this->assertStringContainsString( 'cpage=1', $m );

tests/phpunit/tests/comment/metaCache.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -195,23 +195,23 @@ public function test_comment_meta_should_be_lazy_loaded_for_all_comments_in_comm
195195

196196
$this->go_to( get_permalink( $p ) );
197197

198-
if ( have_posts() ) {
199-
while ( have_posts() ) {
200-
the_post();
201-
202-
// Load comments with `comments_template()`.
203-
$cform = get_echo( 'comments_template' );
204-
205-
// First request will hit the database.
206-
$num_queries = get_num_queries();
207-
get_comment_meta( $comment_ids[0], 'sauce' );
208-
$this->assertSame( 1, get_num_queries() - $num_queries );
209-
210-
// Second and third requests should be in cache.
211-
get_comment_meta( $comment_ids[1], 'sauce' );
212-
get_comment_meta( $comment_ids[2], 'sauce' );
213-
$this->assertSame( 1, get_num_queries() - $num_queries );
214-
}
198+
$this->assertTrue( have_posts() );
199+
200+
while ( have_posts() ) {
201+
the_post();
202+
203+
// Load comments with `comments_template()`.
204+
$cform = get_echo( 'comments_template' );
205+
206+
// First request will hit the database.
207+
$num_queries = get_num_queries();
208+
get_comment_meta( $comment_ids[0], 'sauce' );
209+
$this->assertSame( 1, get_num_queries() - $num_queries );
210+
211+
// Second and third requests should be in cache.
212+
get_comment_meta( $comment_ids[1], 'sauce' );
213+
get_comment_meta( $comment_ids[2], 'sauce' );
214+
$this->assertSame( 1, get_num_queries() - $num_queries );
215215
}
216216
}
217217

tests/phpunit/tests/customize/nav-menus.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -766,25 +766,27 @@ public function test_available_items_template() {
766766
$this->assertStringContainsString( $expected, $template );
767767

768768
$post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
769-
if ( $post_types ) {
770-
foreach ( $post_types as $type ) {
771-
$this->assertStringContainsString( 'available-menu-items-post_type-' . esc_attr( $type->name ), $template );
772-
$this->assertMatchesRegularExpression( '#<h4 class="accordion-section-title".*>\s*<button type="button" class="accordion-trigger" aria-expanded="false" aria-controls=".*">\s*' . esc_html( $type->labels->name ) . '#', $template );
773-
$this->assertStringContainsString( 'data-type="post_type"', $template );
774-
$this->assertStringContainsString( 'data-object="' . esc_attr( $type->name ) . '"', $template );
775-
$this->assertStringContainsString( 'data-type_label="' . esc_attr( $type->labels->singular_name ) . '"', $template );
776-
}
769+
770+
$this->assertNotEmpty( $post_types );
771+
772+
foreach ( $post_types as $type ) {
773+
$this->assertStringContainsString( 'available-menu-items-post_type-' . esc_attr( $type->name ), $template );
774+
$this->assertMatchesRegularExpression( '#<h4 class="accordion-section-title".*>\s*<button type="button" class="accordion-trigger" aria-expanded="false" aria-controls=".*">\s*' . esc_html( $type->labels->name ) . '#', $template );
775+
$this->assertStringContainsString( 'data-type="post_type"', $template );
776+
$this->assertStringContainsString( 'data-object="' . esc_attr( $type->name ) . '"', $template );
777+
$this->assertStringContainsString( 'data-type_label="' . esc_attr( $type->labels->singular_name ) . '"', $template );
777778
}
778779

779780
$taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'object' );
780-
if ( $taxonomies ) {
781-
foreach ( $taxonomies as $tax ) {
782-
$this->assertStringContainsString( 'available-menu-items-taxonomy-' . esc_attr( $tax->name ), $template );
783-
$this->assertMatchesRegularExpression( '#<h4 class="accordion-section-title".*>\s*<button type="button" class="accordion-trigger" aria-expanded="false" aria-controls=".*">\s*' . esc_html( $tax->labels->name ) . '#', $template );
784-
$this->assertStringContainsString( 'data-type="taxonomy"', $template );
785-
$this->assertStringContainsString( 'data-object="' . esc_attr( $tax->name ) . '"', $template );
786-
$this->assertStringContainsString( 'data-type_label="' . esc_attr( $tax->labels->singular_name ) . '"', $template );
787-
}
781+
782+
$this->assertNotEmpty( $taxonomies );
783+
784+
foreach ( $taxonomies as $tax ) {
785+
$this->assertStringContainsString( 'available-menu-items-taxonomy-' . esc_attr( $tax->name ), $template );
786+
$this->assertMatchesRegularExpression( '#<h4 class="accordion-section-title".*>\s*<button type="button" class="accordion-trigger" aria-expanded="false" aria-controls=".*">\s*' . esc_html( $tax->labels->name ) . '#', $template );
787+
$this->assertStringContainsString( 'data-type="taxonomy"', $template );
788+
$this->assertStringContainsString( 'data-object="' . esc_attr( $tax->name ) . '"', $template );
789+
$this->assertStringContainsString( 'data-type_label="' . esc_attr( $tax->labels->singular_name ) . '"', $template );
788790
}
789791

790792
$this->assertStringContainsString( 'available-menu-items-custom_type', $template );

tests/phpunit/tests/db/charset.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,8 @@ public function test_get_column_charset( $drop, $create, $table, $expected_chars
829829
self::$_wpdb->query( $drop );
830830
self::$_wpdb->query( $create );
831831

832+
$this->assertNotEmpty( $expected_charset );
833+
832834
foreach ( $expected_charset as $column => $charset ) {
833835
if ( self::$utf8_is_utf8mb3 && 'utf8' === $charset ) {
834836
$charset = 'utf8mb3';
@@ -854,6 +856,8 @@ public function test_get_column_charset_non_mysql( $drop, $create, $table, $colu
854856

855857
self::$_wpdb->query( $create );
856858

859+
$this->assertNotEmpty( $columns );
860+
857861
$columns = array_keys( $columns );
858862
foreach ( $columns as $column => $charset ) {
859863
$this->assertFalse( self::$_wpdb->get_col_charset( $table, $column ) );
@@ -877,6 +881,8 @@ public function test_get_column_charset_is_mysql_undefined( $drop, $create, $tab
877881

878882
self::$_wpdb->query( $create );
879883

884+
$this->assertNotEmpty( $columns );
885+
880886
$columns = array_keys( $columns );
881887
foreach ( $columns as $column => $charset ) {
882888
$this->assertFalse( self::$_wpdb->get_col_charset( $table, $column ) );
@@ -1070,6 +1076,8 @@ public function test_table_collation_check( $create, $expected, $query, $drop, $
10701076
)
10711077
);
10721078

1079+
$this->assertNotEmpty( $always_true );
1080+
10731081
foreach ( $always_true as $true_query ) {
10741082
$return = self::$_wpdb->check_safe_collation( $true_query );
10751083
$this->assertTrue(

tests/phpunit/tests/feed/atom.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ public function test_atom_enclosure_with_extended_url_length_type_parsing() {
279279
$entries = xml_find( $xml, 'feed', 'entry' );
280280
$entries = array_slice( $entries, 0, 1 );
281281

282+
$this->assertNotEmpty( $entries );
283+
282284
foreach ( $entries as $key => $entry ) {
283285
$links = xml_find( $entries[ $key ]['child'], 'link' );
284286
$i = 0;

tests/phpunit/tests/feed/rss2.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ public function test_items_comments_closed() {
289289
// Get all the rss -> channel -> item elements.
290290
$items = xml_find( $xml, 'rss', 'channel', 'item' );
291291

292+
$this->assertNotEmpty( $items );
293+
292294
// Check each of the items against the known post data.
293295
foreach ( $items as $key => $item ) {
294296
// Get post for comparison.

tests/phpunit/tests/file.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public function test_get_file_data() {
4040
'AuthorURI' => 'http://binarybonsai.com/',
4141
);
4242

43+
$this->assertNotEmpty( $actual );
44+
4345
foreach ( $actual as $header => $value ) {
4446
$this->assertSame( $expected[ $header ], $value, $header );
4547
}
@@ -64,6 +66,8 @@ public function test_get_file_data_with_cr_line_endings() {
6466
'Author' => 'A Very Old Mac',
6567
);
6668

69+
$this->assertNotEmpty( $actual );
70+
6771
foreach ( $actual as $header => $value ) {
6872
$this->assertSame( $expected[ $header ], $value, $header );
6973
}
@@ -84,6 +88,8 @@ public function test_get_file_data_with_php_open_tag_prefix() {
8488
'TemplateName' => 'Something',
8589
);
8690

91+
$this->assertNotEmpty( $actual );
92+
8793
foreach ( $actual as $header => $value ) {
8894
$this->assertSame( $expected[ $header ], $value, $header );
8995
}

tests/phpunit/tests/formatting/escUrl.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ public function test_protocol() {
124124
)
125125
);
126126

127-
foreach ( wp_allowed_protocols() as $scheme ) {
127+
$protocols = wp_allowed_protocols();
128+
129+
$this->assertNotEmpty( $protocols );
130+
131+
foreach ( $protocols as $scheme ) {
128132
$this->assertSame( "{$scheme}://example.com", esc_url( "{$scheme}://example.com" ), $scheme );
129133
$this->assertSame(
130134
"{$scheme}://example.com",

tests/phpunit/tests/formatting/sanitizePost.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public function test_int_fields() {
2929
case 'string':
3030
$this->assertIsString( $post->$field, "field $field" );
3131
break;
32+
default:
33+
$this->fail( "Type $type is not handled by this test." );
34+
break;
3235
}
3336
}
3437
}

0 commit comments

Comments
 (0)