All Data Structures Functions Variables Pages
UserTest.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
37  class UserTest extends ZurmoBaseTest
38  {
39  public static function setUpBeforeClass()
40  {
41  parent::setUpBeforeClass();
42  SecurityTestHelper::createSuperAdmin();
43  }
44 
45  public function setUp()
46  {
47  parent::setUp();
48  Yii::app()->user->userModel = User::getByUsername('super');
49  }
50 
56  {
57  $user = User::getByUsername('super');
58  Yii::app()->user->userModel = $user;
59  $user = new User();
60  $user->isSystemUser = true;
61  }
62 
63  public function testEmailUniquenessValidation()
64  {
65  $user = User::getByUsername('super');
66  Yii::app()->user->userModel = $user;
67 
68  $user = new User();
69  $user->username = 'usera';
70  $user->lastName = 'UserA';
71  $user->setPassword('myuser');
72  $emailAddress = 'userA@example.com';
73  $user->primaryEmail->emailAddress = $emailAddress;
74  $saved = $user->save();
75  $this->assertTrue($saved);
76 
77  $user2 = new User();
78  $user2->username = 'userb';
79  $user2->lastName = 'UserB';
80  $user2->setPassword('myuser');
81  $emailAddress = 'userA@example.com';
82  $user2->primaryEmail->emailAddress = $emailAddress;
83  $user2->secondaryEmail->emailAddress = $emailAddress;
84  $saved = $user2->save();
85  $this->assertFalse($saved);
86 
87  $validationErrors = $user2->getErrors();
88  $this->assertTrue(count($validationErrors) > 0);
89 
90  // Todo: fix array keys below
91  $this->assertTrue(isset($validationErrors['primaryEmail']));
92  $this->assertTrue(isset($validationErrors['primaryEmail']['emailAddress']));
93  $this->assertEquals('Email address already exists in system.', $validationErrors['primaryEmail']['emailAddress'][0]);
94  $this->assertTrue(isset($validationErrors['secondaryEmail']));
95  $this->assertTrue(isset($validationErrors['secondaryEmail']['emailAddress']));
96  $this->assertEquals('Secondary email address cannot be the same as the primary email address.', $validationErrors['secondaryEmail']['emailAddress'][0]);
97 
98  $user2a = new User();
99  $user2a->username = 'userb';
100  $user2a->lastName = 'UserB';
101  $user2a->setPassword('myuser');
102  $emailAddress = 'userA@example.com';
103  $user2a->secondaryEmail->emailAddress = $emailAddress;
104  $saved = $user2a->save();
105  $this->assertFalse($saved);
106 
107  $validationErrors = $user2a->getErrors();
108  $this->assertTrue(count($validationErrors) > 0);
109 
110  // Todo: fix array keys below
111  $this->assertTrue(isset($validationErrors['secondaryEmail']));
112  $this->assertTrue(isset($validationErrors['secondaryEmail']['emailAddress']));
113  $this->assertEquals('Email address already exists in system.', $validationErrors['secondaryEmail']['emailAddress'][0]);
114 
115  // Try to save user without email address
116  $user3 = new User();
117  $user3->username = 'userc';
118  $user3->lastName = 'UserC';
119  $user3->setPassword('myuser');
120  $saved = $user3->save();
121  $this->assertTrue($saved);
122  }
123 
124  public function testSetTitleValuesAndRetrieveTitleValuesFromUser()
125  {
126  $titles = array('Mr.', 'Mrs.', 'Ms.', 'Dr.', 'Swami');
127  $customFieldData = CustomFieldData::getByName('Titles');
128  $customFieldData->serializedData = serialize($titles);
129  $this->assertTrue($customFieldData->save());
130  $dropDownArray = unserialize($customFieldData->serializedData);
131  $this->assertEquals($titles, $dropDownArray);
132  $user = new User();
133  $dropDownModel = $user->title;
134  $dropDownArray = unserialize($dropDownModel->data->serializedData);
135  $this->assertEquals($titles, $dropDownArray);
136  }
137 
138  public function testSaveCurrentUser()
139  {
140  //some endless loop if you are trying to save yourself
141  $user = User::getByUsername('super');
142  Yii::app()->user->userModel = $user;
143  $user->department = 'somethingNew';
144  $this->assertTrue($user->save());
145  }
146 
147  public function testCreateAndGetUserById()
148  {
149  Yii::app()->user->userModel = User::getByUsername('super');
150 
151  $user = new User();
152  $user->username = 'bill';
153  $user->title->value = 'Mr.';
154  $user->firstName = 'Bill';
155  $user->lastName = 'Billson';
156  $user->setPassword('billy');
157  $this->assertTrue($user->save());
158  $id = $user->id;
159  unset($user);
160  $user = User::getById($id);
161  $this->assertEquals('bill', $user->username);
162  }
163 
168  {
169  Yii::app()->user->userModel = User::getByUsername('super');
170 
171  $manager = new User();
172  $manager->username = 'bobi';
173  $manager->title->value = 'Mr.';
174  $manager->firstName = 'Bob';
175  $manager->lastName = 'Bobson';
176  $manager->setPassword('bobii');
177  $this->assertTrue($manager->save());
178 
179  $user = new User();
180  $user->username = 'dick';
181  $user->title->value = 'Mr.';
182  $user->firstName = 'Dick';
183  $user->lastName = 'Dickson';
184  $user->manager = $manager;
185  $user->setPassword('dickster');
186  $this->assertTrue($user->save());
187  $id = $user->id;
188  $managerId = $user->manager->id;
189  unset($user);
190  $manager = User::getById($managerId);
191  $this->assertEquals('bobi', $manager->username);
192  $user = User::getById($id);
193  $this->assertEquals('dick', $user->username);
194  $this->assertEquals('bobi', $user->manager->username);
195  }
196 
202  {
203  $user = User::getById(123456);
204  }
205 
209  public function testGetByUsername()
210  {
211  $user = User::getByUsername('bill');
212  $this->assertEquals('bill', $user->username);
213  }
214 
218  public function testGetLabel()
219  {
220  $user = User::getByUsername('bill');
221  $this->assertEquals('User', $user::getModelLabelByTypeAndLanguage('Singular'));
222  $this->assertEquals('Users', $user::getModelLabelByTypeAndLanguage('Plural'));
223  }
224 
230  {
231  User::getByUsername('noodles');
232  }
233 
237  public function testSearchByPartialName()
238  {
239  $user1= User::getByUsername('dick');
240  $users = UserSearch::getUsersByPartialFullName('di', 5);
241  $this->assertEquals(1, count($users));
242  $this->assertEquals($user1->id, $users[0]->id);
243  $this->assertEquals('dick', $users[0]->username);
244  $this->assertEquals('Dick Dickson', $users[0]->getFullName());
245 
246  $user2 = User::getByUsername('bill');
247  $users = UserSearch::getUsersByPartialFullName('bi', 5);
248  $this->assertEquals(1, count($users));
249  $this->assertEquals($user2->id, $users[0]->id);
250  $this->assertEquals('bill', $users[0]->username);
251  $this->assertEquals('Bill Billson', $users[0]->getFullName());
252 
253  $user3 = new User();
254  $user3->username = 'dison';
255  $user3->title->value = 'Mr.';
256  $user3->firstName = 'Dison';
257  $user3->lastName = 'Smith';
258  $user3->setPassword('dison');
259  $this->assertTrue($user3->save());
260 
261  $user4 = new User();
262  $user4->username = 'graham';
263  $user4->title->value = 'Mr.';
264  $user4->firstName = 'Graham';
265  $user4->lastName = 'Dillon';
266  $user4->setPassword('graham');
267  $this->assertTrue($user4->save());
268 
269  $users = UserSearch::getUsersByPartialFullName('di', 5);
270  $this->assertEquals(3, count($users));
271  $this->assertEquals($user1->id, $users[0]->id);
272  $this->assertEquals('dick', $users[0]->username);
273  $this->assertEquals('Dick Dickson', $users[0]->getFullName());
274  $this->assertEquals($user3->id, $users[1]->id);
275  $this->assertEquals('dison', $users[1]->username);
276  $this->assertEquals('Dison Smith', $users[1]->getFullName());
277  $this->assertEquals($user4->id, $users[2]->id);
278  $this->assertEquals('graham', $users[2]->username);
279  $this->assertEquals('Graham Dillon', $users[2]->getFullName());
280 
281  $users = UserSearch::getUsersByPartialFullName('g', 5);
282  $this->assertEquals(1, count($users));
283  $this->assertEquals($user4->id, $users[0]->id);
284  $this->assertEquals('graham', $users[0]->username);
285  $this->assertEquals('Graham Dillon', $users[0]->getFullName());
286 
287  $users = UserSearch::getUsersByPartialFullName('G', 5);
288  $this->assertEquals(1, count($users));
289  $this->assertEquals($user4->id, $users[0]->id);
290  $this->assertEquals('graham', $users[0]->username);
291  $this->assertEquals('Graham Dillon', $users[0]->getFullName());
292 
293  $users = UserSearch::getUsersByPartialFullName('Dil', 5);
294  $this->assertEquals(1, count($users));
295  $this->assertEquals($user4->id, $users[0]->id);
296  $this->assertEquals('graham', $users[0]->username);
297  $this->assertEquals('Graham Dillon', $users[0]->getFullName());
298  }
299 
304  {
305  $user = User::getByUsername('dick');
306 
307  $users = UserSearch::getUsersByPartialFullName('dick', 5);
308  $this->assertEquals(1, count($users));
309  $this->assertEquals($user->id, $users[0]->id);
310  $this->assertEquals('dick', $users[0]->username);
311  $this->assertEquals('Dick Dickson', $users[0]->getFullName());
312 
313  $users = UserSearch::getUsersByPartialFullName('dick ', 5);
314  $this->assertEquals(1, count($users));
315  $this->assertEquals($user->id, $users[0]->id);
316  $this->assertEquals('dick', $users[0]->username);
317  $this->assertEquals('Dick Dickson', $users[0]->getFullName());
318 
319  $users = UserSearch::getUsersByPartialFullName('dick d', 5);
320  $this->assertEquals(1, count($users));
321  $this->assertEquals($user->id, $users[0]->id);
322  $this->assertEquals('dick', $users[0]->username);
323  $this->assertEquals('Dick Dickson', $users[0]->getFullName());
324 
325  $user = User::getByUsername('dick');
326  $users = UserSearch::getUsersByPartialFullName('dick di', 5);
327  $this->assertEquals(1, count($users));
328  $this->assertEquals($user->id, $users[0]->id);
329  $this->assertEquals('dick', $users[0]->username);
330  $this->assertEquals('Dick Dickson', $users[0]->getFullName());
331 
332  $users = UserSearch::getUsersByPartialFullName('Dick di', 5);
333  $this->assertEquals(1, count($users));
334  $this->assertEquals($user->id, $users[0]->id);
335  $this->assertEquals('dick', $users[0]->username);
336  $this->assertEquals('Dick Dickson', $users[0]->getFullName());
337 
338  $users = UserSearch::getUsersByPartialFullName('dick Di', 5);
339  $this->assertEquals(1, count($users));
340  $this->assertEquals($user->id, $users[0]->id);
341  $this->assertEquals('dick', $users[0]->username);
342  $this->assertEquals('Dick Dickson', $users[0]->getFullName());
343  }
344 
349  {
350  $user = new User();
351  $user->username = 'jason';
352  $user->title->value = 'Mr.';
353  $user->firstName = 'Jason';
354  $user->lastName = 'Jasonson';
355  $user->setPassword('jason');
356  $this->assertTrue($user->save());
357  $id = $user->id;
358  unset($user);
359  $user = User::getById($id);
360  $this->assertEquals('jason', $user->username);
361  $this->assertEquals('Mr.', strval($user->title));
362  $user->title = null;
363  $this->assertNotNull($user->title);
364  $this->assertTrue($user->save());
365  }
366 
371  {
372  $user = User::getByUsername('jason');
373  $user->title->value = 'Mr.';
374  $this->assertEquals('Mr.', strval($user->title));
375  $this->assertTrue($user->save());
376 
377  $_FAKEPOST = array(
378  'User' => array(
379  'title' => array(
380  'value' => '',
381  )
382  )
383  );
384 
385  $user->setAttributes($_FAKEPOST['User']);
386  $this->assertEquals('(None)', strval($user->title));
387  $this->assertTrue($user->save());
388  }
389 
394  {
395  $user = User::getByUsername('jason');
396  $user->title->value = 'Mr.';
397  $this->assertEquals('Mr.', strval($user->title));
398  $this->assertTrue($user->save());
399 
400  $_FAKEPOST = array(
401  'User' => array(
402  'title' => array(
403  'value' => '',
404  )
405  )
406  );
407 
408  $user->setAttributes($_FAKEPOST['User']);
409  $this->assertEquals('(None)', strval($user->title));
410  $this->assertTrue($user->save());
411  }
412 
417  {
418  $user = User::getByUsername('jason');
419  $user->title->value = 'Mr.';
420  $this->assertEquals('Mr.', strval($user->title));
421  $this->assertTrue($user->save());
422 
423  $_FAKEPOST = array(
424  'User' => array(
425  'title' => array(
426  'value' => 'Sir',
427  )
428  )
429  );
430 
431  $user->setAttributes($_FAKEPOST['User']);
432  $this->assertEquals('Sir', strval($user->title));
433  $this->assertTrue($user->save());
434  }
435 
436  public function testSaveUserWithNoManager()
437  {
438  $user = UserTestHelper::createBasicUser('Steven');
439  $_FAKEPOST = array(
440  'User' => array(
441  'manager' => array(
442  'id' => '',
443  ),
444  ),
445  );
446 
447  $user->setAttributes($_FAKEPOST['User']);
448  $user->validate();
449  $this->assertEquals(array(), $user->getErrors());
450  }
451 
457  {
458  $user = User::getByUsername('jason');
459  $_FAKEPOST = array(
460  'User' => array(
461  'title' => array(
462  'value' => '',
463  ),
464  'firstName' => 'Jason',
465  'lastName' => 'Jasonson',
466  'username' => 'jason',
467  'jobTitle' => '',
468  'officePhone' => '',
469  'manager' => array(
470  'id' => '',
471  ),
472  'mobilePhone' => '',
473  'department' => '',
474  'primaryEmail' => array(
475  'emailAddress' => '',
476  'optOut' => 0,
477  'isInvalid' => 0,
478  ),
479  'primaryAddress' => array(
480  'street1' => '',
481  'street2' => '',
482  'city' => '',
483  'state' => '',
484  'postalCode' => '',
485  'country' => '',
486  )
487  )
488  );
489  $user->setAttributes($_FAKEPOST['User']);
490  $user->validate();
491  $this->assertEquals(array(), $user->getErrors());
492  $this->assertTrue($user->save());
493  }
494 
499  {
500  $user = User::getByUsername('jason');
501  $_FAKEPOST = array(
502  'User' => array(
503  'title' => array(
504  'value' => '',
505  ),
506  'firstName' => 'Jason',
507  'lastName' => 'Jasonson',
508  'username' => 'jason',
509  'jobTitle' => '',
510  'officePhone' => '',
511  'manager' => array(
512  'id' => $user->id,
513  ),
514  )
515  );
516  /*
517  $user->setAttributes($_FAKEPOST['User']);
518  $this->assertFalse($user->save());
519  $errors = $user->getErrors();
520  //todo: assert an error is present for manager, assert the error says can't
521  //select self or something along those lines.
522  */
523 
524  //probably should also check if you are picking a manager that is creating recursion,
525  //not necessarily yourself, but someone in the chain of yourself already.
526  }
527 
528  public function testUserMixingInPerson()
529  {
530  // See comments on User::getDefaultMetadata().
531 
532  $user = new User();
533  $this->assertTrue($user->isAttribute('username'));
534  $this->assertTrue($user->isAttribute('title'));
535  $this->assertTrue($user->isAttribute('firstName'));
536  $this->assertTrue($user->isAttribute('lastName'));
537  $this->assertTrue($user->isAttribute('jobTitle'));
538 
539  $user->username = 'oliver';
540  $user->title->value = 'Mr.';
541  $user->firstName = 'Oliver';
542  $user->lastName = 'Oliverson';
543  $user->jobTitle = 'Recruiter';
544  $this->assertEquals('oliver', $user->username);
545  $this->assertEquals('Oliver Oliverson', strval($user));
546  $this->assertEquals('Recruiter', $user->jobTitle);
547  $user->setPassword('oliver');
548  $this->assertTrue($user->save());
549 
550  $id = $user->id;
551  $user->forget();
552  unset($user);
553 
554  $user = User::getById($id);
555  $this->assertEquals('oliver', $user->username);
556  $this->assertEquals('Oliver Oliverson', strval($user));
557  $this->assertEquals('Recruiter', $user->jobTitle);
558  }
559 
560  public function testCreateNewUserFromPostNoBadValues()
561  {
562  $_FAKEPOST = array(
563  'UserPasswordForm' => array(
564  'title' => array(
565  'value' => '',
566  ),
567  'firstName' => 'Red',
568  'lastName' => 'Jiambo',
569  'username' => 'redjiambo',
570  'newPassword' => '123456',
571  'newPassword_repeat' => '123456',
572  'jobTitle' => '',
573  'officePhone' => '',
574  'manager' => array(
575  'id' => '',
576  ),
577  'mobilePhone' => '',
578  'department' => '',
579  'primaryEmail' => array(
580  'emailAddress' => '',
581  'optOut' => 0,
582  'isInvalid' => 0,
583  ),
584  'primaryAddress' => array(
585  'street1' => '',
586  'street2' => '',
587  'city' => '',
588  'state' => '',
589  'postalCode' => '',
590  'country' => '',
591  )
592  )
593  );
594  $user = new User();
595  $user->setScenario('createUser');
596  $userPasswordForm = new UserPasswordForm($user);
597  $userPasswordForm->setScenario('createUser');
598  $userPasswordForm->setAttributes($_FAKEPOST['UserPasswordForm']);
599  $userPasswordForm->validate();
600  $this->assertEquals(array(), $userPasswordForm->getErrors());
601  $this->assertTrue($userPasswordForm->save());
602  $user->forget();
603  $user = User::getByUsername('redjiambo');
604  $this->assertEquals('Red', $user->firstName);
605  $this->assertEquals(null, $user->officePhone);
606  $this->assertEquals(null, $user->jobTitle);
607  $this->assertEquals(null, $user->mobilePhone);
608  $this->assertEquals(null, $user->department);
609  }
610 
615  {
616  $group = new Group();
617  $group->name = 'Os mais legais do Rio';
618  $this->assertTrue($group->save());
619 
620  $user = new User();
621  $user->username = 'carioca';
622  $user->title->value = 'Senhor';
623  $user->firstName = 'José';
624  $user->lastName = 'Olivereira';
625  $user->jobTitle = 'Traficante';
626  $user->primaryAddress->street1 = 'R. das Mulheres, 69';
627  $user->primaryAddress->street2 = '';
628  $user->primaryAddress->city = 'Centro';
629  $user->primaryAddress->state = 'RJ';
630  $user->primaryAddress->postalCode = '';
631  $user->primaryAddress->country = 'Brasil';
632  $user->primaryEmail->emailAddress = 'jose@gmail.com';
633  $user->primaryEmail->optOut = 1;
634  $user->primaryEmail->isInvalid = 0;
635  $user->manager = User::getByUsername('bill');
636  $user->setPassword('Senhor');
637  $user->groups->add($group);
638  $user->save();
639  $this->assertTrue($user->save());
640 
641  $titleId = $user->title->id;
642  $primaryAddressId = $user->primaryAddress->id;
643  $primaryEmailId = $user->primaryEmail ->id;
644  $groupId = $group->id;
645 
646  $user->delete();
647  unset($user);
648  unset($group);
649 
650  Group::getById($groupId);
651  User::getByUsername('bill');
652 
653  try
654  {
655  CustomField::getById($titleId);
656  $this->fail("Title should have been deleted.");
657  }
658  catch (NotFoundException $e)
659  {
660  }
661 
662  try
663  {
664  Address::getById($primaryAddressId);
665  $this->fail("Address should have been deleted.");
666  }
667  catch (NotFoundException $e)
668  {
669  }
670 
671  try
672  {
673  Email::getById($primaryEmailId);
674  $this->fail("Email should have been deleted.");
675  }
676  catch (NotFoundException $e)
677  {
678  }
679  }
680 
684  public function testCanRemoveRoleFromUser()
685  {
686  Yii::app()->user->userModel = User::getByUsername('super');
687 
688  $parentRole = new Role();
689  $parentRole->name = 'SomeParentRole';
690  $saved = $parentRole->save();
691  $this->assertTrue($parentRole->id > 0);
692  $this->assertTrue($saved);
693  $role = new Role();
694  $role->name = 'SomeRole';
695  $role->role = $parentRole;
696  $saved = $role->save();
697  $this->assertTrue($parentRole->id > 0);
698  $this->assertEquals($parentRole->id, $role->role->id);
699  $this->assertTrue($role->id > 0);
700  $this->assertTrue($saved);
701  $user = User::getByUsername('bill');
702  $this->assertTrue($user->id > 0);
703  $this->assertFalse($user->role->id > 0);
704  $fakePost = array(
705  'role' => array(
706  'id' => $role->id,
707  )
708  );
709  $user->setAttributes($fakePost);
710  $saved = $user->save();
711  $this->assertTrue($saved);
712  $user->forget();
713  unset($user);
714  $user = User::getByUsername('bill');
715  $this->assertTrue($user->id > 0);
716  $this->assertTrue($role->id > 0);
717  $this->assertEquals($role->id, $user->role->id);
718  $fakePost = array(
719  'role' => array(
720  'id' => '',
721  )
722  );
723  $user->setAttributes($fakePost);
724  $this->assertFalse($user->role->id > 0);
725  $saved = $user->save();
726  $this->assertTrue($saved);
727  $user->forget();
728  unset($user);
729  $user = User::getByUsername('bill');
730  $this->assertTrue($user->id > 0);
731  $this->assertFalse($user->role->id > 0);
732  }
733 
738  {
739  $bill = User::getByUsername('bill');
740  $bill->setScenario('changePassword');
741  $billPasswordForm = new UserPasswordForm($bill);
742  $billPasswordForm->setScenario('changePassword');
743  $this->assertEquals(null, $bill->getEffectivePolicy('UsersModule', UsersModule::POLICY_ENFORCE_STRONG_PASSWORDS));
744  $this->assertEquals(5, $bill->getEffectivePolicy('UsersModule', UsersModule::POLICY_MINIMUM_PASSWORD_LENGTH));
745  $this->assertEquals(3, $bill->getEffectivePolicy('UsersModule', UsersModule::POLICY_MINIMUM_USERNAME_LENGTH));
746  $_FAKEPOST = array(
747  'UserPasswordForm' => array(
748  'username' => 'ab',
749  'newPassword' => 'ab',
750  'newPassword_repeat' => 'ab',
751  )
752  );
753  $billPasswordForm->setAttributes($_FAKEPOST['UserPasswordForm']);
754  $this->assertFalse($billPasswordForm->save());
755  $errors = array(
756  'newPassword' => array(
757  'The password is too short. Minimum length is 5.',
758  ),
759  );
760  $this->assertEquals($errors, $billPasswordForm->getErrors());
761  $_FAKEPOST = array(
762  'UserPasswordForm' => array(
763  'username' => 'abcdefg',
764  'newPassword' => 'abcdefg',
765  'newPassword_repeat' => 'abcdefg',
766  )
767  );
768  $billPasswordForm->setAttributes($_FAKEPOST['UserPasswordForm']);
769  $this->assertEquals('abcdefg', $billPasswordForm->username);
770  $this->assertEquals('abcdefg', $billPasswordForm->newPassword);
771  $validated = $billPasswordForm->validate();
772  $this->assertTrue($validated);
773  $saved = $billPasswordForm->save();
774  $this->assertTrue($saved);
775  $bill->setPolicy('UsersModule', UsersModule::POLICY_ENFORCE_STRONG_PASSWORDS, Policy::YES);
776  // If security is optimized the optimization will see the policy value in the database
777  // and so wont use it in validating, so the non-strong password wont be validated as
778  // invalid until the next save.
779  $this->assertEquals(SECURITY_OPTIMIZED, $billPasswordForm->save());
780  $_FAKEPOST = array(
781  'UserPasswordForm' => array(
782  'newPassword' => 'abcdefg',
783  'newPassword_repeat' => 'abcdefg',
784  )
785  );
786  $billPasswordForm->setAttributes($_FAKEPOST['UserPasswordForm']);
787  $this->assertFalse($billPasswordForm->save());
788 
789  $errors = array(
790  'newPassword' => array(
791  'The password must have at least one uppercase letter',
792  'The password must have at least one number and one letter',
793  ),
794  );
795  $this->assertEquals($errors, $billPasswordForm->getErrors());
796  $_FAKEPOST = array(
797  'UserPasswordForm' => array(
798  'newPassword' => 'abcdefgN',
799  'newPassword_repeat' => 'abcdefgN',
800  )
801  );
802  $billPasswordForm->setAttributes($_FAKEPOST['UserPasswordForm']);
803  $this->assertFalse($billPasswordForm->save());
804  $errors = array(
805  'newPassword' => array(
806  'The password must have at least one number and one letter',
807  ),
808  );
809  $this->assertEquals($errors, $billPasswordForm->getErrors());
810  $_FAKEPOST = array(
811  'UserPasswordForm' => array(
812  'newPassword' => 'ABCDEFGH',
813  'newPassword_repeat' => 'ABCDEFGH',
814  )
815  );
816  $billPasswordForm->setAttributes($_FAKEPOST['UserPasswordForm']);
817  $this->assertFalse($billPasswordForm->save());
818  $errors = array(
819  'newPassword' => array(
820  'The password must have at least one lowercase letter',
821  'The password must have at least one number and one letter',
822  ),
823  );
824  $this->assertEquals($errors, $billPasswordForm->getErrors());
825  $_FAKEPOST = array(
826  'UserPasswordForm' => array(
827  'newPassword' => 'abcdefgN4',
828  'newPassword_repeat' => 'abcdefgN4',
829  )
830  );
831  $billPasswordForm->setAttributes($_FAKEPOST['UserPasswordForm']);
832  $this->assertTrue($billPasswordForm->save());
833  $bill->setRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB);
834  $this->assertTrue($billPasswordForm->save());
835  $this->assertEquals(Right::ALLOW, $bill->getEffectiveRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB));
836  //Now attempt to login as bill
837  $bill->forget();
838  $bill = User::getByUsername('abcdefg');
839  $this->assertEquals($bill, User::authenticate('abcdefg', 'abcdefgN4'));
840  $identity = new UserIdentity('abcdefg', 'abcdefgN4');
841  $authenticated = $identity->authenticate();
842  $this->assertEquals(0, $identity->errorCode);
843  $this->assertTrue($authenticated);
844 
845  //Now turn off login via web for bill
846  Yii::app()->user->userModel = User::getByUsername('super');
847  $bill = User::getByUsername('abcdefg');
848  $bill->setRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB, RIGHT::DENY);
849  $this->assertTrue($bill->save());
850  $identity = new UserIdentity('abcdefg', 'abcdefgN4');
851  $this->assertFalse($identity->authenticate());
852  $this->assertEquals(UserIdentity::ERROR_NO_RIGHT_WEB_LOGIN, $identity->errorCode);
853 
854  //Test creating a new user uses the everyone policy
855  $everyone = Group::getByName(Group::EVERYONE_GROUP_NAME);
856  $newUser = new User();
857  $this->assertEquals(null, $everyone->getEffectivePolicy('UsersModule', UsersModule::POLICY_ENFORCE_STRONG_PASSWORDS));
858  $this->assertEquals(5, $everyone->getEffectivePolicy('UsersModule', UsersModule::POLICY_MINIMUM_PASSWORD_LENGTH));
859  $this->assertEquals(3, $everyone->getEffectivePolicy('UsersModule', UsersModule::POLICY_MINIMUM_USERNAME_LENGTH));
860  $this->assertEquals(null, $newUser->getEffectivePolicy('UsersModule', UsersModule::POLICY_ENFORCE_STRONG_PASSWORDS));
861  $this->assertEquals(5, $newUser->getEffectivePolicy('UsersModule', UsersModule::POLICY_MINIMUM_PASSWORD_LENGTH));
862  $this->assertEquals(3, $newUser->getEffectivePolicy('UsersModule', UsersModule::POLICY_MINIMUM_USERNAME_LENGTH));
863  $everyone->setPolicy('UsersModule', UsersModule::POLICY_ENFORCE_STRONG_PASSWORDS, Policy::YES);
864  $everyone->setPolicy('UsersModule', UsersModule::POLICY_MINIMUM_PASSWORD_LENGTH, 3);
865  $everyone->setPolicy('UsersModule', UsersModule::POLICY_MINIMUM_USERNAME_LENGTH, 15);
866  $everyone->save();
867  $this->assertEquals(Policy::YES, $newUser->getEffectivePolicy('UsersModule', UsersModule::POLICY_ENFORCE_STRONG_PASSWORDS));
868  $this->assertEquals(3, $newUser->getEffectivePolicy('UsersModule', UsersModule::POLICY_MINIMUM_PASSWORD_LENGTH));
869  $this->assertEquals(15, $newUser->getEffectivePolicy('UsersModule', UsersModule::POLICY_MINIMUM_USERNAME_LENGTH));
870 
871  //Make the permission as the default for next tests
872  $everyone->setPolicy('UsersModule', UsersModule::POLICY_MINIMUM_PASSWORD_LENGTH, 5);
873  $everyone->setPolicy('UsersModule', UsersModule::POLICY_MINIMUM_USERNAME_LENGTH, 3);
874  $everyone->save();
875  }
876 
881  {
882  $bill = User::getByUsername('abcdefg');
883  $bill->setScenario('editUser');
884  $this->assertEquals(3, $bill->getEffectivePolicy('UsersModule', UsersModule::POLICY_MINIMUM_USERNAME_LENGTH));
885  $_FAKEPOST = array(
886  'User' => array(
887  'username' => 'ab',
888  )
889  );
890  $bill->setAttributes($_FAKEPOST['User']);
891  $this->assertFalse($bill->save());
892  $errors = array(
893  'username' => array(
894  'The username is too short. Minimum length is 3.',
895  ),
896  );
897  $this->assertEquals($errors, $bill->getErrors());
898 
899  $bill = User::getByUsername('abcdefg');
900  $bill->setScenario('createUser');
901  $this->assertEquals(3, $bill->getEffectivePolicy('UsersModule', UsersModule::POLICY_MINIMUM_USERNAME_LENGTH));
902  $_FAKEPOST = array(
903  'User' => array(
904  'username' => 'ab',
905  )
906  );
907  $bill->setAttributes($_FAKEPOST['User']);
908  $this->assertFalse($bill->save());
909  $errors = array(
910  'username' => array(
911  'The username is too short. Minimum length is 3.',
912  ),
913  );
914  $this->assertEquals($errors, $bill->getErrors());
915  }
916 
917  public function testValidatingUserAfterGettingAttributeValuesFromRelatedUsers()
918  {
919  $super = User::getByUsername('super');
920  Yii::app()->user->userModel = $super;
921  $user = UserTestHelper::createBasicUser('notsuper');
922  $this->assertTrue($user->save());
923  $this->assertTrue($user->createdByUser ->isSame($super));
924  $this->assertTrue($user->modifiedByUser->isSame($super));
925  if (!$user->validate())
926  {
927  $this->assertEquals(array(), $user->getErrors());
928  }
929  // A regular user has a created by and
930  // modified by user so accessing them is no problem.
931  $test = $user->createdByUser->id;
932  $this->assertTrue($user->validate());
933  $this->assertEquals(array(), $user->getErrors());
934  }
935 
936  public function testValidatingSuperAdministratorAfterGettingAttributeValuesFromRelatedUsers()
937  {
938  $super = User::getByUsername('super');
939  $this->assertTrue($super->validate());
940  $this->assertTrue($super->createdByUser->id < 0);
941  $this->assertTrue($super->modifiedByUser->isSame($super));
942  $this->assertTrue($super->validate());
943  $this->assertEquals(array(), $super->getErrors());
944  }
945 
950  {
951  $userCount = User::getCount();
952  $dick = User::getByUsername('dick');
953  $this->assertTrue($dick->save());
954  $this->assertEquals($userCount, User::getCount());
955  }
956 
957  public function testMixedInPersonInUser()
958  {
959  $user = new User();
960  $user->username = 'dude';
961  $user->lastName = 'Dude';
962  $this->assertTrue($user->save());
963 
964  $this->assertTrue($user->isAttribute('id')); // From RedBeanModel.
965  $this->assertTrue($user->isAttribute('createdDateTime')); // From Item.
966  $this->assertTrue($user->isAttribute('firstName')); // From Person.
967  $this->assertTrue($user->isAttribute('username')); // From User.
968 
969  $this->assertTrue($user->isRelation ('createdByUser')); // From Item.
970  $this->assertTrue($user->isRelation ('rights')); // From Permitable.
971  $this->assertTrue($user->isRelation ('title')); // From Person.
972  $this->assertTrue($user->isRelation ('manager')); // From User.
973 
974  unset($user);
975 
976  $user = User::getByUsername('dude');
977 
978  $this->assertTrue($user->isAttribute('id')); // From RedBeanModel.
979  $this->assertTrue($user->isAttribute('createdDateTime')); // From Item.
980  $this->assertTrue($user->isAttribute('firstName')); // From Person.
981  $this->assertTrue($user->isAttribute('username')); // From User.
982 
983  $this->assertTrue($user->isRelation ('createdByUser')); // From Item.
984  $this->assertTrue($user->isRelation ('rights')); // From Permitable.
985  $this->assertTrue($user->isRelation ('title')); // From Person.
986  $this->assertTrue($user->isRelation ('manager')); // From User.
987 
989 
990  $modelIdentifier = $user->getModelIdentifier();
991  unset($user);
992 
993  RedBeanModelsCache::forgetAll(true); // Forget it at the php level.
995 
996  if (MEMCACHE_ON)
997  {
998  $user = RedBeanModelsCache::getModel($modelIdentifier);
999 
1000  $this->assertTrue($user->isAttribute('id')); // From RedBeanModel.
1001  $this->assertTrue($user->isAttribute('createdDateTime')); // From Item.
1002  $this->assertTrue($user->isAttribute('firstName')); // From Person.
1003  $this->assertTrue($user->isAttribute('username')); // From User.
1004 
1005  $this->assertTrue($user->isRelation ('createdByUser')); // From Item.
1006  $this->assertTrue($user->isRelation ('rights')); // From Permitable.
1007  $this->assertTrue($user->isRelation ('title')); // From Person.
1008  $this->assertTrue($user->isRelation ('manager')); // From User.
1009  }
1010  }
1011 
1012  public function testGetModelClassNames()
1013  {
1014  $modelClassNames = UsersModule::getModelClassNames();
1015  $this->assertEquals(3, count($modelClassNames));
1016  $this->assertEquals('User', $modelClassNames[0]);
1017  $this->assertEquals('UserSearch', $modelClassNames[1]);
1018  }
1019 
1020  public function testLogAuditEventsListForCreatedAndModifedCreatingFirstUser()
1021  {
1022  Yii::app()->user->userModel = null;
1023  $user = new User();
1024  $user->username = 'myuser';
1025  $user->title->value = 'Mr.';
1026  $user->firstName = 'My';
1027  $user->lastName = 'Userson';
1028  $user->setPassword('myuser');
1029  $saved = $user->save();
1030  $this->assertTrue($saved);
1031  $this->assertEquals(Yii::app()->user->userModel, $user);
1032 
1033  //Create a second user and confirm the first user is still the current user.
1034  $user2 = new User();
1035  $user2->username = 'myuser2';
1036  $user2->title->value = 'Mr.';
1037  $user2->firstName = 'My';
1038  $user2->lastName = 'Userson2';
1039  $user2->setPassword('myuser2');
1040  $this->assertTrue($user2->save());
1041  $this->assertEquals(Yii::app()->user->userModel, $user);
1042  }
1043 
1044  public function testAvatarForUser()
1045  {
1046  //Create a new user and confirm that gets the default avatar
1047  $user = new User();
1048  $user->username = 'avatar';
1049  $user->lastName = 'User';
1050  $this->assertTrue($user->save());
1051  $this->assertContains('width="250" height="250" src="//www.gravatar.com/avatar/?s=250&amp;r=g&amp;d=mm', // Not Coding Standard
1052  $user->getAvatarImage());
1053  //When calling getAvatarImage it should return the same url to avoid querying gravatar twice
1054  $this->assertContains('width="50" height="50" src="//www.gravatar.com/avatar/?s=50&amp;r=g&amp;d=mm', // Not Coding Standard
1055  $user->getAvatarImage(50));
1056  unset($user);
1057 
1058  //Add avatar info to the user and confirm it gets saved
1059  $user = User::getByUsername('avatar');
1060  $avatar = array('avatarType' => 1);
1061  $user->serializeAndSetAvatarData($avatar);
1062  $this->assertEquals(serialize($avatar), $user->serializedAvatarData);
1063  $this->assertTrue($user->save());
1064  unset($user);
1065  $user = User::getByUsername('avatar');
1066  $this->assertContains('width="250" height="250" src="//www.gravatar.com/avatar/?s=250&amp;r=g&amp;d=mm', // Not Coding Standard
1067  $user->getAvatarImage());
1068  $this->assertContains('width="50" height="50" src="//www.gravatar.com/avatar/?s=50&amp;r=g&amp;d=mm', // Not Coding Standard
1069  $user->getAvatarImage(50));
1070  unset($user);
1071 
1072  //Change avatar to primary email address
1073  $user = new User();
1074  $user->username = 'avatar2';
1075  $user->lastName = 'User';
1076  $emailAddress = 'avatar@zurmo.org';
1077  $user->primaryEmail->emailAddress = $emailAddress;
1078  $user->primaryEmail->optOut = 1;
1079  $user->primaryEmail->isInvalid = 0;
1080  $avatar = array('avatarType' => 2);
1081  $user->serializeAndSetAvatarData($avatar);
1082  $this->assertContains(serialize($avatar), $user->serializedAvatarData);
1083  $this->assertTrue($user->save());
1084  unset($user);
1085  $user = User::getByUsername('avatar2');
1086  $avatarUrl = 'width="250" height="250" src="//www.gravatar.com/avatar/' .
1087  md5(strtolower(trim($emailAddress))) .
1088  '?s=250&amp;r=g&amp;d=identicon'; // Not Coding Standard
1089  $this->assertContains($avatarUrl, $user->getAvatarImage());
1090  $avatarUrl = 'width="5" height="5" src="//www.gravatar.com/avatar/' .
1091  md5(strtolower(trim($emailAddress))) .
1092  '?s=5&amp;r=g&amp;d=identicon'; // Not Coding Standard
1093  $this->assertContains($avatarUrl, $user->getAvatarImage(5));
1094  unset($user);
1095 
1096  //Change avatar to custom avatar email address
1097  $user = new User();
1098  $user->username = 'avatar3';
1099  $user->lastName = 'User';
1100  $emailAddress = 'avatar-custom@zurmo.org';
1101  $avatar = array('avatarType' => 3, 'customAvatarEmailAddress' => $emailAddress);
1102  $user->serializeAndSetAvatarData($avatar);
1103  $this->assertEquals(serialize($avatar), $user->serializedAvatarData);
1104  $this->assertTrue($user->save());
1105  unset($user);
1106  $user = User::getByUsername('avatar3');
1107  $avatarUrl = 'width="250" height="250" src="//www.gravatar.com/avatar/' .
1108  md5(strtolower(trim($emailAddress))) .
1109  "?s=250&amp;r=g&amp;d=identicon"; // Not Coding Standard
1110  $this->assertContains($avatarUrl, $user->getAvatarImage());
1111  $avatarUrl = 'width="2500" height="2500" src="//www.gravatar.com/avatar/' .
1112  md5(strtolower(trim($emailAddress))) .
1113  "?s=2500&amp;r=g&amp;d=identicon"; // Not Coding Standard
1114  $this->assertContains($avatarUrl, $user->getAvatarImage(2500));
1115  unset($user);
1116  }
1117 
1122  {
1123  Yii::app()->user->userModel = User::getByUsername('super');
1124 
1125  $superAdminGroup = Group::getByName(Group::SUPER_ADMINISTRATORS_GROUP_NAME);
1126  //At this point the super administrator is part of this group
1127  $this->assertEquals(1, $superAdminGroup->users->count());
1128 
1129  //Now try to delete super user, It should not work
1130  $this->assertFalse(Yii::app()->user->userModel->delete());
1131  $this->fail();
1132  }
1133 
1137  public function testIsActiveOnUserSave()
1138  {
1139  $user = new User();
1140  $user->username = 'activeuser';
1141  $user->title->value = 'Mr.';
1142  $user->firstName = 'My';
1143  $user->lastName = 'activeuserson';
1144  $user->setPassword('myuser');
1145  $this->assertTrue($user->save());
1146  unset($user);
1147 
1148  $user = User::getByUsername('activeuser');
1149  $this->assertEquals(1, $user->isActive);
1150  unset($user);
1151 
1152  //Change the user's status to inactive and confirm the changes in rights and isActive attribute.
1153  $user = User::getByUsername('activeuser');
1154  $user->setRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB, RIGHT::DENY);
1155  $this->assertTrue($user->save());
1156  $this->assertEquals(0, $user->isActive);
1157  unset($user);
1158 
1159  //Now change the user's status back to active.
1160  $user = User::getByUsername('activeuser');
1161  $user->setRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB, RIGHT::ALLOW);
1162  $this->assertTrue($user->save());
1163  $this->assertEquals(1, $user->isActive);
1164  unset($user);
1165  }
1166 
1167  public function testUserLocaleSettings()
1168  {
1169  $user = new User();
1170  $user->username = 'userforlocaletest';
1171  $user->title->value = 'Mr.';
1172  $user->firstName = 'Locale';
1173  $user->lastName = 'User';
1174  $user->setPassword('localeuser');
1175  $this->assertTrue($user->save());
1176 
1177  $user = User::getByUsername('userForLocaleTest');
1178  $this->assertNull($user->locale);
1179  Yii::app()->user->userModel = $user;
1180  $this->assertEquals('12/1/13 12:00:00 AM',
1181  Yii::app()->dateFormatter->formatDateTime('2013-12-01', 'short'));
1182  $user->locale = 'en_gb';
1183  $this->assertTrue($user->save());
1184 
1185  $user = User::getByUsername('userForLocaleTest');
1186  $this->assertContains($user->locale, ZurmoLocale::getSelectableLocaleIds());
1187  Yii::app()->user->userModel = $user;
1188  $this->assertEquals('01/12/2013 00:00:00',
1189  Yii::app()->dateFormatter->formatDateTime('2013-12-01', 'short'));
1190  }
1191 
1192  public function testLastLoginDateTimeAttribute()
1193  {
1194  $user = new User();
1195  $user->username = 'lastloginuser';
1196  $user->title->value = 'Mr.';
1197  $user->firstName = 'myFirstName';
1198  $user->lastName = 'myLastName';
1199  $user->setPassword('lastlogin');
1200  $user->setRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB);
1201  $this->assertTrue($user->save());
1202 
1203  $user = User::getByUsername('lastloginuser');
1204  $this->assertNull($user->lastLoginDateTime);
1205  unset($user);
1206 
1207  $now = time();
1208  User::authenticate('lastloginuser', 'lastlogin');
1209  $user = User::getByUsername('lastloginuser');
1210  $this->assertLessThanOrEqual(5, $user->lastLoginDateTime - $now);
1211  }
1212 
1213  public function testTrimUsername()
1214  {
1215  $user = new User();
1216  $user->username = ' trimusername ';
1217  $user->title->value = 'Mr.';
1218  $user->firstName = 'trim';
1219  $user->lastName = 'username';
1220  $user->setPassword('trimusername');
1221  $this->assertTrue($user->save());
1222 
1223  $user = User::getByUsername('trimusername');
1224  $this->assertEquals('trimusername', $user->username);
1225  }
1226 
1231  {
1232  $user = new User();
1233  $user->username = 'hidefromselectuser';
1234  $user->title->value = 'Mr.';
1235  $user->firstName = 'My';
1236  $user->lastName = 'hidefromselectuser';
1237  $user->hideFromSelecting = true;
1238  $user->setPassword('myuser');
1239  $this->assertTrue($user->save());
1240  unset($user);
1241 
1242  $user = User::getByUsername('hidefromselectuser');
1243  $this->assertEquals(1, $user->hideFromSelecting);
1244  unset($user);
1245 
1246  $userSet = UserSearch::getUsersByPartialFullName('hide', 20);
1247  $this->assertEquals(0, count($userSet));
1248 
1249  $user = User::getByUsername('hidefromselectuser');
1250  $user->hideFromSelecting = false;
1251  $this->assertTrue($user->save());
1252  unset($user);
1253 
1254  $user = User::getByUsername('hidefromselectuser');
1255  $this->assertEquals(0, $user->hideFromSelecting);
1256  unset($user);
1257 
1258  $userSet = UserSearch::getUsersByPartialFullName('hide', 20);
1259 
1260  $this->assertEquals(1, count($userSet));
1261  }
1262 
1267  {
1268  $user = new User();
1269  $user->username = 'leaderboard';
1270  $user->title->value = 'Mr.';
1271  $user->firstName = 'My';
1272  $user->lastName = 'leaderboard';
1273  $user->hideFromLeaderboard = true;
1274  $user->setPassword('myuser');
1275  $this->assertTrue($user->save());
1276  unset($user);
1277 
1278  $user = User::getByUsername('leaderboard');
1279  Yii::app()->user->userModel = $user;
1280 
1281  $pointTypeAndValueData = array('some type' => 400);
1282  GamePointUtil::addPointsByPointData(Yii::app()->user->userModel, $pointTypeAndValueData);
1283  Yii::app()->gameHelper->processDeferredPoints();
1284 
1285  $user = User::getByUsername('leaderboard');
1286  $this->assertEquals(1, $user->hideFromLeaderboard);
1287  unset($user);
1288 
1289  $userSet = GamePointUtil::getUserLeaderboardData(GamePointUtil::LEADERBOARD_TYPE_OVERALL);
1290  $this->assertEquals(0, count($userSet));
1291 
1292  $user = User::getByUsername('leaderboard');
1293  $user->hideFromLeaderboard = false;
1294  $this->assertTrue($user->save());
1295  unset($user);
1296 
1297  $user = User::getByUsername('leaderboard');
1298  $this->assertEquals(0, $user->hideFromLeaderboard);
1299  unset($user);
1300 
1301  $userSet = GamePointUtil::getUserLeaderboardData(GamePointUtil::LEADERBOARD_TYPE_OVERALL);
1302 
1303  $this->assertTrue(count($userSet) > 0);
1304  }
1305 
1309  public function testIsRootUserOnUserSave()
1310  {
1311  $user = new User();
1312  $user->username = 'rootuser';
1313  $user->title->value = 'Mr.';
1314  $user->firstName = 'My';
1315  $user->lastName = 'rootuser';
1316  $user->setPassword('myuser');
1317  $this->assertTrue($user->save());
1318  unset($user);
1319 
1320  $user = User::getByUsername('rootuser');
1321  $this->assertNull($user->isRootUser);
1322  unset($user);
1323 
1324  $superUser = User::getByUsername('leaderboard');
1325  Yii::app()->user->userModel = $superUser;
1326 
1327  $user = User::getByUsername('rootuser');
1328  $this->assertTrue(UserAccessUtil::resolveCanCurrentUserAccessRootUser($user));
1329 
1330  $user->setIsRootUser();
1331  $this->assertTrue($user->save());
1332  unset($user);
1333  $user = User::getByUsername('rootuser');
1334  $this->assertFalse(UserAccessUtil::resolveCanCurrentUserAccessRootUser($user, false));
1335 
1336  $user = new User();
1337  $user->username = 'rootuser2';
1338  $user->title->value = 'Mr.';
1339  $user->firstName = 'My';
1340  $user->lastName = 'rootuser2';
1341  $user->setPassword('myuser');
1342  $this->assertTrue($user->save());
1343  unset($user);
1344 
1345  //Get root user count
1346  $this->assertEquals(1, User::getRootUserCount());
1347 
1348  //Take care that only root user could be there
1349  $user = User::getByUsername('rootuser2');
1350  try
1351  {
1352  $user->setIsRootUser();
1353  }
1354  catch (Exception $e)
1355  {
1356  $this->assertEquals('ExistingRootUserException', get_class($e));
1357  }
1358  }
1359 
1364  {
1365  $user = new User();
1366  $user->username = 'sysuser';
1367  $user->title->value = 'Mr.';
1368  $user->firstName = 'My';
1369  $user->lastName = 'sysuser';
1370  $user->setPassword('myuser');
1371  $this->assertTrue($user->save());
1372  unset($user);
1373 
1374  $user = User::getByUsername('sysuser');
1375  $this->assertNull($user->isSystemUser);
1376  unset($user);
1377 
1378  //Check active user count
1379  $activeUserCount = User::getActiveUserCount();
1380  $this->assertEquals(26, $activeUserCount);
1381 
1382  $user = User::getByUsername('sysuser');
1383  $this->assertTrue(UserAccessUtil::resolveAccessingASystemUser($user));
1384 
1385  $user->setIsSystemUser();
1386  $this->assertTrue($user->save());
1387  unset($user);
1388  $user = User::getByUsername('sysuser');
1389  $this->assertFalse(UserAccessUtil::resolveAccessingASystemUser($user, false));
1390 
1391  //As the user has been made a system user so count should reduce
1392  $activeUserCount = User::getActiveUserCount();
1393  $this->assertEquals(25, $activeUserCount);
1394 
1395  $user = User::getByUsername('rootuser');
1396  $user->setIsNotRootUser();
1397  $this->assertTrue($user->save());
1398  unset($user);
1399 
1400  //As the user removed from root user so count should increase
1401  $activeUserCount = User::getActiveUserCount();
1402  $this->assertEquals(26, $activeUserCount);
1403  }
1404 
1408  public function testGetUsersByEmailAddress()
1409  {
1410  $user = UserTestHelper::createBasicUserWithEmailAddress("emailhideuser");
1411  $user->hideFromSelecting = true;
1412  $this->assertTrue($user->save());
1413  unset($user);
1414  $users = UserSearch::getUsersByEmailAddress("emailhideuser@zurmo.com", null, false);
1415  $this->assertEquals(true, (bool)$users[0]->hideFromSelecting);
1416  $this->assertEquals(1, count($users));
1417 
1418  $users = UserSearch::getUsersByEmailAddress("emailhideuser@zurmo.com", null, true);
1419  $this->assertEquals(0, count($users));
1420  }
1421 
1426  {
1427  $user = UserTestHelper::createBasicUserWithEmailAddress("partialhideuser");
1428  $user->hideFromSelecting = true;
1429  $this->assertTrue($user->save());
1430  unset($user);
1431  $users = UserSearch::getUsersByPartialFullName("partial", 1);
1432  $this->assertEquals(0, count($users));
1433 
1434  $user = User::getByUsername('partialhideuser');
1435  $user->hideFromSelecting = false;
1436  $this->assertTrue($user->save());
1437  unset($user);
1438  $users = UserSearch::getUsersByPartialFullName("partial", 1);
1439  $this->assertEquals(1, count($users));
1440  }
1441 
1446  {
1447  $nonSystemUsersStateMetadataAdapter = new NonSystemUsersStateMetadataAdapter(array('clauses' => array(), 'structure' => ''));
1448  $metadata = $nonSystemUsersStateMetadataAdapter->getAdaptedDataProviderMetadata();
1449  $this->assertEquals('(1 or 2)', $metadata['structure']);
1450 
1451  $nonSystemUsersStateMetadataAdapter1 = new NonSystemUsersStateMetadataAdapter(array('clauses' => array(), 'structure' => 'x and y'));
1452  $metadata = $nonSystemUsersStateMetadataAdapter1->getAdaptedDataProviderMetadata();
1453  $this->assertEquals('(x and y) and (1 or 2)', $metadata['structure']);
1454  }
1455 
1456  public function testIsSuperAdministrator()
1457  {
1458  $userA = User::getByUsername('super');
1459  $userB = User::getByUsername('dick');
1460  $this->assertTrue($userA->isSuperAdministrator());
1461  $this->assertFalse($userB->isSuperAdministrator());
1462  }
1463 
1464  public function testInactiveUsers()
1465  {
1466  $activeUserCount = User::getActiveUserCount();
1467  $this->assertEquals(28, $activeUserCount);
1468  $this->assertCount(28, User::getActiveUsers());
1469  $user = new User();
1470  $user->username = 'inactiveuser';
1471  $user->title->value = 'Mr.';
1472  $user->firstName = 'My';
1473  $user->lastName = 'inactiveuser';
1474  $user->setPassword('myuser');
1475  $user->setIsSystemUser();
1476  $this->assertTrue($user->save());
1477  $this->assertEquals(28, $activeUserCount);
1478  $this->assertCount(28, User::getActiveUsers());
1479  }
1480 
1481  public function testMakeActiveUsersQuerySearchAttributeData()
1482  {
1483  $searchAttributeData = User::makeActiveUsersQuerySearchAttributeData();
1484  $compareData = array(
1485  'clauses' => array(
1486  1 => array(
1487  "attributeName" => "isActive",
1488  "operatorType" => "equals",
1489  "value" => true
1490  ),
1491  2 => array(
1492  "attributeName" => "isSystemUser",
1493  "operatorType" => "equals",
1494  "value" => 0
1495  ),
1496  3 => array(
1497  "attributeName" => "isSystemUser",
1498  "operatorType" => "isNull",
1499  "value" => null
1500  ),
1501  4 => array(
1502  "attributeName" => "isRootUser",
1503  "operatorType" => "equals",
1504  "value" => 0
1505  ),
1506  5 => array(
1507  "attributeName" => "isRootUser",
1508  "operatorType" => "isNull",
1509  "value" => null
1510  )
1511  ),
1512  'structure' => "1 and (2 or 3) and (4 or 5)"
1513  );
1514  $this->assertEquals($compareData, $searchAttributeData);
1515  $searchAttributeData = User::makeActiveUsersQuerySearchAttributeData(false);
1516  $compareData = array(
1517  'clauses' => array(
1518  1 => array(
1519  "attributeName" => "isActive",
1520  "operatorType" => "equals",
1521  "value" => true
1522  ),
1523  2 => array(
1524  "attributeName" => "isSystemUser",
1525  "operatorType" => "equals",
1526  "value" => 0
1527  ),
1528  3 => array(
1529  "attributeName" => "isSystemUser",
1530  "operatorType" => "isNull",
1531  "value" => null
1532  ),
1533  4 => array(
1534  "attributeName" => "isRootUser",
1535  "operatorType" => "equals",
1536  "value" => 0
1537  ),
1538  5 => array(
1539  "attributeName" => "isRootUser",
1540  "operatorType" => "isNull",
1541  "value" => null
1542  )
1543  ),
1544  'structure' => "1 and (2 or 3) and (4 or 5)"
1545  );
1546  $this->assertEquals($compareData, $searchAttributeData);
1547  $searchAttributeData = User::makeActiveUsersQuerySearchAttributeData(true);
1548  $compareData = array(
1549  'clauses' => array(
1550  1 => array(
1551  "attributeName" => "isActive",
1552  "operatorType" => "equals",
1553  "value" => true
1554  ),
1555  2 => array(
1556  "attributeName" => "isSystemUser",
1557  "operatorType" => "equals",
1558  "value" => 0
1559  ),
1560  3 => array(
1561  "attributeName" => "isSystemUser",
1562  "operatorType" => "isNull",
1563  "value" => null
1564  ),
1565  ),
1566  'structure' => "1 and (2 or 3)"
1567  );
1568  $this->assertEquals($compareData, $searchAttributeData);
1569  }
1570 
1571  public function testActiveUsers()
1572  {
1573  $activeUserCount = User::getActiveUserCount();
1574  $this->assertEquals(28, $activeUserCount);
1575  $this->assertCount(28, User::getActiveUsers());
1576 
1577  $activeUserCount = User::getActiveUserCount(false);
1578  $this->assertEquals(28, $activeUserCount);
1579  $this->assertCount(28, User::getActiveUsers(false));
1580 
1581  $activeUserCount = User::getActiveUserCount(true);
1582  $this->assertEquals(28, $activeUserCount);
1583  $this->assertCount(28, User::getActiveUsers(true));
1584 
1585  $user = User::getByUsername('rootuser');
1586  $this->assertTrue(UserAccessUtil::resolveCanCurrentUserAccessRootUser($user));
1587  $user->setIsRootUser();
1588  $this->assertTrue($user->save());
1589  unset($user);
1590 
1591  $activeUserCount = User::getActiveUserCount();
1592  $this->assertEquals(27, $activeUserCount);
1593  $this->assertCount(27, User::getActiveUsers());
1594 
1595  $activeUserCount = User::getActiveUserCount(false);
1596  $this->assertEquals(27, $activeUserCount);
1597  $this->assertCount(27, User::getActiveUsers(false));
1598 
1599  $activeUserCount = User::getActiveUserCount(true);
1600  $this->assertEquals(28, $activeUserCount);
1601  $this->assertCount(28, User::getActiveUsers(true));
1602  }
1603 
1604  public function testLogAuditEventsForIsActive()
1605  {
1606  $user = new User();
1607  $user->username = 'testlogauditforisactive';
1608  $user->title->value = 'Mr.';
1609  $user->firstName = 'My';
1610  $user->lastName = 'testlogauditforisactive';
1611  $user->setPassword('testlogauditforisactive');
1612  $this->assertTrue($user->save());
1613  unset($user);
1614 
1615  $user = User::getByUsername('testlogauditforisactive');
1616  $this->assertEquals(1, $user->isActive);
1617  unset($user);
1618 
1620 
1621  //Change the user's status to inactive and confirm new audit event is created
1622  $user = User::getByUsername('testlogauditforisactive');
1623  $user->setRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB, RIGHT::DENY);
1624  $this->assertTrue($user->save());
1625  $this->assertEquals(0, $user->isActive);
1626  $auditEvents = AuditEvent::getAll();
1627  $this->assertCount(1, $auditEvents);
1628  $this->assertContains('Item Modified', strval($auditEvents[0]));
1629  unset($user);
1630 
1631  //Now change the user's status back to active and confirm new audit event is created
1632  $user = User::getByUsername('testlogauditforisactive');
1633  $user->setRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB, RIGHT::ALLOW);
1634  $this->assertTrue($user->save());
1635  $this->assertEquals(1, $user->isActive);
1636  $auditEvents = AuditEvent::getAll();
1637  $this->assertCount(2, $auditEvents);
1638  $this->assertContains('Item Modified', strval($auditEvents[1]));
1639  unset($user);
1640  }
1641 
1642  public function testSetMetadata()
1643  {
1644  $metadata = User::getMetadata();
1645  $this->assertArrayHasKey('Person', $metadata);
1646  $this->assertNotEmpty($metadata['Person']);
1647  $this->assertArrayHasKey('User', $metadata);
1648  $this->assertNotEmpty($metadata['User']);
1649  $personMetaData = $metadata['Person'];
1650  $userMetaData = $metadata['User'];
1651  $this->assertArrayHasKey('members', $personMetaData);
1652  $this->assertCount(7, $personMetaData['members']);
1653  $this->assertArrayHasKey('members', $userMetaData);
1654  $this->assertCount(12, $userMetaData['members']);
1655 
1656  // unset a member from person, update metadata
1657  unset($personMetaData['members'][0]);
1658  User::setMetadata(array('Person' => $personMetaData));
1659 
1660  // ensure metadata update has propagated
1661  $metadata = User::getMetadata();
1662  $this->assertArrayHasKey('Person', $metadata);
1663  $this->assertNotEmpty($metadata['Person']);
1664  $this->assertArrayHasKey('User', $metadata);
1665  $this->assertNotEmpty($metadata['User']);
1666  $personMetaData = $metadata['Person'];
1667  $userMetaData = $metadata['User'];
1668  $this->assertArrayHasKey('members', $personMetaData);
1669  $this->assertCount(6, $personMetaData['members']);
1670  $this->assertArrayHasKey('members', $userMetaData);
1671  $this->assertCount(12, $userMetaData['members']);
1672 
1673  // unset a member from User, update metadata
1674  unset($userMetaData['members'][0]);
1675  User::setMetadata(array('User' => $userMetaData));
1676 
1677  // ensure metadata update has propagated
1678  $metadata = User::getMetadata();
1679  $this->assertArrayHasKey('Person', $metadata);
1680  $this->assertNotEmpty($metadata['Person']);
1681  $this->assertArrayHasKey('User', $metadata);
1682  $this->assertNotEmpty($metadata['User']);
1683  $personMetaData = $metadata['Person'];
1684  $userMetaData = $metadata['User'];
1685  $this->assertArrayHasKey('members', $personMetaData);
1686  $this->assertCount(6, $personMetaData['members']);
1687  $this->assertArrayHasKey('members', $userMetaData);
1688  $this->assertCount(11, $userMetaData['members']);
1689 
1690  // unset a member from User and Person, update metadata
1691  unset($userMetaData['members'][1]);
1692  unset($personMetaData['members'][1]);
1693  User::setMetadata(array('Person' => $personMetaData, 'User' => $userMetaData));
1694 
1695  // ensure metadata update has propagated
1696  $metadata = User::getMetadata();
1697  $this->assertArrayHasKey('Person', $metadata);
1698  $this->assertNotEmpty($metadata['Person']);
1699  $this->assertArrayHasKey('User', $metadata);
1700  $this->assertNotEmpty($metadata['User']);
1701  $personMetaData = $metadata['Person'];
1702  $userMetaData = $metadata['User'];
1703  $this->assertArrayHasKey('members', $personMetaData);
1704  $this->assertCount(5, $personMetaData['members']);
1705  $this->assertArrayHasKey('members', $userMetaData);
1706  $this->assertCount(10, $userMetaData['members']);
1707  }
1708  }
1709 ?>
testNonSystemUsersStateMetadataAdapter()
Definition: UserTest.php:1445
testCannotSetIsSystemUserDirectlyOnModel()
Definition: UserTest.php:55
testGetByUsernameForNonExistentUsername()
Definition: UserTest.php:229
static getModel($modelIdentifier)
testSearchByPartialName()
Definition: UserTest.php:237
Definition: Role.php:37
static getByUsername($username)
Definition: User.php:49
testSaveExistingUserWithUsersIdAsManagerId()
Definition: UserTest.php:498
Definition: User.php:37
static forgetAll($onlyForgetPhpCache=false)
static forgetAll()
testGetLabel()
Definition: UserTest.php:218
testGetUsersByEmailAddress()
Definition: UserTest.php:1408
static cacheModel(RedBeanModel $model)
testCreateWithTitleThenClearTitleWithSetAttributesWithEmptyId()
Definition: UserTest.php:370
testIsSystemUserAndActiveUserCountOnUserSave()
Definition: UserTest.php:1363
testIsActiveOnUserSave()
Definition: UserTest.php:1137
testCreateWithTitleThenClearTitleDirectly()
Definition: UserTest.php:348
static addPointsByPointData(User $user, $pointTypeAndValueData)
testSearchByPartialNameWithFirstNamePlusPartialLastName()
Definition: UserTest.php:303
testUserNamePolicyValidatesCorrectlyOnDifferentScenarios()
Definition: UserTest.php:880
testIsRootUserOnUserSave()
Definition: UserTest.php:1309
static getSelectableLocaleIds()
static getModelClassNames()
Definition: Module.php:565
static getByName($name, $shouldCache=true)
testSaveExistingUserWithFakePost()
Definition: UserTest.php:456
testCanRemoveRoleFromUser()
Definition: UserTest.php:684
testDeleteLastUserInSuperAdministratorsGroup()
Definition: UserTest.php:1121
Definition: Group.php:37
static getByName($name)
Definition: Group.php:57
testCreateUserWithRelatedUser()
Definition: UserTest.php:167
static getUsersByPartialFullName($partialName, $pageSize, $autoCompleteOptions=null)
Definition: UserSearch.php:47
static deleteAll()
testPasswordUserNamePolicyChangesValidationAndLogin()
Definition: UserTest.php:737
static getCount(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $where=null, $modelClassName=null, $selectDistinct=false)
testHideFromSelectingOnUserSave()
Definition: UserTest.php:1230
static makeActiveUsersQuerySearchAttributeData($includeRootUser=false)
Definition: User.php:1143
static getById($id, $modelClassName=null)
testCreateWithTitleThenClearTitleWithSetAttributesWithRealId()
Definition: UserTest.php:416
testDeleteUserCascadesToDeleteEverythingItShould()
Definition: UserTest.php:614
testCreateWithTitleThenClearTitleWithSetAttributesWithNullId()
Definition: UserTest.php:393
static getActiveUsers($includeRootUser=false)
Definition: User.php:1185
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
testHideFromLeaderboardOnUserSave()
Definition: UserTest.php:1266
static getUsersByEmailAddress($emailAddress, $operatorType=null, $filterOutHideFromSelecting=false, $autoCompleteOptions=null, $pageSize=null)
Definition: UserSearch.php:104
testSavingExistingUserDoesntCreateRelatedBlankUsers()
Definition: UserTest.php:949
static authenticate($username, $password)
Definition: User.php:72
testCreateAndGetUserByIdThatDoesntExist()
Definition: UserTest.php:201
static getUserLeaderboardData($type, $startingRank=1, $offset=null, $count=null, User $user=null)
testGetUsersByPartialFullName()
Definition: UserTest.php:1425
testGetByUsername()
Definition: UserTest.php:209
Generated on Sun May 24 2020 07:10:35