Version 1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Plugin / Condition / ConditionTestDualUserTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Plugin\Condition;
4
5 use Drupal\Core\Plugin\Context\Context;
6 use Drupal\Core\Plugin\Context\ContextDefinition;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\user\Entity\User;
9
10 /**
11  * Tests a condition that requires two users.
12  *
13  * @group condition_test
14  */
15 class ConditionTestDualUserTest extends KernelTestBase {
16
17   /**
18    * An anonymous user for testing purposes.
19    *
20    * @var \Drupal\user\Entity\User
21    */
22   protected $anonymous;
23
24   /**
25    * An authenticated user for testing purposes.
26    *
27    * @var \Drupal\user\Entity\User
28    */
29   protected $authenticated;
30
31   /**
32    * {@inheritdoc}
33    */
34   public static $modules = ['system', 'user', 'condition_test'];
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setUp() {
40     parent::setUp();
41
42     $this->installSchema('system', 'sequences');
43     $this->installEntitySchema('user');
44
45     $this->anonymous = User::create(['uid' => 0]);
46     $this->authenticated = User::create(['uid' => 1]);
47   }
48
49   /**
50    * Tests the dual user condition.
51    */
52   public function testConditions() {
53     $this->doTestIdenticalUser();
54     $this->doTestDifferentUser();
55   }
56
57   /**
58    * Tests with both contexts mapped to the same user.
59    */
60   protected function doTestIdenticalUser() {
61     /** @var \Drupal\Core\Condition\ConditionPluginBase $condition */
62     $condition = \Drupal::service('plugin.manager.condition')
63       ->createInstance('condition_test_dual_user')
64       // Map the anonymous user to both contexts.
65       ->setContextMapping([
66         'user1' => 'anonymous',
67         'user2' => 'anonymous',
68       ]);
69     $definition = new ContextDefinition('entity:user');
70     $contexts['anonymous'] = new Context($definition, $this->anonymous);
71     \Drupal::service('context.handler')->applyContextMapping($condition, $contexts);
72     $this->assertTrue($condition->execute());
73   }
74
75   /**
76    * Tests with each context mapped to different users.
77    */
78   protected function doTestDifferentUser() {
79     /** @var \Drupal\Core\Condition\ConditionPluginBase $condition */
80     $condition = \Drupal::service('plugin.manager.condition')
81       ->createInstance('condition_test_dual_user')
82       ->setContextMapping([
83         'user1' => 'anonymous',
84         'user2' => 'authenticated',
85       ]);
86     $definition = new ContextDefinition('entity:user');
87     $contexts['anonymous'] = new Context($definition, $this->anonymous);
88     $contexts['authenticated'] = new Context($definition, $this->authenticated);
89     \Drupal::service('context.handler')->applyContextMapping($condition, $contexts);
90     $this->assertFalse($condition->execute());
91   }
92
93 }