You are here

public function LoggingConfigFormTest::testSetChannels in Purge 8.3

@covers \Drupal\purge_ui\Form\LoggingConfigForm::setChannels

File

modules/purge_ui/tests/src/Functional/Form/LoggingConfigFormTest.php, line 93

Class

LoggingConfigFormTest
Tests \Drupal\purge_ui\Form\LoggingConfigForm.

Namespace

Drupal\Tests\purge_ui\Functional\Form

Code

public function testSetChannels() : void {
  $form = $this
    ->formInstance()
    ->buildForm([], $this
    ->getFormStateInstance());

  // Assert that empty submits only close the dialog, nothing else.
  $ajax = $this
    ->formInstance()
    ->setChannels($form, $this
    ->getFormStateInstance());
  $this
    ->assertInstanceOf(AjaxResponse::class, $ajax);
  $this
    ->assertSame('closeDialog', $ajax
    ->getCommands()[0]['command']);
  $this
    ->assertSame(1, count($ajax
    ->getCommands()));

  // Verify that non-existent channels don't lead to saving anything.
  $submitted = $this
    ->getFormStateInstance();
  $submitted
    ->setValue('table', [
    'fake' => [
      "1",
    ],
  ]);
  $ajax = $this
    ->formInstance()
    ->setChannels($form, $submitted);
  $this
    ->assertInstanceOf(AjaxResponse::class, $ajax);
  $this
    ->assertSame('closeDialog', $ajax
    ->getCommands()[0]['command']);
  $this
    ->assertSame(1, count($ajax
    ->getCommands()));

  // Verify that correct data does lead to a write.
  $this->purgeLogger
    ->expects($this
    ->once())
    ->method('setChannel')
    ->with($this
    ->equalTo('testchannel'), $this
    ->equalTo([
    0,
    1,
  ]));
  $submitted = $this
    ->getFormStateInstance();
  $submitted
    ->setValue('table', [
    'testchannel' => [
      "1",
      "1",
      "0",
      0,
    ],
  ]);
  $ajax = $this
    ->formInstance()
    ->setChannels($form, $submitted);
  $this
    ->assertInstanceOf(AjaxResponse::class, $ajax);
  $this
    ->assertSame('closeDialog', $ajax
    ->getCommands()[0]['command']);
  $this
    ->assertSame('redirect', $ajax
    ->getCommands()[1]['command']);
  $this
    ->assertSame(2, count($ajax
    ->getCommands()));
}