Make WordPress Core


Ignore:
Timestamp:
05/26/2025 12:03:18 PM (10 months ago)
Author:
johnbillion
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/customize/nav-menus.php

    r59224 r60251  
    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
Note: See TracChangeset for help on using the changeset viewer.