3 namespace Drupal\Tests\dblog\Kernel;
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;
13 * Tests serializing a form with an injected dblog logger instance.
17 class DbLogFormInjectionTest extends KernelTestBase implements FormInterface {
19 use DependencySerializationTrait;
22 * A Dblog logger instance.
24 * @var \Psr\Log\LoggerInterface
33 public static $modules = ['system', 'dblog', 'user'];
38 public function getFormId() {
39 return 'dblog_test_injection_form';
45 * @param array $element
51 public function process($element) {
58 public function buildForm(array $form, FormStateInterface $form_state) {
59 $form['#process'][] = [$this, 'process'];
66 public function validateForm(array &$form, FormStateInterface $form_state) {}
71 public function submitForm(array &$form, FormStateInterface $form_state) {
72 $form_state->setRebuild();
78 protected function 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([
86 'mail' => 'foobar@example.com',
89 \Drupal::service('current_user')->setAccount($test_user);
93 * Tests db log injection serialization.
95 public function testLoggerSerialization() {
96 $form_state = new FormState();
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);