forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserTest.php
204 lines (178 loc) · 9.38 KB
/
UserTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
namespace Unish;
use Webmozart\PathUtil\Path;
/**
* @group slow
* @group commands
*/
class UserCase extends CommandUnishTestCase
{
const NAME = 'example';
const MAIL = 'example@example.com';
public function setup(): void
{
if (!$this->getSites()) {
$this->setUpDrupal(1, true);
$this->userCreate();
}
}
public function testBlockUnblock()
{
$uid = 2;
$this->drush('user-block', [self::NAME]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$this->assertEquals(0, $output['user_status'], 'User is blocked.');
// user-unblock
$this->drush('user-unblock', [self::NAME]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$this->assertEquals(1, $output['user_status'], 'User is unblocked.');
// user-block user by uid.
$this->drush('user-block', [], ['uid' => $uid]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$this->assertEquals(0, $output['user_status'], 'User (id) is blocked.');
$this->drush('user-unblock', [], ['uid' => $uid]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$this->assertEquals(1, $output['user_status'], 'User (id) is unblocked.');
// user-block user by mail.
$this->drush('user-block', [], ['mail' => self::MAIL]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$this->assertEquals(0, $output['user_status'], 'User (mail) is blocked.');
$this->drush('user-unblock', [], ['uid' => $uid]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$this->assertEquals(1, $output['user_status'], 'User (mail) is unblocked.');
}
public function testUserRole()
{
$uid = 2;
// First, create the role since we use testing install profile.
$this->drush('role-create', ['test role']);
$this->drush('user-add-role', ['test role', self::NAME]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$expected = ['authenticated', 'test role'];
$this->assertEquals($expected, array_values($output['roles']), 'User has test role.');
// user-remove-role
$this->drush('user-remove-role', ['test role', self::NAME]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$expected = ['authenticated'];
$this->assertEquals($expected, array_values($output['roles']), 'User removed test role.');
// user-add-role by uid.
$this->drush('user-add-role', ['test role'], ['uid' => $uid]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$expected = ['authenticated', 'test role'];
$this->assertEquals($expected, array_values($output['roles']), 'User (id) has test role.');
// user-remove-role by uid
$this->drush('user-remove-role', ['test role'], ['uid' => $uid]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$expected = ['authenticated'];
$this->assertEquals($expected, array_values($output['roles']), 'User (id) removed test role.');
// user-add-role by mail.
$this->drush('user-add-role', ['test role'], ['mail' => self::MAIL]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$expected = ['authenticated', 'test role'];
$this->assertEquals($expected, array_values($output['roles']), 'User (mail) has test role.');
// user-remove-role by mail.
$this->drush('user-remove-role', ['test role'], ['mail' => self::MAIL]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$output = $this->getOutputFromJSON($uid);
$expected = ['authenticated'];
$this->assertEquals($expected, array_values($output['roles']), 'User (mail) removed test role.');
}
public function testUserPassword()
{
$newpass = 'newpass';
$name = self::NAME;
$this->drush('user:password', [self::NAME, $newpass]);
$eval = "return Drupal::service(\"user.auth\")->authenticate(\"$name\", \"$newpass\");";
$this->drush('php:eval', [$eval]);
$output = $this->getOutput();
$this->assertEquals("2", $output, 'User can login with new password.');
}
public function testUserLoginNoBootstrappedSite()
{
$this->markTestSkipped('TODO: @none should prevent selection of site at cwd');
// Check if user-login on a non-bootstrapped environment returns error.
$this->drush('user-login', [], [], '@none', null, self::EXIT_ERROR);
}
public function testUserLogin()
{
// Check user-login
$user_login_options = ['simulate' => null, 'browser' => 'unish'];
// Collect full logs so we can check browser.
$this->drush('user-login', [], $user_login_options + ['debug' => null]);
$logOutput = $this->getErrorOutput();
$url = parse_url($this->getOutput());
$this->assertStringContainsString('/user/reset/1', $url['path'], 'Login returned a reset URL for uid 1 by default');
$this->assertStringContainsString('Opening browser unish at http://', $logOutput);
// Check specific user with a path argument.
$uid = 2;
$this->drush('user-login', ['node/add'], $user_login_options + ['name' => self::NAME]);
$output = $this->getOutput();
$url = parse_url($output);
$query = $url['query'];
$this->assertStringContainsString('/user/reset/' . $uid, $url['path'], 'Login with user argument returned a valid reset URL');
$this->assertEquals('destination=node/add', $query, 'Login included destination path in URL');
// Check path used as only argument when using uid option.
$this->drush('user-login', ['node/add'], $user_login_options + ['name' => self::NAME]);
$output = $this->getOutput();
$url = parse_url($output);
$this->assertStringContainsString('/user/reset/' . $uid, $url['path'], 'Login with uid option returned a valid reset URL');
$query = $url['query'];
$this->assertEquals('destination=node/add', $query, 'Login included destination path in URL');
// Test specific user by uid.
$uid = 2;
$this->drush('user-login', [], $user_login_options + ['uid' => $uid]);
$output = $this->getOutput();
$url = parse_url($output);
$this->assertStringContainsString('/user/reset/' . $uid, $url['path'], 'Login with uid option returned a valid reset URL');
// Test specific user by mail.
$uid = 2;
$mail = self::MAIL;
$this->drush('user-login', [], $user_login_options + ['mail' => $mail]);
$output = $this->getOutput();
$url = parse_url($output);
$this->assertStringContainsString('/user/reset/' . $uid, $url['path'], 'Login with mail option returned a valid reset URL');
}
public function testUserCancel()
{
CreateEntityType::createContentEntity($this);
$this->drush('pm-enable', ['text,unish_article']);
$this->drush('php:script', ['create_unish_article_bundles'], ['script-path' => Path::join(__DIR__, 'resources')]);
// Create one unish_article owned by our example user.
$this->drush('php-script', ['create_unish_articles'], ['script-path' => Path::join(__DIR__, 'resources')]);
// Verify that content entity exists.
$code = "echo Drupal::entityTypeManager()->getStorage('unish_article')->load(1)->id()";
$this->drush('php-eval', [$code]);
$this->assertEquals(1, $this->getOutput());
// Cancel user and verify that the account is deleted.
$this->drush('user-cancel', [self::NAME], ['delete-content' => null]);
$this->drush('user-information', [self::NAME], ['fields' => 'user_status', 'format' => 'string'], null, null, self::EXIT_ERROR);
// Verify that the content is deleted.
// Sigh - only nodes actually honor the cancellation methods. @see node_user_cancel().
// $this->drush('php-eval', [$code], [], NULL, NULL, self::EXIT_ERROR);
// $output = $this->getOutput();
// $this->assertEquals('', $this->getOutput());
}
public function userCreate()
{
$this->drush('user-create', [self::NAME], ['password' => 'password', 'mail' => self::MAIL]);
$this->drush('user-information', [self::NAME], ['format' => 'json']);
$uid = 2;
$output = $this->getOutputFromJSON($uid);
$this->assertEquals(self::MAIL, $output['mail']);
$this->assertEquals(self::NAME, $output['name']);
$this->assertEquals(1, $output['user_status'], 'Newly created user is Active.');
$expected = ['authenticated'];
$this->assertEquals($expected, array_values($output['roles']), 'Newly created user has one role.');
}
}