diff --git a/core/lib/Drupal/Core/Datetime/DateHelper.php b/core/lib/Drupal/Core/Datetime/DateHelper.php index ad49016c058a5e74163e46211d5bfc5a38d5431f..4cfc74640fae9892c5d4d10ca37eadecd135796f 100644 --- a/core/lib/Drupal/Core/Datetime/DateHelper.php +++ b/core/lib/Drupal/Core/Datetime/DateHelper.php @@ -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; diff --git a/core/tests/Drupal/Tests/Core/Datetime/DateHelperTest.php b/core/tests/Drupal/Tests/Core/Datetime/DateHelperTest.php new file mode 100644 index 0000000000000000000000000000000000000000..a88861ca5656378f861f7d5ae96c8cfbd0144f33 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Datetime/DateHelperTest.php @@ -0,0 +1,112 @@ +<?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; + } + +}