Make WordPress Core

Changeset 51404


Ignore:
Timestamp:
07/12/2021 10:35:44 AM (5 years ago)
Author:
SergeyBiryukov
Message:

Tests: Use more appropriate assertions in various tests.

This replaces instances of assertTrue( in_array( ... ) ) with assertContains() to use native PHPUnit functionality.

Follow-up to [51335], [51337], [51367], [51397], [51403].

Props hellofromTonya, jrf, SergeyBiryukov.
Fixes #53123. See #53363.

Location:
trunk/tests/phpunit/tests
Files:
25 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/ajax/DeleteComment.php

    r49603 r51404  
    109109                // Check for either possible total.
    110110                $message = sprintf( 'returned value: %1$d $total: %2$d  $recalc_total: %3$d', (int) $xml->response[0]->comment[0]->supplemental[0]->total[0], $total, $recalc_total );
    111                 $this->assertTrue( in_array( (int) $xml->response[0]->comment[0]->supplemental[0]->total[0], array( $total, $recalc_total ), true ), $message );
     111                $this->assertContains( (int) $xml->response[0]->comment[0]->supplemental[0]->total[0], array( $total, $recalc_total ), $message );
    112112        }
    113113
  • trunk/tests/phpunit/tests/ajax/DimComment.php

    r48997 r51404  
    104104
    105105                // Check for either possible total.
    106                 $this->assertTrue( in_array( (int) $xml->response[0]->comment[0]->supplemental[0]->total[0], array( $total, $recalc_total ), true ) );
     106                $this->assertContains( (int) $xml->response[0]->comment[0]->supplemental[0]->total[0], array( $total, $recalc_total ) );
    107107        }
    108108
  • trunk/tests/phpunit/tests/ajax/Response.php

    r49025 r51404  
    8484                ob_end_clean();
    8585
    86                 $this->assertTrue( in_array( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), $headers, true ) );
     86                $this->assertContains( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), $headers );
    8787        }
    8888
  • trunk/tests/phpunit/tests/feed/atom.php

    r49603 r51404  
    187187                        $categories = xml_find( $entries[ $key ]['child'], 'category' );
    188188                        foreach ( $categories as $category ) {
    189                                 $this->assertTrue( in_array( $category['attributes']['term'], $terms, true ) );
     189                                $this->assertContains( $category['attributes']['term'], $terms );
    190190                        }
    191191                        unset( $terms );
  • trunk/tests/phpunit/tests/formatting/EscUrl.php

    r48937 r51404  
    128128                }
    129129
    130                 $this->assertTrue( ! in_array( 'data', wp_allowed_protocols(), true ) );
     130                $this->assertNotContains( 'data', wp_allowed_protocols() );
    131131                $this->assertSame( '', esc_url( 'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D' ) );
    132132
    133                 $this->assertTrue( ! in_array( 'foo', wp_allowed_protocols(), true ) );
     133                $this->assertNotContains( 'foo', wp_allowed_protocols() );
    134134                $this->assertSame(
    135135                        'foo://example.com',
  • trunk/tests/phpunit/tests/oembed/headers.php

    r49025 r51404  
    3232                $headers = xdebug_get_headers();
    3333
    34                 $this->assertTrue( in_array( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), $headers, true ) );
     34                $this->assertContains( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), $headers );
    3535        }
    3636}
  • trunk/tests/phpunit/tests/pluggable.php

    r50916 r51404  
    6363                        $msg = 'Function: ' . $function . '()';
    6464                        $this->assertTrue( function_exists( $function ), $msg );
    65                         $this->assertTrue( in_array( $function, $defined, true ), $msg );
     65                        $this->assertContains( $function, $defined, $msg );
    6666                }
    6767
  • trunk/tests/phpunit/tests/post/meta.php

    r51331 r51404  
    9696                );
    9797                sort( $expected );
    98                 $this->assertTrue( in_array( get_post_meta( self::$post_id, 'nonunique', true ), $expected, true ) );
     98                $this->assertContains( get_post_meta( self::$post_id, 'nonunique', true ), $expected );
    9999                $actual = get_post_meta( self::$post_id, 'nonunique', false );
    100100                sort( $actual );
  • trunk/tests/phpunit/tests/post/objects.php

    r51397 r51404  
    3737                $this->assertIsArray( $post );
    3838                $this->assertArrayNotHasKey( 'post_type', $post );
    39                 $this->assertTrue( in_array( 'post', $post, true ) );
     39                $this->assertContains( 'post', $post );
    4040
    4141                $post = get_post( $id );
  • trunk/tests/phpunit/tests/post/wpPostType.php

    r49327 r51404  
    120120                $post_type_object->add_rewrite_rules();
    121121
    122                 $this->assertFalse( in_array( 'foobar', $wp->public_query_vars, true ) );
     122                $this->assertNotContains( 'foobar', $wp->public_query_vars );
    123123        }
    124124
  • trunk/tests/phpunit/tests/rest-api.php

    r51397 r51404  
    266266        function test_rest_route_query_var() {
    267267                rest_api_init();
    268                 $this->assertTrue( in_array( 'rest_route', $GLOBALS['wp']->public_query_vars, true ) );
     268                $this->assertContains( 'rest_route', $GLOBALS['wp']->public_query_vars );
    269269        }
    270270
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r51397 r51404  
    303303                $this->assertCount( 2, $data );
    304304                $ids = wp_list_pluck( $data, 'id' );
    305                 $this->assertTrue( in_array( $id1, $ids, true ) );
    306                 $this->assertFalse( in_array( $id2, $ids, true ) );
    307                 $this->assertTrue( in_array( $id3, $ids, true ) );
     305                $this->assertContains( $id1, $ids );
     306                $this->assertNotContains( $id2, $ids );
     307                $this->assertContains( $id3, $ids );
    308308
    309309                $this->check_get_posts_response( $response );
     
    344344                $this->assertCount( 3, $data );
    345345                $ids = wp_list_pluck( $data, 'id' );
    346                 $this->assertTrue( in_array( $id1, $ids, true ) );
    347                 $this->assertTrue( in_array( $id2, $ids, true ) );
    348                 $this->assertTrue( in_array( $id3, $ids, true ) );
     346                $this->assertContains( $id1, $ids );
     347                $this->assertContains( $id2, $ids );
     348                $this->assertContains( $id3, $ids );
    349349        }
    350350
  • trunk/tests/phpunit/tests/rest-api/rest-categories-controller.php

    r51397 r51404  
    312312                $data     = $response->get_data();
    313313                $ids      = wp_list_pluck( $data, 'id' );
    314                 $this->assertTrue( in_array( $id1, $ids, true ) );
    315                 $this->assertTrue( in_array( $id2, $ids, true ) );
     314                $this->assertContains( $id1, $ids );
     315                $this->assertContains( $id2, $ids );
    316316
    317317                $request->set_param( 'exclude', array( $id2 ) );
     
    319319                $data     = $response->get_data();
    320320                $ids      = wp_list_pluck( $data, 'id' );
    321                 $this->assertTrue( in_array( $id1, $ids, true ) );
    322                 $this->assertFalse( in_array( $id2, $ids, true ) );
     321                $this->assertContains( $id1, $ids );
     322                $this->assertNotContains( $id2, $ids );
    323323        }
    324324
  • trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r51367 r51404  
    248248
    249249                $collection_data = $response->get_data();
    250                 $this->assertTrue( in_array( $password_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
     250                $this->assertContains( $password_comment, wp_list_pluck( $collection_data, 'id' ) );
    251251        }
    252252
     
    271271
    272272                $collection_data = $response->get_data();
    273                 $this->assertFalse( in_array( $password_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
     273                $this->assertNotContains( $password_comment, wp_list_pluck( $collection_data, 'id' ) );
    274274        }
    275275
     
    311311
    312312                $collection_data = $response->get_data();
    313                 $this->assertFalse( in_array( $password_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
     313                $this->assertNotContains( $password_comment, wp_list_pluck( $collection_data, 'id' ) );
    314314        }
    315315
     
    330330
    331331                $collection_data = $response->get_data();
    332                 $this->assertTrue( in_array( $password_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
     332                $this->assertContains( $password_comment, wp_list_pluck( $collection_data, 'id' ) );
    333333        }
    334334
     
    349349
    350350                $collection_data = $response->get_data();
    351                 $this->assertFalse( in_array( $private_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
     351                $this->assertNotContains( $private_comment, wp_list_pluck( $collection_data, 'id' ) );
    352352        }
    353353
     
    368368
    369369                $collection_data = $response->get_data();
    370                 $this->assertTrue( in_array( $private_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
     370                $this->assertContains( $private_comment, wp_list_pluck( $collection_data, 'id' ) );
    371371        }
    372372
     
    387387
    388388                $collection_data = $response->get_data();
    389                 $this->assertFalse( in_array( $comment_id, wp_list_pluck( $collection_data, 'id' ), true ) );
     389                $this->assertNotContains( $comment_id, wp_list_pluck( $collection_data, 'id' ) );
    390390
    391391                wp_delete_comment( $comment_id );
     
    408408
    409409                $collection_data = $response->get_data();
    410                 $this->assertTrue( in_array( $comment_id, wp_list_pluck( $collection_data, 'id' ), true ) );
     410                $this->assertContains( $comment_id, wp_list_pluck( $collection_data, 'id' ) );
    411411
    412412                wp_delete_comment( $comment_id );
     
    526526                $data     = $response->get_data();
    527527                $ids      = wp_list_pluck( $data, 'id' );
    528                 $this->assertTrue( in_array( $id1, $ids, true ) );
    529                 $this->assertTrue( in_array( $id2, $ids, true ) );
     528                $this->assertContains( $id1, $ids );
     529                $this->assertContains( $id2, $ids );
    530530
    531531                $request->set_param( 'exclude', array( $id2 ) );
     
    533533                $data     = $response->get_data();
    534534                $ids      = wp_list_pluck( $data, 'id' );
    535                 $this->assertTrue( in_array( $id1, $ids, true ) );
    536                 $this->assertFalse( in_array( $id2, $ids, true ) );
     535                $this->assertContains( $id1, $ids );
     536                $this->assertNotContains( $id2, $ids );
    537537
    538538                // Invalid 'exclude' should error.
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r51397 r51404  
    476476                $data     = $response->get_data();
    477477                $ids      = wp_list_pluck( $data, 'id' );
    478                 $this->assertTrue( in_array( $id1, $ids, true ) );
    479                 $this->assertTrue( in_array( $id2, $ids, true ) );
     478                $this->assertContains( $id1, $ids );
     479                $this->assertContains( $id2, $ids );
    480480
    481481                $request->set_param( 'exclude', array( $id2 ) );
     
    483483                $data     = $response->get_data();
    484484                $ids      = wp_list_pluck( $data, 'id' );
    485                 $this->assertTrue( in_array( $id1, $ids, true ) );
    486                 $this->assertFalse( in_array( $id2, $ids, true ) );
     485                $this->assertContains( $id1, $ids );
     486                $this->assertNotContains( $id2, $ids );
    487487
    488488                $request->set_param( 'exclude', (string) $id2 );
     
    490490                $data     = $response->get_data();
    491491                $ids      = wp_list_pluck( $data, 'id' );
    492                 $this->assertTrue( in_array( $id1, $ids, true ) );
    493                 $this->assertFalse( in_array( $id2, $ids, true ) );
     492                $this->assertContains( $id1, $ids );
     493                $this->assertNotContains( $id2, $ids );
    494494
    495495                $request->set_param( 'exclude', 'invalid' );
     
    14831483                $posts = $response->get_data();
    14841484                $ids   = wp_list_pluck( $posts, 'id' );
    1485                 $this->assertTrue( in_array( $id1, $ids, true ) );
    1486                 $this->assertFalse( in_array( $id2, $ids, true ) );
    1487                 $this->assertFalse( in_array( $id3, $ids, true ) );
     1485                $this->assertContains( $id1, $ids );
     1486                $this->assertNotContains( $id2, $ids );
     1487                $this->assertNotContains( $id3, $ids);
    14881488
    14891489                $this->assertPostsWhere( " AND {posts}.ID NOT IN ($id3,$id2) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" );
     
    15091509                $posts = $response->get_data();
    15101510                $ids   = wp_list_pluck( $posts, 'id' );
    1511                 $this->assertTrue( in_array( $id1, $ids, true ) );
    1512                 $this->assertTrue( in_array( $id2, $ids, true ) );
    1513                 $this->assertFalse( in_array( $id3, $ids, true ) );
     1511                $this->assertContains( $id1, $ids );
     1512                $this->assertContains( $id2, $ids );
     1513                $this->assertNotContains( $id3, $ids );
    15141514
    15151515                $this->assertPostsWhere( " AND {posts}.ID NOT IN ($id3) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" );
  • trunk/tests/phpunit/tests/rest-api/rest-request.php

    r51331 r51404  
    617617                $data = $valid->get_error_data( 'rest_missing_callback_param' );
    618618
    619                 $this->assertTrue( in_array( 'someinteger', $data['params'], true ) );
    620                 $this->assertTrue( in_array( 'someotherinteger', $data['params'], true ) );
     619                $this->assertContains( 'someinteger', $data['params'] );
     620                $this->assertContains( 'someotherinteger', $data['params'] );
    621621        }
    622622
  • trunk/tests/phpunit/tests/rest-api/rest-tags-controller.php

    r51397 r51404  
    246246                $data     = $response->get_data();
    247247                $ids      = wp_list_pluck( $data, 'id' );
    248                 $this->assertTrue( in_array( $id1, $ids, true ) );
    249                 $this->assertTrue( in_array( $id2, $ids, true ) );
     248                $this->assertContains( $id1, $ids );
     249                $this->assertContains( $id2, $ids );
    250250
    251251                $request->set_param( 'exclude', array( $id2 ) );
     
    253253                $data     = $response->get_data();
    254254                $ids      = wp_list_pluck( $data, 'id' );
    255                 $this->assertTrue( in_array( $id1, $ids, true ) );
    256                 $this->assertFalse( in_array( $id2, $ids, true ) );
     255                $this->assertContains( $id1, $ids );
     256                $this->assertNotContains( $id2, $ids );
    257257
    258258                // Invalid 'exclude' should error.
  • trunk/tests/phpunit/tests/rest-api/rest-users-controller.php

    r51403 r51404  
    278278                $user_ids = wp_list_pluck( $users, 'id' );
    279279
    280                 $this->assertTrue( in_array( self::$editor, $user_ids, true ) );
    281                 $this->assertTrue( in_array( self::$authors['r_true_p_true'], $user_ids, true ) );
    282                 $this->assertTrue( in_array( self::$authors['r_true_p_false'], $user_ids, true ) );
     280                $this->assertContains( self::$editor, $user_ids );
     281                $this->assertContains( self::$authors['r_true_p_true'], $user_ids );
     282                $this->assertContains( self::$authors['r_true_p_false'], $user_ids );
    283283                $this->assertCount( 3, $user_ids );
    284284        }
     
    290290                $user_ids = wp_list_pluck( $users, 'id' );
    291291
    292                 $this->assertFalse( in_array( self::$authors['r_false_p_true'], $user_ids, true ) );
    293                 $this->assertFalse( in_array( self::$authors['r_false_p_false'], $user_ids, true ) );
     292                $this->assertNotContains( self::$authors['r_false_p_true'], $user_ids );
     293                $this->assertNotContains( self::$authors['r_false_p_false'], $user_ids );
    294294        }
    295295
     
    300300                $user_ids = wp_list_pluck( $users, 'id' );
    301301
    302                 $this->assertFalse( in_array( self::$draft_editor, $user_ids, true ) );
    303                 $this->assertFalse( in_array( self::$user, $user_ids, true ) );
     302                $this->assertNotContains( self::$draft_editor, $user_ids );
     303                $this->assertNotContains( self::$user, $user_ids );
    304304        }
    305305
     
    634634                $data     = $response->get_data();
    635635                $ids      = wp_list_pluck( $data, 'id' );
    636                 $this->assertTrue( in_array( $id1, $ids, true ) );
    637                 $this->assertTrue( in_array( $id2, $ids, true ) );
     636                $this->assertContains( $id1, $ids );
     637                $this->assertContains( $id2, $ids );
    638638
    639639                $request->set_param( 'exclude', array( $id2 ) );
     
    641641                $data     = $response->get_data();
    642642                $ids      = wp_list_pluck( $data, 'id' );
    643                 $this->assertTrue( in_array( $id1, $ids, true ) );
    644                 $this->assertFalse( in_array( $id2, $ids, true ) );
     643                $this->assertContains( $id1, $ids );
     644                $this->assertNotContains( $id2, $ids );
    645645
    646646                // Invalid 'exclude' should error.
  • trunk/tests/phpunit/tests/term/wpInsertTerm.php

    r51403 r51404  
    823823                $cached_children = get_option( 'wptests_tax_children' );
    824824                $this->assertNotEmpty( $cached_children[ $t ] );
    825                 $this->assertTrue( in_array( $found['term_id'], $cached_children[ $t ], true ) );
     825                $this->assertContains( $found['term_id'], $cached_children[ $t ] );
    826826        }
    827827
  • trunk/tests/phpunit/tests/term/wpTaxonomy.php

    r46586 r51404  
    2323
    2424                $taxonomy_object->add_rewrite_rules();
    25                 $this->assertFalse( in_array( 'foobar', $wp->public_query_vars, true ) );
     25                $this->assertNotContains( 'foobar', $wp->public_query_vars );
    2626        }
    2727
  • trunk/tests/phpunit/tests/term/wpUpdateTerm.php

    r51331 r51404  
    686686                $cached_children = get_option( 'wptests_tax_children' );
    687687                $this->assertNotEmpty( $cached_children[ $t2 ] );
    688                 $this->assertTrue( in_array( $found['term_id'], $cached_children[ $t2 ], true ) );
     688                $this->assertContains( $found['term_id'], $cached_children[ $t2 ] );
    689689        }
    690690
  • trunk/tests/phpunit/tests/theme/themeDir.php

    r51403 r51404  
    209209
    210210                $templates = $theme['Template Files'];
    211                 $this->assertTrue( in_array( $this->theme_root . '/page-templates/template-top-level.php', $templates, true ) );
     211                $this->assertContains( $this->theme_root . '/page-templates/template-top-level.php', $templates );
    212212        }
    213213
  • trunk/tests/phpunit/tests/user.php

    r51397 r51404  
    11141114                );
    11151115
    1116                 $this->assertTrue( in_array( (string) self::$contrib_id, $users, true ) );
     1116                $this->assertContains( (string) self::$contrib_id, $users );
    11171117        }
    11181118
     
    11251125                );
    11261126
    1127                 $this->assertTrue( in_array( (string) self::$contrib_id, $users, true ) );
     1127                $this->assertContains( (string) self::$contrib_id, $users );
    11281128        }
    11291129
     
    11361136                );
    11371137
    1138                 $this->assertTrue( in_array( (string) self::$contrib_id, $users, true ) );
     1138                $this->assertContains( (string) self::$contrib_id, $users );
    11391139        }
    11401140
     
    11471147                );
    11481148
    1149                 $this->assertTrue( in_array( (string) self::$contrib_id, $users, true ) );
     1149                $this->assertContains( (string) self::$contrib_id, $users );
    11501150        }
    11511151
     
    11581158                );
    11591159
    1160                 $this->assertTrue( in_array( (string) self::$contrib_id, $users, true ) );
     1160                $this->assertContains( (string) self::$contrib_id, $users );
    11611161        }
    11621162
  • trunk/tests/phpunit/tests/wp.php

    r51367 r51404  
    2323
    2424                $this->assertCount( $public_qv_count + 2, $this->wp->public_query_vars );
    25                 $this->assertTrue( in_array( 'test', $this->wp->public_query_vars, true ) );
    26                 $this->assertTrue( in_array( 'test2', $this->wp->public_query_vars, true ) );
     25                $this->assertContains( 'test', $this->wp->public_query_vars );
     26                $this->assertContains( 'test2', $this->wp->public_query_vars );
    2727        }
    2828
     
    3131
    3232                $this->wp->add_query_var( 'test' );
    33                 $this->assertTrue( in_array( 'test', $this->wp->public_query_vars, true ) );
     33                $this->assertContains( 'test', $this->wp->public_query_vars );
    3434                $this->wp->remove_query_var( 'test' );
    3535
  • trunk/tests/phpunit/tests/xmlrpc/wp/editPost.php

    r51367 r51404  
    446446
    447447                // For good measure, check that the expected value is in the array.
    448                 $this->assertTrue( in_array( $enclosure_string, get_post_meta( $post_id, 'enclosure' ), true ) );
     448                $this->assertContains( $enclosure_string, get_post_meta( $post_id, 'enclosure' ) );
    449449
    450450                // Attempt to add a brand new enclosure via XML-RPC.
     
    456456                // Check that the new enclosure is in the enclosure meta.
    457457                $new_enclosure_string = "{$new_enclosure['url']}\n{$new_enclosure['length']}\n{$new_enclosure['type']}\n";
    458                 $this->assertTrue( in_array( $new_enclosure_string, get_post_meta( $post_id, 'enclosure' ), true ) );
     458                $this->assertContains( $new_enclosure_string, get_post_meta( $post_id, 'enclosure' ) );
    459459
    460460                // Check that the old enclosure is in the enclosure meta.
    461                 $this->assertTrue( in_array( $enclosure_string, get_post_meta( $post_id, 'enclosure' ), true ) );
     461                $this->assertContains( $enclosure_string, get_post_meta( $post_id, 'enclosure' ) );
    462462        }
    463463
Note: See TracChangeset for help on using the changeset viewer.