Commit de68e25f authored by alexpott's avatar alexpott

Issue #2606934 by tim.plunkett:...

Issue #2606934 by tim.plunkett: \Drupal\Core\Datetime\DateHelper::weekDaysOrdered() does not preserve keys when reordering
parent 3c1413a7
......@@ -254,14 +254,24 @@ public static function weekDaysAbbr1($required = FALSE) {
* An array of weekdays.
*
* @return array
* An array of weekdays reordered to match the first day of the week.
* An array of weekdays reordered to match the first day of the week. The
* keys will remain unchanged. For example, if the first day of the week is
* set to be Monday, the array keys will be [1, 2, 3, 4, 5, 6, 0].
*/
public static function weekDaysOrdered($weekdays) {
$first_day = \Drupal::config('system.date')->get('first_day');
if ($first_day > 0) {
for ($i = 1; $i <= $first_day; $i++) {
$last = array_shift($weekdays);
array_push($weekdays, $last);
// Reset the array to the first element.
reset($weekdays);
// Retrieve the first week day value.
$last = current($weekdays);
// Store the corresponding key.
$key = key($weekdays);
// Remove this week day from the beginning of the array.
unset($weekdays[$key]);
// Add this week day to the end of the array.
$weekdays[$key] = $last;
}
}
return $weekdays;
......
<?php
/**
* @file
* Contains \Drupal\Tests\Core\Datetime\DateHelperTest.
*/
namespace Drupal\Tests\Core\Datetime;
use Drupal\Core\Datetime\DateHelper;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Core\Datetime\DateHelper
* @group Datetime
*/
class DateHelperTest extends UnitTestCase {
/**
* @covers ::weekDaysOrdered
* @dataProvider providerTestWeekDaysOrdered
*/
public function testWeekDaysOrdered($first_day, $expected) {
$container = new ContainerBuilder();
$config = ['system.date' => ['first_day' => $first_day]];
$container->set('config.factory', $this->getConfigFactoryStub($config));
\Drupal::setContainer($container);
$weekdays = DateHelper::weekDaysUntranslated();
// self::assertSame() MUST be used here as it checks for array key order.
$this->assertSame($expected, DateHelper::weekDaysOrdered($weekdays));
}
public function providerTestWeekDaysOrdered() {
$data = [];
$data[] = [0, [
0 => 'Sunday',
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
]];
$data[] = [1, [
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
0 => 'Sunday',
]];
$data[] = [2, [
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
0 => 'Sunday',
1 => 'Monday',
]];
$data[] = [3, [
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
0 => 'Sunday',
1 => 'Monday',
2 => 'Tuesday',
]];
$data[] = [4, [
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
0 => 'Sunday',
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
]];
$data[] = [5, [
5 => 'Friday',
6 => 'Saturday',
0 => 'Sunday',
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
]];
$data[] = [6, [
6 => 'Saturday',
0 => 'Sunday',
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
]];
$data[] = [7, [
0 => 'Sunday',
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
]];
return $data;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment