]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/SugarCharts/Bug44696Test.php
Release 6.5.8
[Github/sugarcrm.git] / tests / include / SugarCharts / Bug44696Test.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
6  * This program 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 SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program 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 SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code 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 "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37
38 require_once('include/SugarCharts/SugarChart.php');
39
40 /**
41  * Bug 44696 - Wrong shortcut to the opportunities module from the dashlet
42  * "Pipeline By Sales Stage"
43  *
44  * @ticket 44696
45  * @ticket 53845
46  */
47 class Bug44696Test extends Sugar_PHPUnit_Framework_TestCase
48 {
49     /**
50      * @var SugarChart
51      */
52     protected $sugarChartObject;
53     /**
54      * @var User
55      */
56     protected $currentUser;
57
58     protected function setUp()
59     {
60         parent::setUp();
61
62         SugarTestHelper::init();
63         SugarTestHelper::setUp('current_user');
64         $this->currentUser = $GLOBALS['current_user'];
65         $this->currentUser->setPreference(
66             'default_number_grouping_seperator',
67             '.'
68         );
69         $this->currentUser->setPreference('default_decimal_seperator', ',');
70
71         $sugarChartObject = new SugarChart();
72         $sugarChartObject->group_by = array('sales_stage', 'user_name');
73         $sugarChartObject->data_set = $this->getDataSet();
74         $sugarChartObject->base_url = array(
75             'module' => 'Opportunities',
76             'action' => 'index',
77             'query' => 'true',
78             'searchFormTab' => 'advanced_search'
79         );
80         $sugarChartObject->url_params = array();
81         $sugarChartObject->is_currency = true;
82         // we have 5 users 
83         $sugarChartObject->super_set = array(
84             'will',
85             'max',
86             'sarah',
87             'sally',
88             'chris'
89         );
90         $this->sugarChartObject = $sugarChartObject;
91     }
92
93     protected function tearDown()
94     {
95         SugarTestHelper::tearDown();
96         parent::tearDown();
97     }
98
99     /**
100      * We check, that groups with NULL value remain their order in subgroups tag
101      * and won't fall down under not null valued groups.
102      * This way we guarantee that links will be put correctly to each user in
103      * whole user list (will, max, etc.).
104      *
105      * @group 44696
106      * @group 53845
107      */
108     public function testCorrectXml()
109     {
110         $actual = $this->sugarChartObject->xmlDataGenericChart();
111         $expected = $this->compareXml();
112         $order = array("\r\n", "\n", "\r", "\t");
113         $replace = "";
114         // remove all break lines and spaces and tabs
115         $expected = str_replace($order, $replace, $expected);
116         $actual = str_replace($order, $replace, $actual);
117         $this->assertEquals($expected, $actual);
118     }
119
120     protected function getDataSet()
121     {
122         return array(
123             array(
124                 'sales_stage' => 'Proposal/Price Quote',
125                 'user_name' => 'max',
126                 'assigned_user_id' => 'seed_max_id',
127                 'opp_count' => '1',
128                 'total' => 50.1234,
129                 'key' => 'Proposal/Price Quote',
130                 'value' => 'Proposal/Price Quote',
131             ),
132             array(
133                 'sales_stage' => 'Proposal/Price Quote',
134                 'user_name' => 'sally',
135                 'assigned_user_id' => 'seed_sally_id',
136                 'opp_count' => '2',
137                 'total' => 75.98765,
138                 'key' => 'Proposal/Price Quote',
139                 'value' => 'Proposal/Price Quote',
140             ),
141         );
142     }
143
144     /**
145      * Expected XML from SugarChart after.
146      *
147      * @return string
148      *   The XML string that we expect to be returned by the chart.
149      */
150     protected function compareXml()
151     {
152         $dataSet = $this->getDataSet();
153         $max = $dataSet[0]['total'];
154         $sally = $dataSet[1]['total'];
155         $total = $max + $sally;
156
157         list($maxSubAmount, $maxSubAmountFormatted) = $this->getAmounts($max);
158         list($sallySubAmount, $sallySubAmountFormatted) = $this->getAmounts(
159             $sally
160         );
161         list($totalSubAmount, $totalSubAmountFormatted) = $this->getAmounts(
162             $total
163         );
164
165         return "<group>
166                         <title>Proposal/Price Quote</title>
167                         <value>{$totalSubAmount}</value>
168                         <label>{$totalSubAmountFormatted}</label>
169                         <link>index.php?module=Opportunities&action=index&query=true&searchFormTab=advanced_search&sales_stage=Proposal%2FPrice+Quote</link>
170                         <subgroups>
171                                 <group>
172                                         <title>will</title>
173                                         <value>NULL</value>
174                                         <label></label>
175                                         <link>index.php?module=Opportunities&action=index&query=true&searchFormTab=advanced_search&sales_stage=Proposal%2FPrice+Quote</link>
176                                 </group>
177                                 <group>
178                                         <title>max</title>
179                                         <value>{$maxSubAmount}</value>
180                                         <label>{$maxSubAmountFormatted}</label>
181                                         <link>index.php?module=Opportunities&action=index&query=true&searchFormTab=advanced_search&sales_stage=Proposal%2FPrice+Quote&assigned_user_id[]=seed_max_id</link>
182                                 </group>
183                                 <group>
184                                         <title>sarah</title>
185                                         <value>NULL</value>
186                                         <label></label>
187                                         <link>index.php?module=Opportunities&action=index&query=true&searchFormTab=advanced_search&sales_stage=Proposal%2FPrice+Quote</link>
188                                 </group>
189                                 <group>
190                                         <title>sally</title>
191                                         <value>{$sallySubAmount}</value>
192                                         <label>{$sallySubAmountFormatted}</label>
193                                         <link>index.php?module=Opportunities&action=index&query=true&searchFormTab=advanced_search&sales_stage=Proposal%2FPrice+Quote&assigned_user_id[]=seed_sally_id</link>
194                                 </group>
195                                 <group>
196                                         <title>chris</title>
197                                         <value>NULL</value>
198                                         <label></label>
199                                         <link>index.php?module=Opportunities&action=index&query=true&searchFormTab=advanced_search&sales_stage=Proposal%2FPrice+Quote</link>
200                                 </group>
201                         </subgroups></group>";
202     }
203
204     /**
205      * Get the amounts that the chart should have for the value given.
206      *
207      * @param float $value
208      *   The value to get formatted as chart would.
209      *
210      * @return array
211      *   A list with: the value in K's and the value in currency format.
212      */
213     protected function getAmounts($value)
214     {
215         global $locale;
216         global $sugar_config;
217
218         $subAmount = round($value, $locale->getPrecision($this->currentUser));
219         // TODO use the Localization::getLocaleFormattedNumber when it works!
220         // FIXME SugarCharts should use the user format preferences for charts
221         $subAmountFormatted = number_format(
222             $value,
223             $locale->getPrecision($this->currentUser),
224             $sugar_config['default_decimal_seperator'],
225             $sugar_config['default_number_grouping_seperator']
226         );
227         $subAmountFormatted = $locale->getCurrencySymbol(
228             $this->currentUser
229         ) . $subAmountFormatted . 'K';
230
231         return array($subAmount, $subAmountFormatted);
232     }
233 }