|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @copyright 2013 - 2017 Cross Solution <http://cross-solution.de> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Yawik\Behat; |
|
11
|
|
|
|
|
12
|
|
|
use Auth\Entity\User as User; |
|
13
|
|
|
use Auth\Entity\UserInterface; |
|
14
|
|
|
use Auth\Repository\User as UserRepository; |
|
15
|
|
|
use Auth\Service\Register; |
|
16
|
|
|
use Behat\Behat\Context\Context; |
|
17
|
|
|
use Behat\Behat\Hook\Scope\BeforeScenarioScope; |
|
18
|
|
|
use Behat\Gherkin\Node\TableNode; |
|
19
|
|
|
use Behat\MinkExtension\Context\MinkContext; |
|
20
|
|
|
use Core\Entity\Permissions; |
|
21
|
|
|
use Doctrine\Common\Util\Inflector; |
|
22
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
23
|
|
|
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs; |
|
24
|
|
|
use Doctrine\ODM\MongoDB\Events; |
|
25
|
|
|
use Organizations\Entity\Organization; |
|
26
|
|
|
use Organizations\Entity\OrganizationName; |
|
27
|
|
|
use Organizations\Repository\Organization as OrganizationRepository; |
|
28
|
|
|
use Yawik\Behat\Exception\FailedExpectationException; |
|
29
|
|
|
|
|
30
|
|
|
class UserContext implements Context |
|
31
|
|
|
{ |
|
32
|
|
|
use CommonContextTrait; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var User[] |
|
36
|
|
|
*/ |
|
37
|
|
|
static private $users = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var UserRepository |
|
41
|
|
|
*/ |
|
42
|
|
|
static private $userRepo; |
|
43
|
|
|
|
|
44
|
|
|
private $socialLoginInfo = []; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var DocumentManager |
|
48
|
|
|
*/ |
|
49
|
|
|
static private $dm; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var UserInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
private $loggedInUser; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var Organization |
|
58
|
|
|
*/ |
|
59
|
|
|
private $mainOrganization; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var User |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $currentUser; |
|
65
|
|
|
|
|
66
|
|
|
public function __construct($parameters=[]) |
|
67
|
|
|
{ |
|
68
|
|
|
$defaultLoginInfo = [ |
|
69
|
|
|
'facebook' => [ |
|
70
|
|
|
'email' => getenv('FACEBOOK_USER_EMAIL'), |
|
71
|
|
|
'pass' => getenv('FACEBOOK_USER_PASSWORD') |
|
72
|
|
|
], |
|
73
|
|
|
'linkedin' => [ |
|
74
|
|
|
'session_key-login' => getenv('LINKEDIN_USER_EMAIL'), |
|
75
|
|
|
'session_password-login' => getenv('LINKEDIN_USER_PASSWORD') |
|
76
|
|
|
], |
|
77
|
|
|
]; |
|
78
|
|
|
$socialLoginConfig = isset($parameters['social_login_info']) ? $parameters['social_login_info']:[]; |
|
79
|
|
|
$this->socialLoginInfo = array_merge($defaultLoginInfo,$socialLoginConfig); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Empty all data every each tests |
|
84
|
|
|
* |
|
85
|
|
|
* @AfterSuite |
|
86
|
|
|
*/ |
|
87
|
|
|
static public function tearDown() |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
$dm = static::$dm; |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
$documents = [ |
|
92
|
|
|
'Applications\Entity\Application', |
|
93
|
|
|
'Cv\Entity\Cv', |
|
94
|
|
|
'Jobs\Entity\Job', |
|
95
|
|
|
'Organizations\Entity\Organization', |
|
96
|
|
|
'Auth\Entity\User', |
|
97
|
|
|
'Jobs\Entity\Category', |
|
98
|
|
|
'Auth\Entity\UserImage', |
|
99
|
|
|
'Organizations\Entity\OrganizationName', |
|
100
|
|
|
]; |
|
101
|
|
|
foreach($documents as $document){ |
|
102
|
|
|
$dm->createQueryBuilder($document) |
|
103
|
|
|
->remove() |
|
104
|
|
|
->getQuery() |
|
105
|
|
|
->execute() |
|
106
|
|
|
; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @BeforeScenario |
|
112
|
|
|
* @param BeforeScenarioScope $scope |
|
113
|
|
|
*/ |
|
114
|
|
|
public function beforeScenario(BeforeScenarioScope $scope) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->minkContext = $scope->getEnvironment()->getContext(MinkContext::class); |
|
|
|
|
|
|
117
|
|
|
$this->coreContext = $scope->getEnvironment()->getContext(CoreContext::class); |
|
|
|
|
|
|
118
|
|
|
static::$userRepo = $this->getUserRepository(); |
|
|
|
|
|
|
119
|
|
|
static::$dm = $this->getUserRepository()->getDocumentManager(); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @When I fill in login form with :provider user |
|
124
|
|
|
*/ |
|
125
|
|
|
public function iSignInWithSocialUser($provider) |
|
126
|
|
|
{ |
|
127
|
|
|
$provider = strtolower($provider); |
|
128
|
|
|
$mink = $this->minkContext; |
|
129
|
|
|
foreach($this->socialLoginInfo[$provider] as $field=>$value){ |
|
130
|
|
|
$mink->fillField($field,$value); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @Given I am logged in as a recruiter |
|
136
|
|
|
* @Given I am logged in as a recruiter with :organization as organization |
|
137
|
|
|
*/ |
|
138
|
|
|
public function iAmLoggedInAsARecruiter($organization=null) |
|
139
|
|
|
{ |
|
140
|
|
|
$user = $this->thereIsAUserIdentifiedBy( |
|
141
|
|
|
'[email protected]', |
|
142
|
|
|
'test',User::ROLE_RECRUITER, |
|
143
|
|
|
'Test Recruiter', |
|
144
|
|
|
$organization |
|
145
|
|
|
); |
|
146
|
|
|
$this->startLogin($user,'test'); |
|
|
|
|
|
|
147
|
|
|
if(!is_null($organization)){ |
|
148
|
|
|
$this->iHaveMainOrganization($user,$organization); |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @Given I don't have :login user |
|
154
|
|
|
* @param string $login |
|
155
|
|
|
*/ |
|
156
|
|
|
public function iDonTHaveUser($login) |
|
157
|
|
|
{ |
|
158
|
|
|
$repo = $this->getUserRepository(); |
|
159
|
|
|
$user=$repo->findByLogin($login); |
|
160
|
|
|
if($user instanceof UserInterface){ |
|
161
|
|
|
$repo->remove($user,true); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @Given I have a :role with the following: |
|
167
|
|
|
* @param $role |
|
168
|
|
|
* @param TableNode $fields |
|
169
|
|
|
*/ |
|
170
|
|
|
public function iHaveUserWithTheFollowing($role,TableNode $fields) |
|
171
|
|
|
{ |
|
172
|
|
|
$normalizedFields = [ |
|
173
|
|
|
'login' => '[email protected]', |
|
174
|
|
|
'fullname' => 'Test Login', |
|
175
|
|
|
'role' => User::ROLE_USER, |
|
176
|
|
|
'password' => 'test', |
|
177
|
|
|
'organization' => 'Cross Solution' |
|
178
|
|
|
]; |
|
179
|
|
|
foreach($fields->getRowsHash() as $field=>$value){ |
|
180
|
|
|
$field = Inflector::camelize($field); |
|
181
|
|
|
$normalizedFields[$field] = $value; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
$this->thereIsAUserIdentifiedBy( |
|
185
|
|
|
$normalizedFields['login'], |
|
186
|
|
|
$normalizedFields['password'], |
|
187
|
|
|
$role, |
|
188
|
|
|
$normalizedFields['fullname'], |
|
189
|
|
|
$normalizedFields['organization'] |
|
190
|
|
|
); |
|
191
|
|
|
|
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @Given I am logged in as an administrator |
|
196
|
|
|
*/ |
|
197
|
|
|
public function iAmLoggedInAsAnAdmin() |
|
198
|
|
|
{ |
|
199
|
|
|
$user = $this->thereIsAUserIdentifiedBy('[email protected]','test',User::ROLE_ADMIN); |
|
200
|
|
|
$this->startLogin($user,'test'); |
|
|
|
|
|
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
private function startLogin(UserInterface $user, $password) |
|
204
|
|
|
{ |
|
205
|
|
|
$currentUser = $this->currentUser; |
|
206
|
|
|
if(!is_object($currentUser) || $user->getId()!=$currentUser->getId()){ |
|
207
|
|
|
$this->iWantToLogIn(); |
|
208
|
|
|
$this->iSpecifyTheUsernameAs($user->getLogin()); |
|
209
|
|
|
$this->iSpecifyThePasswordAs($password); |
|
210
|
|
|
$this->iLogIn(); |
|
211
|
|
|
$this->currentUser = $user; |
|
|
|
|
|
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @return UserRepository |
|
217
|
|
|
*/ |
|
218
|
|
|
public function getUserRepository() |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->coreContext->getRepositories()->get('Auth\Entity\User'); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @Given there is a user :email identified by :password |
|
225
|
|
|
*/ |
|
226
|
|
|
public function thereIsAUserIdentifiedBy($email, $password,$role=User::ROLE_RECRUITER,$fullname="Test Recruiter",$organization=null) |
|
227
|
|
|
{ |
|
228
|
|
|
$repo = $this->getUserRepository(); |
|
229
|
|
|
if(!is_object($user=$repo->findByEmail($email))){ |
|
230
|
|
|
$user = $this->createUser($email,$password,$role,$fullname,$organization); |
|
|
|
|
|
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
if(!is_null($organization)){ |
|
234
|
|
|
$this->iHaveMainOrganization($user,$organization); |
|
|
|
|
|
|
235
|
|
|
} |
|
236
|
|
|
$this->addCreatedUser($user); |
|
|
|
|
|
|
237
|
|
|
$repo->getDocumentManager()->refresh($user); |
|
238
|
|
|
return $user; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @param $email |
|
243
|
|
|
* @param $password |
|
244
|
|
|
* @param $username |
|
245
|
|
|
* @param string $fullname |
|
246
|
|
|
* @param string $role |
|
247
|
|
|
* |
|
248
|
|
|
* @return \Auth\Entity\UserInterface |
|
|
|
|
|
|
249
|
|
|
*/ |
|
250
|
|
|
public function createUser($email,$password,$role=User::ROLE_RECRUITER,$fullname="Test Recruiter") |
|
251
|
|
|
{ |
|
252
|
|
|
/* @var Register $service */ |
|
253
|
|
|
/* @var User $user */ |
|
254
|
|
|
$repo = $this->getUserRepository(); |
|
255
|
|
|
$user = $repo->create([]); |
|
256
|
|
|
$user->setLogin($email); |
|
257
|
|
|
$user->setPassword($password); |
|
258
|
|
|
$user->setRole($role); |
|
259
|
|
|
$settings = $user->getSettings('Applications'); |
|
|
|
|
|
|
260
|
|
|
|
|
261
|
|
|
$expFullName = explode(' ',$fullname); |
|
262
|
|
|
$info = $user->getInfo(); |
|
263
|
|
|
$info->setFirstName(array_shift($expFullName)); |
|
264
|
|
|
$info->setLastName(count($expFullName)>0 ? implode(' ',$expFullName):''); |
|
265
|
|
|
$info->setEmail($email); |
|
266
|
|
|
$info->setEmailVerified(true); |
|
267
|
|
|
$repo->store($user); |
|
268
|
|
|
$repo->getDocumentManager()->refresh($user); |
|
269
|
|
|
|
|
270
|
|
|
$eventArgs = new LifecycleEventArgs($user, $repo->getDocumentManager()); |
|
271
|
|
|
$repo->getDocumentManager()->getEventManager()->dispatchEvent( |
|
272
|
|
|
Events::postLoad, |
|
273
|
|
|
$eventArgs |
|
274
|
|
|
); |
|
275
|
|
|
/* @var \Core\EventManager\EventManager $events */ |
|
276
|
|
|
/* @var \Auth\Listener\Events\AuthEvent $event */ |
|
277
|
|
|
//@TODO: [Behat] event not working in travis |
|
278
|
|
|
//$events = $this->coreContext->getEventManager(); |
|
|
|
|
|
|
279
|
|
|
//$event = $events->getEvent(AuthEvent::EVENT_USER_REGISTERED, $this); |
|
|
|
|
|
|
280
|
|
|
//$event->setUser($user); |
|
|
|
|
|
|
281
|
|
|
//$events->triggerEvent($event); |
|
|
|
|
|
|
282
|
|
|
return $user; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* @When I have :organization as my main organization |
|
287
|
|
|
* @param $orgName |
|
288
|
|
|
*/ |
|
289
|
|
|
public function iHaveMainOrganization(UserInterface $user,$orgName) |
|
290
|
|
|
{ |
|
291
|
|
|
/* @var $repoOrganization OrganizationRepository */ |
|
292
|
|
|
$repoOrganization = $this->coreContext->getRepositories()->get('Organizations/Organization'); |
|
293
|
|
|
$result = $repoOrganization->findByName($orgName); |
|
294
|
|
|
$organization = count($result) > 0 ? $result[0]:null; |
|
295
|
|
View Code Duplication |
if(!$organization instanceof Organization){ |
|
|
|
|
|
|
296
|
|
|
$organization = new Organization(); |
|
297
|
|
|
$organizationName = new OrganizationName($orgName); |
|
298
|
|
|
$organization->setOrganizationName($organizationName); |
|
299
|
|
|
} |
|
300
|
|
|
$organization->setProfileSetting(Organization::PROFILE_ALWAYS_ENABLE); |
|
301
|
|
|
$permissions = $organization->getPermissions(); |
|
302
|
|
|
$permissions->grant($user,Permissions::PERMISSION_ALL); |
|
303
|
|
|
|
|
304
|
|
|
$organization->setUser($user); |
|
305
|
|
|
$repoOrganization->store($organization); |
|
306
|
|
|
$repoOrganization->getDocumentManager()->refresh($organization); |
|
307
|
|
|
|
|
308
|
|
|
$this->mainOrganization = $organization; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* @return Organization |
|
313
|
|
|
*/ |
|
314
|
|
|
public function getMainOrganization() |
|
315
|
|
|
{ |
|
316
|
|
|
return $this->mainOrganization; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* @When I want to log in |
|
322
|
|
|
*/ |
|
323
|
|
|
public function iWantToLogIn() |
|
324
|
|
|
{ |
|
325
|
|
|
$session = $this->minkContext->getSession(); |
|
326
|
|
|
$url = $this->generateUrl('lang/auth'); |
|
327
|
|
|
$session->visit($url); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
/** |
|
331
|
|
|
* @When I specify the username as :username |
|
332
|
|
|
*/ |
|
333
|
|
|
public function iSpecifyTheUsernameAs($username) |
|
334
|
|
|
{ |
|
335
|
|
|
$this->minkContext->fillField('Login name',$username); |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* @When I specify the password as :password |
|
340
|
|
|
*/ |
|
341
|
|
|
public function iSpecifyThePasswordAs($password) |
|
342
|
|
|
{ |
|
343
|
|
|
$this->minkContext->fillField('Password',$password); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* @Given I am logged in as :username identified by :password |
|
348
|
|
|
*/ |
|
349
|
|
|
public function iAmLoggedInAsIdentifiedBy($username, $password) |
|
350
|
|
|
{ |
|
351
|
|
|
$repo = $this->getUserRepository(); |
|
352
|
|
|
$user = $repo->findByLogin($username); |
|
353
|
|
|
|
|
354
|
|
|
if(!$user instanceof User){ |
|
355
|
|
|
throw new \Exception(sprintf('There is no user with this login: "%s"',$username)); |
|
356
|
|
|
} |
|
357
|
|
|
$this->iWantToLogIn(); |
|
358
|
|
|
$this->iSpecifyTheUsernameAs($username); |
|
359
|
|
|
$this->iSpecifyThePasswordAs($password); |
|
360
|
|
|
$this->iLogIn(); |
|
361
|
|
|
$this->currentUser = $user; |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* @When I log in |
|
366
|
|
|
*/ |
|
367
|
|
|
public function iLogIn() |
|
368
|
|
|
{ |
|
369
|
|
|
$this->minkContext->pressButton('login'); |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* @When I press logout link |
|
374
|
|
|
*/ |
|
375
|
|
|
public function iPressLogoutLink() |
|
376
|
|
|
{ |
|
377
|
|
|
$url = $this->generateUrl('auth-logout'); |
|
378
|
|
|
$this->visit($url); |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
/** |
|
382
|
|
|
* @Given I log in with username :username and password :password |
|
383
|
|
|
*/ |
|
384
|
|
|
public function iLogInWith($username, $password) |
|
385
|
|
|
{ |
|
386
|
|
|
$repo = $this->getUserRepository(); |
|
387
|
|
|
$user = $repo->findByLogin($username); |
|
388
|
|
|
$this->iWantToLogIn(); |
|
389
|
|
|
$this->iSpecifyTheUsernameAs($username); |
|
390
|
|
|
$this->iSpecifyThePasswordAs($password); |
|
391
|
|
|
$this->iLogIn(); |
|
392
|
|
|
$this->loggedInUser = $user; |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
/** |
|
396
|
|
|
* @When I go to profile page |
|
397
|
|
|
*/ |
|
398
|
|
|
public function iGoToProfilePage() |
|
399
|
|
|
{ |
|
400
|
|
|
$url = $this->generateUrl('lang/my'); |
|
401
|
|
|
$this->visit($url); |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
/** |
|
405
|
|
|
* @Given there is a user with the following: |
|
406
|
|
|
*/ |
|
407
|
|
|
public function thereIsAUserWithTheFollowing(TableNode $table) |
|
408
|
|
|
{ |
|
409
|
|
|
$repo = $this->getUserRepository(); |
|
410
|
|
|
$data = $table->getRowsHash(); |
|
411
|
|
|
$email = isset($data['email']) ? $data['email']:'[email protected]'; |
|
412
|
|
|
$password = isset($data['password']) ? $data['password']:'test'; |
|
413
|
|
|
$fullname = isset($data['fullname']) ? $data['fullname']:'Test User'; |
|
414
|
|
|
$role = isset($data['role']) ? $data['role']:User::ROLE_RECRUITER; |
|
415
|
|
|
|
|
416
|
|
|
if(!is_object($user=$repo->findByLogin($email))){ |
|
417
|
|
|
$user = $this->createUser($email,$password,$role,$fullname); |
|
418
|
|
|
} |
|
419
|
|
|
$this->currentUser = $user; |
|
|
|
|
|
|
420
|
|
|
$this->addCreatedUser($user); |
|
|
|
|
|
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
private function addCreatedUser(UserInterface $user) |
|
424
|
|
|
{ |
|
425
|
|
|
if(!in_array($user,static::$users)){ |
|
|
|
|
|
|
426
|
|
|
static::$users[] = $user; |
|
|
|
|
|
|
427
|
|
|
} |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
/** |
|
431
|
|
|
* @When I want to change my password |
|
432
|
|
|
*/ |
|
433
|
|
|
public function iWantToChangeMyPassword() |
|
434
|
|
|
{ |
|
435
|
|
|
$url = $this->generateUrl('lang/my-password'); |
|
436
|
|
|
$this->visit($url); |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
/** |
|
440
|
|
|
* @return User |
|
441
|
|
|
* @throws FailedExpectationException |
|
442
|
|
|
*/ |
|
443
|
|
|
public function getCurrentUser() |
|
444
|
|
|
{ |
|
445
|
|
|
if(!$this->currentUser instanceof User){ |
|
446
|
|
|
throw new FailedExpectationException('Need to login first before use this step'); |
|
447
|
|
|
} |
|
448
|
|
|
return $this->currentUser; |
|
449
|
|
|
} |
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
|
|
|