protected function TimezoneTest::assertTimesUnderstoodCorrectly in Drupal 10
Same name and namespace in other branches
- 8 core/tests/Drupal/KernelTests/Core/Datetime/Element/TimezoneTest.php \Drupal\KernelTests\Core\Datetime\Element\TimezoneTest::assertTimesUnderstoodCorrectly()
- 9 core/tests/Drupal/KernelTests/Core/Datetime/Element/TimezoneTest.php \Drupal\KernelTests\Core\Datetime\Element\TimezoneTest::assertTimesUnderstoodCorrectly()
Asserts that elements interpret dates using the expected time zones.
@internal
Parameters
string $elementType: The element type to test.
array $inputs: The names of the default input elements used by this element type.
Throws
\Exception
File
- core/
tests/ Drupal/ KernelTests/ Core/ Datetime/ Element/ TimezoneTest.php, line 258
Class
- TimezoneTest
- Tests the timezone handling of datetime and datelist element types.
Namespace
Drupal\KernelTests\Core\Datetime\ElementCode
protected function assertTimesUnderstoodCorrectly(string $elementType, array $inputs) : void {
$this->elementType = $elementType;
// Simulate the form being saved, with the user adding the date for any
// initially empty elements, but not changing other elements.
$form_state = new FormState();
$form_builder = $this->container
->get('form_builder');
$form = $this
->setupForm($form_state, $form_builder);
foreach ($form as $elementName => $element) {
if (isset($element['#type']) && $element['#type'] === $this->elementType && $element['#default_value'] === '') {
$newValues = [];
// Build an array of new values for the initially empty elements,
// depending on the inputs required by the element type, and using
// the timezone that will be expected for that test element.
foreach ($inputs as $input) {
$newValues[$input] = $this->formattedDates[$input][$element['#test_expect_timezone']];
}
$form_state
->setValue([
$elementName,
], $newValues);
}
}
$form_builder
->submitForm($this, $form_state);
// Examine the output of each test element.
$utc = new \DateTimeZone('UTC');
$expectedDateUTC = clone $this->date;
$expectedDateUTC
->setTimezone($utc)
->format('Y-m-d H:i:s');
$wrongDates = [];
$wrongTimezones = [];
$rightDates = 0;
foreach ($form_state
->getCompleteForm() as $elementName => $element) {
if (isset($element['#type']) && $element['#type'] === $this->elementType) {
$actualDate = $form_state
->getValue($elementName);
$actualTimezone = array_search($actualDate
->getTimezone()
->getName(), $this->timezones);
$actualDateUTC = $actualDate
->setTimezone($utc)
->format('Y-m-d H:i:s');
// Check that $this->date has not anywhere been accidentally changed
// from its default timezone, invalidating the test logic.
$this
->assertEquals(date_default_timezone_get(), $this->date
->getTimezone()
->getName(), "Test date still set to user timezone.");
// Build a list of cases where the result is not as expected.
// Check the time has been understood correctly.
if ($actualDate != $this->date) {
$wrongDates[$element['#title']] = $actualDateUTC;
}
else {
// Explicitly counting test passes prevents the test from seeming to
// pass just because the whole loop is being skipped.
$rightDates++;
}
// Check the correct timezone is set on the value object.
if ($element['#test_expect_timezone'] !== $actualTimezone) {
$wrongTimezones[$element['#title']] = [
$element['#test_expect_timezone'],
$actualTimezone,
];
}
}
}
$message = "On all elements the time should be understood correctly as {$expectedDateUTC}: \n" . print_r($wrongDates, TRUE);
$this
->assertEquals($this->testConditions, $rightDates, $message);
$message = "On all elements the correct timezone should be set on the value object: (expected, actual) \n" . print_r($wrongTimezones, TRUE);
$this
->assertCount(0, $wrongTimezones, $message);
}