diff -u b/core/lib/Drupal/Core/Datetime/DateHelper.php b/core/lib/Drupal/Core/Datetime/DateHelper.php --- b/core/lib/Drupal/Core/Datetime/DateHelper.php +++ b/core/lib/Drupal/Core/Datetime/DateHelper.php @@ -2,6 +2,8 @@ namespace Drupal\Core\Datetime; +use Drupal\Core\TypedData\Type\StringInterface; + /** * Defines Gregorian Calendar date values. * @@ -453,10 +455,10 @@ * The number of days in the month. */ public static function daysInMonth($date = NULL) { - if (!$date instanceof DrupalDateTime) { + if ($date && !$date instanceof DrupalDateTime) { $date = new DrupalDateTime($date); } - if (!$date->hasErrors()) { + if ($date && !$date->hasErrors()) { return $date->format('t'); } return NULL; @@ -473,10 +475,10 @@ * The number of days in the year. */ public static function daysInYear($date = NULL) { - if (!$date instanceof DrupalDateTime) { - $date = new DrupalDateTime(); + if ($date && !$date instanceof DrupalDateTime) { + $date = new DrupalDateTime($date); } - if (!$date->hasErrors()) { + if ($date && !$date->hasErrors()) { if ($date->format('L')) { return 366; } @@ -498,10 +500,10 @@ * The number of the day in the week. */ public static function dayOfWeek($date = NULL) { - if (!$date instanceof DrupalDateTime) { + if ($date && !$date instanceof DrupalDateTime) { $date = new DrupalDateTime($date); } - if (!$date->hasErrors()) { + if ($date && !$date->hasErrors()) { return $date->format('w'); } return NULL; @@ -521,12 +523,15 @@ * The name of the day in the week for that date. */ public static function dayOfWeekName($date = NULL, $abbr = TRUE) { - if (!$date instanceof DrupalDateTime) { + if ($date && !$date instanceof DrupalDateTime) { $date = new DrupalDateTime($date); } - $dow = self::dayOfWeek($date); - $days = $abbr ? self::weekDaysAbbr() : self::weekDays(); - return $days[$dow]; + if ($date && !$date->hasErrors()) { + $dow = self::dayOfWeek($date); + $days = $abbr ? self::weekDaysAbbr() : self::weekDays(); + return $days[$dow]; + } + return NULL; } } only in patch2: unchanged: --- a/core/tests/Drupal/Tests/Core/Datetime/DateHelperTest.php +++ b/core/tests/Drupal/Tests/Core/Datetime/DateHelperTest.php @@ -4,6 +4,7 @@ use Drupal\Core\Datetime\DateHelper; use Drupal\Core\DependencyInjection\ContainerBuilder; +use Drupal\Core\Language\Language; use Drupal\Tests\UnitTestCase; /** @@ -12,6 +13,36 @@ */ class DateHelperTest extends UnitTestCase { + /** + * The language manager. + * + * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit\Framework\MockObject\MockObject + */ + protected $languageManager; + + /** + * {@inheritdoc} + */ + protected function setUp(): void { + parent::setUp(); + + $container = new ContainerBuilder(); + $config = ['system.date' => ['first_day' => 'Sunday']]; + $container->set('config.factory', $this->getConfigFactoryStub($config)); + + $this->languageManager = $this->createMock('\Drupal\Core\Language\LanguageManagerInterface'); + $language = new Language(['langcode' => 'en']); + $this->languageManager->expects($this->any()) + ->method('getDefaultLanguage') + ->will($this->returnValue($language)); + $this->languageManager->expects($this->any()) + ->method('getCurrentLanguage') + ->will($this->returnValue($language)); + $container->set('language_manager', $this->languageManager); + + \Drupal::setContainer($container); + } + /** * @covers ::weekDaysOrdered * @dataProvider providerTestWeekDaysOrdered @@ -128,4 +159,78 @@ public function providerTestWeekDaysOrdered() { return $data; } + /** + * @covers ::daysInMonth + */ + public function testDaysInMonth() { + // Pass nothing and expect to get NULL. + $this->assertNull(DateHelper::daysInMonth()); + + // December 31st 2022 is a Saturday. + $value = '2022-12-31 00:00:00'; + $dateString = DateHelper::daysInMonth($value); + $this->assertEquals('31', $dateString); + + // November 30th 2020 is a Monday. + $value = '2020-11-30 00:00:00'; + $dateString = DateHelper::daysInMonth($value); + $this->assertEquals('30', $dateString); + } + + /** + * @covers ::daysInYear + */ + public function testDaysInYear() { + // Pass nothing and expect to get NULL. + $this->assertNull(DateHelper::daysInYear()); + + // December 31st 2022 is a Saturday. + $value = '2022-12-31 00:00:00'; + $dateString = DateHelper::daysInYear($value); + $this->assertEquals('365', $dateString); + + // November 30th 2020 is a Monday. 2020 is a leap year. + $value = '2020-11-30 00:00:00'; + $dateString = DateHelper::daysInYear($value); + $this->assertEquals('366', $dateString); + } + + + /** + * @covers ::dayOfWeek + */ + public function testDayOfWeek() { + // Pass nothing and expect to get NULL. + $this->assertNull(DateHelper::dayOfWeek()); + + // December 31st 2022 is a Saturday. + $value = '2022-12-31 00:00:00'; + $dateString = DateHelper::dayOfWeek($value); + $this->assertEquals('6', $dateString); + + // November 30th 2020 is a Monday. + $value = '2020-11-30 00:00:00'; + $dateString = DateHelper::dayOfWeek($value); + $this->assertEquals('1', $dateString); + } + + + /** + * @covers ::dayOfWeekName + */ + public function testDayOfWeekName() { + // Pass nothing and expect to get NULL. + $this->assertNull(DateHelper::dayOfWeekName()); + + // December 31st 2022 is a Saturday. + $value = '2022-12-31 00:00:00'; + $dateString = DateHelper::dayOfWeekName($value); + $this->assertEquals('Sat', $dateString->getUntranslatedString()); + + // November 30th 2020 is a Monday. + $value = '2020-11-30 00:00:00'; + $dateString = DateHelper::dayOfWeekName($value); + $this->assertEquals('Mon', $dateString->getUntranslatedString()); + } + }