View source
<?php
namespace Drupal\KernelTests\Core\Datetime;
use Drupal\Component\Utility\Variable;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\KernelTests\KernelTestBase;
class DatetimeElementFormTest extends KernelTestBase implements FormInterface, TrustedCallbackInterface {
protected $dateCallbackExecuted = FALSE;
protected $timeCallbackExecuted = FALSE;
protected static $modules = [
'datetime',
'system',
];
protected function setUp() : void {
parent::setUp();
}
public function getFormId() {
return 'test_datetime_element';
}
public function datetimeDateCallbackTrusted() {
$this->dateCallbackExecuted = TRUE;
}
public function datetimeDateCallback() {
$this->dateCallbackExecuted = TRUE;
}
public function datetimeTimeCallbackTrusted() {
$this->timeCallbackExecuted = TRUE;
}
public function datetimeTimeCallback() {
$this->timeCallbackExecuted = TRUE;
}
public function buildForm(array $form, FormStateInterface $form_state, string $date_callback = 'datetimeDateCallbackTrusted', string $time_callback = 'datetimeTimeCallbackTrusted') {
$form['datetime_element'] = [
'#title' => 'datelist test',
'#type' => 'datetime',
'#default_value' => new DrupalDateTime('2000-01-01 00:00:00'),
'#date_date_format' => 'Y-m-d',
'#date_time_format' => 'H:i:s',
'#date_date_element' => 'HTML Date',
'#date_time_element' => 'HTML Time',
'#date_increment' => 1,
'#date_date_callbacks' => [
[
$this,
$date_callback,
],
],
'#date_time_callbacks' => [
[
$this,
$time_callback,
],
],
];
$form['simple_datetime_element'] = [
'#type' => 'datetime',
'#date_date_format' => 'Y-m-d',
'#date_time_format' => 'H:i:s',
'#date_date_element' => 'HTML Date',
'#date_time_element' => 'HTML Time',
];
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Submit'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function testDatetimeElement() {
$form = \Drupal::formBuilder()
->getForm($this);
$this
->render($form);
$this
->assertTrue($this->dateCallbackExecuted);
$this
->assertTrue($this->timeCallbackExecuted);
}
public function testDatetimeElementUntrustedCallbacks(string $date_callback = 'datetimeDateCallbackTrusted', string $time_callback = 'datetimeTimeCallbackTrusted', string $expected_deprecation = NULL) : void {
$form = \Drupal::formBuilder()
->getForm($this, $date_callback, $time_callback);
if ($expected_deprecation) {
$this
->expectDeprecation($expected_deprecation);
}
$this
->render($form);
$this
->assertTrue($this->dateCallbackExecuted);
$this
->assertTrue($this->timeCallbackExecuted);
}
public function providerUntrusted() : array {
return [
'untrusted date' => [
'datetimeDateCallback',
'datetimeTimeCallbackTrusted',
sprintf('DateTime element #date_date_callbacks callbacks must be methods of a class that implements \\Drupal\\Core\\Security\\TrustedCallbackInterface or be an anonymous function. The callback was %s. Support for this callback implementation is deprecated in drupal:9.3.0 and will be removed in drupal:10.0.0. See https://www.drupal.org/node/3217966', Variable::callableToString([
$this,
'datetimeDateCallback',
])),
],
'untrusted time' => [
'datetimeDateCallbackTrusted',
'datetimeTimeCallback',
sprintf('DateTime element #date_time_callbacks callbacks must be methods of a class that implements \\Drupal\\Core\\Security\\TrustedCallbackInterface or be an anonymous function. The callback was %s. Support for this callback implementation is deprecated in drupal:9.3.0 and will be removed in drupal:10.0.0. See https://www.drupal.org/node/3217966', Variable::callableToString([
$this,
'datetimeTimeCallback',
])),
],
];
}
public function testTimezoneHandling() {
$form = \Drupal::formBuilder()
->getForm($this);
$this
->render($form);
$this
->assertEquals('Australia/Sydney', $form['datetime_element']['#date_timezone']);
date_default_timezone_set('UTC');
$form = \Drupal::formBuilder()
->getForm($this);
$this
->render($form);
$this
->assertEquals('UTC', $form['datetime_element']['#date_timezone']);
}
public static function trustedCallbacks() {
return [
'datetimeDateCallbackTrusted',
'datetimeTimeCallbackTrusted',
];
}
}