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\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\Security\UntrustedCallbackException;
use Drupal\KernelTests\KernelTestBase;
class DatetimeElementFormTest extends KernelTestBase implements FormInterface, TrustedCallbackInterface {
protected static $modules = [
'datetime',
'system',
];
protected function setUp() : void {
parent::setUp();
}
public function getFormId() {
return 'test_datetime_element';
}
public function datetimeDateCallbackTrusted(array &$element, FormStateInterface $form_state, DrupalDateTime $date = NULL) {
$element['datetimeDateCallbackExecuted'] = [
'#value' => TRUE,
];
$form_state
->set('datetimeDateCallbackExecuted', TRUE);
}
public function datetimeDateCallback(array &$element, FormStateInterface $form_state, DrupalDateTime $date = NULL) {
$element['datetimeDateCallbackExecuted'] = [
'#value' => TRUE,
];
$form_state
->set('datetimeDateCallbackExecuted', TRUE);
}
public function datetimeTimeCallbackTrusted(array &$element, FormStateInterface $form_state, DrupalDateTime $date = NULL) {
$element['timeCallbackExecuted'] = [
'#value' => TRUE,
];
$form_state
->set('timeCallbackExecuted', TRUE);
}
public function datetimeTimeCallback(array &$element, FormStateInterface $form_state, DrupalDateTime $date = NULL) {
$element['timeCallbackExecuted'] = [
'#value' => TRUE,
];
$form_state
->set('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_state = new FormState();
$form = \Drupal::formBuilder()
->buildForm($this, $form_state);
$this
->render($form);
$this
->assertTrue($form['datetime_element']['datetimeDateCallbackExecuted']['#value']);
$this
->assertTrue($form['datetime_element']['timeCallbackExecuted']['#value']);
$this
->assertTrue($form_state
->get('datetimeDateCallbackExecuted'));
$this
->assertTrue($form_state
->get('timeCallbackExecuted'));
}
public function testDatetimeElementUntrustedCallbacks(string $date_callback = 'datetimeDateCallbackTrusted', string $time_callback = 'datetimeTimeCallbackTrusted', string $expected_exception = NULL) : void {
if ($expected_exception) {
$this
->expectException(UntrustedCallbackException::class);
$this
->expectExceptionMessage($expected_exception);
}
$form = \Drupal::formBuilder()
->getForm($this, $date_callback, $time_callback);
$this
->render($form);
$this
->assertTrue($form['datetime_element']['datetimeDateCallbackExecuted']['#value']);
$this
->assertTrue($form['datetime_element']['timeCallbackExecuted']['#value']);
}
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. 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. 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',
];
}
}