|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\KernelTests\Core\Datetime; |
| 4 | + |
| 5 | +use Drupal\Core\Datetime\DrupalDateTime; |
| 6 | +use Drupal\Core\Form\FormInterface; |
| 7 | +use Drupal\Core\Form\FormStateInterface; |
| 8 | +use Drupal\KernelTests\KernelTestBase; |
| 9 | + |
| 10 | +/** |
| 11 | + * Tests DatetimeElement functionality. |
| 12 | + * |
| 13 | + * @group Form |
| 14 | + */ |
| 15 | +class DatetimeElementFormTest extends KernelTestBase implements FormInterface { |
| 16 | + |
| 17 | + /** |
| 18 | + * The variable under test. |
| 19 | + */ |
| 20 | + protected $flag; |
| 21 | + |
| 22 | + /** |
| 23 | + * Modules to enable. |
| 24 | + * |
| 25 | + * @var array |
| 26 | + */ |
| 27 | + public static $modules = ['datetime', 'system']; |
| 28 | + |
| 29 | + /** |
| 30 | + * Sets up the test. |
| 31 | + */ |
| 32 | + protected function setUp() { |
| 33 | + parent::setUp(); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritdoc} |
| 38 | + */ |
| 39 | + public function getFormId() { |
| 40 | + return 'test_datetime_element'; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * {@inheritdoc} |
| 45 | + */ |
| 46 | + public function datetimecallback($date) { |
| 47 | + $this->flag = 'Date time callback called.'; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * {@inheritdoc} |
| 52 | + */ |
| 53 | + public function buildForm(array $form, FormStateInterface $form_state) { |
| 54 | + |
| 55 | + $form['datetime_element'] = [ |
| 56 | + '#title' => 'datelist test', |
| 57 | + '#type' => 'datetime', |
| 58 | + '#default_value' => new DrupalDateTime('2000-01-01 00:00:00'), |
| 59 | + '#date_date_format' => ['Y-m-d'], |
| 60 | + '#date_time_format' => ['H:i:s'], |
| 61 | + '#date_date_element' => 'HTML Date', |
| 62 | + '#date_time_element' => 'HTML Time', |
| 63 | + '#date_increment' => 1, |
| 64 | + '#date_date_callbacks' => [[$this, 'datetimecallback']], |
| 65 | + ]; |
| 66 | + |
| 67 | + $form['submit'] = [ |
| 68 | + '#type' => 'submit', |
| 69 | + '#value' => t('Submit'), |
| 70 | + ]; |
| 71 | + |
| 72 | + return $form; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * {@inheritdoc} |
| 77 | + */ |
| 78 | + public function submitForm(array &$form, FormStateInterface $form_state) {} |
| 79 | + |
| 80 | + /** |
| 81 | + * Form validation handler. |
| 82 | + * |
| 83 | + * @param array $form |
| 84 | + * An associative array containing the structure of the form. |
| 85 | + * @param \Drupal\Core\Form\FormStateInterface $form_state |
| 86 | + * The current state of the form. |
| 87 | + */ |
| 88 | + public function validateForm(array &$form, FormStateInterface $form_state) {} |
| 89 | + |
| 90 | + /** |
| 91 | + * Tests that default handlers are added even if custom are specified. |
| 92 | + */ |
| 93 | + public function testDatetimeElement() { |
| 94 | + $form = \Drupal::formBuilder()->getForm($this); |
| 95 | + $this->render($form); |
| 96 | + |
| 97 | + $this->assertEqual(t('Date time callback called.'), $this->flag); |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments