Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
NoteTest.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 NoteTest extends ZurmoBaseTest
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 
53  {
54  Yii::app()->user->userModel = User::getByUsername('super');
55 
56  $_FAKEPOST = array(
57  'Note' => array(
58  'owner' => array( 'id' => Yii::app()->user->userModel->id)
59  ),
60  );
61  $metadataAdapter = new SearchDataProviderMetadataAdapter(
62  new Note(false),
63  1,
64  $_FAKEPOST['Note']
65  );
66  $_GET['Note_sort'] = 'description.desc';
67  $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
69  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('Note');
70  $where = RedBeanModelDataProvider::makeWhere('Note', $searchAttributeData, $joinTablesAdapter);
71  $orderByColumnName = RedBeanModelDataProvider::resolveSortAttributeColumnName('Note', $joinTablesAdapter, 'description');
72  $subsetSql = Note::makeSubsetOrCountSqlQuery('note', $joinTablesAdapter, 1, 5, $where, $orderByColumnName);
73  $compareSubsetSql = "select {$quote}note{$quote}.{$quote}id{$quote} id ";
74  $compareSubsetSql .= "from ({$quote}note{$quote}, {$quote}activity{$quote}, {$quote}ownedsecurableitem{$quote})";
75  $compareSubsetSql .= " where ({$quote}ownedsecurableitem{$quote}.{$quote}owner__user_id{$quote} = " . Yii::app()->user->userModel->id . ")";
76  $compareSubsetSql .= " and {$quote}activity{$quote}.{$quote}id{$quote} =";
77  $compareSubsetSql .= " {$quote}note{$quote}.{$quote}activity_id{$quote}";
78  $compareSubsetSql .= " and {$quote}ownedsecurableitem{$quote}.{$quote}id{$quote} = {$quote}activity{$quote}.{$quote}ownedsecurableitem_id{$quote}";
79  $compareSubsetSql .= " order by {$quote}note{$quote}.{$quote}description{$quote} limit 5 offset 1";
80  $this->assertEquals($compareSubsetSql, $subsetSql);
81  }
82 
86  public function testCreateAndGetNoteById()
87  {
88  Yii::app()->user->userModel = User::getByUsername('super');
89 
90  $fileModel = ZurmoTestHelper::createFileModel();
91  $accounts = Account::getByName('anAccount');
92 
93  $user = UserTestHelper::createBasicUser('Billy');
94  $occurredOnStamp = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
95  $note = new Note();
96  $note->owner = $user;
97  $note->occurredOnDateTime = $occurredOnStamp;
98  $note->description = 'myNote';
99  $note->activityItems->add($accounts[0]);
100  $note->files->add($fileModel);
101  $this->assertTrue($note->save());
102  $id = $note->id;
103  unset($note);
104  $note = note::getById($id);
105  $this->assertEquals($occurredOnStamp, $note->occurredOnDateTime);
106  $this->assertEquals('myNote', $note->description);
107  $this->assertEquals($user, $note->owner);
108  $this->assertEquals(1, $note->activityItems->count());
109  $this->assertEquals($accounts[0], $note->activityItems->offsetGet(0));
110  $this->assertEquals(1, $note->files->count());
111  $this->assertEquals($fileModel, $note->files->offsetGet(0));
112  }
113 
117  public function testGetLabel()
118  {
119  Yii::app()->user->userModel = User::getByUsername('super');
120  $notes = Note::getByName('myNote');
121  $this->assertEquals(1, count($notes));
122  $this->assertEquals('Note', $notes[0]::getModelLabelByTypeAndLanguage('Singular'));
123  $this->assertEquals('Notes', $notes[0]::getModelLabelByTypeAndLanguage('Plural'));
124  }
125 
130  {
131  Yii::app()->user->userModel = User::getByUsername('super');
132  $notes = Note::getByName('Test Note 69');
133  $this->assertEquals(0, count($notes));
134  }
135 
139  public function testUpdateNoteFromForm()
140  {
141  Yii::app()->user->userModel = User::getByUsername('super');
142 
143  $user = User::getByUsername('billy');
144  $notes = Note::getByName('myNote');
145  $note = $notes[0];
146  $this->assertEquals($note->description, 'myNote');
147  $postData = array(
148  'owner' => array(
149  'id' => $user->id,
150  ),
151  'description' => 'New Name',
152  'occurredOnDateTime' => '',
153  );
154  $sanitizedPostData = PostUtil::sanitizePostByDesignerTypeForSavingModel($note, $postData);
155  $note->setAttributes($sanitizedPostData);
156  $this->assertTrue($note->save());
157  $id = $note->id;
158  unset($note);
159  $note = Note::getById($id);
160  $this->assertEquals('New Name', $note->description);
161  $this->assertEquals(null, $note->occurredOnDateTime);
162 
163  //create new note from scratch where the DateTime attributes are not populated. It should let you save.
164  $note = new Note();
165  $postData = array(
166  'owner' => array(
167  'id' => $user->id,
168  ),
169  'description' => 'Lamazing',
170  'occurredOnDateTime' => '',
171  );
172  $sanitizedPostData = PostUtil::sanitizePostByDesignerTypeForSavingModel($note, $postData);
173  $note->setAttributes($sanitizedPostData);
174  $this->assertTrue($note->save());
175  $id = $note->id;
176  unset($note);
177  $note = Note::getById($id);
178  $this->assertEquals('Lamazing', $note->description);
179  $this->assertEquals(null, $note->occurredOnDateTime); //will default to NOW
180  }
181 
185  public function testDeleteNote()
186  {
187  Yii::app()->user->userModel = User::getByUsername('super');
188  $notes = Note::getAll();
189  $this->assertEquals(2, count($notes));
190  $notes[0]->delete();
191  $notes = Note::getAll();
192  $this->assertEquals(1, count($notes));
193  }
194 
199  {
200  Yii::app()->user->userModel = User::getByUsername('super');
201 
202  //Creating a new note, the occuredOnDateTime and latestDateTime should default to now.
203  $note = new Note();
204  $note->description = 'aTest';
205  $nowStamp = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
206  $this->assertTrue($note->save());
207  $this->assertEquals($nowStamp, $note->occurredOnDateTime);
208  $this->assertEquals($nowStamp, $note->latestDateTime);
209 
210  //Modify a note. Do not change the occurredOnDateTime, the latestDateTime should not change.
211  $note = Note::getById($note->id);
212  $note->description = 'bTest';
213  $this->assertTrue($note->save());
214  $this->assertEquals($nowStamp, $note->latestDateTime);
215 
216  //Modify a note. Change the occurredOnDateTime and the latestDateTime will change.
217  $note = Note::getById($note->id);
218  $newStamp = DateTimeUtil::convertTimestampToDbFormatDateTime(time() + 1);
219  $this->assertNotEquals($nowStamp, $newStamp);
220  $this->assertEquals($nowStamp, $note->occurredOnDateTime);
221  $note->occurredOnDateTime = $newStamp;
222  $this->assertTrue($note->save());
223  $this->assertEquals($newStamp, $note->occurredOnDateTime);
224  $this->assertEquals($newStamp, $note->latestDateTime);
225  }
226 
231  {
232  Yii::app()->user->userModel = User::getByUsername('super');
233 
234  $fileModel = ZurmoTestHelper::createFileModel();
235  $accounts = Account::getByName('anAccount');
236 
237  //create a nobody user
238  $nobody = UserTestHelper::createBasicUser('nobody');
239 
240  //create a note whoes owner is super
241  $occurredOnStamp = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
242  $note = new Note();
243  $note->owner = User::getByUsername('super');
244  $note->occurredOnDateTime = $occurredOnStamp;
245  $note->description = 'myNote';
246  $note->activityItems->add($accounts[0]);
247  $note->files->add($fileModel);
248  $this->assertTrue($note->save());
249 
250  //add nobody permission to read and write the note
251  $note->addPermissions($nobody, Permission::READ_WRITE_CHANGE_PERMISSIONS);
252  $this->assertTrue($note->save());
253 
254  //revoke the permission from the nobody user to access the note
255  $note->removePermissions($nobody, Permission::READ_WRITE_CHANGE_PERMISSIONS);
256  $this->assertTrue($note->save());
257 
258  //add nobody permission to read, write and delete the note
259  $note->addPermissions($nobody, Permission::READ_WRITE_DELETE);
260  $this->assertTrue($note->save());
262 
263  //now acces to the notes read by nobody should not fail
264  Yii::app()->user->userModel = $nobody;
265  $this->assertEquals($note->description, strval($note));
266  }
267 
272  {
273  //Create superAccount owned by user super.
274  $super = User::getByUsername('super');
275  Yii::app()->user->userModel = $super;
276  $superAccount = AccountTestHelper::createAccountByNameForOwner('AccountTest', $super);
277 
278  //create a nobody user
279  $nobody = User::getByUsername('nobody');
280 
281  //create note for an superAccount using the super user
282  $note = NoteTestHelper::createNoteWithOwnerAndRelatedAccount('noteCreatedBySuper', $super, $superAccount);
283 
284  //give nobody access to both details, edit and delete view in order to check the delete of a note
285  Yii::app()->user->userModel = User::getByUsername('super');
286  $nobody->forget();
287  $nobody = User::getByUsername('nobody');
288  $note->addPermissions($nobody, Permission::READ_WRITE_DELETE);
289  $this->assertTrue($note->save());
291  Yii::app()->user->userModel = User::getByUsername('nobody');
292  $noteId = $note->id;
293  $note->forget();
294  $note = Note::getById($noteId);
295  $note->delete();
296  }
297 
302  {
303  $super = User::getByUsername('super');
304  Yii::app()->user->userModel = $super;
305 
306  $note = NoteTestHelper::createNoteByNameForOwner('Another note with relations', $super);
307  $contact = ContactTestHelper::createContactByNameForOwner('Tom', $super);
308 
309  $note->activityItems->add($contact);
310  $note->save();
311 
312  $this->assertEquals(1, count($note->activityItems));
313  $this->assertEquals($contact->id, $note->activityItems[0]->id);
314  $noteId = $note->id;
315  $note->forget();
316  $contactItemId = $contact->getClassId('Item');
317 
318  $note = Note::getById($noteId);
319  $this->assertEquals(1, count($note->activityItems));
320  $this->assertEquals($contactItemId, $note->activityItems[0]->id);
321  }
322 
327  {
328  $super = User::getByUsername('super');
329  Yii::app()->user->userModel = $super;
330 
331  $firstNote = NoteTestHelper::createNoteByNameForOwner('Note with relations', $super);
332  $secondNote = NoteTestHelper::createNoteByNameForOwner('Second note with relations', $super);
333 
334  $thirdContact = ContactTestHelper::createContactByNameForOwner('Third', $super);
335  $firstContact = ContactTestHelper::createContactByNameForOwner('First', $super);
336  $secondContact = ContactTestHelper::createContactByNameForOwner('Second', $super);
337 
338  $firstNote->activityItems->add($firstContact);
339  $firstNote->activityItems->add($secondContact);
340  $firstNote->save();
341 
342  $this->assertEquals(2, count($firstNote->activityItems));
343  $this->assertEquals($firstContact->id, $firstNote->activityItems[0]->id);
344  $this->assertEquals($secondContact->id, $firstNote->activityItems[1]->id);
345 
346  $noteId = $firstNote->id;
347  $firstNote->forget();
348  $firstNote = Note::getById($noteId);
349  $this->assertEquals(2, count($firstNote->activityItems));
350  $this->assertEquals($firstContact->getClassId('Item'), $firstNote->activityItems[0]->id);
351  $this->assertEquals($secondContact->getClassId('Item'), $firstNote->activityItems[1]->id);
352 
353  $firstNote->activityItems->remove($firstContact);
354  $firstNote->save();
355  $this->assertEquals(1, count($firstNote->activityItems));
356  $this->assertEquals($secondContact->getClassId('Item'), $firstNote->activityItems[0]->id);
357 
358  $firstNote->forget();
359  $firstNote = Note::getById($noteId);
360  $this->assertEquals(1, count($firstNote->activityItems));
361  $this->assertEquals($secondContact->getClassId('Item'), $firstNote->activityItems[0]->id);
362  }
363 
364  public function testGetModelClassNames()
365  {
366  $modelClassNames = NotesModule::getModelClassNames();
367  $this->assertEquals(1, count($modelClassNames));
368  $this->assertEquals('Note', $modelClassNames[0]);
369  }
370  }
371 ?>
static securableItemGivenPermissionsForUser(SecurableItem $securableItem, User $user)
static getByUsername($username)
Definition: User.php:49
testAutomatedOccurredOnDateTimeAndLatestDateTimeChanges()
Definition: NoteTest.php:198
testDeleteNote()
Definition: NoteTest.php:185
static makeSubsetOrCountSqlQuery($tableName, RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $offset=null, $count=null, $where=null, $orderBy=null, $selectCount=false, $selectDistinct=false, array $quotedExtraSelectColumnNameAndAliases=array())
testAUserCanDeleteANoteNotOwnedButHasExplicitDeletePermission()
Definition: NoteTest.php:271
testNobodyCanReadWriteDeleteAndStrValOfNoteFunctionsCorrectly()
Definition: NoteTest.php:230
static makeWhere($modelClassName, array $metadata, &$joinTablesAdapter)
testGetLabel()
Definition: NoteTest.php:117
Definition: Note.php:37
testQueryIsProperlyGeneratedForNoteWithRelatedOwnerSearch()
Definition: NoteTest.php:52
static getModelClassNames()
Definition: Module.php:565
testNoteActivityItemsAreSameAfterLoadNote()
Definition: NoteTest.php:301
testCreateAndGetNoteById()
Definition: NoteTest.php:86
testUpdateNoteFromForm()
Definition: NoteTest.php:139
static getById($id, $modelClassName=null)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
testRemoveActivityItemFromActivity()
Definition: NoteTest.php:326
testGetNotesByNameForNonExistentName()
Definition: NoteTest.php:129
static sanitizePostByDesignerTypeForSavingModel($model, $postData)
Definition: PostUtil.php:94
Generated on Sun Nov 29 2020 07:10:26
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.