class DateElementBaseTest

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Datetime/Element/DateElementBaseTest.php \Drupal\Tests\Core\Datetime\Element\DateElementBaseTest

Tests the DateElementBase class.

Attributes

#[Group('Datetime')]

Hierarchy

Expanded class hierarchy of DateElementBaseTest

File

core/tests/Drupal/Tests/Core/Datetime/Element/DateElementBaseTest.php, line 18

Namespace

Drupal\Tests\Core\Datetime\Element
View source
class DateElementBaseTest extends UnitTestCase {
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $language = $this->createStub(LanguageInterface::class);
    $language->method('getId')
      ->willReturn('en');
    $language_manager = $this->createStub(LanguageManagerInterface::class);
    $language_manager->method('getCurrentLanguage')
      ->willReturn($language);
    $container = new ContainerBuilder();
    $container->set('language_manager', $language_manager);
    \Drupal::setContainer($container);
  }
  
  /**
   * Tests datetimeRangeYears() with various year range strings.
   *
   * @param string $range
   *   The year range string to test.
   * @param int $expected_min
   *   The expected minimum year.
   * @param int $expected_max
   *   The expected maximum year.
   */
  public function testDatetimeRangeYears(string $range, int $expected_min, int $expected_max) : void {
    $method = new \ReflectionMethod(DateElementBase::class, 'datetimeRangeYears');
    /** @var array{string, string} $result */
    $result = $method->invoke(NULL, $range);
    [$min, $max] = $result;
    $this->assertSame($expected_min, (int) $min);
    $this->assertSame($expected_max, (int) $max);
  }
  
  /**
   * Data provider for testDatetimeRangeYears().
   */
  public static function providerTestDatetimeRangeYears() : array {
    $this_year = (int) date('Y');
    return [
      'standard 4-digit range' => [
        '1900:2050',
        1900,
        2050,
      ],
      'year less than 1000' => [
        '500:1500',
        500,
        1500,
      ],
      'year zero as min' => [
        '0:2050',
        0,
        2050,
      ],
      'both years less than 1000' => [
        '0:999',
        0,
        999,
      ],
      'single digit year' => [
        '1:9999',
        1,
        9999,
      ],
      'relative max' => [
        '1900:+0',
        1900,
        $this_year,
      ],
      'year zero with relative max' => [
        '0:+50',
        0,
        $this_year + 50,
      ],
      'both relative' => [
        '-5:+5',
        $this_year - 5,
        $this_year + 5,
      ],
      'both year zero' => [
        '0:0',
        0,
        0,
      ],
      'both this year (+0)' => [
        '+0:+0',
        $this_year,
        $this_year,
      ],
      'both this year (-0)' => [
        '-0:-0',
        $this_year,
        $this_year,
      ],
      'negative four digit year' => [
        '-1000:-0',
        $this_year - 1000,
        $this_year,
      ],
      'negative three digit year' => [
        '-100:-1',
        $this_year - 100,
        $this_year - 1,
      ],
      'negative two digit year' => [
        '-10:-1',
        $this_year - 10,
        $this_year - 1,
      ],
    ];
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
DateElementBaseTest::providerTestDatetimeRangeYears public static function Data provider for testDatetimeRangeYears().
DateElementBaseTest::setUp protected function Overrides UnitTestCase::setUp
DateElementBaseTest::testDatetimeRangeYears public function Tests datetimeRangeYears() with various year range strings.
DrupalTestCaseTrait::$root protected property The Drupal root directory.
DrupalTestCaseTrait::checkErrorHandlerOnTearDown public function Checks the test error handler after test execution. 1
DrupalTestCaseTrait::getDrupalRoot Deprecated protected static function Returns the Drupal root directory. 1
DrupalTestCaseTrait::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
DrupalTestCaseTrait::setUpRoot final protected function Ensure that the $root property is set initially.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
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::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

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