サポート » プラグイン » ユニットテストで日付指定で作った投稿の日付の取得

  • 以下のテストがある際に、get_the_date() で日付を取得すると、January 1, 1970が返ってきます。

    <?php
    
    class SampleTest extends WP_UnitTestCase {
    
    	public $the_post;
    
    	public function setUp() {
    
    		parent::setUp();
    
    		$post_date     = '2015-09-21 00:00:00';
    		$post_date_gmt = get_gmt_from_date( $post_date );
    
    		$this->the_post = $this->factory->post->create_and_get( array(
    			'post_title'    => 'Posted on 21th Sept, 2015',
    			'post_date'     => $post_date,
    			'post_date_gmt' => $post_date_gmt,
    		) );
    
    		update_option( 'date_format', 'F j, Y' );
    
    	}
    
    	public function test_this_test() {
    
    		// 通る
    		$this->assertEquals( 'Posted on 21th Sept, 2015', get_the_title( $this->the_post ) );
    
    	}
    
    	public function test_date_format_none() {
    
    		$this->assertEquals( 'September 21, 2015', get_the_date( '', $this->the_post ) );
    
    	}
    
    }
    $ phpunit
    Installing...
    Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
    Not running ajax tests. To execute these, use --group ajax.
    Not running ms-files tests. To execute these, use --group ms-files.
    Not running external-http tests. To execute these, use --group external-http.
    PHPUnit 4.3.1 by Sebastian Bergmann.
    
    Configuration read from /var/www/wordpress/wp-content/plugins/buddha-calendar-converter/phpunit.xml
    
    .F
    
    Time: 1.75 seconds, Memory: 15.25Mb
    
    There was 1 failure:
    
    1) SampleTest::test_date_format_none
    Failed asserting that two strings are equal.
    --- Expected
    +++ Actual
    @@ @@
    -'September 21, 2015'
    +'January 1, 1970'
    
    /var/www/wordpress/wp-content/plugins/buddha-calendar-converter/tests/test-sample.php:34
    
    FAILURES!
    Tests: 2, Assertions: 2, Failures: 1.
    • WP_Postオブジェクトをvar_dumpすると日付がきちんと入っている
    • post_date_gmtパラメータの有無は結果に関係がない
    • post_dateパラメータの有無も結果に関係がない
    • $this->go_to( ‘/?p=’ . $this->the_post->ID ); してから get_the_date(); しても同じ

    という状態です。

  • トピック「ユニットテストで日付指定で作った投稿の日付の取得」には新たに返信することはできません。