Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ConversationTest.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  public static function setUpBeforeClass()
40  {
41  parent::setUpBeforeClass();
42  SecurityTestHelper::createSuperAdmin();
43  $super = User::getByUsername('super');
44  Yii::app()->user->userModel = $super;
45  AccountTestHelper::createAccountByNameForOwner('anAccount', $super);
46  }
47 
48  public function setUp()
49  {
50  parent::setUp();
51  Yii::app()->user->userModel = User::getByUsername('super');
52  }
53 
54  public function testCreateAndGetConversationById()
55  {
56  $super = User::getByUsername('super');
57  $fileModel = ZurmoTestHelper::createFileModel();
58  $accounts = Account::getByName('anAccount');
59  $steven = UserTestHelper::createBasicUser('steven');
60 
61  $conversation = new Conversation();
62  $conversation->owner = $super;
63  $conversation->subject = 'My test subject';
64  $conversation->description = 'My test description';
65  $conversation->conversationItems->add($accounts[0]);
66  $conversation->files->add($fileModel);
67  $this->assertTrue($conversation->save());
68 
69  $explicitReadWriteModelPermissions = ExplicitReadWriteModelPermissionsUtil::
70  makeBySecurableItem($conversation);
71  $postData = array();
72  $postData['itemIds'] = $steven->getClassId('Item');
74  $conversation, $postData, $explicitReadWriteModelPermissions);
75  $this->assertTrue($conversation->save());
77  resolveExplicitReadWriteModelPermissions($conversation, $explicitReadWriteModelPermissions);
78  $this->assertTrue($success);
79 
80  $id = $conversation->id;
81  $conversation->forget();
82  unset($conversation);
83 
84  $conversation = Conversation::getById($id);
85  $this->assertEquals('My test subject', $conversation->subject);
86  $this->assertEquals('My test description', $conversation->description);
87  $this->assertEquals($super, $conversation->owner);
88  $this->assertEquals(1, $conversation->conversationItems->count());
89  $this->assertEquals($accounts[0]->getClassId('Item'), $conversation->conversationItems->offsetGet(0)->id);
90  $this->assertEquals(1, $conversation->files->count());
91  $this->assertEquals($fileModel, $conversation->files->offsetGet(0));
92  $this->assertEquals(1, $conversation->conversationParticipants->count());
93  $this->assertEquals($steven, $conversation->conversationParticipants->offsetGet(0)->person);
94  $this->assertEquals(1, $conversation->ownerHasReadLatest);
95 
96  //Clear conversation so permissions take properly. Needed because we don't have changes between page loads.
97  $id = $conversation->id;
98  $conversation->forget();
99  unset($conversation);
100 
101  //At this point there should be one readWritePermitable. "Steven"
102  $conversation = Conversation::getById($id);
103  $explicitReadWriteModelPermissions = ExplicitReadWriteModelPermissionsUtil::
104  makeBySecurableItem($conversation);
105  $readWritePermitables = $explicitReadWriteModelPermissions->getReadWritePermitables();
106  $this->assertEquals(1, count($readWritePermitables));
107  $this->assertEquals($steven, $readWritePermitables[$steven->getClassId('Permitable')]);
108  $this->assertEquals(1, $conversation->conversationParticipants->count());
109  $this->assertEquals($steven, $conversation->conversationParticipants[0]->person);
110  }
111 
115  public function testAddingComments()
116  {
117  $conversations = Conversation::getAll();
118  $this->assertEquals(1, count($conversations));
119  $conversation = $conversations[0];
120  $steven = User::getByUserName('steven');
121  $latestStamp = $conversation->latestDateTime;
122 
123  //latestDateTime should not change when just saving the conversation
124  $conversation->conversationParticipants->offsetGet(0)->hasReadLatest = true;
125  $conversation->ownerHasReadLatest = true;
126  $this->assertTrue($conversation->save());
127  $this->assertEquals($latestStamp, $conversation->latestDateTime);
128  $this->assertEquals(1, $conversation->ownerHasReadLatest);
129 
130  sleep(2); // Sleeps are bad in tests, but I need some time to pass
131 
132  //Add comment, this should update the latestDateTime,
133  //and also it should reset hasReadLatest on conversation participants
134  $comment = new Comment();
135  $comment->description = 'This is my first comment';
136  $conversation->comments->add($comment);
137  $this->assertTrue($conversation->save());
138  $this->assertNotEquals($latestStamp, $conversation->latestDateTime);
139  $this->assertEquals(0, $conversation->conversationParticipants->offsetGet(0)->hasReadLatest);
140  //super made the comment, so this should remain the same.
141  $this->assertEquals(1, $conversation->ownerHasReadLatest);
142 
143  //set it to read latest
144  $conversation->conversationParticipants->offsetGet(0)->hasReadLatest = true;
145  $this->assertTrue($conversation->save());
146  $this->assertEquals(1, $conversation->conversationParticipants->offsetGet(0)->hasReadLatest);
147 
148  //have steven make the comment. Now the ownerHasReadLatest should set to false, and hasReadLatest should remain true
149  Yii::app()->user->userModel = $steven;
150  $conversation = Conversation::getById($conversation->id);
151  $comment = new Comment();
152  $comment->description = 'This is steven`\s first comment';
153  $conversation->comments->add($comment);
154  $this->assertTrue($conversation->save());
155  $this->assertEquals(1, $conversation->conversationParticipants->offsetGet(0)->hasReadLatest);
156  $this->assertEquals(0, $conversation->ownerHasReadLatest);
157  }
158 
163  {
164  $super = User::getByUsername('super');
165  $steven = User::getByUsername('steven');
166  $sally = UserTestHelper::createBasicUser('sally');
167  $mary = UserTestHelper::createBasicUser('mary');
168 
169  $conversation = new Conversation();
170  $conversation->owner = $super;
171  $conversation->subject = 'My test subject2';
172  $conversation->description = 'My test description2';
173  $this->assertTrue($conversation->save());
174 
175  //Set explicitPermissions. Should not add any at this point
176  $explicitReadWriteModelPermissions = ExplicitReadWriteModelPermissionsUtil::
177  makeBySecurableItem($conversation);
178  $readWritePermitables = $explicitReadWriteModelPermissions->getReadWritePermitables();
179  $this->assertEquals(0, count($readWritePermitables));
180 
181  //Attempt to resolve against posted conversationParticipants data
182  $postData = array();
183  $postData['itemIds'] = $super->getClassId('Item');
185  $conversation, $postData, $explicitReadWriteModelPermissions);
186  //Should still be 0, because super is the owner, and would not be specially added. (This is just a safety test here)
187  $readWritePermitables = $explicitReadWriteModelPermissions->getReadWritePermitables();
188  $this->assertEquals(0, count($readWritePermitables));
189  $this->assertEquals(0, $conversation->conversationParticipants->count());
190 
191  //Add steven as a conversation participant.
192  $postData = array();
193  $postData['itemIds'] = $super->getClassId('Item') . ',' . $steven->getClassId('Item'); // Not Coding Standard
195  $postData,
196  $explicitReadWriteModelPermissions);
197  $this->assertTrue($conversation->save());
199  resolveExplicitReadWriteModelPermissions($conversation, $explicitReadWriteModelPermissions);
200  $this->assertTrue($success);
201 
202  //At this point there should be one readWritePermitable. "Steven"
203  $explicitReadWriteModelPermissions = ExplicitReadWriteModelPermissionsUtil::
204  makeBySecurableItem($conversation);
205  $readWritePermitables = $explicitReadWriteModelPermissions->getReadWritePermitables();
206  $this->assertEquals(1, count($readWritePermitables));
207  $this->assertEquals($steven, $readWritePermitables[$steven->getClassId('Permitable')]);
208  $this->assertEquals(1, $conversation->conversationParticipants->count());
209  $this->assertEquals($steven, $conversation->conversationParticipants[0]->person);
210  }
211 
216  {
217  $super = User::getByUsername('super');
218  $mary = User::getByUsername('mary');
219  $count = Conversation::getUnreadCountByUser($super);
220  $account2 = AccountTestHelper::createAccountByNameForOwner('anAccount2', $super);
221 
222  $conversation = new Conversation();
223  $conversation->owner = $super;
224  $conversation->subject = 'My test subject2';
225  $conversation->description = 'My test description2';
226  $conversation->conversationItems->add($account2);
227  $this->assertTrue($conversation->save());
228 
229  //when super adds a comment, it should remain same count
230  $comment = new Comment();
231  $comment->description = 'This is my first comment';
232  $conversation->comments->add($comment);
233  $this->assertTrue($conversation->save());
234  $count = Conversation::getUnreadCountByUser($super);
235  $this->assertEquals(1, $count);
236 
237  //Add mary as a participant
238  $explicitReadWriteModelPermissions = ExplicitReadWriteModelPermissionsUtil::
239  makeBySecurableItem($conversation);
240  $postData = array();
241  $postData['itemIds'] = $super->getClassId('Item') . ',' . $mary->getClassId('Item'); // Not Coding Standard
243  $postData,
244  $explicitReadWriteModelPermissions);
247  $explicitReadWriteModelPermissions);
248  $this->assertTrue($success);
249  $conversation->save();
250 
251  //when mary adds a comment, super's count should go up (assumming count was previously 0)
252  Yii::app()->user->userModel = $mary;
253  $comment = new Comment();
254  $comment->description = 'This is mary\'s first comment';
255  $conversation->comments->add($comment);
256  $this->assertTrue($conversation->save());
257  Yii::app()->user->userModel = $super;
258  $count = Conversation::getUnreadCountByUser($super);
259  $this->assertEquals(2, $count);
260  }
261 
265  public function testDeleteConversation()
266  {
267  $conversations = Conversation::getAll();
268  $this->assertEquals(3, count($conversations));
269  $comments = Comment::getAll();
270  $this->assertEquals(4, count($comments));
271 
272  //check count of conversation_items
273  $count = ZurmoRedBean::getRow('select count(*) count from conversation_item');
274  $this->assertEquals(2, $count['count']);
275 
276  //remove the account, tests tthat ConversationObserver is correctly removing data from conversation_item
277  $accounts = Account::getByName('anAccount2');
278  $this->assertTrue($accounts[0]->delete());
279 
280  $count = ZurmoRedBean::getRow('select count(*) count from conversation_item');
281  $this->assertEquals(1, $count['count']);
282 
283  foreach ($conversations as $conversation)
284  {
285  $conversationId = $conversation->id;
286  $conversation->forget();
287  $conversation = Conversation::getById($conversationId);
288  $deleted = $conversation->delete();
289  $this->assertTrue($deleted);
290  }
291 
292  //Count of conversation items should be 0 since the ConversationsObserver should make sure it gets removed correctly.
293  $count = ZurmoRedBean::getRow('select count(*) count from conversation_item');
294  $this->assertEquals(0, $count['count']);
295 
296  //check that all comments are removed, since they are owned.
297  $comments = Comment::getAll();
298  $this->assertEquals(0, count($comments));
299  }
300  }
301 ?>
static makeBySecurableItem(SecurableItem $securableItem)
static getByUsername($username)
Definition: User.php:49
static resolveExplicitReadWriteModelPermissions(SecurableItem $securableItem, ExplicitReadWriteModelPermissions $explicitReadWriteModelPermissions, $validate=false)
testResolveConversationParticipantsForExplicitModelPermissions()
static resolveConversationHasManyParticipantsFromPost(Conversation $conversation, $postData, $explicitReadWriteModelPermissions)
static getUnreadCountByUser(User $user)
static getById($id, $modelClassName=null)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
Generated on Fri Jul 3 2020 07:10:31
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.