Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
OpportunityTest.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  }
44 
45  public function testCreateStageValues()
46  {
47  $stageValues = array(
48  'Prospecting',
49  'Negotiating',
50  'Closed Won',
51  );
52  $stageFieldData = CustomFieldData::getByName('SalesStages');
53  $stageFieldData->serializedData = serialize($stageValues);
54  $this->assertTrue($stageFieldData->save());
55  }
56 
61  {
62  $this->assertEquals(6, count(OpportunitiesModule::getStageToProbabilityMappingData()));
63  }
64 
69  {
70  $this->assertEquals(10, OpportunitiesModule::getProbabilityByStageValue ('Prospecting'));
71  $this->assertEquals(50, OpportunitiesModule::getProbabilityByStageValue ('Negotiating'));
72  $this->assertEquals(100, OpportunitiesModule::getProbabilityByStageValue ('Closed Won'));
73  }
74 
78  public function testVariousCurrencyValues()
79  {
80  $super = User::getByUsername('super');
81  Yii::app()->user->userModel = $super;
82  $currencies = Currency::getAll();
83  $currencyValue = new CurrencyValue();
84  $currencyValue->value = 100;
85  $currencyValue->currency = $currencies[0];
86  $this->assertEquals('USD', $currencyValue->currency->code);
87  $opportunity = new Opportunity();
88  $opportunity->owner = $super;
89  $opportunity->name = 'test';
90  $opportunity->amount = $currencyValue;
91  $opportunity->closeDate = '2011-01-01';
92  $opportunity->stage->value = 'Verbal';
93  $this->assertEquals(0, $opportunity->probability);
94  $saved = $opportunity->save();
95  $this->assertTrue($saved);
96  $this->assertEquals(75, $opportunity->probability);
97  $opportunity1Id = $opportunity->id;
98  $opportunity->forget();
99 
100  $currencyValue = new CurrencyValue();
101  $currencyValue->value = 800;
102  $currencyValue->currency = $currencies[0];
103  $this->assertEquals('USD', $currencyValue->currency->code);
104  $opportunity = new Opportunity();
105  $opportunity->owner = $super;
106  $opportunity->name = 'test';
107  $opportunity->amount = $currencyValue;
108  $opportunity->closeDate = '2011-01-01';
109  $opportunity->stage->value = 'Verbal';
110  $saved = $opportunity->save();
111  $this->assertTrue($saved);
112  $opportunity2Id = $opportunity->id;
113  $opportunity->forget();
114  $currencyValue->forget(); //need to forget this to pull the accurate value from the database
115 
116  $opportunity1 = Opportunity::getById($opportunity1Id);
117  $this->assertEquals(100, $opportunity1->amount->value);
118 
119  $opportunity2 = Opportunity::getById($opportunity2Id);
120  $this->assertEquals(800, $opportunity2->amount->value);
121 
122  $opportunity1->delete();
123  $opportunity2->delete();
124  }
125 
130  {
131  Yii::app()->user->userModel = User::getByUsername('super');
132 
133  $user = UserTestHelper::createBasicUser('Billy');
134  $currencies = Currency::getAll();
135  $currencyValue = new CurrencyValue();
136  $currencyValue->value = 500.54;
137  $currencyValue->currency = $currencies[0];
138  $opportunity = new Opportunity();
139  $opportunity->owner = $user;
140  $opportunity->name = 'Test Opportunity';
141  $opportunity->amount = $currencyValue;
142  $opportunity->closeDate = '2011-01-01'; //eventually fix to make correct format
143  $opportunity->stage->value = 'Negotiating';
144  $this->assertTrue($opportunity->save());
145  $id = $opportunity->id;
146  unset($opportunity);
147  $opportunity = Opportunity::getById($id);
148  $this->assertEquals('Test Opportunity', $opportunity->name);
149  $this->assertEquals('500.54', $opportunity->amount->value);
150  $this->assertEquals('Negotiating', $opportunity->stage->value);
151  $this->assertEquals('2011-01-01', $opportunity->closeDate);
152  $this->assertEquals(1, $currencies[0]->rateToBase);
153  $this->assertTrue($opportunity->isAttributeReadOnly('stageModifiedDateTime'));
154  }
155 
159  public function testGetOpportunitiesByName()
160  {
161  Yii::app()->user->userModel = User::getByUsername('super');
162 
163  $opportunities = Opportunity::getByName('Test Opportunity');
164  $this->assertEquals(1, count($opportunities));
165  $this->assertEquals('Test Opportunity', $opportunities[0]->name);
166  }
167 
171  public function testGetLabel()
172  {
173  Yii::app()->user->userModel = User::getByUsername('super');
174  $opportunities = Opportunity::getByName('Test Opportunity');
175  $this->assertEquals(1, count($opportunities));
176  $this->assertEquals('Opportunity', $opportunities[0]::getModelLabelByTypeAndLanguage('Singular'));
177  $this->assertEquals('Opportunities', $opportunities[0]::getModelLabelByTypeAndLanguage('Plural'));
178  }
179 
184  {
185  Yii::app()->user->userModel = User::getByUsername('super');
186  $opportunities = Opportunity::getByName('Test Opportunity 69');
187  $this->assertEquals(0, count($opportunities));
188  }
189 
193  public function testGetAll()
194  {
195  Yii::app()->user->userModel = User::getByUsername('super');
196 
197  $user = User::getByUsername('billy');
198  $currencies = Currency::getAll();
199  $currencyValue = new CurrencyValue();
200  $currencyValue->value = 500.54;
201  $currencyValue->currency = $currencies[0];
202  $opportunity = new Opportunity();
203  $opportunity->owner = $user;
204  $opportunity->name = 'Test Opportunity 2';
205  $opportunity->amount = $currencyValue;
206  $opportunity->closeDate = '2011-01-01'; //eventually fix to make correct format
207  $opportunity->stage->value = 'Negotiating';
208  $this->assertTrue($opportunity->save());
209  $opportunities = Opportunity::getAll();
210  $this->assertEquals(2, count($opportunities));
211  $this->assertTrue('Test Opportunity' == $opportunities[0]->name &&
212  'Test Opportunity 2' == $opportunities[1]->name ||
213  'Test Opportunity 2' == $opportunities[0]->name &&
214  'Test Opportunity' == $opportunities[1]->name);
215  $this->assertEquals(1, $currencies[0]->rateToBase);
216  }
217 
221  public function testSetAndGetOwner()
222  {
223  Yii::app()->user->userModel = User::getByUsername('super');
224 
225  $user = UserTestHelper::createBasicUser('Dicky');
226 
227  $opportunities = Opportunity::getByName('Test Opportunity');
228  $this->assertEquals(1, count($opportunities));
229  $opportunity = $opportunities[0];
230  $opportunity->owner = $user;
231  $this->assertTrue($opportunity->save());
232  unset($user);
233  $this->assertTrue($opportunity->owner !== null);
234  $opportunity->owner = null;
235  $this->assertFalse($opportunity->validate());
236  $opportunity->forget();
237  unset($opportunity);
238  }
239 
243  public function testReplaceOwner()
244  {
245  Yii::app()->user->userModel = User::getByUsername('super');
246 
247  $opportunities = Opportunity::getByName('Test Opportunity');
248  $this->assertEquals(1, count($opportunities));
249  $opportunity = $opportunities[0];
250  $user = User::getByUsername('dicky');
251  $this->assertEquals($user->id, $opportunity->owner->id);
252  unset($user);
253  $user2 = UserTestHelper::createBasicUser('Benny');
254  $opportunity->owner = $user2;
255  unset($user2);
256  $this->assertTrue($opportunity->owner !== null);
257  $user = $opportunity->owner;
258  $this->assertEquals('benny', $user->username);
259  unset($user);
260  }
261 
266  {
267  Yii::app()->user->userModel = User::getByUsername('super');
268 
269  $user = User::getByUsername('billy');
270  $opportunities = Opportunity::getByName('Test Opportunity');
271  $opportunity = $opportunities[0];
272  $this->assertEquals($opportunity->name, 'Test Opportunity');
273  $currencies = Currency::getAll();
274  $postData = array(
275  'owner' => array(
276  'id' => $user->id,
277  ),
278  'name' => 'New Name',
279  'amount' => array('value' => '500.54', 'currency' => array('id' => $currencies[0]->id)),
280  'closeDate' => '2011-01-01',
281  'stage' => array(
282  'value' => 'Negotiating'
283  ),
284  );
285  $opportunity->setAttributes($postData);
286  $this->assertTrue($opportunity->save());
287 
288  $id = $opportunity->id;
289  unset($opportunity);
290  $opportunity = Opportunity::getById($id);
291  $this->assertEquals('New Name', $opportunity->name);
292  $this->assertEquals(500.54, $opportunity->amount->value);
293  $this->assertEquals(50, $opportunity->probability);
294  $this->assertEquals(1, $currencies[0]->rateToBase);
295 
296  //Updating probability mapping should make changes on saving opportunity
297  $metadata = OpportunitiesModule::getMetadata();
298  $metadata['global']['stageToProbabilityMapping']['Negotiating'] = 60;
300  $postData = array();
301  $opportunity->setAttributes($postData);
302  $this->assertTrue($opportunity->save());
303  unset($opportunity);
304  $opportunity = Opportunity::getById($id);
305  $this->assertEquals('New Name', $opportunity->name);
306  $this->assertEquals(500.54, $opportunity->amount->value);
307  $this->assertEquals(60, $opportunity->probability);
308  $this->assertEquals(1, $currencies[0]->rateToBase);
309  }
310 
311  public function testDeleteOpportunity()
312  {
313  Yii::app()->user->userModel = User::getByUsername('super');
314 
315  $opportunities = Opportunity::getAll();
316  $this->assertEquals(2, count($opportunities));
317  $opportunities[0]->delete();
318  $opportunities = Opportunity::getAll();
319  $this->assertEquals(1, count($opportunities));
320  $opportunities[0]->delete();
321  $opportunities = Opportunity::getAll();
322  $this->assertEquals(0, count($opportunities));
323  $currencies = Currency::getAll();
324  $this->assertEquals(1, $currencies[0]->rateToBase);
325  }
326 
327  public function testGetAllWhenThereAreNone()
328  {
329  Yii::app()->user->userModel = User::getByUsername('super');
330  $opportunities = Opportunity::getAll();
331  $this->assertEquals(0, count($opportunities));
332  }
333 
338  {
339  Yii::app()->user->userModel = User::getByUsername('super');
340 
341  $user = User::getByUsername('billy');
342 
343  $stageValues = array(
344  'Prospecting',
345  'Negotiating',
346  'Closed Won',
347  );
348  $stageFieldData = CustomFieldData::getByName('SalesStages');
349  $stageFieldData->serializedData = serialize($stageValues);
350  $this->assertTrue($stageFieldData->save());
351 
352  $sourceValues = array(
353  'Word of Mouth',
354  'Outbound',
355  'Trade Show',
356  );
357  $sourceFieldData = CustomFieldData::getByName('LeadSources');
358  $sourceFieldData->serializedData = serialize($sourceValues);
359  $this->assertTrue($sourceFieldData->save());
360 
361  $currencies = Currency::getAll();
362  $currencyValue = new CurrencyValue();
363  $currencyValue->value = 500.54;
364  $currencyValue->currency = $currencies[0];
365  $opportunity = new Opportunity();
366  $opportunity->owner = $user;
367  $opportunity->name = '1000 Widgets';
368  $opportunity->amount = $currencyValue;
369  $opportunity->closeDate = '2011-01-01'; //eventually fix to make correct format
370  $opportunity->stage->value = $stageValues[1];
371  $opportunity->source->value = $sourceValues[1];
372  $saved = $opportunity->save();
373  $this->assertTrue($saved);
374  $this->assertTrue($opportunity->id !== null);
375  $id = $opportunity->id;
376  unset($opportunity);
377  $opportunity = Opportunity::getById($id);
378  $this->assertEquals('Negotiating', $opportunity->stage->value);
379  $this->assertEquals('Outbound', $opportunity->source->value);
380  $this->assertEquals(1, $currencies[0]->rateToBase);
381  }
382 
383  public function testGetModelClassNames()
384  {
385  $modelClassNames = OpportunitiesModule::getModelClassNames();
386  $this->assertEquals(2, count($modelClassNames));
387  $this->assertEquals('Opportunity', $modelClassNames[0]);
388  $this->assertEquals('OpportunityStarred', $modelClassNames[1]);
389  }
390 
395  {
396  Yii::app()->user->userModel = User::getByUsername('super');
397  $opportunity = new Opportunity();
398  $opportunity->stageModifiedDateTime = '2015-01-01 21:00:00';
399  }
400  }
401 ?>
testOpportunityReadOnlyFieldStageModifiedTime()
testGetStageToProbabilityMappingData()
static getMetadata(User $user=null)
Definition: Module.php:502
static getByUsername($username)
Definition: User.php:49
static getProbabilityByStageValue($value)
static getModelClassNames()
Definition: Module.php:565
static getByName($name, $shouldCache=true)
testGetOpportunitiesByNameForNonExistentName()
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null, $buildFirstCurrency=true)
Definition: Currency.php:107
static getById($id, $modelClassName=null)
static setMetadata(array $metadata, User $user=null)
Definition: Module.php:533
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
testSetStageAndSourceAndRetrieveDisplayName()
Generated on Tue Jul 7 2020 07:10:34
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.