3 namespace Drupal\Tests\dblog\Kernel;
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;
14 * Tests serializing a form with an injected dblog logger instance.
18 class DbLogFormInjectionTest extends KernelTestBase implements FormInterface {
20 use DependencySerializationTrait;
23 * A Dblog logger instance.
25 * @var \Psr\Log\LoggerInterface
34 public static $modules = ['system', 'dblog', 'user'];
39 public function getFormId() {
40 return 'dblog_test_injection_form';
46 * @param array $element
52 public function process($element) {
59 public function buildForm(array $form, FormStateInterface $form_state) {
60 $form['#process'][] = [$this, 'process'];
67 public function validateForm(array &$form, FormStateInterface $form_state) {}
72 public function submitForm(array &$form, FormStateInterface $form_state) {
73 $form_state->setRebuild();
79 protected function 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([
87 'mail' => 'foobar@example.com',
90 \Drupal::service('current_user')->setAccount($test_user);
94 * Tests db log injection serialization.
96 public function testLoggerSerialization() {
97 $form_state = new FormState();
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);