Same filename and directory in other branches
- 9 core/tests/Drupal/KernelTests/Core/Datetime/DatelistElementFormTest.php
Namespace
Drupal\KernelTests\Core\Datetime
File
core/tests/Drupal/KernelTests/Core/Datetime/DatelistElementFormTest.php
View source
<?php
declare (strict_types=1);
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 DatelistElementFormTest extends KernelTestBase implements FormInterface, TrustedCallbackInterface {
protected static $modules = [
'datetime',
'system',
];
protected function setUp() : void {
parent::setUp();
}
public function getFormId() {
return 'test_datelist_element';
}
public function datelistDateCallbackTrusted(array &$element, FormStateInterface $form_state, DrupalDateTime $date = NULL) {
$element['datelistDateCallbackExecuted'] = [
'#value' => TRUE,
];
$form_state
->set('datelistDateCallbackExecuted', TRUE);
}
public function datelistDateCallback(array &$element, FormStateInterface $form_state, DrupalDateTime $date = NULL) {
$element['datelistDateCallbackExecuted'] = [
'#value' => TRUE,
];
$form_state
->set('datelistDateCallbackExecuted', TRUE);
}
public function buildForm(array $form, FormStateInterface $form_state, string $date_callback = 'datelistDateCallbackTrusted') {
$form['datelist_element'] = [
'#title' => 'datelist test',
'#type' => 'datelist',
'#default_value' => new DrupalDateTime('2000-01-01 00:00:00'),
'#date_part_order' => [
'month',
'day',
'year',
'hour',
'minute',
'ampm',
],
'#date_text_parts' => [
'year',
],
'#date_year_range' => '2010:2020',
'#date_increment' => 15,
'#date_date_callbacks' => [
[
$this,
$date_callback,
],
],
];
$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 testDatelistElement() {
$form_state = new FormState();
$form = \Drupal::formBuilder()
->buildForm($this, $form_state);
$this
->render($form);
$this
->assertTrue($form['datelist_element']['datelistDateCallbackExecuted']['#value']);
$this
->assertTrue($form_state
->get('datelistDateCallbackExecuted'));
}
public function testDatelistElementUntrustedCallbacks() : void {
$this
->expectException(UntrustedCallbackException::class);
$this
->expectExceptionMessage(sprintf('Datelist 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,
'datelistDateCallback',
])));
$form = \Drupal::formBuilder()
->getForm($this, 'datelistDateCallback');
}
public static function trustedCallbacks() {
return [
'datelistDateCallbackTrusted',
];
}
}
Classes