DomainConditionTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Drupal\Tests\domain\Functional\Condition;
  3. use Drupal\Tests\domain\Functional\DomainTestBase;
  4. /**
  5. * Tests the domain condition.
  6. *
  7. * @group domain
  8. */
  9. class DomainConditionTest extends DomainTestBase {
  10. /**
  11. * The condition plugin manager.
  12. *
  13. * @var \Drupal\Core\Condition\ConditionManager
  14. */
  15. protected $manager;
  16. /**
  17. * A test domain.
  18. *
  19. * @var \Drupal\domain\DomainInterface
  20. */
  21. protected $testDomain;
  22. /**
  23. * A test domain that never matches $test_domain.
  24. *
  25. * @var \Drupal\domain\DomainInterface
  26. */
  27. protected $notDomain;
  28. /**
  29. * An array of all testing domains.
  30. *
  31. * @var \Drupal\domain\DomainInterface[]
  32. */
  33. protected $domains;
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function setUp() {
  38. parent::setUp();
  39. // Set the condition manager.
  40. $this->manager = $this->container->get('plugin.manager.condition');
  41. // Create test domains.
  42. $this->domainCreateTestDomains(5);
  43. // Get two sample domains.
  44. $this->domains = \Drupal::entityTypeManager()->getStorage('domain')->loadMultiple();
  45. $this->testDomain = array_shift($this->domains);
  46. $this->notDomain = array_shift($this->domains);
  47. }
  48. /**
  49. * Test the domain condition.
  50. */
  51. public function testConditions() {
  52. // Grab the domain condition and configure it to check against one domain.
  53. $condition = $this->manager->createInstance('domain')
  54. ->setConfig('domains', [$this->testDomain->id() => $this->testDomain->id()])
  55. ->setContextValue('entity:domain', $this->notDomain);
  56. $this->assertFalse($condition->execute(), 'Domain request condition fails on wrong domain.');
  57. // Grab the domain condition and configure it to check against itself.
  58. $condition = $this->manager->createInstance('domain')
  59. ->setConfig('domains', [$this->testDomain->id() => $this->testDomain->id()])
  60. ->setContextValue('entity:domain', $this->testDomain);
  61. $this->assertTrue($condition->execute(), 'Domain request condition succeeds on matching domain.');
  62. // Check for the proper summary.
  63. // Summaries require an extra space due to negate handling in summary().
  64. $this->assertEqual($condition->summary(), 'Active domain is ' . $this->testDomain->label());
  65. // Check the negated summary.
  66. $condition->setConfig('negate', TRUE);
  67. $this->assertEqual($condition->summary(), 'Active domain is not ' . $this->testDomain->label());
  68. // Check the negated condition.
  69. $this->assertFalse($condition->execute(), 'Domain request condition fails when condition negated.');
  70. }
  71. }