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 = 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 = 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. switch (UNISH_DRUPAL_MAJOR_VERSION) {
  58. case 7:
  59. $eval = "return user_authenticate('$name', '$newpass')";
  60. break;
  61. case 8:
  62. $eval = "return \\Drupal::service('user.auth')->authenticate('$name', '$newpass');";
  63. break;
  64. }
  65. $this->drush('php-eval', array($eval), $this->options());
  66. $output = $this->getOutput();
  67. $this->assertEquals("2", $output, 'User can login with new password.');
  68. }
  69. function testUserLogin() {
  70. // Check if user-login on non-bootstrapped environment returns error.
  71. $this->drush('user-login', array(), array(), NULL, NULL, self::EXIT_ERROR);
  72. // Check user-login
  73. $user_login_options = $this->options() + array('simulate' => TRUE, 'browser' => 'unish');
  74. // Collect full logs so we can check browser.
  75. $this->drush('user-login', array(), $user_login_options + array('backend' => NULL));
  76. $parsed = $this->parse_backend_output($this->getOutput());
  77. $url = parse_url($parsed['output']);
  78. $this->assertContains('/user/reset/1', $url['path'], 'Login returned a reset URL for uid 1 by default');
  79. $browser = FALSE;
  80. foreach ($parsed['log'] as $key => $log) {
  81. // Regarding 'strip_tags', see https://github.com/drush-ops/drush/issues/1637
  82. if (strpos(strip_tags($log['message']), 'Opening browser unish at http://') === 0) {
  83. $browser = TRUE;
  84. }
  85. }
  86. $this->assertEquals($browser, TRUE, 'Correct browser opened at correct URL');
  87. // Check specific user and path argument.
  88. $uid = 2;
  89. $this->drush('user-login', array(self::NAME, 'node/add'), $user_login_options);
  90. $output = $this->getOutput();
  91. $url = parse_url($output);
  92. // user_pass_reset_url encodes the URL in D6, but not in D7 or D8
  93. $query = $url['query'];
  94. $this->assertContains('/user/reset/' . $uid, $url['path'], 'Login with user argument returned a valid reset URL');
  95. $this->assertEquals('destination=node/add', $query, 'Login included destination path in URL');
  96. // Check path used as only argument when using uid option.
  97. $this->drush('user-login', array('node/add'), $user_login_options + array('uid' => $uid));
  98. $output = $this->getOutput();
  99. $url = parse_url($output);
  100. $this->assertContains('/user/reset/' . $uid, $url['path'], 'Login with uid option returned a valid reset URL');
  101. $query = $url['query'];
  102. $this->assertEquals('destination=node/add', $query, 'Login included destination path in URL');
  103. }
  104. function testUserCancel() {
  105. // create content
  106. // @todo Creation of node types and content has changed in D8.
  107. if (UNISH_DRUPAL_MAJOR_VERSION == 8) {
  108. $this->markTestSkipped("@todo Creation of node types and content has changed in D8.");
  109. }
  110. if (UNISH_DRUPAL_MAJOR_VERSION >= 7) {
  111. // create_node_types script does not work for D6
  112. $this->drush('php-script', array('create_node_types'), $this->options() + array('script-path' => dirname(__FILE__) . '/resources'));
  113. $name = self::NAME;
  114. $newpass = 'newpass';
  115. $eval = "return user_authenticate('$name', '$newpass')";
  116. $this->drush('php-eval', array($eval), $this->options());
  117. $eval = "\$node = (object) array('title' => 'foo', 'uid' => 2, 'type' => 'page',);";
  118. if (UNISH_DRUPAL_MAJOR_VERSION >= 8) {
  119. $eval .= " \$node = node_submit(entity_create('node', \$node));";
  120. }
  121. $eval .= " node_save(\$node);";
  122. $this->drush('php-eval', array($eval), $this->options());
  123. $this->drush('user-cancel', array(self::NAME), $this->options() + array('delete-content' => NULL));
  124. $eval = 'print (string) user_load(2)';
  125. $this->drush('php-eval', array($eval), $this->options());
  126. $output = $this->getOutput();
  127. $this->assertEmpty($output, 'User was deleted');
  128. $eval = 'print (string) node_load(2)';
  129. $this->drush('php-eval', array($eval), $this->options());
  130. $output = $this->getOutput();
  131. $this->assertEmpty($output, 'Content was deleted');
  132. }
  133. }
  134. function UserCreate() {
  135. $this->drush('user-create', array(self::NAME), $this->options() + array('password' => 'password', 'mail' => "example@example.com"));
  136. $this->drush('user-information', array(self::NAME), $this->options() + array('format' => 'json'));
  137. $uid = 2;
  138. $output = $this->getOutputFromJSON($uid);
  139. $this->assertEquals('example@example.com', $output->mail);
  140. $this->assertEquals(self::NAME, $output->name);
  141. $this->assertEquals(1, $output->{self::$status_prop}, 'Newly created user is Active.');
  142. $expected = array(self::$authenticated);
  143. $this->assertEquals($expected, array_values((array)$output->roles), 'Newly created user has one role.');
  144. }
  145. function options() {
  146. return array(
  147. 'root' => $this->webroot(),
  148. 'uri' => key($this->getSites()),
  149. 'yes' => NULL,
  150. );
  151. }
  152. }