wordpress-develop/tests/phpunit/tests/general/wpTitle.php
Tonya Mork 06dc5e170f Template: Fix "undefined index: 00" when archive month query is empty in wp_title().
When `m` query_tag has a valid year, i.e. `?m=2021`, and there are posts for that year, `substr()` returns a `false` on PHP 5.6 and an empty string on PHP 7.0+. Passing either of those values to `$wp_locale->get_month()` results in a PHP notice on PHP 5.6 to PHP 7.4 and a PHP Warning on PHP 8.0+.

Why? The `$month` lookup table has zeroized keys from '01' to '12'. A empty value is passed to `zeroise()` returns `'00'` which is directly passed as a key in the month property. That key does not exist.

While `$wp_locale->get_month()` would benefit from guarding/validation, this fix ensures a falsey value is not passed as a month.

Tests are added including a test that fails with this fix not applied.

Follow-up to [801], [35294], [35624].

Props antpb, audrasjb, costdev, davidmosterd, drewapicture, herregroen, hellofromTonya, michelwppi, sergeybiryukov.
Fixes #31521.

git-svn-id: https://develop.svn.wordpress.org/trunk@52136 602fd350-edb4-49c9-b593-d223f7449a82
2021-11-11 16:27:44 +00:00

63 lines
1.4 KiB
PHP

<?php
/**
* @group general
* @group template
* @covers ::wp_title
*/
class Tests_General_WpTitle extends WP_UnitTestCase {
/**
* @ticket 31521
*
* @dataProvider data_wp_title_archive
*/
public function test_wp_title_archive( $query, $expected ) {
self::factory()->post->create(
array(
'post_status' => 'publish',
'post_title' => 'Test Post',
'post_type' => 'post',
'post_date' => '2021-11-01 18:52:17',
)
);
$this->go_to( '?m=' . $query );
$this->assertSame( $expected, wp_title( '&raquo;', false ) );
}
/**
* Data provider.
*
* @return array
*/
public function data_wp_title_archive() {
return array(
'year with posts' => array(
'query' => '2021',
'expected' => ' &raquo; 2021',
),
'year without posts' => array(
'query' => '1910',
'expected' => ' &raquo; Page not found',
),
'year and month with posts' => array(
'query' => '202111',
'expected' => ' &raquo; 2021 &raquo; November',
),
'year and month without posts' => array(
'query' => '202101',
'expected' => ' &raquo; Page not found',
),
'year, month, day with posts' => array(
'query' => '20211101',
'expected' => ' &raquo; 2021 &raquo; November &raquo; 1',
),
'year, month, day without posts' => array(
'query' => '20210101',
'expected' => ' &raquo; Page not found',
),
);
}
}