All Data Structures Functions Variables Pages
AccountReadPermissionsSubscriptionUtilTest.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 
38  {
39  protected static $johnny;
40 
41  protected static $billy;
42 
43  protected static $david;
44 
45  public static function setUpBeforeClass()
46  {
47  parent::setUpBeforeClass();
48  SecurityTestHelper::createSuperAdmin();
49  self::$johnny = UserTestHelper::createBasicUser('Johnny');
50  self::$billy = UserTestHelper::createBasicUser('Billy');
51  self::$david = UserTestHelper::createBasicUser('David');
52  Yii::app()->readPermissionSubscriptionObserver->enabled = true;
53 
54  $role1 = new Role();
55  $role1->name = 'Role1';
56  $saved = $role1->save();
57  assert('$saved'); // Not Coding Standard
58 
59  $role2 = new Role();
60  $role2->name = 'Role2';
61  $saved = $role2->save();
62  assert('$saved'); // Not Coding Standard
63 
64  $role3 = new Role();
65  $role3->name = 'Role3';
66  $saved = $role3->save();
67  assert('$saved'); // Not Coding Standard
68 
69  $role4 = new Role();
70  $role4->name = 'Role4';
71  $saved = $role4->save();
72  assert('$saved'); // Not Coding Standard
73 
74  $role5 = new Role();
75  $role5->name = 'Role5';
76  $saved = $role5->save();
77  assert('$saved'); // Not Coding Standard
78 
79  $role3->roles->add($role2);
80  $role2->roles->add($role1);
81  $role5->roles->add($role4);
82  $saved = $role3->save();
83  assert('$saved'); // Not Coding Standard
84  $saved = $role2->save();
85  assert('$saved'); // Not Coding Standard
86  $saved = $role5->save();
87  assert('$saved'); // Not Coding Standard
88  }
89 
90  public function setUp()
91  {
92  parent::setUp();
93  }
94 
95  public static function tearDownAfterClass()
96  {
97  Yii::app()->readPermissionSubscriptionObserver->enabled = false;
98  parent::tearDownAfterClass();
99  }
100 
108  {
109  $super = User::getByUsername('super');
110  Yii::app()->user->userModel = $super;
111  $johnny = self::$johnny;
114  Yii::app()->jobQueue->deleteAll();
115 
116  $this->deleteAllModelsAndRecordsFromReadPermissionTable('Account');
117  $account = AccountTestHelper::createAccountByNameForOwner('First Account', $super);
118  Yii::app()->jobQueue->deleteAll();
119  sleep(1);
120 
121  $queuedJobs = Yii::app()->jobQueue->getAll();
122  $this->assertEquals(0, count($queuedJobs));
123  Yii::app()->jobQueue->deleteAll();
124  $this->assertTrue($job->run());
125 
126  $sql = "SELECT * FROM account_read_subscription order by userid";
127  $rows = ZurmoRedBean::getAll($sql);
128  $this->assertEquals(1, count($rows));
129  $this->assertEquals($super->id, $rows[0]['userid']);
130  $this->assertEquals($account->id, $rows[0]['modelid']);
131  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
132 
133  $group = new Group();
134  $group->name = 'Group1';
135  $this->assertTrue($group->save());
136 
137  //$group->users->add($johnny);
138  //$this->assertTrue($group->save());
139  // We need to add user to group using GroupUserMembershipForm, so ReadPermissionsSubscriptionUtil::userAddedToGroup(); will be triggered
140  $form = new GroupUserMembershipForm();
141  $fakePostData = array(
142  'userMembershipData' => array(0 => $johnny->id),
143  'userNonMembershipData' => array()
144  );
145  $form = GroupUserMembershipFormUtil::setFormFromCastedPost($form, $fakePostData);
147  $this->assertTrue($saved);
148 
149  // Because we save group, new queued job will be created, but read permission table should stay same
150  $queuedJobs = Yii::app()->jobQueue->getAll();
151  $this->assertEquals(1, count($queuedJobs[5]));
152  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
153  Yii::app()->jobQueue->deleteAll();
154  $sql = "SELECT * FROM account_read_subscription order by userid";
155  $rows = ZurmoRedBean::getAll($sql);
156  $this->assertEquals(1, count($rows));
157  $this->assertEquals($super->id, $rows[0]['userid']);
158  $this->assertEquals($account->id, $rows[0]['modelid']);
159  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
160 
161  // Now add permissions to group
162  $account->addPermissions($group, Permission::READ);
163  $this->assertTrue($account->save());
166 
167  $queuedJobs = Yii::app()->jobQueue->getAll();
168  $this->assertEquals(1, count($queuedJobs[5]));
169  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', $queuedJobs[5][0]['jobType']);
170  Yii::app()->jobQueue->deleteAll();
171  $this->assertTrue($jobBasedOnBuildTable->run());
172 
173  // Because user is added to group, and group have read access to account, this account should be in
174  // read permission table for user
175  $sql = "SELECT * FROM account_read_subscription order by userid";
176  $rows = ZurmoRedBean::getAll($sql);
177  $this->assertEquals(2, count($rows));
178  $this->assertEquals($super->id, $rows[0]['userid']);
179  $this->assertEquals($account->id, $rows[0]['modelid']);
180  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
181  $this->assertEquals($johnny->id, $rows[1]['userid']);
182  $this->assertEquals($account->id, $rows[1]['modelid']);
183  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
184 
185  // Test delete group
186  $group = Group::getByName('Group1');
187  $group->delete();
190  $queuedJobs = Yii::app()->jobQueue->getAll();
191  $this->assertEquals(1, count($queuedJobs[5]));
192  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
193  Yii::app()->jobQueue->deleteAll();
194  $this->assertTrue($job->run());
195 
196  // Because user is added to group, and group have read access to account, this account should be in
197  // read permission table for user
198  $sql = "SELECT * FROM account_read_subscription order by userid";
199  $rows = ZurmoRedBean::getAll($sql);
200  $this->assertEquals(2, count($rows));
201  $this->assertEquals($super->id, $rows[0]['userid']);
202  $this->assertEquals($account->id, $rows[0]['modelid']);
203  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
204  $this->assertEquals($johnny->id, $rows[1]['userid']);
205  $this->assertEquals($account->id, $rows[1]['modelid']);
206  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_DELETE, $rows[1]['subscriptiontype']);
207  }
208 
215  {
216  $super = User::getByUsername('super');
217  Yii::app()->user->userModel = $super;
220  Yii::app()->jobQueue->deleteAll();
221  $this->deleteAllModelsAndRecordsFromReadPermissionTable('Account');
222  $johnny = self::$johnny;
223 
224  $account = AccountTestHelper::createAccountByNameForOwner('Second Account', $super);
225  Yii::app()->jobQueue->deleteAll();
226  sleep(1);
227 
228  $group = new Group();
229  $group->name = 'Group2';
230  $this->assertTrue($group->save());
231  $group->users->add($johnny);
232  $this->assertTrue($group->save());
233 
234  $account->addPermissions($group, Permission::READ);
235  $this->assertTrue($account->save());
238  AllPermissionsOptimizationCache::forgetAll();
239 
240  $queuedJobs = Yii::app()->jobQueue->getAll();
241  $this->assertEquals(1, count($queuedJobs[5]));
242  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', $queuedJobs[5][0]['jobType']);
243  Yii::app()->jobQueue->deleteAll();
244  $this->assertTrue($jobBasedOnBuildTable->run());
245 
246  // Check if everything is added correctly
247  $sql = "SELECT * FROM account_read_subscription order by userid";
248  $rows = ZurmoRedBean::getAll($sql);
249  $this->assertEquals(2, count($rows));
250  $this->assertEquals($super->id, $rows[0]['userid']);
251  $this->assertEquals($account->id, $rows[0]['modelid']);
252  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
253  $this->assertEquals($johnny->id, $rows[1]['userid']);
254  $this->assertEquals($account->id, $rows[1]['modelid']);
255  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
256 
257  // Remove user from group
258  //$group->users->remove($johnny);
259  //$this->assertTrue($group->save());
260  $form = new GroupUserMembershipForm();
261  $fakePostData = array(
262  'userMembershipData' => array(),
263  'userNonMembershipData' => array()
264  );
265  $form = GroupUserMembershipFormUtil::setFormFromCastedPost($form, $fakePostData);
267  $this->assertTrue($saved);
270 
271  $queuedJobs = Yii::app()->jobQueue->getAll();
272  $this->assertEquals(1, count($queuedJobs[5]));
273  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
274  Yii::app()->jobQueue->deleteAll();
275  $this->assertTrue($job->run());
276 
277  // Because user is added to group, and group have read access to account, this account should be in
278  // read permission table for user
279  $sql = "SELECT * FROM account_read_subscription order by userid";
280  $rows = ZurmoRedBean::getAll($sql);
281  $this->assertEquals(2, count($rows));
282  $this->assertEquals($super->id, $rows[0]['userid']);
283  $this->assertEquals($account->id, $rows[0]['modelid']);
284  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
285  $this->assertEquals($johnny->id, $rows[1]['userid']);
286  $this->assertEquals($account->id, $rows[1]['modelid']);
287  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_DELETE, $rows[1]['subscriptiontype']);
288 
289  // Now add user to group again and test
290  //$group->users->add($johnny);
291  //$this->assertTrue($group->save());
292  // We need to add user to group using GroupUserMembershipForm, so ReadPermissionsSubscriptionUtil::userAddedToGroup(); will be triggered
293  $form = new GroupUserMembershipForm();
294  $fakePostData = array(
295  'userMembershipData' => array(0 => $johnny->id),
296  'userNonMembershipData' => array()
297  );
298  $form = GroupUserMembershipFormUtil::setFormFromCastedPost($form, $fakePostData);
300  $this->assertTrue($saved);
303 
304  $queuedJobs = Yii::app()->jobQueue->getAll();
305  $this->assertEquals(1, count($queuedJobs[5]));
306  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
307  Yii::app()->jobQueue->deleteAll();
308  $this->assertTrue($job->run());
309 
310  // Because user is added to group, and group have read access to account, this account should be in
311  // read permission table for user
312  $sql = "SELECT * FROM account_read_subscription order by userid";
313  $rows = ZurmoRedBean::getAll($sql);
314  $this->assertEquals(2, count($rows));
315  $this->assertEquals($super->id, $rows[0]['userid']);
316  $this->assertEquals($account->id, $rows[0]['modelid']);
317  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
318  $this->assertEquals($johnny->id, $rows[1]['userid']);
319  $this->assertEquals($account->id, $rows[1]['modelid']);
320  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
321  }
322 
328  {
329  $super = User::getByUsername('super');
330  Yii::app()->user->userModel = $super;
332 
333  $johnny = self::$johnny;
334  $group = Group::getByName('Group2');
335  $accounts = Account::getByName('Second Account');
336  $account = $accounts[0];
337 
338  $account->removePermissions($group, Permission::READ);
339  $this->assertTrue($account->save());
342 
343  $queuedJobs = Yii::app()->jobQueue->getAll();
344  $this->assertEquals(1, count($queuedJobs[5]));
345  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', $queuedJobs[5][0]['jobType']);
346  Yii::app()->jobQueue->deleteAll();
347  $this->assertTrue($jobBasedOnBuildTable->run());
348 
349  // Because user is added to group, and group have read access to account, this account should be in
350  // read permission table for user
351  $sql = "SELECT * FROM account_read_subscription order by userid";
352  $rows = ZurmoRedBean::getAll($sql);
353  $this->assertEquals(2, count($rows));
354  $this->assertEquals($super->id, $rows[0]['userid']);
355  $this->assertEquals($account->id, $rows[0]['modelid']);
356  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
357  $this->assertEquals($johnny->id, $rows[1]['userid']);
358  $this->assertEquals($account->id, $rows[1]['modelid']);
359  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_DELETE, $rows[1]['subscriptiontype']);
360  }
361 
366  {
367  $super = User::getByUsername('super');
368  Yii::app()->user->userModel = $super;
371 
372  $johnny = self::$johnny;
373  $this->deleteAllModelsAndRecordsFromReadPermissionTable('Account');
374 
375  $account = AccountTestHelper::createAccountByNameForOwner('Third Account', $super);
376  Yii::app()->jobQueue->deleteAll();
377  sleep(1);
378 
379  $parentGroup = new Group();
380  $parentGroup->name = 'Parent';
381  $this->assertTrue($parentGroup->save());
382 
383  $group = new Group();
384  $group->name = 'Child';
385  $group->group = $parentGroup;
386  $saved = $group->save();
387  $this->assertTrue($saved);
388  $group->users->add($johnny);
389  $this->assertTrue($group->save());
390 
391  $queuedJobs = Yii::app()->jobQueue->getAll();
392  $this->assertEquals(1, count($queuedJobs[5]));
393  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
394  Yii::app()->jobQueue->deleteAll();
395  $this->assertTrue($job->run());
396 
397  $sql = "SELECT * FROM account_read_subscription order by userid";
398  $rows = ZurmoRedBean::getAll($sql);
399  $this->assertEquals(1, count($rows));
400  $this->assertEquals($super->id, $rows[0]['userid']);
401  $this->assertEquals($account->id, $rows[0]['modelid']);
402  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
403 
404  // Add permissions for parentGroup to READ account
405  $account->addPermissions($parentGroup, Permission::READ);
406  $this->assertTrue($account->save());
409 
410  $queuedJobs = Yii::app()->jobQueue->getAll();
411  $this->assertEquals(1, count($queuedJobs[5]));
412  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', $queuedJobs[5][0]['jobType']);
413  Yii::app()->jobQueue->deleteAll();
414  $this->assertTrue($jobBasedOnBuildTable->run());
415 
416  $sql = "SELECT * FROM account_read_subscription order by userid";
417  $rows = ZurmoRedBean::getAll($sql);
418  $this->assertEquals(2, count($rows));
419  $this->assertEquals($super->id, $rows[0]['userid']);
420  $this->assertEquals($account->id, $rows[0]['modelid']);
421  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
422  $this->assertEquals($johnny->id, $rows[1]['userid']);
423  $this->assertEquals($account->id, $rows[1]['modelid']);
424  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
425 
426  // Remove permissions from parentGroup to READ account
427  $account->removePermissions($parentGroup, Permission::READ);
428  $this->assertTrue($account->save());
431 
432  $queuedJobs = Yii::app()->jobQueue->getAll();
433  $this->assertEquals(1, count($queuedJobs[5]));
434  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', $queuedJobs[5][0]['jobType']);
435  Yii::app()->jobQueue->deleteAll();
436  $this->assertTrue($jobBasedOnBuildTable->run());
437 
438  $sql = "SELECT * FROM account_read_subscription order by userid";
439  $rows = ZurmoRedBean::getAll($sql);
440  $this->assertEquals(2, count($rows));
441  $this->assertEquals($super->id, $rows[0]['userid']);
442  $this->assertEquals($account->id, $rows[0]['modelid']);
443  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
444  $this->assertEquals($johnny->id, $rows[1]['userid']);
445  $this->assertEquals($account->id, $rows[1]['modelid']);
446  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_DELETE, $rows[1]['subscriptiontype']);
447 
448  // Test parent group adding/removing
449  $account->addPermissions($parentGroup, Permission::READ);
450  $this->assertTrue($account->save());
453 
454  $queuedJobs = Yii::app()->jobQueue->getAll();
455  $this->assertEquals(1, count($queuedJobs[5]));
456  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', $queuedJobs[5][0]['jobType']);
457  Yii::app()->jobQueue->deleteAll();
458  $this->assertTrue($jobBasedOnBuildTable->run());
459 
460  $sql = "SELECT * FROM account_read_subscription order by userid";
461  $rows = ZurmoRedBean::getAll($sql);
462  $this->assertEquals(2, count($rows));
463  $this->assertEquals($super->id, $rows[0]['userid']);
464  $this->assertEquals($account->id, $rows[0]['modelid']);
465  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
466  $this->assertEquals($johnny->id, $rows[1]['userid']);
467  $this->assertEquals($account->id, $rows[1]['modelid']);
468  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
469 
470  // Delete parent group
471  $parentGroup->delete();
474 
475  $queuedJobs = Yii::app()->jobQueue->getAll();
476  $this->assertEquals(1, count($queuedJobs[5]));
477  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
478  Yii::app()->jobQueue->deleteAll();
479  $this->assertTrue($job->run());
480 
481  $sql = "SELECT * FROM account_read_subscription order by userid";
482  $rows = ZurmoRedBean::getAll($sql);
483  $this->assertEquals(2, count($rows));
484  $this->assertEquals($super->id, $rows[0]['userid']);
485  $this->assertEquals($account->id, $rows[0]['modelid']);
486  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
487  $this->assertEquals($johnny->id, $rows[1]['userid']);
488  $this->assertEquals($account->id, $rows[1]['modelid']);
489  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_DELETE, $rows[1]['subscriptiontype']);
490 
491  // Now test adding parent group
492  $group->forget();
493  $group = Group::getByName('Child');
494  $accountId = $account->id;
495  $account->forget();
496  $account = Account::getById($accountId);
497  $parentGroup2 = new Group();
498  $parentGroup2->name = 'Parent';
499  $this->assertTrue($parentGroup2->save());
500 
501  $group->group = $parentGroup2;
502  $saved = $group->save();
503  $this->assertTrue($saved);
504 
505  $queuedJobs = Yii::app()->jobQueue->getAll();
506  $this->assertEquals(1, count($queuedJobs[5]));
507  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
508  Yii::app()->jobQueue->deleteAll();
509  $this->assertTrue($job->run());
510 
511  $account->addPermissions($parentGroup2, Permission::READ);
512  $this->assertTrue($account->save());
515 
516  $queuedJobs = Yii::app()->jobQueue->getAll();
517  $this->assertEquals(1, count($queuedJobs[5]));
518  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', $queuedJobs[5][0]['jobType']);
519  Yii::app()->jobQueue->deleteAll();
520  $this->assertTrue($jobBasedOnBuildTable->run());
521 
522  $sql = "SELECT * FROM account_read_subscription order by userid";
523  $rows = ZurmoRedBean::getAll($sql);
524  $this->assertEquals(2, count($rows));
525  $this->assertEquals($super->id, $rows[0]['userid']);
526  $this->assertEquals($account->id, $rows[0]['modelid']);
527  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
528  $this->assertEquals($johnny->id, $rows[1]['userid']);
529  $this->assertEquals($account->id, $rows[1]['modelid']);
530  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
531  }
532 
533  // Test when module permissions for group changes
534  // It just test if job is triggered, we do not test generated read permission subscription table data
535  public function testGroupChangeOrDeleteScenario5()
536  {
537  $super = User::getByUsername('super');
538  Yii::app()->user->userModel = $super;
541 
542  $johnny = self::$johnny;
543  $this->deleteAllModelsAndRecordsFromReadPermissionTable('Account');
544 
545  $account = AccountTestHelper::createAccountByNameForOwner('Fifth Account', $super);
546  Yii::app()->jobQueue->deleteAll();
547  sleep(1);
548 
549  $group = new Group();
550  $group->name = 'Group5';
551  $this->assertTrue($group->save());
552  $group->users->add($johnny);
553  $this->assertTrue($group->save());
554  Yii::app()->jobQueue->deleteAll();
555 
556  $fakePost = array(
557  'AccountsModule__' . Permission::CHANGE_PERMISSIONS => strval(Permission::ALLOW),
558  );
559  $validatedPost = ModulePermissionsFormUtil::typeCastPostData($fakePost);
560  $saved = ModulePermissionsFormUtil::setPermissionsFromCastedPost($validatedPost, $group);
561  $this->assertTrue($saved);
562 
563  $queuedJobs = Yii::app()->jobQueue->getAll();
564  $this->assertEquals(1, count($queuedJobs[5]));
565  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
566  Yii::app()->jobQueue->deleteAll();
567  $this->assertTrue($job->run());
568  }
569 
575  {
576  $super = User::getByUsername('super');
577  Yii::app()->user->userModel = $super;
578  $johnny = self::$johnny;
581  Yii::app()->jobQueue->deleteAll();
582 
583  $this->deleteAllModelsAndRecordsFromReadPermissionTable('Account');
584  $account = AccountTestHelper::createAccountByNameForOwner('First Account For Roles', $super);
585  Yii::app()->jobQueue->deleteAll();
586  sleep(1);
587 
588  $queuedJobs = Yii::app()->jobQueue->getAll();
589  $this->assertEquals(0, count($queuedJobs));
590  Yii::app()->jobQueue->deleteAll();
591  $this->assertTrue($job->run());
592 
593  $sql = "SELECT * FROM account_read_subscription order by userid";
594  $rows = ZurmoRedBean::getAll($sql);
595  $this->assertEquals(1, count($rows));
596  $this->assertEquals($super->id, $rows[0]['userid']);
597  $this->assertEquals($account->id, $rows[0]['modelid']);
598  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
599 
600  // Set user role
601  $role1 = Role::getByName('Role1');
602  $johnny->role = $role1;
603  $this->assertTrue($johnny->save());
604 
605  // Because we save role, new queued job will be created, but read permission table should stay same
606  $queuedJobs = Yii::app()->jobQueue->getAll();
607  $this->assertEquals(1, count($queuedJobs[5]));
608  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
609  Yii::app()->jobQueue->deleteAll();
610  $sql = "SELECT * FROM account_read_subscription order by userid";
611  $rows = ZurmoRedBean::getAll($sql);
612  $this->assertEquals(1, count($rows));
613  $this->assertEquals($super->id, $rows[0]['userid']);
614  $this->assertEquals($account->id, $rows[0]['modelid']);
615  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
616 
617  Yii::app()->jobQueue->deleteAll();
618  $johnny->role = null;
619  $this->assertTrue($johnny->save());
620 
621  // Because we save role, new queued job will be created, but read permission table should stay same
622  $queuedJobs = Yii::app()->jobQueue->getAll();
623  $this->assertEquals(1, count($queuedJobs[5]));
624  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
625  Yii::app()->jobQueue->deleteAll();
626  $sql = "SELECT * FROM account_read_subscription order by userid";
627  $rows = ZurmoRedBean::getAll($sql);
628  $this->assertEquals(1, count($rows));
629  $this->assertEquals($super->id, $rows[0]['userid']);
630  $this->assertEquals($account->id, $rows[0]['modelid']);
631  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
632  }
633 
645  {
646  $super = User::getByUsername('super');
647  Yii::app()->user->userModel = $super;
650  Yii::app()->jobQueue->deleteAll();
651 
652  $this->deleteAllModelsAndRecordsFromReadPermissionTable('Account');
653  Yii::app()->jobQueue->deleteAll();
654  sleep(1);
655 
656  $user1 = self::$johnny;
657  $user2 = self::$billy;
658  $user3 = self::$david;
659 
660  $account = AccountTestHelper::createAccountByNameForOwner('Second Account For Roles', $user1);
661  Yii::app()->jobQueue->deleteAll();
662 
663  // Set user role
664  $role1 = Role::getByName('Role1');
665  $role2 = Role::getByName('Role2');
666  $role3 = Role::getByName('Role3');
667  $role4 = Role::getByName('Role4');
668  $role5 = Role::getByName('Role5');
669 
670  $user1->role = $role1;
671  $this->assertTrue($user1->save());
672  Yii::app()->jobQueue->deleteAll();
673  $user2->role = $role3;
674  $this->assertTrue($user2->save());
675  $user3->role = $role5;
676  $this->assertTrue($user3->save());
679 
680  $queuedJobs = Yii::app()->jobQueue->getAll();
681  $this->assertEquals(1, count($queuedJobs[5]));
682  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
683  Yii::app()->jobQueue->deleteAll();
684  $this->assertTrue($job->run());
685 
686  $sql = "SELECT * FROM account_read_subscription order by userid";
687  $rows = ZurmoRedBean::getAll($sql);
688  $this->assertEquals(3, count($rows));
689  $this->assertEquals($super->id, $rows[0]['userid']);
690  $this->assertEquals($account->id, $rows[0]['modelid']);
691  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
692  $this->assertEquals($user1->id, $rows[1]['userid']);
693  $this->assertEquals($account->id, $rows[1]['modelid']);
694  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
695  $this->assertEquals($user2->id, $rows[2]['userid']);
696  $this->assertEquals($account->id, $rows[2]['modelid']);
697  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[2]['subscriptiontype']);
698 
699  // Remove role1 from role2 - this is way how it works in uI
700  //$role2->roles->remove($role1);
701  //$this->assertTrue($role2->save());
702  Yii::app()->jobQueue->deleteAll();
703  $role1->role = null;
704  $this->assertTrue($role1->save());
705  $role1->forget();
708 
709  $queuedJobs = Yii::app()->jobQueue->getAll();
710  $this->assertEquals(1, count($queuedJobs[5]));
711  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
712  Yii::app()->jobQueue->deleteAll();
713  $this->assertTrue($job->run());
714 
715  $sql = "SELECT * FROM account_read_subscription order by userid";
716  $rows = ZurmoRedBean::getAll($sql);
717  $this->assertEquals(3, count($rows));
718  $this->assertEquals($super->id, $rows[0]['userid']);
719  $this->assertEquals($account->id, $rows[0]['modelid']);
720  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
721  $this->assertEquals($user1->id, $rows[1]['userid']);
722  $this->assertEquals($account->id, $rows[1]['modelid']);
723  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
724  $this->assertEquals($user2->id, $rows[2]['userid']);
725  $this->assertEquals($account->id, $rows[2]['modelid']);
726  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_DELETE, $rows[2]['subscriptiontype']);
727 
728  // Now add role4 as parent of role1
729  Yii::app()->jobQueue->deleteAll();
730  $role1 = Role::getByName('Role1');
731  $role1->role = $role4;
732  $this->assertTrue($role1->save());
733  $role4->forgetAll();
734  $role2->forgetAll();
737  $role4 = Role::getByName('Role4');
738  $role2 = Role::getByName('Role2');
739 
740  $queuedJobs = Yii::app()->jobQueue->getAll();
741  $this->assertEquals(1, count($queuedJobs[5]));
742  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
743  Yii::app()->jobQueue->deleteAll();
744  $this->assertTrue($job->run());
745 
746  $sql = "SELECT * FROM account_read_subscription order by userid";
747  $rows = ZurmoRedBean::getAll($sql);
748  $this->assertEquals(4, count($rows));
749  $this->assertEquals($super->id, $rows[0]['userid']);
750  $this->assertEquals($account->id, $rows[0]['modelid']);
751  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
752  $this->assertEquals($user1->id, $rows[1]['userid']);
753  $this->assertEquals($account->id, $rows[1]['modelid']);
754  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
755  $this->assertEquals($user2->id, $rows[2]['userid']);
756  $this->assertEquals($account->id, $rows[2]['modelid']);
757  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_DELETE, $rows[2]['subscriptiontype']);
758  $this->assertEquals($user3->id, $rows[3]['userid']);
759  $this->assertEquals($account->id, $rows[3]['modelid']);
760  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[3]['subscriptiontype']);
761 
762  // Revert role nesting
763  $role4->roles->remove($role1);
764  $this->assertTrue($role4->save());
765  $role2->roles->add($role1);
766  $this->assertTrue($role2->save());
767  }
768 
781  {
782  $super = User::getByUsername('super');
783  Yii::app()->user->userModel = $super;
786  Yii::app()->jobQueue->deleteAll();
787 
788  $this->deleteAllModelsAndRecordsFromReadPermissionTable('Account');
789  Yii::app()->jobQueue->deleteAll();
790  sleep(1);
791 
792  $user1 = self::$johnny;
793  $user2 = self::$billy;
794 
795  $account = AccountTestHelper::createAccountByNameForOwner('Third Account For Roles', $user1);
796  Yii::app()->jobQueue->deleteAll();
797 
798  // Set user role
799  $role1 = Role::getByName('Role1');
800  $role2 = Role::getByName('Role2');
801  $role3 = Role::getByName('Role3');
802 
803  // Just to trigger role changes
804  $user2->role = $role1;
805  $this->assertTrue($user2->save());
806  $user2->forget();
807  $user2 = User::getByUsername('billy');
808 
809  Yii::app()->jobQueue->deleteAll();
810  $user2->role = $role3;
811  $this->assertTrue($user2->save());
814 
815  $queuedJobs = Yii::app()->jobQueue->getAll();
816  $this->assertEquals(1, count($queuedJobs[5]));
817  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
818  Yii::app()->jobQueue->deleteAll();
819  $this->assertTrue($job->run());
820 
821  $sql = "SELECT * FROM account_read_subscription order by userid";
822  $rows = ZurmoRedBean::getAll($sql);
823  $this->assertEquals(3, count($rows));
824  $this->assertEquals($super->id, $rows[0]['userid']);
825  $this->assertEquals($account->id, $rows[0]['modelid']);
826  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
827  $this->assertEquals($user1->id, $rows[1]['userid']);
828  $this->assertEquals($account->id, $rows[1]['modelid']);
829  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
830  $this->assertEquals($user2->id, $rows[2]['userid']);
831  $this->assertEquals($account->id, $rows[2]['modelid']);
832  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[2]['subscriptiontype']);
833 
834  // Delete role2
835  Yii::app()->jobQueue->deleteAll();
836  $this->assertTrue($role2->delete());
839 
840  $queuedJobs = Yii::app()->jobQueue->getAll();
841  $this->assertEquals(1, count($queuedJobs[5]));
842  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
843  Yii::app()->jobQueue->deleteAll();
844  $this->assertTrue($job->run());
845 
846  $sql = "SELECT * FROM account_read_subscription order by userid";
847  $rows = ZurmoRedBean::getAll($sql);
848  $this->assertEquals(3, count($rows));
849  $this->assertEquals($super->id, $rows[0]['userid']);
850  $this->assertEquals($account->id, $rows[0]['modelid']);
851  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
852  $this->assertEquals($user1->id, $rows[1]['userid']);
853  $this->assertEquals($account->id, $rows[1]['modelid']);
854  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
855  $this->assertEquals($user2->id, $rows[2]['userid']);
856  $this->assertEquals($account->id, $rows[2]['modelid']);
857  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_DELETE, $rows[2]['subscriptiontype']);
858 
859  // Set role3 to be parent of role1, $user2 should get access to the account
860  Yii::app()->jobQueue->deleteAll();
861  $role1->role = $role3;
862  $this->assertTrue($role1->save());
865 
866  $queuedJobs = Yii::app()->jobQueue->getAll();
867  $this->assertEquals(1, count($queuedJobs[5]));
868  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
869  Yii::app()->jobQueue->deleteAll();
870  $this->assertTrue($job->run());
871 
872  $sql = "SELECT * FROM account_read_subscription order by userid";
873  $rows = ZurmoRedBean::getAll($sql);
874  $this->assertEquals(3, count($rows));
875  $this->assertEquals($super->id, $rows[0]['userid']);
876  $this->assertEquals($account->id, $rows[0]['modelid']);
877  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
878  $this->assertEquals($user1->id, $rows[1]['userid']);
879  $this->assertEquals($account->id, $rows[1]['modelid']);
880  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
881  $this->assertEquals($user2->id, $rows[2]['userid']);
882  $this->assertEquals($account->id, $rows[2]['modelid']);
883  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[2]['subscriptiontype']);
884 
885  Yii::app()->jobQueue->deleteAll();
886  $this->assertTrue($role3->delete());
889 
890  $queuedJobs = Yii::app()->jobQueue->getAll();
891  $this->assertEquals(1, count($queuedJobs[5]));
892  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
893  Yii::app()->jobQueue->deleteAll();
894  $this->assertTrue($job->run());
895 
896  $sql = "SELECT * FROM account_read_subscription order by userid";
897  $rows = ZurmoRedBean::getAll($sql);
898  $this->assertEquals(3, count($rows));
899  $this->assertEquals($super->id, $rows[0]['userid']);
900  $this->assertEquals($account->id, $rows[0]['modelid']);
901  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
902  $this->assertEquals($user1->id, $rows[1]['userid']);
903  $this->assertEquals($account->id, $rows[1]['modelid']);
904  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
905  $this->assertEquals($user2->id, $rows[2]['userid']);
906  $this->assertEquals($account->id, $rows[2]['modelid']);
907  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_DELETE, $rows[2]['subscriptiontype']);
908 
909  // Revert roles for next test
910  $role2 = new Role();
911  $role2->name = 'Role2';
912  $this->assertTrue($role2->save());
913 
914  $role3 = new Role();
915  $role3->name = 'Role3';
916  $this->assertTrue($role3->save());
917 
918  $role3->roles->add($role2);
919  $role2->roles->add($role1);
920  $this->assertTrue($role3->save());
921  $this->assertTrue($role2->save());
922 
923  $user2->forget();
924  $user2 = User::getByUsername('billy');
925  $user2->role = $role3;
926  $this->assertTrue($user2->save());
929  }
930 
936  {
937  $super = User::getByUsername('super');
938  Yii::app()->user->userModel = $super;
941  Yii::app()->jobQueue->deleteAll();
942 
943  $this->deleteAllModelsAndRecordsFromReadPermissionTable('Account');
944  Yii::app()->jobQueue->deleteAll();
945  sleep(1);
946 
947  $user1 = self::$johnny;
948  $user2 = self::$billy;
949  $user3 = self::$david;
950 
951  $account = AccountTestHelper::createAccountByNameForOwner('Forth Account For Roles', $user1);
952  Yii::app()->jobQueue->deleteAll();
953 
954  // Set user role
955  $role1 = Role::getByName('Role1');
956  $role2 = Role::getByName('Role2');
957  $role3 = Role::getByName('Role3');
958  $role4 = Role::getByName('Role4');
959 
960  // Just to trigger role changes
961  Yii::app()->jobQueue->deleteAll();
962  $user1->role = null;
963  $this->assertTrue($user1->save());
966 
967  $queuedJobs = Yii::app()->jobQueue->getAll();
968  $this->assertEquals(1, count($queuedJobs[5]));
969  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
970  Yii::app()->jobQueue->deleteAll();
971  $this->assertTrue($job->run());
972 
973  $sql = "SELECT * FROM account_read_subscription order by userid";
974  $rows = ZurmoRedBean::getAll($sql);
975  $this->assertEquals(2, count($rows));
976  $this->assertEquals($super->id, $rows[0]['userid']);
977  $this->assertEquals($account->id, $rows[0]['modelid']);
978  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
979  $this->assertEquals($user1->id, $rows[1]['userid']);
980  $this->assertEquals($account->id, $rows[1]['modelid']);
981  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
982 
983  // Now set $role1 for $user1
984  Yii::app()->jobQueue->deleteAll();
985  $user1->role = $role1;
986  $this->assertTrue($user1->save());
989 
990  $queuedJobs = Yii::app()->jobQueue->getAll();
991  $this->assertEquals(1, count($queuedJobs[5]));
992  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
993  Yii::app()->jobQueue->deleteAll();
994  $this->assertTrue($job->run());
995 
996  $sql = "SELECT * FROM account_read_subscription order by userid";
997  $rows = ZurmoRedBean::getAll($sql);
998  $this->assertEquals(3, count($rows));
999  $this->assertEquals($super->id, $rows[0]['userid']);
1000  $this->assertEquals($account->id, $rows[0]['modelid']);
1001  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
1002  $this->assertEquals($user1->id, $rows[1]['userid']);
1003  $this->assertEquals($account->id, $rows[1]['modelid']);
1004  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
1005  $this->assertEquals($user2->id, $rows[2]['userid']);
1006  $this->assertEquals($account->id, $rows[2]['modelid']);
1007  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[2]['subscriptiontype']);
1008 
1009  // Now set $role4 for $user1
1010  Yii::app()->jobQueue->deleteAll();
1011  $user1->role = $role4;
1012  $this->assertTrue($user1->save());
1015 
1016  $queuedJobs = Yii::app()->jobQueue->getAll();
1017  $this->assertEquals(1, count($queuedJobs[5]));
1018  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
1019  Yii::app()->jobQueue->deleteAll();
1020  $this->assertTrue($job->run());
1021 
1022  $sql = "SELECT * FROM account_read_subscription order by userid";
1023  $rows = ZurmoRedBean::getAll($sql);
1024  $this->assertEquals(4, count($rows));
1025  $this->assertEquals($super->id, $rows[0]['userid']);
1026  $this->assertEquals($account->id, $rows[0]['modelid']);
1027  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
1028  $this->assertEquals($user1->id, $rows[1]['userid']);
1029  $this->assertEquals($account->id, $rows[1]['modelid']);
1030  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
1031  $this->assertEquals($user2->id, $rows[2]['userid']);
1032  $this->assertEquals($account->id, $rows[2]['modelid']);
1033  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_DELETE, $rows[2]['subscriptiontype']);
1034  $this->assertEquals($user3->id, $rows[3]['userid']);
1035  $this->assertEquals($account->id, $rows[3]['modelid']);
1036  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[3]['subscriptiontype']);
1037 
1038  // Now set $role1 for $user1
1039  Yii::app()->jobQueue->deleteAll();
1040  $user1->role = null;
1041  $this->assertTrue($user1->save());
1044 
1045  $queuedJobs = Yii::app()->jobQueue->getAll();
1046  $this->assertEquals(1, count($queuedJobs[5]));
1047  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
1048  Yii::app()->jobQueue->deleteAll();
1049  $this->assertTrue($job->run());
1050 
1051  $sql = "SELECT * FROM account_read_subscription order by userid";
1052  $rows = ZurmoRedBean::getAll($sql);
1053  $this->assertEquals(4, count($rows));
1054  $this->assertEquals($super->id, $rows[0]['userid']);
1055  $this->assertEquals($account->id, $rows[0]['modelid']);
1056  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
1057  $this->assertEquals($user1->id, $rows[1]['userid']);
1058  $this->assertEquals($account->id, $rows[1]['modelid']);
1059  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
1060  $this->assertEquals($user2->id, $rows[2]['userid']);
1061  $this->assertEquals($account->id, $rows[2]['modelid']);
1062  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_DELETE, $rows[2]['subscriptiontype']);
1063  $this->assertEquals($user3->id, $rows[3]['userid']);
1064  $this->assertEquals($account->id, $rows[3]['modelid']);
1065  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_DELETE, $rows[3]['subscriptiontype']);
1066  }
1067 
1076  {
1077  $super = User::getByUsername('super');
1078  Yii::app()->user->userModel = $super;
1079  $this->deleteAllModelsAndRecordsFromReadPermissionTable('Account');
1080  Yii::app()->jobQueue->deleteAll();
1083  $account = AccountTestHelper::createAccountByNameForOwner('First Account For Users', $super);
1084  Yii::app()->jobQueue->deleteAll();
1085  sleep(1);
1086 
1087  $user = new User();
1088  $user->username = 'smith';
1089  $user->lastName = 'Smitson';
1090  $user->setPassword(strtolower('password'));
1091  $this->assertTrue($user->save());
1092 
1093  $queuedJobs = Yii::app()->jobQueue->getAll();
1094  $this->assertEquals(1, count($queuedJobs[5]));
1095  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccount', $queuedJobs[5][0]['jobType']);
1096  Yii::app()->jobQueue->deleteAll();
1097  $this->assertTrue($job->run());
1098 
1099  $sql = "SELECT * FROM account_read_subscription order by userid";
1100  $rows = ZurmoRedBean::getAll($sql);
1101  $this->assertEquals(1, count($rows));
1102  $this->assertEquals($super->id, $rows[0]['userid']);
1103  $this->assertEquals($account->id, $rows[0]['modelid']);
1104  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
1105 
1106  // Test add read permissions to user for account
1107  $account->addPermissions($user, Permission::READ);
1108  $this->assertTrue($account->save());
1110 
1111  $queuedJobs = Yii::app()->jobQueue->getAll();
1112  $this->assertEquals(1, count($queuedJobs[5]));
1113  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', $queuedJobs[5][0]['jobType']);
1114  Yii::app()->jobQueue->deleteAll();
1115  $this->assertTrue($jobBasedOnBuildTable->run());
1116 
1117  // Check if everything is added correctly
1118  $sql = "SELECT * FROM account_read_subscription order by userid";
1119  $rows = ZurmoRedBean::getAll($sql);
1120  $this->assertEquals(2, count($rows));
1121  $this->assertEquals($super->id, $rows[0]['userid']);
1122  $this->assertEquals($account->id, $rows[0]['modelid']);
1123  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
1124  $this->assertEquals($user->id, $rows[1]['userid']);
1125  $this->assertEquals($account->id, $rows[1]['modelid']);
1126  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
1127 
1128  // Test remove read permissions from user for account
1129  $account->removePermissions($user, Permission::READ);
1130  $this->assertTrue($account->save());
1132 
1133  $queuedJobs = Yii::app()->jobQueue->getAll();
1134  $this->assertEquals(1, count($queuedJobs[5]));
1135  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', $queuedJobs[5][0]['jobType']);
1136  Yii::app()->jobQueue->deleteAll();
1137  $this->assertTrue($jobBasedOnBuildTable->run());
1138 
1139  // Because user is added to group, and group have read access to account, this account should be in
1140  // read permission table for user
1141  $sql = "SELECT * FROM account_read_subscription order by userid";
1142  $rows = ZurmoRedBean::getAll($sql);
1143  $this->assertEquals(2, count($rows));
1144  $this->assertEquals($super->id, $rows[0]['userid']);
1145  $this->assertEquals($account->id, $rows[0]['modelid']);
1146  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
1147  $this->assertEquals($user->id, $rows[1]['userid']);
1148  $this->assertEquals($account->id, $rows[1]['modelid']);
1149  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_DELETE, $rows[1]['subscriptiontype']);
1150 
1151  // Test add read permissions AGAIN to user for account
1152  $account->addPermissions($user, Permission::READ);
1153  $this->assertTrue($account->save());
1155 
1156  $queuedJobs = Yii::app()->jobQueue->getAll();
1157  $this->assertEquals(1, count($queuedJobs[5]));
1158  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', $queuedJobs[5][0]['jobType']);
1159  Yii::app()->jobQueue->deleteAll();
1160  $this->assertTrue($jobBasedOnBuildTable->run());
1161 
1162  // Check if everything is added correctly
1163  $sql = "SELECT * FROM account_read_subscription order by userid";
1164  $rows = ZurmoRedBean::getAll($sql);
1165  $this->assertEquals(2, count($rows));
1166  $this->assertEquals($super->id, $rows[0]['userid']);
1167  $this->assertEquals($account->id, $rows[0]['modelid']);
1168  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
1169  $this->assertEquals($user->id, $rows[1]['userid']);
1170  $this->assertEquals($account->id, $rows[1]['modelid']);
1171  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
1172  }
1173 
1174  public function testSecurableItemGivenOrLostPermissionsForGroup()
1175  {
1176  $super = User::getByUsername('super');
1177  Yii::app()->user->userModel = $super;
1178  $this->deleteAllModelsAndRecordsFromReadPermissionTable('Account');
1179  Yii::app()->jobQueue->deleteAll();
1182  $account = AccountTestHelper::createAccountByNameForOwner('Test Account 1', $super);
1183  Yii::app()->jobQueue->deleteAll();
1184  sleep(1);
1185 
1187  $queuedJobs = Yii::app()->jobQueue->getAll();
1188  $this->assertEquals(1, count($queuedJobs[5]));
1189  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', $queuedJobs[5][0]['jobType']);
1190  Yii::app()->jobQueue->deleteAll();
1191 
1193  $queuedJobs = Yii::app()->jobQueue->getAll();
1194  $this->assertEquals(1, count($queuedJobs[5]));
1195  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', $queuedJobs[5][0]['jobType']);
1196  Yii::app()->jobQueue->deleteAll();
1197  }
1198 
1199  public function testSecurableItemGivenOrLostPermissionsForUser()
1200  {
1201  $super = User::getByUsername('super');
1202  Yii::app()->user->userModel = $super;
1203  $this->deleteAllModelsAndRecordsFromReadPermissionTable('Account');
1204  Yii::app()->jobQueue->deleteAll();
1207  $account = AccountTestHelper::createAccountByNameForOwner('Test Account 2', $super);
1208  Yii::app()->jobQueue->deleteAll();
1209  sleep(1);
1210 
1211  $user = self::$johnny;
1212 
1214  $queuedJobs = Yii::app()->jobQueue->getAll();
1215  $this->assertEquals(1, count($queuedJobs[5]));
1216  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', $queuedJobs[5][0]['jobType']);
1217  Yii::app()->jobQueue->deleteAll();
1218 
1220  $queuedJobs = Yii::app()->jobQueue->getAll();
1221  $this->assertEquals(1, count($queuedJobs[5]));
1222  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', $queuedJobs[5][0]['jobType']);
1223  Yii::app()->jobQueue->deleteAll();
1224  }
1225 
1226  protected function deleteAllModelsAndRecordsFromReadPermissionTable($modelClassName)
1227  {
1228  $models = $modelClassName::getAll();
1229  foreach ($models as $model)
1230  {
1231  $model->delete();
1232  }
1233  $tableName = ReadPermissionsSubscriptionUtil::getSubscriptionTableName($modelClassName);
1234  $sql = "DELETE FROM $tableName";
1235  ZurmoRedBean::exec($sql);
1237  $sql = "DELETE FROM $tableName";
1238  ZurmoRedBean::exec($sql);
1239  }
1240  }
1241 ?>
Definition: Role.php:37
static getByUsername($username)
Definition: User.php:49
Definition: User.php:37
static forgetAll()
static setPermissionsFromCastedPost(array $validatedAndCastedPostData, $permitable)
static rebuild($overwriteExistingTables=true, $forcePhp=false, $messageStreamer=null)
static securableItemLostPermissionsForGroup(SecurableItem $securableItem)
static getByName($name)
Definition: Role.php:45
Definition: Group.php:37
static securableItemLostPermissionsForUser(SecurableItem $securableItem)
static getByName($name)
Definition: Group.php:57
static getById($id, $modelClassName=null)
static securableItemGivenPermissionsForGroup(SecurableItem $securableItem)
static securableItemGivenPermissionsForUser(SecurableItem $securableItem)
static setFormFromCastedPost(GroupUserMembershipForm $membershipForm, array $postData)
Generated on Wed Feb 26 2020 07:10:36