wordpress-develop/tests/phpunit/tests/date/getTheModifiedDate.php
Sergey Biryukov 164b22cf6a 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
2020-09-02 00:35:36 +00:00

192 lines
5.2 KiB
PHP

<?php
/**
* @group date
* @group datetime
* @group post
*/
class Tests_Date_Get_The_Modified_Date extends WP_UnitTestCase {
/**
* Test get_the_modified_time with post_id parameter.
*
* @ticket 37059
*
* @since 4.6.0
*/
public function test_get_the_modified_date_with_post_id() {
$details = array(
'post_date' => '2016-01-21 15:34:36',
'post_date_gmt' => '2016-01-21 15:34:36',
);
$post_id = $this->factory->post->create( $details );
$format = 'Y-m-d';
$expected = '2016-01-21';
$actual = get_the_modified_date( $format, $post_id );
$this->assertSame( $expected, $actual );
}
/**
* Test get_the_modified_date
*
* @ticket 37059
*
* @since 4.6.0
*/
public function test_get_the_modified_date_default() {
$details = array(
'post_date' => '2016-01-21 15:34:36',
'post_date_gmt' => '2016-01-21 15:34:36',
);
$post_id = $this->factory->post->create( $details );
$post = get_post( $post_id );
$GLOBALS['post'] = $post;
$expected = '2016-01-21';
$format = 'Y-m-d';
$actual = get_the_modified_date( $format );
$this->assertSame( $expected, $actual );
}
/**
* Test get_the_modified_date failures are filtered
*
* @ticket 37059
*
* @since 4.6.0
*/
public function test_get_the_modified_date_failures_are_filtered() {
// Remove global post object.
$GLOBALS['post'] = null;
$expected = 'filtered modified date failure result';
add_filter( 'get_the_modified_date', array( $this, '_filter_get_the_modified_date_failure' ) );
$actual = get_the_modified_date();
$this->assertSame( $expected, $actual );
remove_filter( 'get_the_modified_date', array( $this, '_filter_get_the_modified_date_failure' ) );
}
public function _filter_get_the_modified_date_failure( $the_date ) {
$expected = false;
$actual = $the_date;
$this->assertSame( $expected, $actual );
if ( false === $the_date ) {
return 'filtered modified date failure result';
}
return $the_date;
}
/**
* @ticket 51184
*/
public function test_get_the_modified_date_returns_false_with_null_or_non_existing_post() {
$this->assertFalse( get_the_modified_date() );
$this->assertFalse( get_the_modified_date( 'F j, Y h:i:s' ) );
$this->assertFalse( get_the_modified_date( '', 9 ) );
$this->assertFalse( get_the_modified_date( 'F j, Y h:i:s', 9 ) );
}
/**
* @ticket 51184
*/
public function test_get_the_modified_date_returns_correct_time_with_empty_format() {
$post_id = self::factory()->post->create( array( 'post_date' => '2020-08-31 23:14:00' ) );
$this->assertSame( 'August 31, 2020', get_the_modified_date( '', $post_id ) );
$this->assertSame( 'August 31, 2020', get_the_modified_date( false, $post_id ) );
}
/**
* Test get_the_modified_time with post_id parameter.
*
* @ticket 37059
*
* @since 4.6.0
*/
public function test_get_the_modified_time_with_post_id() {
$details = array(
'post_date' => '2016-01-21 15:34:36',
'post_date_gmt' => '2016-01-21 15:34:36',
);
$post_id = $this->factory->post->create( $details );
$format = 'G';
$expected = 1453390476;
$actual = get_the_modified_time( $format, $post_id );
$this->assertSame( $expected, $actual );
}
/**
* Test get_the_modified_time
*
* @ticket 37059
*
* @since 4.6.0
*/
public function test_get_the_modified_time_default() {
$details = array(
'post_date' => '2016-01-21 15:34:36',
'post_date_gmt' => '2016-01-21 15:34:36',
);
$post_id = $this->factory->post->create( $details );
$post = get_post( $post_id );
$GLOBALS['post'] = $post;
$expected = 1453390476;
$format = 'G';
$actual = get_the_modified_time( $format );
$this->assertSame( $expected, $actual );
}
/**
* Test get_the_modified_time failures are filtered
*
* @ticket 37059
*
* @since 4.6.0
*/
public function test_get_the_modified_time_failures_are_filtered() {
// Remove global post object.
$GLOBALS['post'] = null;
$expected = 'filtered modified time failure result';
add_filter( 'get_the_modified_time', array( $this, '_filter_get_the_modified_time_failure' ) );
$actual = get_the_modified_time();
$this->assertSame( $expected, $actual );
remove_filter( 'get_the_modified_time', array( $this, '_filter_get_the_modified_time_failure' ) );
}
public function _filter_get_the_modified_time_failure( $the_time ) {
$expected = false;
$actual = $the_time;
$this->assertSame( $expected, $actual );
if ( false === $the_time ) {
return 'filtered modified time failure result';
}
return $the_time;
}
/**
* @ticket 51184
*/
public function test_get_the_modified_time_returns_false_with_null_or_non_existing_post() {
$this->assertFalse( get_the_modified_time() );
$this->assertFalse( get_the_modified_time( 'h:i:s' ) );
$this->assertFalse( get_the_modified_time( '', 9 ) );
$this->assertFalse( get_the_modified_time( 'h:i:s', 9 ) );
}
/**
* @ticket 51184
*/
public function test_get_the_modified_time_returns_correct_time_with_empty_format() {
$post_id = self::factory()->post->create( array( 'post_date' => '2020-08-31 23:14:00' ) );
$this->assertSame( '11:14 pm', get_the_modified_time( '', $post_id ) );
$this->assertSame( '11:14 pm', get_the_modified_time( false, $post_id ) );
}
}