forked from ezsystems/repository-forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserRegistrationContext.php
362 lines (317 loc) · 12.1 KB
/
UserRegistrationContext.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
/**
* This file is part of the ezplatform package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\RepositoryForms\Features\Context;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\PyStringNode;
use Behat\MinkExtension\Context\RawMinkContext;
use eZ\Bundle\EzPublishCoreBundle\Features\Context\YamlConfigurationContext;
use eZ\Publish\API\Repository\Repository;
use eZ\Publish\API\Repository\Values\User\Role;
use eZ\Publish\API\Repository\Values\User\User;
use eZ\Publish\API\Repository\Values\User\UserGroup;
use eZ\Publish\Core\Repository\Values\User\RoleCreateStruct;
use EzSystems\PlatformBehatBundle\Context\RepositoryContext;
use PHPUnit\Framework\Assert as Assertion;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;
class UserRegistrationContext extends RawMinkContext implements Context, SnippetAcceptingContext
{
use RepositoryContext;
/** @var string Regex matching the way the Twig template name is inserted in debug mode */
const TWIG_DEBUG_STOP_REGEX = '<!-- STOP .*%s.* -->';
private static $password = 'publish';
private static $language = 'eng-GB';
private static $groupId = 4;
private $registrationUsername;
/**
* Used to cover registration group customization.
* @var UserGroup
*/
private $customUserGroup;
/**
* @var YamlConfigurationContext
*/
private $yamlConfigurationContext;
/**
* @injectService $repository @ezpublish.api.repository
*/
public function __construct(Repository $repository)
{
$this->setRepository($repository);
}
/** @BeforeScenario */
public function gatherContexts(BeforeScenarioScope $scope)
{
$this->yamlConfigurationContext = $scope->getEnvironment()->getContext(
'eZ\Bundle\EzPublishCoreBundle\Features\Context\YamlConfigurationContext'
);
}
/**
* @Given /^I do not have the user\/register policy$/
*/
public function loginAsUserWithoutRegisterPolicy()
{
$role = $this->createRegistrationRole(false);
$user = $this->createUserWithRole($role);
$this->loginAs($user);
}
/**
* @Given /^I do have the user\/register policy$/
*/
public function loginAsUserWithUserRegisterPolicy()
{
$role = $this->createRegistrationRole(true);
$user = $this->createUserWithRole($role);
$this->loginAs($user);
}
/**
* Creates a user for registration testing, and assigns it the role $role.
*
* @param Role $role
*
* @return User
*/
private function createUserWithRole(Role $role)
{
$userService = $this->getRepository()->getUserService();
$username = uniqid($role->identifier);
$createStruct = $userService->newUserCreateStruct(
$username,
$username . '@example.com',
self::$password,
'eng-GB'
);
$createStruct->setField('first_name', $username);
$createStruct->setField('last_name', 'The first');
$user = $userService->createUser($createStruct, [$userService->loadUserGroup(self::$groupId)]);
$this->getRepository()->getRoleService()->assignRoleToUser($role, $user);
return $user;
}
/**
* Creates a role for user registration test.
*
* It always has the minimal set of policies to operate (user/login and content/read).
*
* @param bool $withUserRegisterPolicy Determines if the role gets the user/register policy
*
* @return Role
*/
private function createRegistrationRole($withUserRegisterPolicy = true)
{
$roleIdentifier = uniqid(
'anonymous_role_' . ($withUserRegisterPolicy ? 'with' : 'without') . '_register'
);
$roleService = $this->getRepository()->getRoleService();
$roleCreateStruct = new RoleCreateStruct(['identifier' => $roleIdentifier]);
$roleCreateStruct->addPolicy($roleService->newPolicyCreateStruct('user', 'login'));
$roleCreateStruct->addPolicy($roleService->newPolicyCreateStruct('content', 'read'));
if ($withUserRegisterPolicy === true) {
$roleCreateStruct->addPolicy($roleService->newPolicyCreateStruct('user', 'register'));
}
$roleService->publishRoleDraft($roleService->createRole($roleCreateStruct));
return $roleService->loadRoleByIdentifier($roleIdentifier);
}
/**
* @Then /^I see an error message saying that I can not register$/
*/
public function iSeeAnErrorMessageSayingThatICanNotRegister()
{
$this->assertSession()->pageTextContains('You are not allowed to register a new account');
}
/**
* @param User $user
* @throws \Behat\Mink\Exception\ElementNotFoundException
*/
private function loginAs(User $user)
{
$this->visitPath('/login');
$page = $this->getSession()->getPage();
$page->fillField('username', $user->login);
$page->fillField('password', self::$password);
$page->findButton('Login')->press();
$this->assertSession()->statusCodeEquals(200);
}
/**
* @Then /^I can see the registration form$/
*/
public function iCanSeeTheRegistrationForm()
{
$this->assertSession()->pageTextNotContains('You are not allowed to register a new account');
$this->assertSession()->elementExists('css', 'form[name=ezrepoforms_user_register]');
}
/**
* @Given /^it matches the structure of the configured registration user Content Type$/
*/
public function itMatchesTheStructureOfTheConfiguredRegistrationUserContentType()
{
$userContentType = $this->getRepository()->getContentTypeService()
->loadContentTypeByIdentifier('user');
foreach ($userContentType->getFieldDefinitions() as $fieldDefinition) {
$this->assertSession()->elementExists(
'css',
sprintf(
'div.ez-field-edit-%s',
$fieldDefinition->fieldTypeIdentifier
)
);
/** @todo It should also check if there is corresponding input created once all types are implemented */
}
}
/**
* @Given /^it has a register button$/
*/
public function itHasARegisterButton()
{
$this->assertSession()->elementExists(
'css',
'form[name=ezrepoforms_user_register] button[type=submit]'
);
}
/**
* @When /^I fill in the form with valid values$/
*/
public function iFillInTheFormWithValidValues()
{
$page = $this->getSession()->getPage();
$this->registrationUsername = uniqid('registration_username_');
$page->fillField('ezrepoforms_user_register[fieldsData][first_name][value]', 'firstname');
$page->fillField('ezrepoforms_user_register[fieldsData][last_name][value]', 'firstname');
$page->fillField('ezrepoforms_user_register[fieldsData][user_account][value][username]', $this->registrationUsername);
$page->fillField('ezrepoforms_user_register[fieldsData][user_account][value][email]', $this->registrationUsername . '@example.com');
$page->fillField('ezrepoforms_user_register[fieldsData][user_account][value][password][first]', self::$password);
$page->fillField('ezrepoforms_user_register[fieldsData][user_account][value][password][second]', self::$password);
}
/**
* @When /^I click on the register button$/
*/
public function iClickOnTheRegisterButton()
{
$this->getSession()->getPage()->pressButton('ezrepoforms_user_register[publish]');
$this->assertSession()->statusCodeEquals(200);
}
/**
* @Then /^I am on the registration confirmation page$/
*/
public function iAmOnTheRegistrationConfirmationPage()
{
$this->assertSession()->addressEquals('/register-confirm');
}
/**
* @Given /^I see a registration confirmation message$/
*/
public function iSeeARegistrationConfirmationMessage()
{
$this->assertSession()->pageTextContains('Your account has been created');
}
/**
* @Given /^the user account has been created$/
*/
public function theUserAccountHasBeenCreated()
{
$this->getRepository()->getUserService()->loadUserByLogin($this->registrationUsername);
}
/**
* @Given /^a User Group$/
*/
public function createUserGroup()
{
$userService = $this->getRepository()->getUserService();
$groupCreateStruct = $userService->newUserGroupCreateStruct(self::$language);
$groupCreateStruct->setField('name', uniqid('User registration group '));
$this->customUserGroup = $userService->createUserGroup(
$groupCreateStruct,
$userService->loadUserGroup(self::$groupId)
);
}
/**
* @Given /^the following user registration group configuration:$/
*/
public function addUserRegistrationConfiguration(PyStringNode $extraConfigurationString)
{
$extraConfigurationString = str_replace(
'<userGroupContentId>',
$this->customUserGroup->id,
$extraConfigurationString
);
$this->yamlConfigurationContext->addConfiguration(Yaml::parse($extraConfigurationString));
}
/**
* @When /^I register a user account$/
*/
public function iRegisterAUserAccount()
{
$this->loginAsUserWithUserRegisterPolicy();
$this->visitPath('/register');
$this->assertSession()->statusCodeEquals(200);
$this->iFillInTheFormWithValidValues();
$this->iClickOnTheRegisterButton();
$this->iAmOnTheRegistrationConfirmationPage();
$this->iSeeARegistrationConfirmationMessage();
}
/**
* @Then /^the user is created in this user group$/
*/
public function theUserIsCreatedInThisUserGroup()
{
$userService = $this->getRepository()->getUserService();
$user = $userService->loadUserByLogin($this->registrationUsername);
$userGroups = $userService->loadUserGroupsOfUser($user);
Assertion::assertEquals(
$this->customUserGroup->id,
$userGroups[0]->id
);
}
/**
* @Given /^the following user registration templates configuration:$/
*/
public function addRegistrationTemplatesConfiguration(PyStringNode $string)
{
$this->yamlConfigurationContext->addConfiguration(Yaml::parse($string));
}
/**
* @Given /^the following template in "([^"]*)":$/
*/
public function createTemplateAt($path, PyStringNode $contents)
{
$fs = new Filesystem();
$fs->mkdir(dirname($path));
$fs->dumpFile($path, $contents);
}
/**
* @Then /^the confirmation page is rendered using the "([^"]*)" template$/
* @Then /^the form is rendered using the "([^"]*)" template$/
*
* @param string $template
* The template path to look for.
* If relative to app/Resources/views (example: user/register.html.twig),
* the path is checked with the :path:file.html.twig syntax as well.
*/
public function thePageIsRenderedUsingTheTemplateConfiguredIn($template)
{
$html = $this->getSession()->getPage()->getOuterHtml();
$searchedPattern = sprintf(self::TWIG_DEBUG_STOP_REGEX, preg_quote($template, null));
$found = preg_match($searchedPattern, $html) === 1;
if (!$found && strpos($template, ':') === false) {
$alternativeTemplate = sprintf(
':%s:%s',
dirname($template),
basename($template)
);
$searchedPattern = sprintf(self::TWIG_DEBUG_STOP_REGEX, preg_quote($alternativeTemplate, null));
$found = preg_match($searchedPattern, $html) === 1;
}
Assertion::assertTrue(
$found,
"Couldn't find $template " .
(isset($alternativeTemplate) ? "nor $alternativeTemplate " : ' ') .
"in HTML:\n\n$html"
);
}
}