View source
<?php
namespace Drupal\Tests\webform\Kernel;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
class WebformConditionTest extends EntityKernelTestBase {
public static $modules = [
'webform',
];
public function testConditions() {
$this
->installSchema('webform', [
'webform',
]);
$manager = $this->container
->get('plugin.manager.condition');
$this
->createUser();
$webform = Webform::create([
'id' => 'test',
]);
$webform
->save();
$condition = $manager
->createInstance('webform')
->setConfig('webforms', [
'not_test' => 'not_test',
])
->setContextValue('webform', $webform);
$this
->assertFalse($condition
->execute(), 'Webform check fails.');
$this
->assertEquals('The webform is not_test', $condition
->summary());
$condition
->setConfig('webforms', [
'test' => 'test',
]);
$this
->assertTrue($condition
->execute(), 'Webform test pass webform condition check for test');
$this
->assertEquals('The webform is test', $condition
->summary());
$condition
->setConfig('webforms', [
'not_test' => 'not_test',
'test' => 'test',
]);
$this
->assertTrue($condition
->execute(), 'Webform test pass webform condition check for not_test or test');
$this
->assertEquals('The webform is not_test or test', $condition
->summary());
$condition
->setConfig('webforms', [
'not_test' => 'not_test',
'test' => 'test',
'other_test' => 'other_test',
]);
$this
->assertEquals('The webform is not_test, test or other_test', $condition
->summary());
$condition = $manager
->createInstance('webform', [
'webforms' => [
'test' => 'test',
],
'context' => [
'webform' => $webform,
],
]);
$this
->assertTrue($condition
->execute(), 'Constructor injection of context and configuration working as anticipated.');
$webform_submission = WebformSubmission::create([
'webform_id' => $webform
->id(),
]);
$condition = $manager
->createInstance('webform')
->setConfig('webforms', [
'test' => 'test',
])
->setContextValue('webform_submission', $webform_submission);
$this
->assertEquals('The webform is test', $condition
->summary());
}
}