Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / dblog / tests / src / Kernel / DbLogFormInjectionTest.php
1 <?php
2
3 namespace Drupal\Tests\dblog\Kernel;
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 dblog logger instance.
14  *
15  * @group dblog
16  */
17 class DbLogFormInjectionTest 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', 'dblog', 'user'];
34
35   /**
36    * {@inheritdoc}
37    */
38   public function getFormId() {
39     return 'dblog_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('dblog', ['watchdog']);
81     $this->installSchema('system', ['key_value_expire', 'sequences']);
82     $this->installEntitySchema('user');
83     $this->logger = \Drupal::logger('test_logger');
84     $test_user = User::create([
85       'name' => 'foobar',
86       'mail' => 'foobar@example.com',
87     ]);
88     $test_user->save();
89     \Drupal::service('current_user')->setAccount($test_user);
90   }
91
92   /**
93    * Tests db log injection serialization.
94    */
95   public function testLoggerSerialization() {
96     $form_state = new FormState();
97
98     // Forms are only serialized during POST requests.
99     $form_state->setRequestMethod('POST');
100     $form_state->setCached();
101     $form_builder = $this->container->get('form_builder');
102     $form_id = $form_builder->getFormId($this, $form_state);
103     $form = $form_builder->retrieveForm($form_id, $form_state);
104     $form_builder->prepareForm($form_id, $form, $form_state);
105     $form_builder->processForm($form_id, $form, $form_state);
106   }
107
108 }