mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-01 15:50:09 +00:00
Tests: First pass at using assertSame() instead of assertEquals() in most of the unit 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. Props johnbillion, jrf, SergeyBiryukov. See #38266. git-svn-id: https://develop.svn.wordpress.org/trunk@48937 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -42,36 +42,36 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
* have a default priority of 10.
|
||||
*/
|
||||
function test_init_action_added() {
|
||||
$this->assertEquals( 10, has_action( 'init', 'rest_api_init' ) );
|
||||
$this->assertSame( 10, has_action( 'init', 'rest_api_init' ) );
|
||||
}
|
||||
|
||||
public function test_add_extra_api_taxonomy_arguments() {
|
||||
$taxonomy = get_taxonomy( 'category' );
|
||||
$this->assertTrue( $taxonomy->show_in_rest );
|
||||
$this->assertEquals( 'categories', $taxonomy->rest_base );
|
||||
$this->assertEquals( 'WP_REST_Terms_Controller', $taxonomy->rest_controller_class );
|
||||
$this->assertSame( 'categories', $taxonomy->rest_base );
|
||||
$this->assertSame( 'WP_REST_Terms_Controller', $taxonomy->rest_controller_class );
|
||||
|
||||
$taxonomy = get_taxonomy( 'post_tag' );
|
||||
$this->assertTrue( $taxonomy->show_in_rest );
|
||||
$this->assertEquals( 'tags', $taxonomy->rest_base );
|
||||
$this->assertEquals( 'WP_REST_Terms_Controller', $taxonomy->rest_controller_class );
|
||||
$this->assertSame( 'tags', $taxonomy->rest_base );
|
||||
$this->assertSame( 'WP_REST_Terms_Controller', $taxonomy->rest_controller_class );
|
||||
}
|
||||
|
||||
public function test_add_extra_api_post_type_arguments() {
|
||||
$post_type = get_post_type_object( 'post' );
|
||||
$this->assertTrue( $post_type->show_in_rest );
|
||||
$this->assertEquals( 'posts', $post_type->rest_base );
|
||||
$this->assertEquals( 'WP_REST_Posts_Controller', $post_type->rest_controller_class );
|
||||
$this->assertSame( 'posts', $post_type->rest_base );
|
||||
$this->assertSame( 'WP_REST_Posts_Controller', $post_type->rest_controller_class );
|
||||
|
||||
$post_type = get_post_type_object( 'page' );
|
||||
$this->assertTrue( $post_type->show_in_rest );
|
||||
$this->assertEquals( 'pages', $post_type->rest_base );
|
||||
$this->assertEquals( 'WP_REST_Posts_Controller', $post_type->rest_controller_class );
|
||||
$this->assertSame( 'pages', $post_type->rest_base );
|
||||
$this->assertSame( 'WP_REST_Posts_Controller', $post_type->rest_controller_class );
|
||||
|
||||
$post_type = get_post_type_object( 'attachment' );
|
||||
$this->assertTrue( $post_type->show_in_rest );
|
||||
$this->assertEquals( 'media', $post_type->rest_base );
|
||||
$this->assertEquals( 'WP_REST_Attachments_Controller', $post_type->rest_controller_class );
|
||||
$this->assertSame( 'media', $post_type->rest_base );
|
||||
$this->assertSame( 'WP_REST_Attachments_Controller', $post_type->rest_controller_class );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,7 +98,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
$endpoint = $endpoints['/test-ns/test'];
|
||||
$this->assertArrayNotHasKey( 'callback', $endpoint );
|
||||
$this->assertArrayHasKey( 'namespace', $endpoint );
|
||||
$this->assertEquals( 'test-ns', $endpoint['namespace'] );
|
||||
$this->assertSame( 'test-ns', $endpoint['namespace'] );
|
||||
|
||||
// Grab the filtered data.
|
||||
$filtered_endpoints = $GLOBALS['wp_rest_server']->get_routes();
|
||||
@@ -141,7 +141,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
$endpoint = $endpoints['/test-ns/test'];
|
||||
$this->assertArrayNotHasKey( 'callback', $endpoint );
|
||||
$this->assertArrayHasKey( 'namespace', $endpoint );
|
||||
$this->assertEquals( 'test-ns', $endpoint['namespace'] );
|
||||
$this->assertSame( 'test-ns', $endpoint['namespace'] );
|
||||
|
||||
$filtered_endpoints = $GLOBALS['wp_rest_server']->get_routes();
|
||||
$endpoint = $filtered_endpoints['/test-ns/test'];
|
||||
@@ -281,7 +281,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
|
||||
$routes = $GLOBALS['wp_rest_server']->get_routes();
|
||||
|
||||
$this->assertEquals( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) );
|
||||
$this->assertSame( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -300,7 +300,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
|
||||
$routes = $GLOBALS['wp_rest_server']->get_routes();
|
||||
|
||||
$this->assertEquals( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) );
|
||||
$this->assertSame( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -319,7 +319,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
|
||||
$routes = $GLOBALS['wp_rest_server']->get_routes();
|
||||
|
||||
$this->assertEquals(
|
||||
$this->assertSame(
|
||||
$routes['/test-ns/test'][0]['methods'],
|
||||
array(
|
||||
'GET' => true,
|
||||
@@ -344,7 +344,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
|
||||
$routes = $GLOBALS['wp_rest_server']->get_routes();
|
||||
|
||||
$this->assertEquals(
|
||||
$this->assertSame(
|
||||
$routes['/test-ns/test'][0]['methods'],
|
||||
array(
|
||||
'GET' => true,
|
||||
@@ -370,7 +370,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
$headers = $response->get_headers();
|
||||
$this->assertArrayHasKey( 'Allow', $headers );
|
||||
|
||||
$this->assertEquals( 'GET, POST', $headers['Allow'] );
|
||||
$this->assertSame( 'GET, POST', $headers['Allow'] );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -402,7 +402,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
$request = array();
|
||||
|
||||
$response = rest_filter_response_fields( $response, null, $request );
|
||||
$this->assertEquals( array( 'a' => true ), $response->get_data() );
|
||||
$this->assertSame( array( 'a' => true ), $response->get_data() );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -422,7 +422,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
$response = rest_filter_response_fields( $response, null, $request );
|
||||
$this->assertEquals( array( 'b' => 1 ), $response->get_data() );
|
||||
$this->assertSame( array( 'b' => 1 ), $response->get_data() );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -445,7 +445,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
$response = rest_filter_response_fields( $response, null, $request );
|
||||
$this->assertEquals(
|
||||
$this->assertSame(
|
||||
array(
|
||||
'b' => 1,
|
||||
'c' => 2,
|
||||
@@ -477,7 +477,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
$response = rest_filter_response_fields( $response, null, $request );
|
||||
$this->assertEquals(
|
||||
$this->assertSame(
|
||||
array(
|
||||
'b' => 1,
|
||||
'c' => 2,
|
||||
@@ -516,7 +516,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
$response = rest_filter_response_fields( $response, null, $request );
|
||||
$this->assertEquals(
|
||||
$this->assertSame(
|
||||
array(
|
||||
array(
|
||||
'b' => 1,
|
||||
@@ -562,7 +562,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
$response = rest_filter_response_fields( $response, null, $request );
|
||||
$this->assertEquals(
|
||||
$this->assertSame(
|
||||
array(
|
||||
'b' => array(
|
||||
'1' => 1,
|
||||
@@ -603,7 +603,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
$response = rest_filter_response_fields( $response, null, $request );
|
||||
$this->assertEquals(
|
||||
$this->assertSame(
|
||||
array(
|
||||
'field' => array(
|
||||
'a' => array(
|
||||
@@ -639,7 +639,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
$response = rest_filter_response_fields( $response, null, $request );
|
||||
$this->assertEquals(
|
||||
$this->assertSame(
|
||||
array(
|
||||
'meta' => array(
|
||||
'key1' => 1,
|
||||
@@ -671,7 +671,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
$response = rest_filter_response_fields( $response, null, $request );
|
||||
$this->assertEquals(
|
||||
$this->assertSame(
|
||||
array(
|
||||
'meta' => array(
|
||||
'key1' => 1,
|
||||
@@ -703,7 +703,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
$response = rest_filter_response_fields( $response, null, $request );
|
||||
$this->assertEquals(
|
||||
$this->assertSame(
|
||||
array(
|
||||
'meta' => array(
|
||||
'key1' => 1,
|
||||
@@ -745,15 +745,15 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
public function test_rest_url_generation() {
|
||||
// In pretty permalinks case, we expect a path of wp-json/ with no query.
|
||||
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
|
||||
$this->assertEquals( 'http://' . WP_TESTS_DOMAIN . '/wp-json/', get_rest_url() );
|
||||
$this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/wp-json/', get_rest_url() );
|
||||
|
||||
// In index permalinks case, we expect a path of index.php/wp-json/ with no query.
|
||||
$this->set_permalink_structure( '/index.php/%year%/%monthnum%/%day%/%postname%/' );
|
||||
$this->assertEquals( 'http://' . WP_TESTS_DOMAIN . '/index.php/wp-json/', get_rest_url() );
|
||||
$this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/index.php/wp-json/', get_rest_url() );
|
||||
|
||||
// In non-pretty case, we get a query string to invoke the rest router.
|
||||
$this->set_permalink_structure( '' );
|
||||
$this->assertEquals( 'http://' . WP_TESTS_DOMAIN . '/index.php?rest_route=/', get_rest_url() );
|
||||
$this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/index.php?rest_route=/', get_rest_url() );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -807,19 +807,19 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
// Passing no path should return a slash.
|
||||
get_rest_url();
|
||||
$args = $filter->get_args();
|
||||
$this->assertEquals( '/', $args[0][1] );
|
||||
$this->assertSame( '/', $args[0][1] );
|
||||
$filter->reset();
|
||||
|
||||
// Paths without a prepended slash should have one added.
|
||||
get_rest_url( null, 'wp/media/' );
|
||||
$args = $filter->get_args();
|
||||
$this->assertEquals( '/wp/media/', $args[0][1] );
|
||||
$this->assertSame( '/wp/media/', $args[0][1] );
|
||||
$filter->reset();
|
||||
|
||||
// Do not modify paths with a prepended slash.
|
||||
get_rest_url( null, '/wp/media/' );
|
||||
$args = $filter->get_args();
|
||||
$this->assertEquals( '/wp/media/', $args[0][1] );
|
||||
$this->assertSame( '/wp/media/', $args[0][1] );
|
||||
|
||||
unset( $filter );
|
||||
}
|
||||
@@ -846,7 +846,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
* @dataProvider jsonp_callback_provider
|
||||
*/
|
||||
public function test_jsonp_callback_check( $callback, $valid ) {
|
||||
$this->assertEquals( $valid, wp_check_jsonp_callback( $callback ) );
|
||||
$this->assertSame( $valid, wp_check_jsonp_callback( $callback ) );
|
||||
}
|
||||
|
||||
public function rest_date_provider() {
|
||||
@@ -910,7 +910,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
* @dataProvider rest_date_force_utc_provider
|
||||
*/
|
||||
public function test_rest_parse_date_force_utc( $string, $value ) {
|
||||
$this->assertEquals( $value, rest_parse_date( $string, true ) );
|
||||
$this->assertSame( $value, rest_parse_date( $string, true ) );
|
||||
}
|
||||
|
||||
public function filter_wp_rest_server_class( $class_name ) {
|
||||
@@ -932,7 +932,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
$routes = $GLOBALS['wp_rest_server']->get_routes();
|
||||
$this->assertEquals( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) );
|
||||
$this->assertSame( $routes['/test-ns/test'][0]['methods'], array( 'GET' => true ) );
|
||||
}
|
||||
|
||||
function test_rest_preload_api_request_with_method() {
|
||||
@@ -962,15 +962,15 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
function test_rest_ensure_request_accepts_path_string() {
|
||||
$request = rest_ensure_request( '/wp/v2/posts' );
|
||||
$this->assertInstanceOf( 'WP_REST_Request', $request );
|
||||
$this->assertEquals( '/wp/v2/posts', $request->get_route() );
|
||||
$this->assertEquals( 'GET', $request->get_method() );
|
||||
$this->assertSame( '/wp/v2/posts', $request->get_route() );
|
||||
$this->assertSame( 'GET', $request->get_method() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider _dp_rest_parse_embed_param
|
||||
*/
|
||||
public function test_rest_parse_embed_param( $expected, $embed ) {
|
||||
$this->assertEquals( $expected, rest_parse_embed_param( $embed ) );
|
||||
$this->assertSame( $expected, rest_parse_embed_param( $embed ) );
|
||||
}
|
||||
|
||||
public function _dp_rest_parse_embed_param() {
|
||||
@@ -999,7 +999,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
* @dataProvider _dp_rest_filter_response_by_context
|
||||
*/
|
||||
public function test_rest_filter_response_by_context( $schema, $data, $expected ) {
|
||||
$this->assertEquals( $expected, rest_filter_response_by_context( $data, $schema, 'view' ) );
|
||||
$this->assertSame( $expected, rest_filter_response_by_context( $data, $schema, 'view' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1552,7 +1552,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
* @ticket 49116
|
||||
*/
|
||||
public function test_rest_get_route_for_post_non_post() {
|
||||
$this->assertEquals( '', rest_get_route_for_post( 'garbage' ) );
|
||||
$this->assertSame( '', rest_get_route_for_post( 'garbage' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1563,7 +1563,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
$post = self::factory()->post->create_and_get( array( 'post_type' => 'invalid' ) );
|
||||
unregister_post_type( 'invalid' );
|
||||
|
||||
$this->assertEquals( '', rest_get_route_for_post( $post ) );
|
||||
$this->assertSame( '', rest_get_route_for_post( $post ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1571,7 +1571,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
*/
|
||||
public function test_rest_get_route_for_post_non_rest() {
|
||||
$post = self::factory()->post->create_and_get( array( 'post_type' => 'custom_css' ) );
|
||||
$this->assertEquals( '', rest_get_route_for_post( $post ) );
|
||||
$this->assertSame( '', rest_get_route_for_post( $post ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1579,7 +1579,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
*/
|
||||
public function test_rest_get_route_for_post_custom_controller() {
|
||||
$post = self::factory()->post->create_and_get( array( 'post_type' => 'wp_block' ) );
|
||||
$this->assertEquals( '', rest_get_route_for_post( $post ) );
|
||||
$this->assertSame( '', rest_get_route_for_post( $post ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1587,7 +1587,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
*/
|
||||
public function test_rest_get_route_for_post() {
|
||||
$post = self::factory()->post->create_and_get();
|
||||
$this->assertEquals( '/wp/v2/posts/' . $post->ID, rest_get_route_for_post( $post ) );
|
||||
$this->assertSame( '/wp/v2/posts/' . $post->ID, rest_get_route_for_post( $post ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1595,7 +1595,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
*/
|
||||
public function test_rest_get_route_for_media() {
|
||||
$post = self::factory()->attachment->create_and_get();
|
||||
$this->assertEquals( '/wp/v2/media/' . $post->ID, rest_get_route_for_post( $post ) );
|
||||
$this->assertSame( '/wp/v2/media/' . $post->ID, rest_get_route_for_post( $post ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1603,14 +1603,14 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
*/
|
||||
public function test_rest_get_route_for_post_id() {
|
||||
$post = self::factory()->post->create_and_get();
|
||||
$this->assertEquals( '/wp/v2/posts/' . $post->ID, rest_get_route_for_post( $post->ID ) );
|
||||
$this->assertSame( '/wp/v2/posts/' . $post->ID, rest_get_route_for_post( $post->ID ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 49116
|
||||
*/
|
||||
public function test_rest_get_route_for_term_non_term() {
|
||||
$this->assertEquals( '', rest_get_route_for_term( 'garbage' ) );
|
||||
$this->assertSame( '', rest_get_route_for_term( 'garbage' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1621,7 +1621,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
$term = self::factory()->term->create_and_get( array( 'taxonomy' => 'invalid' ) );
|
||||
unregister_taxonomy( 'invalid' );
|
||||
|
||||
$this->assertEquals( '', rest_get_route_for_term( $term ) );
|
||||
$this->assertSame( '', rest_get_route_for_term( $term ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1629,7 +1629,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
*/
|
||||
public function test_rest_get_route_for_term_non_rest() {
|
||||
$term = self::factory()->term->create_and_get( array( 'taxonomy' => 'post_format' ) );
|
||||
$this->assertEquals( '', rest_get_route_for_term( $term ) );
|
||||
$this->assertSame( '', rest_get_route_for_term( $term ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1637,7 +1637,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
*/
|
||||
public function test_rest_get_route_for_term() {
|
||||
$term = self::factory()->term->create_and_get();
|
||||
$this->assertEquals( '/wp/v2/tags/' . $term->term_id, rest_get_route_for_term( $term ) );
|
||||
$this->assertSame( '/wp/v2/tags/' . $term->term_id, rest_get_route_for_term( $term ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1645,7 +1645,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
*/
|
||||
public function test_rest_get_route_for_category() {
|
||||
$term = self::factory()->category->create_and_get();
|
||||
$this->assertEquals( '/wp/v2/categories/' . $term->term_id, rest_get_route_for_term( $term ) );
|
||||
$this->assertSame( '/wp/v2/categories/' . $term->term_id, rest_get_route_for_term( $term ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1653,7 +1653,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
*/
|
||||
public function test_rest_get_route_for_term_id() {
|
||||
$term = self::factory()->term->create_and_get();
|
||||
$this->assertEquals( '/wp/v2/tags/' . $term->term_id, rest_get_route_for_term( $term->term_id ) );
|
||||
$this->assertSame( '/wp/v2/tags/' . $term->term_id, rest_get_route_for_term( $term->term_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1729,7 +1729,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
*/
|
||||
public function test_rest_sanitize_object( $expected, $value ) {
|
||||
$sanitized = rest_sanitize_object( $value );
|
||||
$this->assertEquals( $expected, $sanitized );
|
||||
$this->assertSame( $expected, $sanitized );
|
||||
}
|
||||
|
||||
public function _dp_rest_sanitize_object() {
|
||||
@@ -1858,7 +1858,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
*/
|
||||
public function test_rest_sanitize_array( $expected, $value ) {
|
||||
$sanitized = rest_sanitize_array( $value );
|
||||
$this->assertEquals( $expected, $sanitized );
|
||||
$this->assertSame( $expected, $sanitized );
|
||||
}
|
||||
|
||||
public function _dp_rest_sanitize_array() {
|
||||
@@ -1991,7 +1991,7 @@ class Tests_REST_API extends WP_UnitTestCase {
|
||||
* @param array $types The list of available types.
|
||||
*/
|
||||
public function test_get_best_type_for_value( $expected, $value, $types ) {
|
||||
$this->assertEquals( $expected, rest_get_best_type_for_value( $value, $types ) );
|
||||
$this->assertSame( $expected, rest_get_best_type_for_value( $value, $types ) );
|
||||
}
|
||||
|
||||
public function _dp_get_best_type_for_value() {
|
||||
|
||||
Reference in New Issue
Block a user