class DateHelperTest

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Datetime/DateHelperTest.php \Drupal\Tests\Core\Datetime\DateHelperTest
  2. 10 core/tests/Drupal/Tests/Core/Datetime/DateHelperTest.php \Drupal\Tests\Core\Datetime\DateHelperTest
  3. 11.x core/tests/Drupal/Tests/Core/Datetime/DateHelperTest.php \Drupal\Tests\Core\Datetime\DateHelperTest

@coversDefaultClass \Drupal\Core\Datetime\DateHelper @group Datetime

Hierarchy

Expanded class hierarchy of DateHelperTest

File

core/tests/Drupal/Tests/Core/Datetime/DateHelperTest.php, line 14

Namespace

Drupal\Tests\Core\Datetime
View source
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
     */
    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;
    }
    
    /**
     * @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);
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
DateHelperTest::$languageManager protected property The language manager.
DateHelperTest::providerTestWeekDaysOrdered public function
DateHelperTest::setUp protected function Overrides UnitTestCase::setUp
DateHelperTest::testDayOfWeek public function @covers ::dayOfWeek
DateHelperTest::testDayOfWeekName public function @covers ::dayOfWeekName
DateHelperTest::testDaysInMonth public function @covers ::daysInMonth
DateHelperTest::testDaysInYear public function @covers ::daysInYear
DateHelperTest::testWeekDaysOrdered public function @covers ::weekDaysOrdered
@dataProvider providerTestWeekDaysOrdered
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.