WordPress.org

Make WordPress Core

Changeset 50284


Ignore:
Timestamp:
02/10/2021 01:24:24 PM (9 months ago)
Author:
SergeyBiryukov
Message:

Tests: Use assertSame() in some newly introduced tests.

This ensures that not only the return values match the expected results, but also that their type is the same.

Going forward, stricter type checking by using assertSame() should generally be preferred to assertEquals() where appropriate, to make the tests more reliable.

Follow-up to [49904], [49925], [49992], [50012], [50013], [50065], [50075], [50131], [50150], [50157].

See #38266, #52482.

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

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/auth.php

    r50065 r50284  
    469469            'Application passwords should be allowed for API authentication'
    470470        );
    471         $this->assertEquals( $item['uuid'], rest_get_authenticated_app_password() );
     471        $this->assertSame( $item['uuid'], rest_get_authenticated_app_password() );
    472472    }
    473473
  • trunk/tests/phpunit/tests/functions.php

    r49551 r50284  
    282282            $this->assertSame( null, $callback_value );
    283283        } else {
    284             $this->assertEquals( $value->count(), unserialize( $serialized )->count() );
     284            $this->assertSame( $value->count(), unserialize( $serialized )->count() );
    285285        }
    286286    }
  • trunk/tests/phpunit/tests/https-detection.php

    r50075 r50284  
    6666        add_filter( 'pre_http_request', array( $this, 'mock_success_with_sslverify' ), 10, 2 );
    6767        wp_update_https_detection_errors();
    68         $this->assertEquals( array(), get_option( 'https_detection_errors' ) );
     68        $this->assertSame( array(), get_option( 'https_detection_errors' ) );
    6969
    7070        // If initial request fails and request without SSL verification succeeds,
     
    7373        add_filter( 'pre_http_request', array( $this, 'mock_success_without_sslverify' ), 10, 2 );
    7474        wp_update_https_detection_errors();
    75         $this->assertEquals(
     75        $this->assertSame(
    7676            array( 'ssl_verification_failed' => array( 'Bad SSL certificate.' ) ),
    7777            get_option( 'https_detection_errors' )
     
    8383        add_filter( 'pre_http_request', array( $this, 'mock_error_without_sslverify' ), 10, 2 );
    8484        wp_update_https_detection_errors();
    85         $this->assertEquals(
     85        $this->assertSame(
    8686            array( 'bad_ssl_certificate' => array( 'Bad SSL certificate.' ) ),
    8787            get_option( 'https_detection_errors' )
     
    9292        add_filter( 'pre_http_request', array( $this, 'mock_not_found' ), 10, 2 );
    9393        wp_update_https_detection_errors();
    94         $this->assertEquals(
     94        $this->assertSame(
    9595            array( 'bad_response_code' => array( 'Not Found' ) ),
    9696            get_option( 'https_detection_errors' )
     
    101101        add_filter( 'pre_http_request', array( $this, 'mock_bad_source' ), 10, 2 );
    102102        wp_update_https_detection_errors();
    103         $this->assertEquals(
     103        $this->assertSame(
    104104            array( 'bad_response_source' => array( 'It looks like the response did not come from this site.' ) ),
    105105            get_option( 'https_detection_errors' )
     
    107107
    108108        // Check that the requests are made to the correct URL.
    109         $this->assertEquals( 'https://example.com/', $this->last_request_url );
     109        $this->assertSame( 'https://example.com/', $this->last_request_url );
    110110    }
    111111
     
    122122        );
    123123        wp_update_https_detection_errors();
    124         $this->assertEquals( array(), get_option( 'https_detection_errors' ) );
     124        $this->assertSame( array(), get_option( 'https_detection_errors' ) );
    125125
    126126        // Override to enforce an error being detected.
     
    135135        );
    136136        wp_update_https_detection_errors();
    137         $this->assertEquals(
     137        $this->assertSame(
    138138            array( 'ssl_verification_failed' => array( 'Bad SSL certificate.' ) ),
    139139            get_option( 'https_detection_errors' )
     
    146146    public function test_wp_schedule_https_detection() {
    147147        wp_schedule_https_detection();
    148         $this->assertEquals( 'twicedaily', wp_get_schedule( 'wp_https_detection' ) );
     148        $this->assertSame( 'twicedaily', wp_get_schedule( 'wp_https_detection' ) );
    149149    }
    150150
     
    158158            'args' => array( 'sslverify' => true ),
    159159        );
    160         $this->assertEquals( $request, wp_cron_conditionally_prevent_sslverify( $request ) );
     160        $this->assertSame( $request, wp_cron_conditionally_prevent_sslverify( $request ) );
    161161
    162162        // If URL is using HTTPS, set 'sslverify' to false.
     
    167167        $expected                      = $request;
    168168        $expected['args']['sslverify'] = false;
    169         $this->assertEquals( $expected, wp_cron_conditionally_prevent_sslverify( $request ) );
     169        $this->assertSame( $expected, wp_cron_conditionally_prevent_sslverify( $request ) );
    170170    }
    171171
  • trunk/tests/phpunit/tests/https-migration.php

    r50131 r50284  
    6262        // Replaces URLs, including its encoded variant.
    6363        add_filter( 'wp_should_replace_insecure_home_url', '__return_true' );
    64         $this->assertEquals( $https_content, wp_replace_insecure_home_url( $http_content ) );
     64        $this->assertSame( $https_content, wp_replace_insecure_home_url( $http_content ) );
    6565
    6666        // Does not replace anything if determined as unnecessary.
    6767        add_filter( 'wp_should_replace_insecure_home_url', '__return_false' );
    68         $this->assertEquals( $http_content, wp_replace_insecure_home_url( $http_content ) );
     68        $this->assertSame( $http_content, wp_replace_insecure_home_url( $http_content ) );
    6969    }
    7070
     
    8787        // Update URLs to HTTPS (successfully).
    8888        $this->assertTrue( wp_update_urls_to_https() );
    89         $this->assertEquals( $https_url, get_option( 'home' ) );
    90         $this->assertEquals( $https_url, get_option( 'siteurl' ) );
     89        $this->assertSame( $https_url, get_option( 'home' ) );
     90        $this->assertSame( $https_url, get_option( 'siteurl' ) );
    9191
    9292        // Switch options back to use HTTP URLs, but now add filter to
     
    9999        // option. Therefore the change is expected to be reverted.
    100100        $this->assertFalse( wp_update_urls_to_https() );
    101         $this->assertEquals( $http_url, get_option( 'home' ) );
    102         $this->assertEquals( $http_url, get_option( 'siteurl' ) );
     101        $this->assertSame( $http_url, get_option( 'home' ) );
     102        $this->assertSame( $http_url, get_option( 'siteurl' ) );
    103103    }
    104104
     
    110110        update_option( 'fresh_site', '0' );
    111111        wp_update_https_migration_required( 'http://example.org', 'https://example.org' );
    112         $this->assertEquals( '1', get_option( 'https_migration_required' ) );
     112        $this->assertTrue( get_option( 'https_migration_required' ) );
    113113
    114114        // Changing another part than the scheme should delete/reset the flag because changing those parts (e.g. the
     
    120120        update_option( 'fresh_site', '1' );
    121121        wp_update_https_migration_required( 'http://example.org', 'https://example.org' );
    122         $this->assertEquals( '', get_option( 'https_migration_required' ) );
     122        $this->assertFalse( get_option( 'https_migration_required' ) );
    123123
    124124        // Changing (back) from HTTPS to HTTP should delete/reset the flag.
  • trunk/tests/phpunit/tests/post.php

    r50012 r50284  
    14121412        $post    = get_post( $post_id );
    14131413        $this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
    1414         $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
     1414        $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt );
    14151415
    14161416        $post_id = self::factory()->post->create(
     
    14221422        $post    = get_post( $post_id );
    14231423        $this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
    1424         $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
     1424        $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt );
    14251425
    14261426        // Empty post_date_gmt without floating status
     
    14511451        );
    14521452        $post    = get_post( $post_id );
    1453         $this->assertEquals( get_date_from_gmt( $post_date_gmt ), $post->post_date );
    1454         $this->assertEquals( $post_date_gmt, $post->post_date_gmt );
     1453        $this->assertSame( get_date_from_gmt( $post_date_gmt ), $post->post_date );
     1454        $this->assertSame( $post_date_gmt, $post->post_date_gmt );
    14551455
    14561456        // Invalid post_date_gmt
     
    14611461        );
    14621462        $post    = get_post( $post_id );
    1463         $this->assertEquals( '1970-01-01 00:00:00', $post->post_date );
    1464         $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
     1463        $this->assertSame( '1970-01-01 00:00:00', $post->post_date );
     1464        $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt );
    14651465    }
    14661466
     
    14811481        );
    14821482        $post    = get_post( $post_id );
    1483         $this->assertEquals( $post_date, $post->post_date );
    1484         $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
     1483        $this->assertSame( $post_date, $post->post_date );
     1484        $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt );
    14851485
    14861486        $post_id = self::factory()->post->create(
     
    14921492        );
    14931493        $post    = get_post( $post_id );
    1494         $this->assertEquals( $post_date, $post->post_date );
    1495         $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
     1494        $this->assertSame( $post_date, $post->post_date );
     1495        $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt );
    14961496
    14971497        // Empty post_date_gmt without floating status
     
    15031503        );
    15041504        $post    = get_post( $post_id );
    1505         $this->assertEquals( $post_date, $post->post_date );
    1506         $this->assertEquals( get_gmt_from_date( $post_date ), $post->post_date_gmt );
     1505        $this->assertSame( $post_date, $post->post_date );
     1506        $this->assertSame( get_gmt_from_date( $post_date ), $post->post_date_gmt );
    15071507
    15081508        $post_id = self::factory()->post->create(
     
    15141514        );
    15151515        $post    = get_post( $post_id );
    1516         $this->assertEquals( $post_date, $post->post_date );
    1517         $this->assertEquals( get_gmt_from_date( $post_date ), $post->post_date_gmt );
     1516        $this->assertSame( $post_date, $post->post_date );
     1517        $this->assertSame( get_gmt_from_date( $post_date ), $post->post_date_gmt );
    15181518
    15191519        // Valid post_date_gmt
     
    15251525        );
    15261526        $post    = get_post( $post_id );
    1527         $this->assertEquals( $post_date, $post->post_date );
    1528         $this->assertEquals( $post_date_gmt, $post->post_date_gmt );
     1527        $this->assertSame( $post_date, $post->post_date );
     1528        $this->assertSame( $post_date_gmt, $post->post_date_gmt );
    15291529
    15301530        // Invalid post_date_gmt
     
    15361536        );
    15371537        $post    = get_post( $post_id );
    1538         $this->assertEquals( $post_date, $post->post_date );
    1539         $this->assertEquals( '0000-00-00 00:00:00', $post->post_date_gmt );
     1538        $this->assertSame( $post_date, $post->post_date );
     1539        $this->assertSame( '0000-00-00 00:00:00', $post->post_date_gmt );
    15401540    }
    15411541
     
    15551555            )
    15561556        );
    1557         $this->assertEquals( 0, $post_id );
     1557        $this->assertSame( 0, $post_id );
    15581558
    15591559        $post_id = self::factory()->post->create(
     
    15641564            )
    15651565        );
    1566         $this->assertEquals( 0, $post_id );
     1566        $this->assertSame( 0, $post_id );
    15671567
    15681568        // Empty post_date_gmt without floating status
     
    15731573            )
    15741574        );
    1575         $this->assertEquals( 0, $post_id );
     1575        $this->assertSame( 0, $post_id );
    15761576
    15771577        $post_id = self::factory()->post->create(
     
    15821582            )
    15831583        );
    1584         $this->assertEquals( 0, $post_id );
     1584        $this->assertSame( 0, $post_id );
    15851585
    15861586        // Valid post_date_gmt
     
    15911591            )
    15921592        );
    1593         $this->assertEquals( 0, $post_id );
     1593        $this->assertSame( 0, $post_id );
    15941594
    15951595        // Invalid post_date_gmt
     
    16001600            )
    16011601        );
    1602         $this->assertEquals( 0, $post_id );
     1602        $this->assertSame( 0, $post_id );
    16031603    }
    16041604
     
    16151615
    16161616        $resolved_post_date = wp_resolve_post_date( '', $post_date_gmt );
    1617         $this->assertEquals( get_date_from_gmt( $post_date_gmt ), $resolved_post_date );
     1617        $this->assertSame( get_date_from_gmt( $post_date_gmt ), $resolved_post_date );
    16181618
    16191619        $resolved_post_date = wp_resolve_post_date( '', $invalid_date );
    1620         $this->assertEquals( '1970-01-01 00:00:00', $resolved_post_date );
     1620        $this->assertSame( '1970-01-01 00:00:00', $resolved_post_date );
    16211621
    16221622        $resolved_post_date = wp_resolve_post_date( $post_date );
    1623         $this->assertEquals( $post_date, $resolved_post_date );
     1623        $this->assertSame( $post_date, $resolved_post_date );
    16241624
    16251625        $resolved_post_date = wp_resolve_post_date( $post_date, $post_date_gmt );
    1626         $this->assertEquals( $post_date, $resolved_post_date );
     1626        $this->assertSame( $post_date, $resolved_post_date );
    16271627
    16281628        $resolved_post_date = wp_resolve_post_date( $post_date, $invalid_date );
    1629         $this->assertEquals( $post_date, $resolved_post_date );
     1629        $this->assertSame( $post_date, $resolved_post_date );
    16301630
    16311631        $resolved_post_date = wp_resolve_post_date( $invalid_date );
  • trunk/tests/phpunit/tests/post/nav-menu.php

    r50014 r50284  
    10381038        );
    10391039        $post         = get_post( $menu_item_id );
    1040         $this->assertEquals( get_date_from_gmt( $post_date_gmt ), $post->post_date );
     1040        $this->assertSame( get_date_from_gmt( $post_date_gmt ), $post->post_date );
    10411041
    10421042        $menu_item_id = wp_update_nav_menu_item(
     
    10521052        );
    10531053        $post         = get_post( $menu_item_id );
    1054         $this->assertEquals( '1970-01-01 00:00:00', $post->post_date );
     1054        $this->assertSame( '1970-01-01 00:00:00', $post->post_date );
    10551055
    10561056        $menu_item_id = wp_update_nav_menu_item(
     
    10661066        );
    10671067        $post         = get_post( $menu_item_id );
    1068         $this->assertEquals( $post_date, $post->post_date );
     1068        $this->assertSame( $post_date, $post->post_date );
    10691069
    10701070        $menu_item_id = wp_update_nav_menu_item(
     
    10811081        );
    10821082        $post         = get_post( $menu_item_id );
    1083         $this->assertEquals( $post_date, $post->post_date );
     1083        $this->assertSame( $post_date, $post->post_date );
    10841084
    10851085        $menu_item_id = wp_update_nav_menu_item(
     
    10961096        );
    10971097        $post         = get_post( $menu_item_id );
    1098         $this->assertEquals( $post_date, $post->post_date );
     1098        $this->assertSame( $post_date, $post->post_date );
    10991099
    11001100        $menu_item_id = wp_update_nav_menu_item(
  • trunk/tests/phpunit/tests/rest-api/rest-application-passwords-controller.php

    r50114 r50284  
    892892        $this->assertNotWPError( $response->as_error() );
    893893
    894         $this->assertEquals( $password['uuid'], $response->get_data()['uuid'] );
     894        $this->assertSame( $password['uuid'], $response->get_data()['uuid'] );
    895895    }
    896896
     
    902902        $response = rest_do_request( '/wp/v2/users/' . self::$admin . '/application-passwords/introspect' );
    903903
    904         $this->assertEquals( $password['uuid'], $response->get_data()['uuid'] );
     904        $this->assertSame( $password['uuid'], $response->get_data()['uuid'] );
    905905    }
    906906
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r50157 r50284  
    12001200
    12011201        $this->assertCount( 1, $data );
    1202         $this->assertEquals( $post_ids[0], $data[0]['id'] );
     1202        $this->assertSame( $post_ids[0], $data[0]['id'] );
    12031203    }
    12041204
  • trunk/tests/phpunit/tests/rest-api/rest-request.php

    r50150 r50284  
    495495        $this->assertArrayHasKey( 'params', $data );
    496496        $this->assertArrayHasKey( 'failparam', $data['params'] );
    497         $this->assertEquals( 'Invalid. Super Invalid. Broken.', $data['params']['failparam'] );
     497        $this->assertSame( 'Invalid. Super Invalid. Broken.', $data['params']['failparam'] );
    498498    }
    499499
     
    525525        $this->assertArrayHasKey( 'details', $data );
    526526        $this->assertArrayHasKey( 'failparam', $data['details'] );
    527         $this->assertEquals(
     527        $this->assertSame(
    528528            array(
    529529                'code'    => 'invalid',
     
    758758        $this->assertArrayHasKey( 'params', $data );
    759759        $this->assertArrayHasKey( 'failparam', $data['params'] );
    760         $this->assertEquals( 'Invalid. Super Invalid. Broken.', $data['params']['failparam'] );
     760        $this->assertSame( 'Invalid. Super Invalid. Broken.', $data['params']['failparam'] );
    761761    }
    762762
     
    788788        $this->assertArrayHasKey( 'details', $data );
    789789        $this->assertArrayHasKey( 'failparam', $data['details'] );
    790         $this->assertEquals(
     790        $this->assertSame(
    791791            array(
    792792                'code'    => 'invalid',
  • trunk/tests/phpunit/tests/rest-api/rest-server.php

    r50150 r50284  
    466466        $response = rest_convert_error_to_response( $error );
    467467        $this->assertSame( 400, $response->get_status() );
    468         $this->assertEquals( 'more_data', $response->get_data()['data'] );
    469         $this->assertEquals( array( array( 'status' => 400 ) ), $response->get_data()['additional_data'] );
     468        $this->assertSame( 'more_data', $response->get_data()['data'] );
     469        $this->assertSame( array( array( 'status' => 400 ) ), $response->get_data()['additional_data'] );
    470470    }
    471471
  • trunk/tests/phpunit/tests/rest-api/rest-themes-controller.php

    r49925 r50284  
    200200        $response = rest_get_server()->dispatch( $request );
    201201
    202         $this->assertEquals( 200, $response->get_status() );
     202        $this->assertSame( 200, $response->get_status() );
    203203        $data = $response->get_data();
    204204
     
    238238        $response = rest_get_server()->dispatch( $request );
    239239
    240         $this->assertEquals( 200, $response->get_status() );
     240        $this->assertSame( 200, $response->get_status() );
    241241        $data = $response->get_data();
    242242
     
    298298            $this->assertErrorResponse( $error_code, $response, 403 );
    299299        } else {
    300             $this->assertEquals( 200, $response->get_status() );
     300            $this->assertSame( 200, $response->get_status() );
    301301        }
    302302    }
     
    12161216        $response = rest_get_server()->dispatch( $request );
    12171217
    1218         $this->assertEquals( 200, $response->get_status() );
     1218        $this->assertSame( 200, $response->get_status() );
    12191219        $data         = $response->get_data();
    12201220        $links        = $response->get_links();
     
    12791279        $response = rest_get_server()->dispatch( $request );
    12801280
    1281         $this->assertEquals( 200, $response->get_status() );
     1281        $this->assertSame( 200, $response->get_status() );
    12821282    }
    12831283
  • trunk/tests/phpunit/tests/robots.php

    r50078 r50284  
    3030        add_filter( 'wp_robots', array( $this, 'add_noindex_directive' ) );
    3131        $output = get_echo( 'wp_robots' );
    32         $this->assertEquals( "<meta name='robots' content='noindex' />\n", $output );
     32        $this->assertSame( "<meta name='robots' content='noindex' />\n", $output );
    3333
    3434        // Do not render robots meta tag when there are only false-y directives.
Note: See TracChangeset for help on using the changeset viewer.