Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
MashableUtilTest.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 $user;
40 
41  public static function setUpBeforeClass()
42  {
43  parent::setUpBeforeClass();
44  SecurityTestHelper::createSuperAdmin();
45  Yii::app()->user->userModel = User::getByUsername('super');
46  $billy = UserTestHelper::createBasicUser('billy');
47  }
48 
49  public function setUp()
50  {
51  parent::setUp();
52  Yii::app()->user->userModel = User::getByUsername('super');
53  $this->user = Yii::app()->user->userModel;
54  }
55 
56  public function testCreateMashableInboxRulesByModel()
57  {
58  $mashableInboxRules = MashableUtil::createMashableInboxRulesByModel('conversation');
59  $this->assertEquals('ConversationMashableInboxRules', get_class($mashableInboxRules));
60  }
61 
62  public function testGetModelDataForCurrentUserByInterfaceName()
63  {
64  Yii::app()->user->userModel = User::getByUsername('super');
65  $mashableModelData = MashableUtil::getModelDataForCurrentUserByInterfaceName('MashableInboxInterface');
66  $this->assertEquals(3, count($mashableModelData));
67  Yii::app()->user->userModel = User::getByUsername('billy');
68  $mashableModelData = MashableUtil::getModelDataForCurrentUserByInterfaceName('MashableInboxInterface');
69  $this->assertEquals(1, count($mashableModelData));
70  }
71 
72  public function testGetUnreadCountForCurrentUserByModelClassName()
73  {
76  $this->assertEquals($count, 0);
77  $this->makeANewUnreadMissionOwnedByCurrentUser();
79  $this->assertEquals($count, 1);
80  }
81 
82  public function testGetUnreadCountMashableInboxForCurrentUser()
83  {
86  $count = MashableUtil::GetUnreadCountMashableInboxForCurrentUser();
87  $this->assertEquals($count, 0);
88  $this->makeANewUnreadMissionOwnedByCurrentUser();
89  $count = MashableUtil::GetUnreadCountMashableInboxForCurrentUser();
90  $this->assertEquals($count, 1);
91  $this->makeANewUnreadConversationOwnedByCurrentUser();
92  $count = MashableUtil::GetUnreadCountMashableInboxForCurrentUser();
93  $this->assertEquals($count, 2);
94  }
95 
96  public function testGetSearchAttributeMetadataForMashableInboxByModelClassName()
97  {
98  $conversationRules = new ConversationMashableInboxRules();
99  $expected = MashableUtil::mergeMetadata(
100  MashableUtil::mergeMetadata($conversationRules->getMetadataForMashableInbox(), $conversationRules->getSearchAttributeData('test')),
101  $conversationRules->getMetadataFilteredByFilteredBy(MashableInboxForm::FILTERED_BY_ALL)
102  );
104  array('Conversation'),
105  MashableInboxForm::FILTERED_BY_ALL,
106  'test'
107  );
108  $this->assertEquals($expected, $searchAttributesData[0]['Conversation']);
109 
110  $missionRules = new MissionMashableInboxRules();
111  $expected2 = MashableUtil::mergeMetadata(
112  MashableUtil::mergeMetadata($missionRules->getMetadataForMashableInbox(), $missionRules->getSearchAttributeData('test')),
113  $missionRules->getMetadataFilteredByFilteredBy(MashableInboxForm::FILTERED_BY_ALL)
114  );
116  array('Conversation', 'Mission'),
117  MashableInboxForm::FILTERED_BY_ALL,
118  'test'
119  );
120  $this->assertEquals($expected, $searchAttributesData[0]['Conversation']);
121  $this->assertEquals($expected2, $searchAttributesData[1]['Mission']);
122  }
123 
124  public function testGetSortAttributesByMashableInboxModelClassNames()
125  {
126  $conversationRules = new ConversationMashableInboxRules();
127  $missionRules = new MissionMashableInboxRules();
129  array('Conversation', 'Mission'));
130  $this->assertEquals($conversationRules->getMachableInboxOrderByAttributeName(),
131  $sortAttributes['Conversation']);
132  $this->assertEquals($missionRules->getMachableInboxOrderByAttributeName(),
133  $sortAttributes['Mission']);
134  }
135 
136  public function testRenderSummaryContent()
137  {
138  $conversation = $this->makeANewUnreadConversationOwnedByCurrentUser();
139  $conversationRules = new ConversationMashableInboxRules();
140  $content = MashableUtil::renderSummaryContent($conversation);
141  $contentWithRemovedId = preg_replace("/(DetailsLinkActionElement.*yt)(\d*)/", "$1", $content);
142  $this->assertContains('<div class="model-tag conversation">', $contentWithRemovedId);
143  $expectedContent = str_replace('{modelCreationTimeContent}', $conversationRules->getModelCreationTimeContent($conversation),
144  str_replace('{modelStringContent}', $conversationRules->getModelStringContent($conversation),
145  $conversationRules->getSummaryContentTemplate()));
146  $expectedContentWithRemovedId = preg_replace("/(DetailsLinkActionElement.*yt)(\d*)/", "$1", $expectedContent);
147  $this->assertContains($expectedContentWithRemovedId, $contentWithRemovedId);
148  }
149 
150  public function testResolveContentTemplate()
151  {
152  $data = array(
153  'testVar1' => 'subVar1',
154  'testVar2' => 'subVar2',
155  );
156  $template = '{testVar1} will be resolved and {testVar2} too';
157  $content = MashableUtil::resolveContentTemplate($template, $data);
158  $this->assertEquals('subVar1 will be resolved and subVar2 too', $content);
159  $data = array(
160  'testVar1' => 'subVar1',
161  );
162  $content = MashableUtil::resolveContentTemplate($template, $data);
163  $this->assertEquals($content, 'subVar1 will be resolved and {testVar2} too');
164  }
165 
166  public function testMergeMetada()
167  {
168  $firstMetadata = null;
169  $secondMetadata = null;
170  $mergedMetadata = MashableUtil::mergeMetadata($firstMetadata, $secondMetadata);
171  $this->assertEquals($mergedMetadata['clauses'], array());
172  $this->assertEquals($mergedMetadata['structure'], null);
173 
174  $firstMetadata = array(
175  'clauses' => array(1 => 'testClause1'),
176  'structure' => '1',
177  );
178  $secondMetadata = null;
179  $mergedMetadata = MashableUtil::mergeMetadata($firstMetadata, $secondMetadata);
180  $this->assertEquals(array(1 => 'testClause1'), $mergedMetadata['clauses']);
181  $this->assertEquals('1', $mergedMetadata['structure']);
182 
183  $firstMetadata = null;
184  $secondMetadata = array(
185  'clauses' => array(1 => 'testClause1'),
186  'structure' => '1',
187  );
188  $mergedMetadata = MashableUtil::mergeMetadata($firstMetadata, $secondMetadata);
189  $this->assertEquals($mergedMetadata['clauses'], array(1 => 'testClause1'));
190  $this->assertEquals($mergedMetadata['structure'], '1');
191 
192  $firstMetadata = array(
193  'clauses' => array(1 => 'testClause1'),
194  'structure' => '1',
195  );
196  $secondMetadata = array(
197  'clauses' => array(1 => 'testClause1ForSecondMetadata'),
198  'structure' => '1',
199  );
200  $mergedMetadata = MashableUtil::mergeMetadata($firstMetadata, $secondMetadata);
201  $this->assertEquals(array(1 => 'testClause1',
202  2 => 'testClause1ForSecondMetadata'),
203  $mergedMetadata['clauses']);
204  $this->assertEquals('(1) and (2)', $mergedMetadata['structure']);
205 
206  $firstMetadata = array(
207  'clauses' => array(1 => 'testClause1',
208  2 => 'testClause2',
209  3 => 'testClause3',
210  ),
211  'structure' => '1 and (2 or 3)',
212  );
213  $secondMetadata = array(
214  'clauses' => array(1 => 'testClause1ForSecondMetadata',
215  2 => 'testClause2ForSecondMetadata',
216  ),
217  'structure' => '1 and 2',
218  );
219  $mergedMetadata = MashableUtil::mergeMetadata($firstMetadata, $secondMetadata, false);
220  $this->assertEquals($mergedMetadata['clauses'], array(1 => 'testClause1',
221  2 => 'testClause2',
222  3 => 'testClause3',
223  4 => 'testClause1ForSecondMetadata',
224  5 => 'testClause2ForSecondMetadata'));
225  $this->assertEquals($mergedMetadata['structure'], '(1 and (2 or 3)) or (4 and 5)');
226 
227  $firstMetadata = array(
228  'clauses' => array(1 => 'testClause1'),
229  'structure' => '1',
230  );
231  $secondMetadata = array(
232  'clauses' => array(1 => 'testClause1ForSecondMetadata'),
233  'structure' => '1',
234  );
235  $mergedMetadata = MashableUtil::mergeMetadata($firstMetadata, $secondMetadata);
236  $this->assertEquals(array(1 => 'testClause1',
237  2 => 'testClause1ForSecondMetadata'),
238  $mergedMetadata['clauses']);
239  $this->assertEquals('(1) and (2)', $mergedMetadata['structure']);
240 
241  $firstMetadata = array(
242  'clauses' => array(1 => 'testClause1'),
243  'structure' => '1',
244  );
245  $secondMetadata = array(
246  'clauses' => array(1 => 'testClause1ForSecondMetadata',
247  2 => 'testClause2ForSecondMetadata',
248  3 => 'testClause3ForSecondMetadata',
249  4 => 'testClause4ForSecondMetadata'),
250  'structure' => '((1 and 2) or (3 and 4))',
251  );
252  $mergedMetadata = MashableUtil::mergeMetadata($firstMetadata, $secondMetadata);
253  $this->assertEquals(array(1 => 'testClause1',
254  2 => 'testClause1ForSecondMetadata',
255  3 => 'testClause2ForSecondMetadata',
256  4 => 'testClause3ForSecondMetadata',
257  5 => 'testClause4ForSecondMetadata'),
258  $mergedMetadata['clauses']);
259  $this->assertEquals('(1) and (((2 and 3) or (4 and 5)))', $mergedMetadata['structure']);
260  }
261 
262  public function testSaveSelectedOptionsAsStickyData()
263  {
264  $testData = array(
265  'optionForModel' => 'aaaaa',
266  'filteredBy' => 'bbbbb',
267  'searchTerm' => 'ccccc');
268  $mashableInboxForm = new MashableInboxForm();
269  $mashableInboxForm->setAttributes($testData);
270  $key = MashableUtil::resolveKeyByModuleAndModel('MashableInboxModule', 'testClassName');
271  MashableUtil::saveSelectedOptionsAsStickyData($mashableInboxForm, 'testClassName');
272  $this->assertEquals($testData, StickyUtil::getDataByKey($key));
273 
274  $testData2 = array(
275  'optionForModel' => 'aaaaa',
276  'filteredBy' => 'bbbbb',
277  'searchTerm' => 'ccccc',
278  'selectedIds' => 'ddddd',
279  'massAction' => 'eeeee');
280  $mashableInboxForm = new MashableInboxForm();
281  $mashableInboxForm->setAttributes($testData);
282  StickyUtil::clearDataByKey($key);
283  MashableUtil::saveSelectedOptionsAsStickyData($mashableInboxForm, 'testClassName');
284  $this->assertEquals($testData, StickyUtil::getDataByKey($key));
285  }
286 
287  public function testRestoreSelectedOptionsAsStickyData()
288  {
289  $key = MashableUtil::
290  resolveKeyByModuleAndModel('MashableInboxModule', 'testClassName');
291  StickyUtil::clearDataByKey($key);
292  $mashableInboxForm = MashableUtil::
293  restoreSelectedOptionsAsStickyData('testClassName');
294  $mashableInboxFormForCompare = new MashableInboxForm();
295  $this->assertEquals($mashableInboxFormForCompare->attributes,
296  $mashableInboxForm->attributes);
297  $testData = array(
298  'optionForModel' => 'aaaaa',
299  'filteredBy' => 'bbbbb',
300  'searchTerm' => 'ccccc');
301  $key = MashableUtil::
302  resolveKeyByModuleAndModel('MashableInboxModule', 'testClassName');
303  StickyUtil::clearDataByKey($key);
304  StickyUtil::setDataByKeyAndData($key, $testData);
305  $mashableInboxForm = MashableUtil::
306  restoreSelectedOptionsAsStickyData('testClassName');
307  $this->assertEquals($testData, array_intersect($testData, StickyUtil::getDataByKey($key)));
308  }
309 
310  public function testResolveKeyByModuleAndModel()
311  {
312  $key = MashableUtil::resolveKeyByModuleAndModel('testModule', 'testClassName');
313  $this->assertEquals('testModule_testClassName', $key);
314  }
315 
316  protected function makeANewUnreadConversationOwnedByCurrentUser()
317  {
318  $conversation = new Conversation();
319  $conversation->owner = $this->user;
320  $conversation->subject = 'My test subject';
321  $conversation->description = 'My test description';
322  $this->assertTrue($conversation->save());
323  $conversation->ownerHasReadLatest = false;
324  $this->assertTrue($conversation->save());
325  return $conversation;
326  }
327 
328  protected function makeANewUnreadMissionOwnedByCurrentUser()
329  {
330  $mission = new Mission();
331  $mission->owner = $this->user;
332  $mission->dueDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
333  $mission->description = 'My test description';
334  $mission->reward = 'My test reward';
335  $mission->status = Mission::STATUS_AVAILABLE;
336  $this->assertTrue($mission->save());
338  $rules->markUserAsHavingUnreadLatestModel($mission, $this->user);
339  return $mission;
340  }
341  }
342 ?>
static getSortAttributesByMashableInboxModelClassNames($modelClassNames)
static resolveKeyByModuleAndModel($moduleClassName, $modelClassName)
static getByUsername($username)
Definition: User.php:49
static getSearchAttributeMetadataForMashableInboxByModelClassName($modelClassNames, $filteredBy, $searchTerm= '')
static resolveContentTemplate($template, $data)
static mergeMetadata($firstMetadata, $secondMetadata, $isAnd=true)
static renderSummaryContent(RedBeanModel $model)
static getModelDataForCurrentUserByInterfaceName($interfaceClassName, $includeHavingRelatedItems=true)
static createMashableInboxRulesByModel($modelClassName)
static saveSelectedOptionsAsStickyData(MashableInboxForm $mashableInboxForm, $modelClassName)
static deleteAll()
static getUnreadCountForCurrentUserByModelClassName($modelClassName)
static restoreSelectedOptionsAsStickyData($modelClassName)
Generated on Sat Jul 4 2020 07:10:33
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.