4d1a320330912067ada78a788b093ae2ffdd13a9
[yaffs-website] / vendor / drush / drush / tests / UserTest.php
1 <?php
2
3 namespace Unish;
4
5 use Webmozart\PathUtil\Path;
6
7 /**
8  *  @group slow
9  *  @group commands
10  */
11 class UserCase extends CommandUnishTestCase
12 {
13
14     const NAME = 'example';
15
16     public function setUp()
17     {
18         if (!$this->getSites()) {
19             $this->setUpDrupal(1, true);
20             $this->userCreate();
21         }
22     }
23
24     public function testBlockUnblock()
25     {
26         $this->drush('user-block', [self::NAME]);
27         $this->drush('user-information', [self::NAME], ['format' => 'json']);
28         $uid = 2;
29         $output = $this->getOutputFromJSON($uid);
30         $this->assertEquals(0, $output->user_status, 'User is blocked.');
31
32         // user-unblock
33         $this->drush('user-unblock', [self::NAME]);
34         $this->drush('user-information', [self::NAME], ['format' => 'json']);
35         $output = $this->getOutputFromJSON($uid);
36         $this->assertEquals(1, $output->user_status, 'User is unblocked.');
37     }
38
39     public function testUserRole()
40     {
41       // First, create the role since we use testing install profile.
42         $this->drush('role-create', ['test role']);
43         $this->drush('user-add-role', ['test role', self::NAME]);
44         $this->drush('user-information', [self::NAME], ['format' => 'json']);
45         $uid = 2;
46         $output = $this->getOutputFromJSON($uid);
47         $expected = ['authenticated', 'test role'];
48         $this->assertEquals($expected, array_values((array)$output->roles), 'User has test role.');
49
50       // user-remove-role
51         $this->drush('user-remove-role', ['test role', self::NAME]);
52         $this->drush('user-information', [self::NAME], ['format' => 'json']);
53         $output = $this->getOutputFromJSON($uid);
54         $expected = ['authenticated'];
55         $this->assertEquals($expected, array_values((array)$output->roles), 'User removed test role.');
56     }
57
58     public function testUserPassword()
59     {
60         $newpass = 'newpass';
61         $name = self::NAME;
62         $this->drush('user-password', [self::NAME, $newpass]);
63         $eval = "return \\Drupal::service('user.auth')->authenticate('$name', '$newpass');";
64         $this->drush('php-eval', [$eval]);
65         $output = $this->getOutput();
66         $this->assertEquals("2", $output, 'User can login with new password.');
67     }
68
69     public function testUserLogin()
70     {
71       // Check if user-login on a non-bootstrapped environment returns error.
72         $this->drush('user-login', [], ['uri' => 'OMIT'], null, null, self::EXIT_ERROR);
73
74       // Check user-login
75         $user_login_options = ['simulate' => null, 'browser' => 'unish'];
76       // Collect full logs so we can check browser.
77         $this->drush('user-login', [], $user_login_options + ['debug' => null]);
78         $logOutput = $this->getErrorOutput();
79         $url = parse_url($this->getOutput());
80         $this->assertContains('/user/reset/1', $url['path'], 'Login returned a reset URL for uid 1 by default');
81         $this->assertContains('Opening browser unish at http://', $logOutput);
82       // Check specific user with a path argument.
83         $uid = 2;
84         $this->drush('user-login', ['node/add'], $user_login_options + ['name' => self::NAME]);
85         $output = $this->getOutput();
86         $url = parse_url($output);
87         $query = $url['query'];
88         $this->assertContains('/user/reset/' . $uid, $url['path'], 'Login with user argument returned a valid reset URL');
89         $this->assertEquals('destination=node/add', $query, 'Login included destination path in URL');
90       // Check path used as only argument when using uid option.
91         $this->drush('user-login', ['node/add'], $user_login_options + ['name' => self::NAME]);
92         $output = $this->getOutput();
93         $url = parse_url($output);
94         $this->assertContains('/user/reset/' . $uid, $url['path'], 'Login with uid option returned a valid reset URL');
95         $query = $url['query'];
96         $this->assertEquals('destination=node/add', $query, 'Login included destination path in URL');
97     }
98
99     public function testUserCancel()
100     {
101         // Create a content entity type and enable its module.
102         $answers = [
103             'name' => 'UnishArticle',
104             'machine_name' => 'unish_article',
105             'package' => 'custom',
106             'version' => '8.x-1.0-dev',
107             'dependencies' => 'text',
108             'entity_type_label' => 'UnishArticle',
109             'entity_type_id' => 'unish_article',
110             'entity_base_path' => 'admin/content/unish_article',
111             'fieldable' => 'no',
112             'revisionable' => 'no',
113             'template' => 'no',
114             'access_controller' => 'no',
115             'title_base_field' => 'yes',
116             'status_base_field' => 'yes',
117             'created_base_field' => 'yes',
118             'changed_base_field' => 'yes',
119             'author_base_field' => 'yes',
120             'description_base_field' => 'no',
121             'rest_configuration' => 'no',
122         ];
123         $answers = json_encode($answers);
124         $original = getenv('SHELL_INTERACTIVE');
125         $this->setEnv(['SHELL_INTERACTIVE' => 1]);
126         $this->drush('generate', ['content-entity'], ['answers' => $answers, 'directory' => Path::join(self::webroot(), 'modules/contrib')]);
127         $this->setEnv(['SHELL_INTERACTIVE' => $original]);
128         $this->drush('pm-enable', ['text,unish_article']);
129         // Create one unish_article owned by our example user.
130         $this->drush('php-script', ['create_unish_articles'], ['script-path' => '../vendor/drush/drush/tests/resources']);
131         // Verify that content entity exists.
132         $code = "echo entity_load('unish_article', 1)->id()";
133         $this->drush('php-eval', [$code]);
134         $this->assertEquals(1, $this->getOutput());
135
136         // Cancel user and verify that the account is deleted.
137         $this->drush('user-cancel', [self::NAME], ['delete-content' => null]);
138         $this->drush('user-information', [self::NAME], ['fields' => 'user_status', 'format' => 'string'], null, null, self::EXIT_ERROR);
139
140         // Verify that the content is deleted.
141         // Sigh - only nodes actually honor the cancellation methods. @see node_user_cancel().
142         // $this->drush('php-eval', [$code], [], NULL, NULL, self::EXIT_ERROR);
143         // $output = $this->getOutput();
144         // $this->assertEquals('', $this->getOutput());
145     }
146
147     public function userCreate()
148     {
149         $this->drush('user-create', [self::NAME], ['password' => 'password', 'mail' => "example@example.com"]);
150         $this->drush('user-information', [self::NAME], ['format' => 'json']);
151         $uid = 2;
152         $output = $this->getOutputFromJSON($uid);
153         $this->assertEquals('example@example.com', $output->mail);
154         $this->assertEquals(self::NAME, $output->name);
155         $this->assertEquals(1, $output->user_status, 'Newly created user is Active.');
156         $expected = ['authenticated'];
157         $this->assertEquals($expected, array_values((array)$output->roles), 'Newly created user has one role.');
158     }
159 }