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. if (strpos($log['message'], 'Opening browser unish at http://') === 0) {
  86. $browser = TRUE;
  87. }
  88. }
  89. $this->assertEquals($browser, TRUE, 'Correct browser opened at correct URL');
  90. // Check specific user and path argument.
  91. $uid = UNISH_DRUPAL_MAJOR_VERSION == 6 ? 3 : 2;
  92. $this->drush('user-login', array(self::NAME, 'node/add'), $user_login_options);
  93. $output = $this->getOutput();
  94. $url = parse_url($output);
  95. // user_pass_reset_url encodes the URL in D6, but not in D7 or D8
  96. $query = $url['query'];
  97. if (UNISH_DRUPAL_MAJOR_VERSION < 7) {
  98. $query = urldecode($query);
  99. }
  100. $this->assertContains('/user/reset/' . $uid, $url['path'], 'Login with user argument returned a valid reset URL');
  101. $this->assertEquals('destination=node/add', $query, 'Login included destination path in URL');
  102. // Check path used as only argument when using uid option.
  103. $this->drush('user-login', array('node/add'), $user_login_options + array('uid' => $uid));
  104. $output = $this->getOutput();
  105. $url = parse_url($output);
  106. $this->assertContains('/user/reset/' . $uid, $url['path'], 'Login with uid option returned a valid reset URL');
  107. $query = $url['query'];
  108. if (UNISH_DRUPAL_MAJOR_VERSION < 7) {
  109. $query = urldecode($query);
  110. }
  111. $this->assertEquals('destination=node/add', $query, 'Login included destination path in URL');
  112. }
  113. function testUserCancel() {
  114. // create content
  115. // @todo Creation of node types and content has changed in D8.
  116. if (UNISH_DRUPAL_MAJOR_VERSION == 8) {
  117. $this->markTestSkipped("@todo Creation of node types and content has changed in D8.");
  118. }
  119. if (UNISH_DRUPAL_MAJOR_VERSION >= 7) {
  120. // create_node_types script does not work for D6
  121. $this->drush('php-script', array('create_node_types'), $this->options() + array('script-path' => dirname(__FILE__) . '/resources'));
  122. $name = self::NAME;
  123. $newpass = 'newpass';
  124. $eval = "return user_authenticate('$name', '$newpass')";
  125. $this->drush('php-eval', array($eval), $this->options());
  126. $eval = "\$node = (object) array('title' => 'foo', 'uid' => 2, 'type' => 'page',);";
  127. if (UNISH_DRUPAL_MAJOR_VERSION >= 8) {
  128. $eval .= " \$node = node_submit(entity_create('node', \$node));";
  129. }
  130. $eval .= " node_save(\$node);";
  131. $this->drush('php-eval', array($eval), $this->options());
  132. $this->drush('user-cancel', array(self::NAME), $this->options() + array('delete-content' => NULL));
  133. $eval = 'print (string) user_load(2)';
  134. $this->drush('php-eval', array($eval), $this->options());
  135. $output = $this->getOutput();
  136. $this->assertEmpty($output, 'User was deleted');
  137. $eval = 'print (string) node_load(2)';
  138. $this->drush('php-eval', array($eval), $this->options());
  139. $output = $this->getOutput();
  140. $this->assertEmpty($output, 'Content was deleted');
  141. }
  142. }
  143. function UserCreate() {
  144. $this->drush('user-create', array(self::NAME), $this->options() + array('password' => 'password', 'mail' => "example@example.com"));
  145. $this->drush('user-information', array(self::NAME), $this->options() + array('format' => 'json'));
  146. $uid = UNISH_DRUPAL_MAJOR_VERSION == 6 ? 3 : 2;
  147. $output = $this->getOutputFromJSON($uid);
  148. $this->assertEquals('example@example.com', $output->mail);
  149. $this->assertEquals(self::NAME, $output->name);
  150. $this->assertEquals(1, $output->{self::$status_prop}, 'Newly created user is Active.');
  151. $expected = array(self::$authenticated);
  152. $this->assertEquals($expected, array_values((array)$output->roles), 'Newly created user has one role.');
  153. }
  154. function options() {
  155. return array(
  156. 'root' => $this->webroot(),
  157. 'uri' => key($this->getSites()),
  158. 'yes' => NULL,
  159. );
  160. }
  161. }