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

Tests for user.drush.inc

Classes

Namesort descending Description
userCase @group slow @group commands

File

tests/userTest.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Tests for user.drush.inc
  5. */
  6. /**
  7. * @group slow
  8. * @group commands
  9. */
  10. class userCase extends Drush_CommandTestCase {
  11. /**
  12. * Create, edit, block, and cancel users.
  13. */
  14. public function testUser() {
  15. // user-create
  16. $sites = $this->setUpDrupal(1, TRUE);
  17. $root = $this->webroot();
  18. $name = "example";
  19. $options = array(
  20. 'root' => $root,
  21. 'uri' => key($sites),
  22. 'yes' => NULL,
  23. );
  24. $authenticated = 'authenticated';
  25. if (UNISH_DRUPAL_MAJOR_VERSION < 8) {
  26. $authenticated .= ' user';
  27. }
  28. $this->drush('user-create', array($name), $options + array('password' => 'password', 'mail' => "example@example.com"));
  29. $this->drush('user-information', array($name), $options + array('format' => 'json'));
  30. $output = $this->getOutputFromJSON('2');
  31. $this->assertEquals('example@example.com', $output->mail);
  32. $this->assertEquals($name, $output->name);
  33. $this->assertEquals(1, $output->status, 'Newly created user is Active.');
  34. $obj_authenticated = (object) array(2 => $authenticated);
  35. $this->assertEquals(json_encode($obj_authenticated), json_encode($output->roles), 'Newly created user has one role.');
  36. // user-block
  37. $this->drush('user-block', array($name), $options);
  38. $this->drush('user-information', array($name), $options + array('format' => 'json'));
  39. $output = $this->getOutputFromJSON('2');
  40. $this->assertEquals(0, $output->status, 'User is blocked.');
  41. // user-unblock
  42. $this->drush('user-unblock', array($name), $options);
  43. $this->drush('user-information', array($name), $options + array('format' => 'json'));
  44. $output = $this->getOutputFromJSON('2');
  45. $this->assertEquals(1, $output->status, 'User is unblocked.');
  46. // user-add-role
  47. // first, create the fole since we use testing install profile.
  48. $this->drush('role-create', array('test role'), $options);
  49. $this->drush('user-add-role', array('test role', $name), $options);
  50. $this->drush('user-information', array($name), $options + array('format' => 'json'));
  51. $output = $this->getOutputFromJSON('2');
  52. $expected = (object) array(2 => $authenticated, 3 => 'test role');
  53. $this->assertEquals(json_encode($expected), json_encode($output->roles), 'User has test role.');
  54. // user-remove-role
  55. $this->drush('user-remove-role', array('test role', $name), $options);
  56. $this->drush('user-information', array($name), $options + array('format' => 'json'));
  57. $output = $this->getOutputFromJSON('2');
  58. $this->assertEquals(json_encode($obj_authenticated), json_encode($output->roles), 'User removed test role.');
  59. // user-password
  60. $newpass = 'newpass';
  61. $this->drush('user-password', array($name), $options + array('password' => $newpass));
  62. // There is no user_check_password in D6
  63. if (UNISH_DRUPAL_MAJOR_VERSION >= 7) {
  64. $eval = "require_once DRUSH_DRUPAL_CORE . '/' . variable_get('password_inc', 'includes/password.inc');";
  65. $eval .= "\$account = user_load_by_name('example');";
  66. $eval .= "print (string) user_check_password('$newpass', \$account)";
  67. $this->drush('php-eval', array($eval), $options);
  68. $output = $this->getOutput();
  69. $this->assertEquals('1', $output, 'User can login with new password.');
  70. }
  71. // user-login
  72. // Check if user-login on non-bootstrapped environment returns error.
  73. $this->drush('user-login', array(), array(), NULL, NULL, self::EXIT_ERROR);
  74. // Check user-login
  75. $user_login_options = $options + array('simulate' => TRUE, 'browser' => 'unish');
  76. // Collect full logs so we can check browser.
  77. $this->drush('user-login', array(), $user_login_options + array('backend' => NULL));
  78. $parsed = parse_backend_output($this->getOutput());
  79. $url = parse_url($parsed['output']);
  80. $this->assertContains('/user/reset/1', $url['path'], 'Login returned a reset URL for uid 1 by default');
  81. $browser = FALSE;
  82. foreach ($parsed['log'] as $key => $log) {
  83. if (strpos($log['message'], 'Opening browser unish at http://') === 0) {
  84. $browser = TRUE;
  85. }
  86. }
  87. $this->assertEquals($browser, TRUE, 'Correct browser opened at correct URL');
  88. // Check specific user and path argument.
  89. $uid = 2;
  90. $this->drush('user-login', array($name, 'node/add'), $user_login_options);
  91. $output = $this->getOutput();
  92. $url = parse_url($output);
  93. // user_pass_reset_url encodes the URL in D6, but not in D7 or D8
  94. $query = $url['query'];
  95. if (UNISH_DRUPAL_MAJOR_VERSION < 7) {
  96. $query = urldecode($query);
  97. }
  98. $this->assertContains('/user/reset/' . $uid, $url['path'], 'Login with user argument returned a valid reset URL');
  99. $this->assertEquals('destination=node/add', $query, 'Login included destination path in URL');
  100. // Check path used as only argument when using uid option.
  101. $this->drush('user-login', array('node/add'), $user_login_options + array('uid' => $uid));
  102. $output = $this->getOutput();
  103. $url = parse_url($output);
  104. $this->assertContains('/user/reset/' . $uid, $url['path'], 'Login with uid option returned a valid reset URL');
  105. $query = $url['query'];
  106. if (UNISH_DRUPAL_MAJOR_VERSION < 7) {
  107. $query = urldecode($query);
  108. }
  109. $this->assertEquals('destination=node/add', $query, 'Login included destination path in URL');
  110. // user-cancel
  111. // create content
  112. if (UNISH_DRUPAL_MAJOR_VERSION >= 7) {
  113. // create_node_types script does not work for D6
  114. $this->drush('php-script', array('create_node_types'), $options + array('script-path' => dirname(__FILE__) . '/resources'));
  115. $this->drush('php-eval', array($eval), $options);
  116. $eval = "\$node = (object) array('title' => 'foo', 'uid' => 2, 'type' => 'page',);";
  117. if (UNISH_DRUPAL_MAJOR_VERSION >= 8) {
  118. $eval .= " \$node = node_submit(entity_create('node', \$node));";
  119. }
  120. $eval .= " node_save(\$node);";
  121. $this->drush('php-eval', array($eval), $options);
  122. $this->drush('user-cancel', array($name), $options + array('delete-content' => NULL));
  123. $eval = 'print (string) user_load(2)';
  124. $this->drush('php-eval', array($eval), $options);
  125. $output = $this->getOutput();
  126. $this->assertEmpty($output, 'User was deleted');
  127. $eval = 'print (string) node_load(2)';
  128. $this->drush('php-eval', array($eval), $options);
  129. $output = $this->getOutput();
  130. $this->assertEmpty($output, 'Content was deleted');
  131. }
  132. }
  133. }