Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / datetime / tests / src / Kernel / DateTimeFormInjectionTest.php
1 <?php
2
3 namespace Drupal\Tests\datetime\Kernel;
4
5 use Drupal\Core\Datetime\DrupalDateTime;
6 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
7 use Drupal\Core\Form\FormInterface;
8 use Drupal\Core\Form\FormState;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\KernelTests\KernelTestBase;
11
12 /**
13  * Tests serializing a form with an injected datetime instance.
14  *
15  * @group datetime
16  */
17 class DateTimeFormInjectionTest extends KernelTestBase implements FormInterface {
18
19   use DependencySerializationTrait;
20
21   /**
22    * A Dblog logger instance.
23    *
24    * @var \Psr\Log\LoggerInterface
25    */
26   protected $logger;
27
28   /**
29    * Modules to enable.
30    *
31    * @var array
32    */
33   public static $modules = ['system', 'datetime'];
34
35   /**
36    * {@inheritdoc}
37    */
38   protected function setUp() {
39     parent::setUp();
40     $this->installSchema('system', ['key_value_expire', 'sequences']);
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function getFormId() {
47     return 'datetime_test_injection_form';
48   }
49
50   /**
51    * Process callback.
52    *
53    * @param array $element
54    *   Form element.
55    *
56    * @return array
57    *   Processed element.
58    */
59   public function process($element) {
60     return $element;
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function buildForm(array $form, FormStateInterface $form_state) {
67     $form['datelist_element'] = [
68       '#title' => 'datelist test',
69       '#type' => 'datelist',
70       '#default_value' => new DrupalDateTime('2000-01-01 00:00:00'),
71       '#date_part_order' => [
72         'month',
73         'day',
74         'year',
75         'hour',
76         'minute', 'ampm',
77       ],
78       '#date_text_parts' => ['year'],
79       '#date_year_range' => '2010:2020',
80       '#date_increment' => 15,
81     ];
82     $form['#process'][] = [$this, 'process'];
83     return $form;
84   }
85
86   /**
87    * {@inheritdoc}
88    */
89   public function validateForm(array &$form, FormStateInterface $form_state) {}
90
91   /**
92    * {@inheritdoc}
93    */
94   public function submitForm(array &$form, FormStateInterface $form_state) {
95     $this->assertTrue(TRUE);
96     $form_state->setRebuild();
97   }
98
99   /**
100    * Tests custom string injection serialization.
101    */
102   public function testDatetimeSerialization() {
103     $form_state = new FormState();
104     $form_state->setRequestMethod('POST');
105     $form_state->setCached();
106     $form_builder = $this->container->get('form_builder');
107     $form_id = $form_builder->getFormId($this, $form_state);
108     $form = $form_builder->retrieveForm($form_id, $form_state);
109     $form_builder->prepareForm($form_id, $form, $form_state);
110     $form_builder->processForm($form_id, $form, $form_state);
111   }
112
113 }