You are here

class DbLogFormInjectionTest in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/dblog/tests/src/Kernel/DbLogFormInjectionTest.php \Drupal\Tests\dblog\Kernel\DbLogFormInjectionTest
  2. 9 core/modules/dblog/tests/src/Kernel/DbLogFormInjectionTest.php \Drupal\Tests\dblog\Kernel\DbLogFormInjectionTest

Tests serializing a form with an injected dblog logger instance.

@group dblog

Hierarchy

Expanded class hierarchy of DbLogFormInjectionTest

File

core/modules/dblog/tests/src/Kernel/DbLogFormInjectionTest.php, line 17

Namespace

Drupal\Tests\dblog\Kernel
View source
class DbLogFormInjectionTest extends KernelTestBase implements FormInterface {
  use DependencySerializationTrait;

  /**
   * A Dblog logger instance.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * Modules to enable.
   *
   * @var array
   */
  protected static $modules = [
    'system',
    'dblog',
    'user',
  ];

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'dblog_test_injection_form';
  }

  /**
   * Process callback.
   *
   * @param array $element
   *   Form element
   *
   * @return array
   *   Processed element.
   */
  public function process($element) {
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['#process'][] = [
      $this,
      'process',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state
      ->setRebuild();
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this
      ->installSchema('dblog', [
      'watchdog',
    ]);
    $this
      ->installSchema('system', [
      'sequences',
    ]);
    $this
      ->installEntitySchema('user');
    $this->logger = \Drupal::logger('test_logger');
    $test_user = User::create([
      'name' => 'foobar',
      'mail' => 'foobar@example.com',
    ]);
    $test_user
      ->save();
    \Drupal::service('current_user')
      ->setAccount($test_user);
  }

  /**
   * Tests db log injection serialization.
   */
  public function testLoggerSerialization() {
    $form_state = new FormState();

    // Forms are only serialized during POST requests.
    $form_state
      ->setRequestMethod('POST');
    $form_state
      ->setCached();
    $form_builder = $this->container
      ->get('form_builder');
    $form_id = $form_builder
      ->getFormId($this, $form_state);
    $form = $form_builder
      ->retrieveForm($form_id, $form_state);
    $form_builder
      ->prepareForm($form_id, $form, $form_state);
    $form_builder
      ->processForm($form_id, $form, $form_state);
  }

}

Members