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\Listener\Events\AuthEvent; |
15
|
|
|
use Auth\Repository\User as UserRepository; |
16
|
|
|
use Auth\Service\Register; |
17
|
|
|
use Behat\Behat\Context\Context; |
18
|
|
|
use Behat\Behat\Hook\Scope\BeforeScenarioScope; |
19
|
|
|
use Behat\Behat\Tester\Exception\PendingException; |
20
|
|
|
use Behat\Gherkin\Node\TableNode; |
21
|
|
|
use Behat\MinkExtension\Context\MinkContext; |
22
|
|
|
use Behat\Testwork\Hook\Scope\AfterSuiteScope; |
23
|
|
|
use Core\Entity\Permissions; |
24
|
|
|
use Doctrine\Common\Util\Inflector; |
25
|
|
|
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs; |
26
|
|
|
use Doctrine\ODM\MongoDB\Events; |
27
|
|
|
use Geo\Service\Photon; |
28
|
|
|
use Organizations\Entity\Organization; |
29
|
|
|
use Organizations\Entity\OrganizationName; |
30
|
|
|
use Organizations\Repository\Organization as OrganizationRepository; |
31
|
|
|
use Zend\Stdlib\ArrayObject; |
32
|
|
|
|
33
|
|
|
class UserContext implements Context |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var CoreContext |
37
|
|
|
*/ |
38
|
|
|
private $coreContext; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var MinkContext |
42
|
|
|
*/ |
43
|
|
|
private $minkContext; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var User |
47
|
|
|
*/ |
48
|
|
|
private $currentUser; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var User[] |
52
|
|
|
*/ |
53
|
|
|
static private $users = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var UserRepository |
57
|
|
|
*/ |
58
|
|
|
static private $userRepo; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
static private $currentSession; |
|
|
|
|
64
|
|
|
|
65
|
|
|
private $socialLoginInfo = []; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var UserInterface |
69
|
|
|
*/ |
70
|
|
|
private $loggedInUser; |
71
|
|
|
|
72
|
|
|
public function __construct($parameters=[]) |
73
|
|
|
{ |
74
|
|
|
$defaultLoginInfo = [ |
75
|
|
|
'facebook' => [ |
76
|
|
|
'email' => getenv('FACEBOOK_USER_EMAIL'), |
77
|
|
|
'pass' => getenv('FACEBOOK_USER_PASSWORD') |
78
|
|
|
], |
79
|
|
|
'linkedin' => [ |
80
|
|
|
'session_key-login' => getenv('LINKEDIN_USER_EMAIL'), |
81
|
|
|
'session_password-login' => getenv('LINKEDIN_USER_PASSWORD') |
82
|
|
|
], |
83
|
|
|
]; |
84
|
|
|
$socialLoginConfig = isset($parameters['social_login_info']) ? $parameters['social_login_info']:[]; |
85
|
|
|
$this->socialLoginInfo = array_merge($defaultLoginInfo,$socialLoginConfig); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @AfterSuite |
90
|
|
|
* @param AfterSuiteScope $scope |
91
|
|
|
*/ |
92
|
|
|
static public function afterSuite(AfterSuiteScope $scope) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
$repo = static::$userRepo; |
|
|
|
|
95
|
|
|
foreach(static::$users as $user){ |
|
|
|
|
96
|
|
|
if($repo->findByLogin($user->getLogin())){ |
97
|
|
|
try{ |
98
|
|
|
JobContext::removeJobByUser($user); |
99
|
|
|
$repo->remove($user,true); |
100
|
|
|
}catch (\Exception $e){ |
|
|
|
|
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @BeforeScenario |
109
|
|
|
* @param BeforeScenarioScope $scope |
110
|
|
|
*/ |
111
|
|
|
public function beforeScenario(BeforeScenarioScope $scope) |
112
|
|
|
{ |
113
|
|
|
$this->minkContext = $scope->getEnvironment()->getContext(MinkContext::class); |
|
|
|
|
114
|
|
|
$this->coreContext = $scope->getEnvironment()->getContext(CoreContext::class); |
|
|
|
|
115
|
|
|
static::$userRepo = $this->getUserRepository(); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return User |
120
|
|
|
*/ |
121
|
|
|
public function getCurrentUser() |
122
|
|
|
{ |
123
|
|
|
return $this->currentUser; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @When I fill in login form with :provider user |
128
|
|
|
*/ |
129
|
|
|
public function iSignInWithSocialUser($provider) |
130
|
|
|
{ |
131
|
|
|
$provider = strtolower($provider); |
132
|
|
|
$mink = $this->minkContext; |
133
|
|
|
foreach($this->socialLoginInfo[$provider] as $field=>$value){ |
134
|
|
|
$mink->fillField($field,$value); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @Given I am logged in as a recruiter |
140
|
|
|
* @Given I am logged in as a recruiter with :organization as organization |
141
|
|
|
*/ |
142
|
|
|
public function iAmLoggedInAsARecruiter($organization=null) |
143
|
|
|
{ |
144
|
|
|
$user = $this->thereIsAUserIdentifiedBy( |
145
|
|
|
'[email protected]', |
146
|
|
|
'test',User::ROLE_RECRUITER, |
147
|
|
|
'Test Recruiter', |
148
|
|
|
$organization |
149
|
|
|
); |
150
|
|
|
$this->startLogin($user,'test'); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @Given I don't have :login user |
155
|
|
|
* @param string $login |
156
|
|
|
*/ |
157
|
|
|
public function iDonTHaveUser($login) |
158
|
|
|
{ |
159
|
|
|
$repo = $this->getUserRepository(); |
160
|
|
|
$user=$repo->findByLogin($login); |
161
|
|
|
if($user instanceof UserInterface){ |
162
|
|
|
$repo->remove($user,true); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @Given I have a :role with the following: |
168
|
|
|
* @param $role |
169
|
|
|
* @param TableNode $fields |
170
|
|
|
*/ |
171
|
|
|
public function iHaveUserWithTheFollowing($role,TableNode $fields) |
172
|
|
|
{ |
173
|
|
|
$normalizedFields = [ |
174
|
|
|
'login' => '[email protected]', |
175
|
|
|
'fullname' => 'Test Login', |
176
|
|
|
'role' => User::ROLE_USER, |
177
|
|
|
'password' => 'test', |
178
|
|
|
'organization' => 'Cross Solution' |
179
|
|
|
]; |
180
|
|
|
foreach($fields->getRowsHash() as $field=>$value){ |
181
|
|
|
$field = Inflector::camelize($field); |
182
|
|
|
$normalizedFields[$field] = $value; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$this->thereIsAUserIdentifiedBy( |
186
|
|
|
$normalizedFields['login'], |
187
|
|
|
$normalizedFields['password'], |
188
|
|
|
$role, |
189
|
|
|
$normalizedFields['fullname'], |
190
|
|
|
$normalizedFields['organization'] |
191
|
|
|
); |
192
|
|
|
|
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @Given I am logged in as an administrator |
197
|
|
|
*/ |
198
|
|
|
public function iAmLoggedInAsAnAdmin() |
199
|
|
|
{ |
200
|
|
|
$user = $this->thereIsAUserIdentifiedBy('[email protected]','test',User::ROLE_ADMIN); |
201
|
|
|
$this->startLogin($user,'test'); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
private function startLogin(UserInterface $user, $password) |
205
|
|
|
{ |
206
|
|
|
$currentUser = $this->currentUser; |
207
|
|
|
if(!is_object($currentUser) || $user->getId()!=$currentUser->getId()){ |
208
|
|
|
$this->iWantToLogIn(); |
209
|
|
|
$this->iSpecifyTheUsernameAs($user->getLogin()); |
210
|
|
|
$this->iSpecifyThePasswordAs($password); |
211
|
|
|
$this->iLogIn(); |
212
|
|
|
$this->currentUser = $user; |
|
|
|
|
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return UserRepository |
218
|
|
|
*/ |
219
|
|
|
public function getUserRepository() |
220
|
|
|
{ |
221
|
|
|
return $this->coreContext->getRepositories()->get('Auth\Entity\User'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @Given there is a user :email identified by :password |
226
|
|
|
*/ |
227
|
|
|
public function thereIsAUserIdentifiedBy($email, $password,$role=User::ROLE_RECRUITER,$fullname="Test Recruiter",$organization=null) |
228
|
|
|
{ |
229
|
|
|
$repo = $this->getUserRepository(); |
230
|
|
|
if(!is_object($user=$repo->findByEmail($email))){ |
231
|
|
|
$user = $this->createUser($email,$password,$role,$fullname,$organization); |
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
if(!is_null($organization)){ |
235
|
|
|
$this->iHaveMainOrganization($user,$organization); |
|
|
|
|
236
|
|
|
} |
237
|
|
|
$this->addCreatedUser($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
|
|
|
$organization=$repoOrganization->findByName($orgName); |
294
|
|
|
if(!$organization instanceof Organization){ |
295
|
|
|
$organization = new Organization(); |
296
|
|
|
$organizationName = new OrganizationName($orgName); |
297
|
|
|
$organization->setOrganizationName($organizationName); |
298
|
|
|
$permissions = $organization->getPermissions(); |
299
|
|
|
$permissions->grant($user,Permissions::PERMISSION_ALL); |
300
|
|
|
}else { |
301
|
|
|
$organization->getPermissions()->grant($user,Permissions::PERMISSION_ALL); |
302
|
|
|
} |
303
|
|
|
$organization->setUser($user); |
304
|
|
|
$repoOrganization->store($organization); |
305
|
|
|
$repoOrganization->getDocumentManager()->refresh($organization); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @When I want to log in |
310
|
|
|
*/ |
311
|
|
|
public function iWantToLogIn() |
312
|
|
|
{ |
313
|
|
|
$session = $this->minkContext->getSession(); |
314
|
|
|
$url = $this->minkContext->locatePath('/en/login'); |
315
|
|
|
$session->visit($url); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @When I specify the username as :username |
320
|
|
|
*/ |
321
|
|
|
public function iSpecifyTheUsernameAs($username) |
322
|
|
|
{ |
323
|
|
|
$this->minkContext->fillField('Login name',$username); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @When I specify the password as :password |
328
|
|
|
*/ |
329
|
|
|
public function iSpecifyThePasswordAs($password) |
330
|
|
|
{ |
331
|
|
|
$this->minkContext->fillField('Password',$password); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @Given I am logged in as :username identified by :password |
336
|
|
|
*/ |
337
|
|
|
public function iAmLoggedInAsIdentifiedBy($username, $password) |
338
|
|
|
{ |
339
|
|
|
$repo = $this->getUserRepository(); |
340
|
|
|
$user = $repo->findByLogin($username); |
341
|
|
|
|
342
|
|
|
if(!$user instanceof User){ |
343
|
|
|
throw new \Exception(sprintf('There is no user with this login: "%s"',$username)); |
344
|
|
|
} |
345
|
|
|
$this->currentUser = $user; |
346
|
|
|
$this->iWantToLogIn(); |
347
|
|
|
$this->iSpecifyTheUsernameAs($username); |
348
|
|
|
$this->iSpecifyThePasswordAs($password); |
349
|
|
|
$this->iLogIn(); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* @When I log in |
354
|
|
|
*/ |
355
|
|
|
public function iLogIn() |
356
|
|
|
{ |
357
|
|
|
$this->minkContext->pressButton('login'); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* @When I press logout link |
362
|
|
|
*/ |
363
|
|
|
public function iPressLogoutLink() |
364
|
|
|
{ |
365
|
|
|
//@TODO: [ZF3] replace this with click method |
366
|
|
|
$url = $this->coreContext->generateUrl('/logout'); |
367
|
|
|
$this->minkContext->visit($url); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* @Given I log in with username :username and password :password |
372
|
|
|
*/ |
373
|
|
|
public function iLogInWith($username, $password) |
374
|
|
|
{ |
375
|
|
|
$repo = $this->getUserRepository(); |
376
|
|
|
$user = $repo->findByLogin($username); |
377
|
|
|
$this->iWantToLogIn(); |
378
|
|
|
$this->iSpecifyTheUsernameAs($username); |
379
|
|
|
$this->iSpecifyThePasswordAs($password); |
380
|
|
|
$this->iLogIn(); |
381
|
|
|
$this->loggedInUser = $user; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @When I go to profile page |
386
|
|
|
*/ |
387
|
|
|
public function iGoToProfilePage() |
388
|
|
|
{ |
389
|
|
|
$url = $this->coreContext->generateUrl('/en/my/profile'); |
390
|
|
|
$this->minkContext->visit($url); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* @Given there is a user with the following: |
395
|
|
|
*/ |
396
|
|
|
public function thereIsAUserWithTheFollowing(TableNode $table) |
397
|
|
|
{ |
398
|
|
|
$repo = $this->getUserRepository(); |
399
|
|
|
$data = $table->getRowsHash(); |
400
|
|
|
$email = isset($data['email']) ? $data['email']:'[email protected]'; |
401
|
|
|
$password = isset($data['password']) ? $data['password']:'test'; |
402
|
|
|
$fullname = isset($data['fullname']) ? $data['fullname']:'Test User'; |
403
|
|
|
$role = isset($data['role']) ? $data['role']:User::ROLE_RECRUITER; |
404
|
|
|
|
405
|
|
|
if(!is_object($user=$repo->findByLogin($email))){ |
406
|
|
|
$user = $this->createUser($email,$password,$role,$fullname); |
407
|
|
|
} |
408
|
|
|
$this->currentUser = $user; |
|
|
|
|
409
|
|
|
$this->addCreatedUser($user); |
|
|
|
|
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
private function addCreatedUser(UserInterface $user) |
413
|
|
|
{ |
414
|
|
|
if(!in_array($user,static::$users)){ |
|
|
|
|
415
|
|
|
static::$users[] = $user; |
|
|
|
|
416
|
|
|
} |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* @When I want to change my password |
421
|
|
|
*/ |
422
|
|
|
public function iWantToChangeMyPassword() |
423
|
|
|
{ |
424
|
|
|
$mink = $this->minkContext; |
425
|
|
|
$url = $this->coreContext->generateUrl('/en/my/password'); |
426
|
|
|
$mink->getSession()->visit($url); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
} |
This check marks private properties in classes that are never used. Those properties can be removed.