ConditionTestDualUserTest.php in Zircon Profile 8
File
core/modules/system/tests/modules/condition_test/src/Tests/ConditionTestDualUserTest.php
View source
<?php
namespace Drupal\condition_test\Tests;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\simpletest\KernelTestBase;
use Drupal\user\Entity\User;
class ConditionTestDualUserTest extends KernelTestBase {
protected $anonymous;
protected $authenticated;
public static $modules = [
'system',
'user',
'condition_test',
];
protected function setUp() {
parent::setUp();
$this
->installSchema('system', 'sequences');
$this
->installEntitySchema('user');
$this->anonymous = User::create([
'uid' => 0,
]);
$this->authenticated = User::create([
'uid' => 1,
]);
}
public function testConditions() {
$this
->doTestIdenticalUser();
$this
->doTestDifferentUser();
}
protected function doTestIdenticalUser() {
$condition = \Drupal::service('plugin.manager.condition')
->createInstance('condition_test_dual_user')
->setContextMapping([
'user1' => 'anonymous',
'user2' => 'anonymous',
]);
$definition = new ContextDefinition('entity:user');
$contexts['anonymous'] = new Context($definition, $this->anonymous);
\Drupal::service('context.handler')
->applyContextMapping($condition, $contexts);
$this
->assertTrue($condition
->execute());
}
protected function doTestDifferentUser() {
$condition = \Drupal::service('plugin.manager.condition')
->createInstance('condition_test_dual_user')
->setContextMapping([
'user1' => 'anonymous',
'user2' => 'authenticated',
]);
$definition = new ContextDefinition('entity:user');
$contexts['anonymous'] = new Context($definition, $this->anonymous);
$contexts['authenticated'] = new Context($definition, $this->authenticated);
\Drupal::service('context.handler')
->applyContextMapping($condition, $contexts);
$this
->assertFalse($condition
->execute());
}
}