DomainTestBase.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Drupal\domain\Tests;
  3. use Drupal\Component\Render\FormattableMarkup;
  4. use Drupal\domain\DomainInterface;
  5. use Drupal\simpletest\WebTestBase;
  6. use Drupal\Core\StringTranslation\StringTranslationTrait;
  7. use Drupal\Component\Utility\Crypt;
  8. use Drupal\user\UserInterface;
  9. use Drupal\Tests\domain\Traits\DomainTestTrait;
  10. /**
  11. * Base class with helper methods and setup for domain tests.
  12. *
  13. * @deprecated
  14. * This class will be removed before the 8.1.0 release.
  15. * Use DomainStorage instead, loaded through the EntityTypeManager.
  16. */
  17. abstract class DomainTestBase extends WebTestBase {
  18. use DomainTestTrait;
  19. use StringTranslationTrait;
  20. /**
  21. * Sets a base hostname for running tests.
  22. *
  23. * When creating test domains, try to use $this->baseHostname or the
  24. * domainCreateTestDomains() method.
  25. *
  26. * @var string
  27. */
  28. public $baseHostname;
  29. /**
  30. * Modules to enable.
  31. *
  32. * @var array
  33. */
  34. public static $modules = ['domain', 'node'];
  35. /**
  36. * {@inheritdoc}
  37. */
  38. protected function setUp() {
  39. parent::setUp();
  40. // Create Basic page and Article node types.
  41. if ($this->profile != 'standard') {
  42. $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
  43. }
  44. // Set the base hostname for domains.
  45. $this->setBaseHostname();
  46. }
  47. /**
  48. * Returns whether a given user account is logged in.
  49. *
  50. * @param \Drupal\user\UserInterface $account
  51. * The user account object to check.
  52. *
  53. * @return bool
  54. * TRUE if a given user account is logged in, or FALSE.
  55. */
  56. protected function drupalUserIsLoggedIn(UserInterface $account) {
  57. // @TODO: This is a temporary hack for the test login fails when setting $cookie_domain.
  58. if (!isset($account->session_id)) {
  59. return (bool) $account->id();
  60. }
  61. // The session ID is hashed before being stored in the database.
  62. // @see \Drupal\Core\Session\SessionHandler::read()
  63. return (bool) db_query("SELECT sid FROM {users_field_data} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = :sid", [':sid' => Crypt::hashBase64($account->session_id)])->fetchField();
  64. }
  65. /**
  66. * Login a user on a specific domain.
  67. *
  68. * @param \Drupal\domain\DomainInterface $domain
  69. * The domain to log the user into.
  70. * @param \Drupal\user\UserInterface $account
  71. * The user account to login.
  72. */
  73. public function domainLogin(DomainInterface $domain, UserInterface $account) {
  74. if ($this->loggedInUser) {
  75. $this->drupalLogout();
  76. }
  77. // Login.
  78. $url = $domain->getPath() . 'user/login';
  79. $edit = ['name' => $account->getAccountName(), 'pass' => $account->passRaw];
  80. $this->drupalPostForm($url, $edit, t('Log in'));
  81. // @see WebTestBase::drupalUserIsLoggedIn()
  82. if (isset($this->sessionId)) {
  83. $account->session_id = $this->sessionId;
  84. }
  85. $pass = $this->assert($this->drupalUserIsLoggedIn($account), new FormattableMarkup('User %name successfully logged in.', ['%name' => $account->getUsername()]), 'User login');
  86. if ($pass) {
  87. $this->loggedInUser = $account;
  88. $this->container->get('current_user')->setAccount($account);
  89. }
  90. }
  91. }