All Data Structures Functions Variables Pages
ModelAutoCompleteUtilTest.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  ZurmoDatabaseCompatibilityUtil::dropStoredFunctionsAndProcedures();
43  SecurityTestHelper::createSuperAdmin();
44  Yii::app()->user->userModel = User::getByUsername('super');
46  assert('$loaded'); // Not Coding Standard
47  }
48 
49  public function testGetCustomFieldDataByPartialName()
50  {
51  Yii::app()->user->userModel = User::getByUsername('super');
52 
53  $values = array(
54  'Automotive',
55  'Adult Entertainment',
56  'Financial Services',
57  'Mercenaries & Armaments',
58  'autam',
59  );
60  $industryFieldData = CustomFieldData::getByName('testData');
61  $industryFieldData->defaultValue = $values[0];
62  $industryFieldData->serializedData = serialize($values);
63  $this->assertTrue($industryFieldData->save());
65  $this->assertEquals(5, count($results));
66 
67  $results = ModelAutoCompleteUtil::getCustomFieldDataByPartialName('testData', 'Au');
68  $this->assertEquals(2, count($results));
69 
70  $results = ModelAutoCompleteUtil::getCustomFieldDataByPartialName('testData', 'Mer');
71  $this->assertEquals(1, count($results));
72 
73  $results = ModelAutoCompleteUtil::getCustomFieldDataByPartialName('testData', 'sat');
74  $this->assertEquals(0, count($results));
75 
76  $results = ModelAutoCompleteUtil::getCustomFieldDataByPartialName('testData', 'Aux');
77  $this->assertEquals(0, count($results));
78  }
79 
83  public function testGetByPartialName()
84  {
85  Yii::app()->user->userModel = User::getByUsername('super');
86  $userData = array(
87  'Billy',
88  'Jimmy',
89  'Sunny',
90  'Jinny',
91  'Johnny',
92  'Appay'
93  );
94  foreach ($userData as $firstName)
95  {
96  $user = UserTestHelper::createBasicUser($firstName);
97  }
98 
99  $user = User::getByUsername('billy');
100  $contactData = array(
101  'Sam',
102  'Sally',
103  'Sarah',
104  'Jason',
105  'James',
106  'Roger'
107  );
108  $contactStates = ContactState::getAll();
109  foreach ($contactData as $firstName)
110  {
111  $contact = new Contact();
112  $contact->title->value = 'Mr.';
113  $contact->firstName = $firstName;
114  $contact->lastName = $firstName . 'son';
115  $contact->owner = $user;
116  $contact->state = $contactStates[count($contactStates) - 1];
117  $this->assertTrue($contact->save());
118  }
119  $accountData = array(
120  'ABC Company',
121  'Rabbit Systems',
122  'Rabid Technology',
123  'Super Company',
124  'Legalland',
125  'Toycity'
126  );
127  foreach ($accountData as $name)
128  {
129  $account = new Account();
130  $account->name = $name;
131  $account->owner = $user;
132  $this->assertTrue($account->save());
133  }
134  $data = ModelAutoCompleteUtil::getByPartialName('User', 'j', 5);
135  $compareData = array(
136  array(
137  'id' => $data[0]['id'],
138  'value' => 'Jimmy Jimmyson',
139  'label' => 'Jimmy Jimmyson (jimmy)',
140  ),
141  array(
142  'id' => $data[1]['id'],
143  'value' => 'Jinny Jinnyson',
144  'label' => 'Jinny Jinnyson (jinny)',
145  ),
146  array(
147  'id' => $data[2]['id'],
148  'value' => 'Johnny Johnnyson',
149  'label' => 'Johnny Johnnyson (johnny)',
150  ),
151  );
152  $this->assertEquals($compareData, $data);
154  $compareData = array(
155  array(
156  'id' => $data[0]['id'],
157  'value' => 'Sally Sallyson',
158  'label' => 'Sally Sallyson',
159  ),
160  array(
161  'id' => $data[1]['id'],
162  'value' => 'Sam Samson',
163  'label' => 'Sam Samson',
164  ),
165  array(
166  'id' => $data[2]['id'],
167  'value' => 'Sarah Sarahson',
168  'label' => 'Sarah Sarahson',
169  ),
170  );
171  $this->assertEquals($compareData, $data);
173  $compareData = array();
174  $this->assertEquals($compareData, $data);
176  $compareData = array(
177  array(
178  'id' => $data[0]['id'],
179  'value' => 'Sally Sallyson',
180  'label' => 'Sally Sallyson',
181  ),
182  );
183  $this->assertEquals($compareData, $data);
184  $data = ModelAutoCompleteUtil::getByPartialName('Account', 'rab', 5);
185  $compareData = array(
186  array(
187  'id' => $data[0]['id'],
188  'value' => 'Rabbit Systems',
189  'label' => 'Rabbit Systems',
190  ),
191  array(
192  'id' => $data[1]['id'],
193  'value' => 'Rabid Technology',
194  'label' => 'Rabid Technology',
195  ),
196  );
197  $this->assertEquals($compareData, $data);
198  }
199 
204  {
205  $super = User::getByUsername('super');
206  Yii::app()->user->userModel = $super;
207 
208  //Add an account with an email address.
209  $account = new Account();
210  $account->name = 'The Zoo';
211  $account->owner = $super;
212  $email = new Email();
213  $email->optOut = 0;
214  $email->emailAddress = 'animal@zoo.com';
215  $account->primaryEmail = $email;
216  $this->assertTrue($account->save());
217 
218  //Create a contact with a similar e-mail address
219  $contactStates = ContactState::getAll();
220  $contact = new Contact();
221  $contact->title->value = 'Mr.';
222  $contact->firstName = 'Big';
223  $contact->lastName = 'Elephant';
224  $contact->owner = $super;
225  $contact->state = $contactStates[count($contactStates) - 1];
226  $email = new Email();
227  $email->optOut = 0;
228  $email->emailAddress = 'animal@africa.com';
229  $contact->primaryEmail = $email;
230  $this->assertTrue($contact->save());
231 
232  //Add an opportunity
233  $currencies = Currency::getAll();
234  $currencyValue = new CurrencyValue();
235  $currencyValue->value = 500.54;
236  $currencyValue->currency = $currencies[0];
237  $opportunity = new Opportunity();
238  $opportunity->owner = $super;
239  $opportunity->name = 'Animal Crackers';
240  $opportunity->amount = $currencyValue;
241  $opportunity->closeDate = '2011-01-01'; //eventually fix to make correct format
242  $opportunity->stage->value = 'Negotiating';
243  $this->assertTrue($opportunity->save());
244 
245  //Test where no results are expected.
247  $this->assertEquals(array(array('href' => '', 'label' => 'No Results Found', 'iconClass' => '')), $data);
248 
249  //Test where one account is expected searching by account name.
251  $this->assertEquals(1, count($data));
252  $this->assertEquals('Rabbit Systems', $data[0]['label']);
253 
254  //test anyEmail where results are across more than one module. This will also pick up an opportunity that
255  //has the name 'animal' in it.
257  $this->assertEquals(3, count($data));
258  $this->assertEquals('The Zoo', $data[0]['label']);
259  $this->assertEquals('Big Elephant', $data[1]['label']);
260  $this->assertEquals('Animal Crackers', $data[2]['label']);
261  }
262 
267  {
268  $super = User::getByUsername('super');
269  Yii::app()->user->userModel = $super;
270 
271  $data = ModelAutoCompleteUtil::getGlobalSearchResultsByPartialTerm('animal', 5, $super, array('accounts'));
272  $this->assertEquals(1, count($data));
273  $this->assertEquals('The Zoo', $data[0]['label']);
274 
275  $data = ModelAutoCompleteUtil::getGlobalSearchResultsByPartialTerm('animal', 5, $super, array('contacts'));
276  $this->assertEquals(1, count($data));
277  $this->assertEquals('Big Elephant', $data[0]['label']);
278 
280  array('contacts', 'opportunities'));
281  $this->assertEquals(2, count($data));
282  $this->assertEquals('Big Elephant', $data[0]['label']);
283  $this->assertEquals('Animal Crackers', $data[1]['label']);
284  }
285 
290  {
291  $super = User::getByUsername('super');
292  $jimmy = User::getByUsername('jimmy');
293  Yii::app()->user->userModel = $super;
294 
295  //Jimmy does not have read access, so he should not be able to see any results.
296  $this->assertEquals(Right::DENY, $jimmy->getEffectiveRight('AccountsModule', AccountsModule::RIGHT_ACCESS_ACCOUNTS));
297  $this->assertEquals(Right::DENY, $jimmy->getEffectiveRight('ContactsModule', ContactsModule::RIGHT_ACCESS_CONTACTS));
298  $this->assertEquals(Right::DENY, $jimmy->getEffectiveRight('OpportunitiesModule', OpportunitiesModule::RIGHT_ACCESS_OPPORTUNITIES));
299  Yii::app()->user->userModel = $jimmy;
300  $data = ModelAutoCompleteUtil::getGlobalSearchResultsByPartialTerm('animal', 5, Yii::app()->user->userModel);
301  $this->assertEquals(array(array('href' => '', 'label' => 'No Results Found', 'iconClass' => '')), $data);
302 
303  //Give Jimmy access to the module, he still will not be able to see results.
304  Yii::app()->user->userModel = $super;
305  $jimmy->setRight ('AccountsModule', AccountsModule::RIGHT_ACCESS_ACCOUNTS);
306  $jimmy->setRight ('ContactsModule', ContactsModule::RIGHT_ACCESS_CONTACTS);
307  $jimmy->setRight ('LeadsModule', LeadsModule::RIGHT_ACCESS_LEADS);
308  $jimmy->setRight ('OpportunitiesModule', OpportunitiesModule::RIGHT_ACCESS_OPPORTUNITIES);
309  $this->assertTrue ($jimmy->save());
310  Yii::app()->user->userModel = $jimmy;
311  $data = ModelAutoCompleteUtil::getGlobalSearchResultsByPartialTerm('animal', 5, Yii::app()->user->userModel);
312  $this->assertEquals(array(array('href' => '', 'label' => 'No Results Found', 'iconClass' => '')), $data);
313 
314  //Give Jimmy read on 1 model. The search then should pick up this model.
315  Yii::app()->user->userModel = $super;
316  $accounts = Account::getByName('The Zoo');
317  $this->assertEquals(1, count($accounts));
318  $account = $accounts[0];
319  $this->assertEquals(Permission::NONE, $account->getEffectivePermissions ($jimmy));
320  $account->addPermissions($jimmy, Permission::READ);
321  $this->assertTrue ($account->save());
323  Yii::app()->user->userModel = $jimmy;
324  $data = ModelAutoCompleteUtil::getGlobalSearchResultsByPartialTerm('animal', 5, Yii::app()->user->userModel);
325  $this->assertEquals(1, count($data));
326  $this->assertEquals('The Zoo', $data[0]['label']);
327 
328  //Give Jimmy read on 2 more models. The search then should pick up these models.
329  Yii::app()->user->userModel = $super;
330  $contacts = Contact::getByName('Big Elephant');
331  $this->assertEquals(1, count($contacts));
332  $contact = $contacts[0];
333  $this->assertEquals(Permission::NONE, $contact->getEffectivePermissions ($jimmy));
334  $contact->addPermissions($jimmy, Permission::READ);
335  $this->assertTrue ($contact->save());
337  $opportunities = Opportunity::getByName('Animal Crackers');
338  $this->assertEquals(1, count($opportunities));
339  $opportunity = $opportunities[0];
340  $this->assertEquals(Permission::NONE, $opportunity->getEffectivePermissions ($jimmy));
341  $opportunity->addPermissions($jimmy, Permission::READ);
342  $this->assertTrue ($opportunity->save());
344  Yii::app()->user->userModel = $jimmy;
345  $data = ModelAutoCompleteUtil::getGlobalSearchResultsByPartialTerm('animal', 5, Yii::app()->user->userModel);
346  $this->assertEquals(3, count($data));
347  $this->assertEquals('The Zoo', $data[0]['label']);
348  $this->assertEquals('Big Elephant', $data[1]['label']);
349  $this->assertEquals('Animal Crackers', $data[2]['label']);
350  }
351 
352  public function testGetGenericResultSanitizePartialName()
353  {
354  $super = User::getByUsername('super');
355  Yii::app()->user->userModel = $super;
356  $this->assertTrue(ZurmoRedBean::$writer->doesTableExist(User::getTableName()));
357  AccountTestHelper::createAccountByNameForOwner('Test account', $super);
358  AccountTestHelper::createAccountByNameForOwner('Test account 2', $super);
359  $term = "Test%');drop TABLE _user-- ";
360  $data = ModelAutoCompleteUtil::getByPartialName('Account', $term, 5);
361  $this->assertTrue(ZurmoRedBean::$writer->doesTableExist(User::getTableName()));
362  $this->assertEmpty($data);
363  }
364  }
365 ?>
Definition: Email.php:37
static securableItemGivenPermissionsForUser(SecurableItem $securableItem, User $user)
static getByUsername($username)
Definition: User.php:49
static getGlobalSearchResultsByPartialTerm($partialTerm, $pageSize, User $user, $scopeData=null)
static loadStartingData()
static getByName($name, $shouldCache=true)
static getByPartialName($modelClassName, $partialName, $pageSize, $autoCompleteOptions=null)
static getByPartialName($partialName, $pageSize, $stateMetadataAdapterClassName=null, $autoCompleteOptions=null)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null, $buildFirstCurrency=true)
Definition: Currency.php:107
testGetGlobalSearchResultsByPartialTermWithRegularUserAndElevationStepsForRegularUser()
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
static getTableName()
static getCustomFieldDataByPartialName($customFieldDataName, $partialName)
Generated on Fri Jun 5 2020 07:10:36