DomainInactiveTest.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Drupal\Tests\domain\Functional;
  3. use Drupal\Core\Session\AccountInterface;
  4. use Drupal\user\RoleInterface;
  5. /**
  6. * Tests the access rules and redirects for inactive domains.
  7. *
  8. * @group domain
  9. */
  10. class DomainInactiveTest extends DomainTestBase {
  11. /**
  12. * Modules to enable.
  13. *
  14. * @var array
  15. */
  16. public static $modules = ['domain', 'node', 'views'];
  17. /**
  18. * Test inactive domain.
  19. */
  20. public function testInactiveDomain() {
  21. // Configure 'node' as front page, else the test loads the login form.
  22. $site_config = $this->config('system.site');
  23. $site_config->set('page.front', '/node')->save();
  24. // Create four new domains programmatically.
  25. $this->domainCreateTestDomains(4);
  26. $domains = \Drupal::entityTypeManager()->getStorage('domain')->loadMultiple();
  27. // Grab a known domain for testing.
  28. $domain = $domains['two_example_com'];
  29. $this->drupalGet($domain->getPath());
  30. $this->assertTrue($domain->status(), 'Tested domain is set to active.');
  31. $this->assertTrue($domain->getPath() == $this->getUrl(), 'Loaded the active domain.');
  32. // Disable the domain and test for redirect.
  33. $domain->disable();
  34. $default = \Drupal::entityTypeManager()->getStorage('domain')->loadDefaultDomain();
  35. // Our postSave() cache tag clear should allow this to work properly.
  36. $this->drupalGet($domain->getPath());
  37. $this->assertFalse($domain->status(), 'Tested domain is set to inactive.');
  38. $this->assertTrue($default->getPath() == $this->getUrl(), 'Redirected an inactive domain to the default domain.');
  39. // Check to see if the user can login.
  40. $url = $domain->getPath() . 'user/login';
  41. $this->drupalGet($url);
  42. $this->assertResponse(200, 'Request to login on inactive domain allowed.');
  43. // Check to see if the user can reset password.
  44. $url = $domain->getPath() . 'user/password';
  45. $this->drupalGet($url);
  46. $this->assertResponse(200, 'Request to reset password on inactive domain allowed.');
  47. // Try to access with the proper permission.
  48. user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, ['access inactive domains']);
  49. // Must flush cache because we did not resave the domain.
  50. drupal_flush_all_caches();
  51. $this->assertFalse($domain->status(), 'Tested domain is set to inactive.');
  52. $this->drupalGet($domain->getPath());
  53. // Set up two additional domains.
  54. $domain2 = $domains['one_example_com'];
  55. $domain3 = $domains['three_example_com'];
  56. // Check against trusted host patterns.
  57. $settings['settings']['trusted_host_patterns'] = (object) [
  58. 'value' => ['^' . $this->prepareTrustedHostname($domain->getHostname()) . '$',
  59. '^' . $this->prepareTrustedHostname($domain2->getHostname()) . '$',
  60. ],
  61. 'required' => TRUE,
  62. ];
  63. $this->writeSettings($settings);
  64. // Revoke the permission change.
  65. user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['access inactive domains']);
  66. $domain2->saveDefault();
  67. // Test the trusted host, which should redirect to default.
  68. $this->drupalGet($domain->getPath());
  69. $this->assertTrue($domain2->getPath() == $this->getUrl(), 'Redirected from the inactive domain.');
  70. $this->assertResponse(200, 'Request to trusted host allowed.');
  71. // The redirect is cached, so we must flush cache to test again.
  72. drupal_flush_all_caches();
  73. // Test another inactive domain that is not trusted.
  74. // Disable the domain and test for redirect.
  75. $domain3->saveDefault();
  76. $this->drupalGet($domain->getPath());
  77. $this->assertRaw('The provided host name is not a valid redirect.');
  78. }
  79. }