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