userTest.php

  1. 8.0.x tests/userTest.php
  2. 6.x tests/userTest.php
  3. 7.x tests/userTest.php
  4. 4.x tests/userTest.php
  5. 5.x tests/userTest.php
  6. master tests/userTest.php

Namespace

Unish

Classes

Namesort descending Description
userCase @group slow @group commands

File

tests/userTest.php
View source
  1. <?php
  2. namespace Unish;
  3. /**
  4. * @group slow
  5. * @group commands
  6. */
  7. class userCase extends CommandUnishTestCase {
  8. const NAME = 'example';
  9. private static $authenticated;
  10. private static $status_prop;
  11. function setUp() {
  12. if (!$this->getSites()) {
  13. $this->setUpDrupal(1, TRUE);
  14. self::$authenticated = 'authenticated';
  15. self::$status_prop = 'status';
  16. if (UNISH_DRUPAL_MAJOR_VERSION < 8) {
  17. self::$authenticated .= ' user';
  18. }
  19. else {
  20. self::$status_prop = 'user_status';
  21. }
  22. $this->userCreate();
  23. }
  24. }
  25. function testBlockUnblock() {
  26. $this->drush('user-block', array(self::NAME), $this->options());
  27. $this->drush('user-information', array(self::NAME), $this->options() + array('format' => 'json'));
  28. $uid = UNISH_DRUPAL_MAJOR_VERSION == 6 ? 3 : 2;
  29. $output = $this->getOutputFromJSON($uid);
  30. $this->assertEquals(0, $output->{self::$status_prop}, 'User is blocked.');
  31. // user-unblock
  32. $this->drush('user-unblock', array(self::NAME), $this->options());
  33. $this->drush('user-information', array(self::NAME), $this->options() + array('format' => 'json'));
  34. $output = $this->getOutputFromJSON($uid);
  35. $this->assertEquals(1, $output->{self::$status_prop}, 'User is unblocked.');
  36. }
  37. function testUserRole() {
  38. // First, create the role since we use testing install profile.
  39. $this->drush('role-create', array('test role'), $this->options());
  40. $this->drush('user-add-role', array('test role', self::NAME), $this->options());
  41. $this->drush('user-information', array(self::NAME), $this->options() + array('format' => 'json'));
  42. $uid = UNISH_DRUPAL_MAJOR_VERSION == 6 ? 3 : 2;
  43. $output = $this->getOutputFromJSON($uid);
  44. $expected = array(self::$authenticated, 'test role');
  45. $this->assertEquals($expected, array_values((array)$output->roles), 'User has test role.');
  46. // user-remove-role
  47. $this->drush('user-remove-role', array('test role', self::NAME), $this->options());
  48. $this->drush('user-information', array(self::NAME), $this->options() + array('format' => 'json'));
  49. $output = $this->getOutputFromJSON($uid);
  50. $expected = array(self::$authenticated);
  51. $this->assertEquals($expected, array_values((array)$output->roles), 'User removed test role.');
  52. }
  53. function testUserPassword() {
  54. $newpass = 'newpass';
  55. $name = self::NAME;
  56. $this->drush('user-password', array(self::NAME), $this->options() + array('password' => $newpass));
  57. // user_authenticate() is more complex in D6 so skip it.
  58. switch (UNISH_DRUPAL_MAJOR_VERSION) {
  59. case 6:
  60. $this->markTestSkipped('Drupal 6 authentication too complex for testing.');
  61. break;
  62. case 7:
  63. $eval = "return user_authenticate('$name', '$newpass')";
  64. break;
  65. case 8:
  66. $eval = "return \\Drupal::service('user.auth')->authenticate('$name', '$newpass');";
  67. break;
  68. }
  69. $this->drush('php-eval', array($eval), $this->options());
  70. $output = $this->getOutput();
  71. $this->assertEquals("'2'", $output, 'User can login with new password.');
  72. }
  73. function testUserLogin() {
  74. // Check if user-login on non-bootstrapped environment returns error.
  75. $this->drush('user-login', array(), array(), NULL, NULL, self::EXIT_ERROR);
  76. // Check user-login
  77. $user_login_options = $this->options() + array('simulate' => TRUE, 'browser' => 'unish');
  78. // Collect full logs so we can check browser.
  79. $this->drush('user-login', array(), $user_login_options + array('backend' => NULL));
  80. $parsed = $this->parse_backend_output($this->getOutput());
  81. $url = parse_url($parsed['output']);
  82. $this->assertContains('/user/reset/1', $url['path'], 'Login returned a reset URL for uid 1 by default');
  83. $browser = FALSE;
  84. foreach ($parsed['log'] as $key => $log) {
  85. // Regarding 'strip_tags', see https://github.com/drush-ops/drush/issues/1637
  86. if (strpos(strip_tags($log['message']), 'Opening browser unish at http://') === 0) {
  87. $browser = TRUE;
  88. }
  89. }
  90. $this->assertEquals($browser, TRUE, 'Correct browser opened at correct URL');
  91. // Check specific user and path argument.
  92. $uid = UNISH_DRUPAL_MAJOR_VERSION == 6 ? 3 : 2;
  93. $this->drush('user-login', array(self::NAME, 'node/add'), $user_login_options);
  94. $output = $this->getOutput();
  95. $url = parse_url($output);
  96. // user_pass_reset_url encodes the URL in D6, but not in D7 or D8
  97. $query = $url['query'];
  98. if (UNISH_DRUPAL_MAJOR_VERSION < 7) {
  99. $query = urldecode($query);
  100. }
  101. $this->assertContains('/user/reset/' . $uid, $url['path'], 'Login with user argument returned a valid reset URL');
  102. $this->assertEquals('destination=node/add', $query, 'Login included destination path in URL');
  103. // Check path used as only argument when using uid option.
  104. $this->drush('user-login', array('node/add'), $user_login_options + array('uid' => $uid));
  105. $output = $this->getOutput();
  106. $url = parse_url($output);
  107. $this->assertContains('/user/reset/' . $uid, $url['path'], 'Login with uid option returned a valid reset URL');
  108. $query = $url['query'];
  109. if (UNISH_DRUPAL_MAJOR_VERSION < 7) {
  110. $query = urldecode($query);
  111. }
  112. $this->assertEquals('destination=node/add', $query, 'Login included destination path in URL');
  113. }
  114. function testUserCancel() {
  115. // create content
  116. // @todo Creation of node types and content has changed in D8.
  117. if (UNISH_DRUPAL_MAJOR_VERSION == 8) {
  118. $this->markTestSkipped("@todo Creation of node types and content has changed in D8.");
  119. }
  120. if (UNISH_DRUPAL_MAJOR_VERSION >= 7) {
  121. // create_node_types script does not work for D6
  122. $this->drush('php-script', array('create_node_types'), $this->options() + array('script-path' => dirname(__FILE__) . '/resources'));
  123. $name = self::NAME;
  124. $newpass = 'newpass';
  125. $eval = "return user_authenticate('$name', '$newpass')";
  126. $this->drush('php-eval', array($eval), $this->options());
  127. $eval = "\$node = (object) array('title' => 'foo', 'uid' => 2, 'type' => 'page',);";
  128. if (UNISH_DRUPAL_MAJOR_VERSION >= 8) {
  129. $eval .= " \$node = node_submit(entity_create('node', \$node));";
  130. }
  131. $eval .= " node_save(\$node);";
  132. $this->drush('php-eval', array($eval), $this->options());
  133. $this->drush('user-cancel', array(self::NAME), $this->options() + array('delete-content' => NULL));
  134. $eval = 'print (string) user_load(2)';
  135. $this->drush('php-eval', array($eval), $this->options());
  136. $output = $this->getOutput();
  137. $this->assertEmpty($output, 'User was deleted');
  138. $eval = 'print (string) node_load(2)';
  139. $this->drush('php-eval', array($eval), $this->options());
  140. $output = $this->getOutput();
  141. $this->assertEmpty($output, 'Content was deleted');
  142. }
  143. }
  144. function UserCreate() {
  145. $this->drush('user-create', array(self::NAME), $this->options() + array('password' => 'password', 'mail' => "example@example.com"));
  146. $this->drush('user-information', array(self::NAME), $this->options() + array('format' => 'json'));
  147. $uid = UNISH_DRUPAL_MAJOR_VERSION == 6 ? 3 : 2;
  148. $output = $this->getOutputFromJSON($uid);
  149. $this->assertEquals('example@example.com', $output->mail);
  150. $this->assertEquals(self::NAME, $output->name);
  151. $this->assertEquals(1, $output->{self::$status_prop}, 'Newly created user is Active.');
  152. $expected = array(self::$authenticated);
  153. $this->assertEquals($expected, array_values((array)$output->roles), 'Newly created user has one role.');
  154. }
  155. function options() {
  156. return array(
  157. 'root' => $this->webroot(),
  158. 'uri' => key($this->getSites()),
  159. 'yes' => NULL,
  160. );
  161. }
  162. }