Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ReadPermissionSubscriptionObserverTest.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 $billy;
40  protected static $johnny;
41 
42  public static function setUpBeforeClass()
43  {
44  parent::setUpBeforeClass();
45  SecurityTestHelper::createSuperAdmin();
46  self::$billy = UserTestHelper::createBasicUser('Billy');
47  $group = Group::getByName('Super Administrators');
48  $group->users->add(self::$billy);
49  $group->save();
50 
51  self::$johnny = UserTestHelper::createBasicUser('Johnny');
53 
54  Yii::app()->readPermissionSubscriptionObserver->enabled = true;
55  }
56 
57  public function setUp()
58  {
59  parent::setUp();
60  Yii::app()->user->userModel = User::getByUsername('super');
61  }
62 
63  public static function tearDownAfterClass()
64  {
65  Yii::app()->readPermissionSubscriptionObserver->enabled = false;
66  parent::tearDownAfterClass();
67  }
68 
69  public function testOnCreateOwnerChangeAndDeleteModel()
70  {
71  $super = User::getByUsername('super');
72  Yii::app()->user->userModel = $super;
73 
74  // Clean contact table
75  $sql = "SELECT * FROM contact_read_subscription";
76  $rows = ZurmoRedBean::getAll($sql);
77  $this->assertTrue(empty($rows));
78 
79  $contact1 = ContactTestHelper::createContactByNameForOwner('Jason', $super);
80  $sql = "SELECT * FROM contact_read_subscription";
81  $rows = ZurmoRedBean::getAll($sql);
82  $this->assertEquals(1, count($rows));
83  $this->assertEquals($super->id, $rows[0]['userid']);
84  $this->assertEquals($contact1->id, $rows[0]['modelid']);
85  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
86  sleep(1);
87 
88  // Test deletion
89  $contact1->delete();
90  $sql = "SELECT * FROM contact_read_subscription";
91  $rows2 = ZurmoRedBean::getAll($sql);
92  $this->assertEquals(1, count($rows2));
93  $this->assertEquals($super->id, $rows2[0]['userid']);
94  $this->assertEquals($contact1->id, $rows2[0]['modelid']);
95  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_DELETE, $rows2[0]['subscriptiontype']);
96  $this->assertNotEquals($rows[0]['modifieddatetime'], $rows2[0]['modifieddatetime']);
97 
98  // Test owner change
99  $sql = "DELETE FROM contact_read_subscription";
100  ZurmoRedBean::exec($sql);
101  $sql = "SELECT * FROM contact_read_subscription";
102  $rows = ZurmoRedBean::getAll($sql);
103  $this->assertTrue(empty($rows));
104 
105  $contact2 = ContactTestHelper::createContactByNameForOwner('Ray', $super);
106  $sql = "SELECT * FROM contact_read_subscription";
107  $rows = ZurmoRedBean::getAll($sql);
108  $this->assertEquals(1, count($rows));
109  $this->assertEquals($super->id, $rows[0]['userid']);
110  $this->assertEquals($contact2->id, $rows[0]['modelid']);
111  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
112 
113  $contact2->owner = self::$billy;
114  $this->assertTrue($contact2->save());
115  $sql = "SELECT * FROM contact_read_subscription order by id";
116  $rows = ZurmoRedBean::getAll($sql);
117  $this->assertEquals(2, count($rows));
118  $this->assertEquals($super->id, $rows[0]['userid']);
119  $this->assertEquals($contact2->id, $rows[0]['modelid']);
120  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_DELETE, $rows[0]['subscriptiontype']);
121  $this->assertEquals(self::$billy->id, $rows[1]['userid']);
122  $this->assertEquals($contact2->id, $rows[1]['modelid']);
123  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
124  }
125 
126  public function testOnCreateOwnerChangeAndDeleteAccountModel()
127  {
128  $super = User::getByUsername('super');
129  $billy = self::$billy;
130  Yii::app()->user->userModel = $super;
131 
133  Yii::app()->jobQueue->deleteAll();
134 
135  // Clean contact table
136  $sql = "SELECT * FROM account_read_subscription";
137  $rows = ZurmoRedBean::getAll($sql);
138  $this->assertTrue(empty($rows));
139 
140  $account1 = AccountTestHelper::createAccountByNameForOwner('First Account', $super);
141  sleep(1);
142 
143  $queuedJobs = Yii::app()->jobQueue->getAll();
144  $this->assertEquals(1, count($queuedJobs[5]));
145  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', $queuedJobs[5][0]['jobType']);
146  Yii::app()->jobQueue->deleteAll();
147  $this->assertTrue($job->run());
148 
149  $sql = "SELECT * FROM account_read_subscription order by userid";
150  $rows = ZurmoRedBean::getAll($sql);
151  $this->assertEquals(2, count($rows));
152 
153  $this->assertEquals($super->id, $rows[0]['userid']);
154  $this->assertEquals($account1->id, $rows[0]['modelid']);
155  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
156  $this->assertEquals($billy->id, $rows[1]['userid']);
157  $this->assertEquals($account1->id, $rows[1]['modelid']);
158  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
159  sleep(1);
160 
161  // Test deletion
162  $account1->delete();
163  sleep(1);
164  $queuedJobs = Yii::app()->jobQueue->getAll();
165  $this->assertEquals(1, count($queuedJobs[5]));
166  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', $queuedJobs[5][0]['jobType']);
167  Yii::app()->jobQueue->deleteAll();
168  $this->assertTrue($job->run());
169 
170  $sql = "SELECT * FROM account_read_subscription order by userid";
171  $rows2 = ZurmoRedBean::getAll($sql);
172  $this->assertEquals(2, count($rows2));
173  $this->assertEquals($super->id, $rows2[0]['userid']);
174  $this->assertEquals($account1->id, $rows2[0]['modelid']);
175  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_DELETE, $rows2[0]['subscriptiontype']);
176  $this->assertNotEquals($rows[0]['modifieddatetime'], $rows2[0]['modifieddatetime']);
177  $this->assertEquals($billy->id, $rows2[1]['userid']);
178  $this->assertEquals($account1->id, $rows2[1]['modelid']);
179  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_DELETE, $rows2[1]['subscriptiontype']);
180  $this->assertNotEquals($rows[1]['modifieddatetime'], $rows2[1]['modifieddatetime']);
181 
182  // Test owner change, but when both users have permissions to access the account
183  $sql = "DELETE FROM account_read_subscription";
184  ZurmoRedBean::exec($sql);
185  $sql = "SELECT * FROM account_read_subscription";
186  $rows = ZurmoRedBean::getAll($sql);
187  $this->assertTrue(empty($rows));
188 
189  $account2 = AccountTestHelper::createAccountByNameForOwner('Second Account', $super);
190  sleep(1);
191  $queuedJobs = Yii::app()->jobQueue->getAll();
192  $this->assertEquals(1, count($queuedJobs[5]));
193  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', $queuedJobs[5][0]['jobType']);
194  Yii::app()->jobQueue->deleteAll();
195  $this->assertTrue($job->run());
196 
197  $sql = "SELECT * FROM account_read_subscription order by userid";
198  $rows = ZurmoRedBean::getAll($sql);
199  $this->assertEquals(2, count($rows));
200 
201  $this->assertEquals($super->id, $rows[0]['userid']);
202  $this->assertEquals($account2->id, $rows[0]['modelid']);
203  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
204  $this->assertEquals($billy->id, $rows[1]['userid']);
205  $this->assertEquals($account2->id, $rows[1]['modelid']);
206  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
207  sleep(1);
208 
209  $account2->owner = self::$billy;
210  $this->assertTrue($account2->save());
211  sleep(1);
212 
213  $queuedJobs = Yii::app()->jobQueue->getAll();
214  $this->assertEquals(1, count($queuedJobs[5]));
215  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', $queuedJobs[5][0]['jobType']);
216  Yii::app()->jobQueue->deleteAll();
217  $this->assertTrue($job->run());
218 
219  $sql = "SELECT * FROM account_read_subscription order by userid";
220  $rows = ZurmoRedBean::getAll($sql);
221  $this->assertEquals(2, count($rows));
222  $this->assertEquals($super->id, $rows[0]['userid']);
223  $this->assertEquals($account2->id, $rows[0]['modelid']);
224  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
225  $this->assertEquals(self::$billy->id, $rows[1]['userid']);
226  $this->assertEquals($account2->id, $rows[1]['modelid']);
227  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
228 
229  // Clean account table
230  $accounts = Account::getAll();
231  foreach ($accounts as $account)
232  {
233  $account->delete();
234  }
235  $sql = "DELETE FROM account_read_subscription";
236  ZurmoRedBean::exec($sql);
237  $johnny = self::$johnny;
238  $account3 = AccountTestHelper::createAccountByNameForOwner('Third Account', $johnny);
239  sleep(1);
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($job->run());
245 
246  $sql = "SELECT * FROM account_read_subscription order by userid";
247  $rows = ZurmoRedBean::getAll($sql);
248  $this->assertEquals(3, count($rows));
249 
250  $this->assertEquals($super->id, $rows[0]['userid']);
251  $this->assertEquals($account3->id, $rows[0]['modelid']);
252  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
253  $this->assertEquals($billy->id, $rows[1]['userid']);
254  $this->assertEquals($account3->id, $rows[1]['modelid']);
255  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
256  $this->assertEquals($johnny->id, $rows[2]['userid']);
257  $this->assertEquals($account3->id, $rows[2]['modelid']);
258  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[2]['subscriptiontype']);
259 
260  $account3Id = $account3->id;
261  $account3->forgetAll();
262  $account3 = Account::getById($account3Id);
263  $this->assertTrue($account3->save());
264  $account3->forgetAll();
265 
266  PermissionsCache::forgetAll();
267  $account3 = Account::getById($account3Id);
268  $account3->owner = $super;
269  $this->assertTrue($account3->save());
270  sleep(1);
271 
272  $queuedJobs = Yii::app()->jobQueue->getAll();
273  $this->assertEquals(1, count($queuedJobs[5]));
274  $this->assertEquals('ReadPermissionSubscriptionUpdateForAccountFromBuildTable', $queuedJobs[5][0]['jobType']);
275  Yii::app()->jobQueue->deleteAll();
276  $this->assertTrue($job->run());
277 
278  $sql = "SELECT * FROM account_read_subscription order by userid";
279  $rows = ZurmoRedBean::getAll($sql);
280  $this->assertEquals(3, count($rows));
281 
282  $this->assertEquals($super->id, $rows[0]['userid']);
283  $this->assertEquals($account3->id, $rows[0]['modelid']);
284  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[0]['subscriptiontype']);
285  $this->assertEquals($billy->id, $rows[1]['userid']);
286  $this->assertEquals($account3->id, $rows[1]['modelid']);
287  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_ADD, $rows[1]['subscriptiontype']);
288  $this->assertEquals($johnny->id, $rows[2]['userid']);
289  $this->assertEquals($account3->id, $rows[2]['modelid']);
290  $this->assertEquals(ReadPermissionsSubscriptionUtil::TYPE_DELETE, $rows[2]['subscriptiontype']);
291  }
292  }
293 ?>
static getByUsername($username)
Definition: User.php:49
static loadStartingData()
static getByName($name)
Definition: Group.php:57
static getById($id, $modelClassName=null)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
Generated on Tue Jul 7 2020 07:10:37
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.