View source
<?php
namespace Drupal\Tests\purge_ui\Functional\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\purge_ui\Form\LoggingConfigForm;
class LoggingConfigFormTest extends AjaxFormTestBase {
public static $modules = [
'purge_ui',
];
protected $formClass = LoggingConfigForm::class;
protected $route = 'purge_ui.logging_config_form';
protected $routeTitle = 'Configure logging';
public function setUp($switch_to_memory_queue = TRUE) : void {
parent::setUp($switch_to_memory_queue);
$defaults = [
[
'id' => 'testchannel',
'grants' => [
2,
4,
1,
],
],
];
$this->purgeLogger = $this
->createMock('Drupal\\purge\\Logger\\LoggerServiceInterface');
$this->purgeLogger
->method('getChannels')
->willReturn($defaults);
$this->purgeLogger
->method('hasChannel')
->will($this
->returnCallback(function ($subject) {
return $subject === 'testchannel';
}));
$this->container
->set('purge.logger', $this->purgeLogger);
}
public function testBuildForm() : void {
$form = $this
->formInstance()
->buildForm([], $this
->getFormStateInstance());
$this
->assertSame(TRUE, isset($form['msg']['#markup']));
$this
->assertSame(TRUE, (bool) strpos($form['msg']['#markup']
->render(), 'named <i><code>purge'));
$this
->assertSame(TRUE, isset($form['table']['#header']['id']));
$this
->assertSame('Id', $form['table']['#header']['id']
->render());
$this
->assertSame(9, count($form['table']['#header']));
$this
->assertSame('checkbox', $form['table']['testchannel'][0]['#type']);
$this
->assertSame(FALSE, $form['table']['testchannel'][0]['#default_value']);
$this
->assertSame(TRUE, $form['table']['testchannel'][1]['#default_value']);
$this
->assertSame(TRUE, $form['table']['testchannel'][2]['#default_value']);
$this
->assertSame(FALSE, $form['table']['testchannel'][3]['#default_value']);
$this
->assertSame(TRUE, $form['table']['testchannel'][4]['#default_value']);
$this
->assertSame(FALSE, $form['table']['testchannel'][5]['#default_value']);
$this
->assertSame(FALSE, $form['table']['testchannel'][6]['#default_value']);
$this
->assertSame(FALSE, $form['table']['testchannel'][7]['#default_value']);
$this
->assertSame(3, count($form['table']));
$this
->assertSame('submit', $form['actions']['submit']['#type']);
$this
->assertSame('Save', $form['actions']['submit']['#value']
->render());
$this
->assertSame('primary', $form['actions']['submit']['#button_type']);
$this
->assertSame('::setChannels', $form['actions']['submit']['#ajax']['callback']);
$this
->assertSame('submit', $form['actions']['cancel']['#type']);
$this
->assertSame('Cancel', $form['actions']['cancel']['#value']
->render());
$this
->assertSame('danger', $form['actions']['cancel']['#button_type']);
$this
->assertSame('::closeDialog', $form['actions']['cancel']['#ajax']['callback']);
}
public function testSetChannels() : void {
$form = $this
->formInstance()
->buildForm([], $this
->getFormStateInstance());
$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()));
$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()));
$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()));
}
public function testSubmitForm() : void {
$form = $this
->formInstance()
->buildForm([], $this
->getFormStateInstance());
$this
->assertSame(FALSE, $this
->formInstance()
->submitForm($form, $this
->getFormStateInstance()));
$submitted = $this
->getFormStateInstance();
$submitted
->setValue('table', [
'fake' => [
"1",
],
]);
$this
->assertSame(FALSE, $this
->formInstance()
->submitForm($form, $submitted));
$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,
],
]);
$this
->assertSame(TRUE, $this
->formInstance()
->submitForm($form, $submitted));
}
}