Skip to content
Snippets Groups Projects
Verified Commit d8436d19 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3281557 by smustgrave, ipo4ka704, acbramley, ravi.shankar, benjifisher,...

Issue #3281557 by smustgrave, ipo4ka704, acbramley, ravi.shankar, benjifisher, Sardis, ameymudras, adeshsharma, Rishabh Vishwakarma, rollins, TanujJain-TJ, larowlan, catch, igorbiki: DateTime::__construct(): Passing null to parameter #1 ($datetime) of type string is deprecated

(cherry picked from commit 2ce27f62)
parent 10441d96
Branches
Tags
5 merge requests!8506Draft: Issue #3456536 by ibrahim tameme,!5646Issue #3350972 by nod_: [random test failure]...,!5600Issue #3350972 by nod_: [random test failure]...,!5343Issue #3305066 by quietone, Rename RedirectLeadingSlashesSubscriber,!3603#ISSUE 3346218 Add a different message on edit comment
......@@ -450,9 +450,10 @@ public static function ampm($required = FALSE) {
* Defaults to NULL, which means to use the current date.
*
* @return int
* The number of days in the month.
* The number of days in the month, or null if the $date has errors.
*/
public static function daysInMonth($date = NULL) {
$date = $date ?? 'now';
if (!$date instanceof DrupalDateTime) {
$date = new DrupalDateTime($date);
}
......@@ -469,10 +470,11 @@ public static function daysInMonth($date = NULL) {
* (optional) A DrupalDateTime object or a date string.
* Defaults to NULL, which means to use the current date.
*
* @return int
* The number of days in the year.
* @return int|null
* The number of days in the year, or null if the $date has errors.
*/
public static function daysInYear($date = NULL) {
$date = $date ?? 'now';
if (!$date instanceof DrupalDateTime) {
$date = new DrupalDateTime($date);
}
......@@ -494,10 +496,11 @@ public static function daysInYear($date = NULL) {
* (optional) A DrupalDateTime object or a date string.
* Defaults to NULL, which means use the current date.
*
* @return int
* The number of the day in the week.
* @return int|null
* The number of the day in the week, or null if the $date has errors.
*/
public static function dayOfWeek($date = NULL) {
$date = $date ?? 'now';
if (!$date instanceof DrupalDateTime) {
$date = new DrupalDateTime($date);
}
......@@ -517,16 +520,21 @@ public static function dayOfWeek($date = NULL) {
* (optional) Whether to return the abbreviated name for that day.
* Defaults to TRUE.
*
* @return string
* The name of the day in the week for that date.
* @return string|null
* The name of the day in the week for that date, or null if the $date has
* errors.
*/
public static function dayOfWeekName($date = NULL, $abbr = TRUE) {
$date = $date ?? 'now';
if (!$date instanceof DrupalDateTime) {
$date = new DrupalDateTime($date);
}
$dow = self::dayOfWeek($date);
$days = $abbr ? self::weekDaysAbbr() : self::weekDays();
return $days[$dow];
if (!$date->hasErrors()) {
$dow = self::dayOfWeek($date);
$days = $abbr ? self::weekDaysAbbr() : self::weekDays();
return $days[$dow]->getUntranslatedString();
}
return NULL;
}
}
......@@ -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,103 @@ public function providerTestWeekDaysOrdered() {
return $data;
}
/**
* @covers ::daysInMonth
*/
public function testDaysInMonth() {
// @todo Consider deprecating passing NULL in
// https://www.drupal.org/project/drupal/issues/3299788
// Passing NULL, FALSE, or an empty string should default to now. Just
// check these are NOT null to avoid copying the implementation here.
$this->assertNotNull(DateHelper::daysInMonth());
$this->assertNotNull(DateHelper::daysInMonth(FALSE));
$this->assertNotNull(DateHelper::daysInMonth(''));
// Pass nothing and expect to get NULL.
$this->assertNull(DateHelper::daysInMonth(0));
$this->assertNull(DateHelper::daysInMonth('0'));
$value = '2022-12-31 00:00:00';
$dateString = DateHelper::daysInMonth($value);
$this->assertEquals('31', $dateString);
$value = '2020-11-30 00:00:00';
$dateString = DateHelper::daysInMonth($value);
$this->assertEquals('30', $dateString);
}
/**
* @covers ::daysInYear
*/
public function testDaysInYear() {
// Passing NULL, FALSE, or an empty string should default to now. Just
// check these are NOT null to avoid copying the implementation here.
$this->assertNotNull(DateHelper::daysInYear());
$this->assertNotNull(DateHelper::daysInYear(FALSE));
$this->assertNotNull(DateHelper::daysInYear(''));
// Pass nothing and expect to get NULL.
$this->assertNull(DateHelper::daysInYear(0));
$this->assertNull(DateHelper::daysInYear('0'));
$value = '2022-12-31 00:00:00';
$dateString = DateHelper::daysInYear($value);
$this->assertEquals('365', $dateString);
// 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() {
// Passing NULL, FALSE, or an empty string should default to now. Just
// check these are NOT null to avoid copying the implementation here.
$this->assertNotNull(DateHelper::dayOfWeek());
$this->assertNotNull(DateHelper::dayOfWeek(FALSE));
$this->assertNotNull(DateHelper::dayOfWeek(''));
// Pass nothing and expect to get NULL.
$this->assertNull(DateHelper::dayOfWeek(0));
$this->assertNull(DateHelper::dayOfWeek('0'));
// 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() {
// Passing NULL, FALSE, or an empty string should default to now. Just
// check these are NOT null to avoid copying the implementation here.
$this->assertNotNull(DateHelper::dayOfWeekName());
$this->assertNotNull(DateHelper::dayOfWeekName(FALSE));
$this->assertNotNull(DateHelper::dayOfWeekName(''));
// Pass nothing and expect to get NULL.
$this->assertNull(DateHelper::dayOfWeekName(0));
$this->assertNull(DateHelper::dayOfWeekName('0'));
// December 31st 2022 is a Saturday.
$value = '2022-12-31 00:00:00';
$dateString = DateHelper::dayOfWeekName($value);
$this->assertEquals('Sat', $dateString);
// November 30th 2020 is a Monday.
$value = '2020-11-30 00:00:00';
$dateString = DateHelper::dayOfWeekName($value);
$this->assertEquals('Mon', $dateString);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment