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