Version 1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Queue / QueueSerializationTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Queue;
4
5 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
6 use Drupal\Core\Form\FormInterface;
7 use Drupal\Core\Form\FormState;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\KernelTests\KernelTestBase;
10 use Drupal\user\Entity\User;
11
12 /**
13  * Tests serializing a form with an injected DatabaseQueue instance.
14  *
15  * @group Queue
16  */
17 class QueueSerializationTest extends KernelTestBase implements FormInterface {
18
19   use DependencySerializationTrait;
20
21   /**
22    * A queue instance.
23    *
24    * @var \Drupal\Core\Queue\DatabaseQueue
25    */
26   protected $queue;
27
28   /**
29    * Modules to enable.
30    *
31    * @var array
32    */
33   public static $modules = ['system', 'user', 'aggregator'];
34
35   /**
36    * {@inheritdoc}
37    */
38   public function getFormId() {
39     return 'queue_test_injection_form';
40   }
41
42   /**
43    * Process callback.
44    *
45    * @param array $element
46    *   Form element
47    *
48    * @return array
49    *   Processed element.
50    */
51   public function process($element) {
52     return $element;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function buildForm(array $form, FormStateInterface $form_state) {
59     $form['#process'][] = [$this, 'process'];
60     return $form;
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function validateForm(array &$form, FormStateInterface $form_state) {}
67
68   /**
69    * {@inheritdoc}
70    */
71   public function submitForm(array &$form, FormStateInterface $form_state) {
72     $form_state->setRebuild();
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   protected function setUp() {
79     parent::setUp();
80     $this->installSchema('system', ['key_value_expire', 'sequences']);
81     $this->installEntitySchema('user');
82     $this->queue = \Drupal::service('queue.database')->get('aggregator_refresh');
83     $test_user = User::create([
84       'name' => 'foobar',
85       'mail' => 'foobar@example.com',
86     ]);
87     $test_user->save();
88     \Drupal::service('current_user')->setAccount($test_user);
89   }
90
91   /**
92    * Tests queue injection serialization.
93    */
94   public function testQueueSerialization() {
95     $form_state = new FormState();
96     $form_state->setRequestMethod('POST');
97     $form_state->setCached();
98     $form_builder = $this->container->get('form_builder');
99     $form_id = $form_builder->getFormId($this, $form_state);
100     $form = $form_builder->retrieveForm($form_id, $form_state);
101     $form_builder->prepareForm($form_id, $form, $form_state);
102     $form_builder->processForm($form_id, $form, $form_state);
103   }
104
105 }