ConditionTestDualUserTest.php 2.6 KB

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