]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/MVC/View/SugarViewTest.php
Release 6.4.3
[Github/sugarcrm.git] / tests / include / MVC / View / SugarViewTest.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/MVC/View/SugarView.php';
39
40 class SugarViewTest extends Sugar_PHPUnit_Framework_TestCase
41 {
42     private $_backup = array();
43
44     /**
45      * @var SugarViewTestMock
46      */
47     private $_view;
48
49     public function setUp()
50     {
51         $this->_view = new SugarViewTestMock();
52         $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
53         $GLOBALS['mod_strings'] = return_module_language($GLOBALS['current_language'], 'Users');
54         $this->_backup['currentTheme'] = SugarThemeRegistry::current();
55     }
56     
57     public function tearDown()
58     {
59         unset($GLOBALS['mod_strings']);
60         unset($GLOBALS['app_strings']);
61         SugarThemeRegistry::set($this->_backup['currentTheme']->dirName);
62     }
63     
64     public function testGetModuleTab()
65     {
66         $_REQUEST['module_tab'] = 'ADMIN';
67         $moduleTab = $this->_view->getModuleTab();
68         $this->assertEquals('ADMIN', $moduleTab, 'Module Tab names are not equal from request');
69     }
70
71     public function testGetMetaDataFile()
72     {
73         // backup custom file if it already exists
74         if(file_exists('custom/modules/Contacts/metadata/listviewdefs.php')){
75             copy('custom/modules/Contacts/metadata/listviewdefs.php', 'custom/modules/Contacts/metadata/listviewdefs.php.bak');
76             unlink('custom/modules/Contacts/metadata/listviewdefs.php');
77         }
78         $this->_view->module = 'Contacts';
79         $this->_view->type = 'list';
80         $metaDataFile = $this->_view->getMetaDataFile();
81         $this->assertEquals('modules/Contacts/metadata/listviewdefs.php', $metaDataFile, 'Did not load the correct metadata file');
82
83         //test custom file
84         if(!file_exists('custom/modules/Contacts/metadata/')){
85             sugar_mkdir('custom/modules/Contacts/metadata/', null, true);
86         }
87         $customFile = 'custom/modules/Contacts/metadata/listviewdefs.php';
88         if(!file_exists($customFile))
89         {
90             sugar_file_put_contents($customFile, array());
91             $customMetaDataFile = $this->_view->getMetaDataFile();
92             $this->assertEquals($customFile, $customMetaDataFile, 'Did not load the correct custom metadata file');
93             unlink($customFile);
94         }
95         // Restore custom file if we backed it up
96         if(file_exists('custom/modules/Contacts/metadata/listviewdefs.php.bak')){
97             rename('custom/modules/Contacts/metadata/listviewdefs.php.bak', 'custom/modules/Contacts/metadata/listviewdefs.php');
98         }
99     }
100     
101     public function testInit()
102     {
103         $bean = new SugarBean;
104         $view_object_map = array('foo'=>'bar');
105         $GLOBALS['action'] = 'barbar';
106         $GLOBALS['module'] = 'foofoo';
107         
108         $this->_view->init($bean,$view_object_map);
109         
110         $this->assertInstanceOf('SugarBean',$this->_view->bean);
111         $this->assertEquals($view_object_map,$this->_view->view_object_map);
112         $this->assertEquals($GLOBALS['action'],$this->_view->action);
113         $this->assertEquals($GLOBALS['module'],$this->_view->module);
114         $this->assertInstanceOf('Sugar_Smarty',$this->_view->ss);
115     }
116     
117     public function testInitNoParameters()
118     {
119         $GLOBALS['action'] = 'barbar';
120         $GLOBALS['module'] = 'foofoo';
121         
122         $this->_view->init();
123         
124         $this->assertNull($this->_view->bean);
125         $this->assertEquals(array(),$this->_view->view_object_map);
126         $this->assertEquals($GLOBALS['action'],$this->_view->action);
127         $this->assertEquals($GLOBALS['module'],$this->_view->module);
128         $this->assertInstanceOf('Sugar_Smarty',$this->_view->ss);
129     }
130     
131     public function testInitSmarty()
132     {
133         $this->_view->initSmarty();
134         
135         $this->assertInstanceOf('Sugar_Smarty',$this->_view->ss);
136         $this->assertEquals($this->_view->ss->get_template_vars('MOD'),$GLOBALS['mod_strings']);
137         $this->assertEquals($this->_view->ss->get_template_vars('APP'),$GLOBALS['app_strings']);
138     }
139     
140     /**
141      * @outputBuffering enabled
142      */
143     public function testDisplayErrors()
144     {
145         $this->_view->errors = array('error1','error2');
146         $this->_view->suppressDisplayErrors = true;
147         
148         $this->assertEquals(
149             '<span class="error">error1</span><br><span class="error">error2</span><br>',
150             $this->_view->displayErrors()
151             );
152     }
153     
154     /**
155      * @outputBuffering enabled
156      */
157     public function testDisplayErrorsDoNotSupressOutput()
158     {
159         $this->_view->errors = array('error1','error2');
160         $this->_view->suppressDisplayErrors = false;
161         
162         $this->assertEmpty($this->_view->displayErrors());
163     }
164     
165     public function testGetBrowserTitle()
166     {
167         $viewMock = $this->getMock('SugarViewTestMock',array('_getModuleTitleParams'));
168         $viewMock->expects($this->any())
169                  ->method('_getModuleTitleParams')
170                  ->will($this->returnValue(array('foo','bar')));
171         
172         $this->assertEquals(
173             "bar &raquo; foo &raquo; {$GLOBALS['app_strings']['LBL_BROWSER_TITLE']}",
174             $viewMock->getBrowserTitle()
175             );
176     }
177     
178     public function testGetBrowserTitleUserLogin()
179     {
180         $this->_view->module = 'Users';
181         $this->_view->action = 'Login';
182         
183         $this->assertEquals(
184             "{$GLOBALS['app_strings']['LBL_BROWSER_TITLE']}",
185             $this->_view->getBrowserTitle()
186             );
187     }
188     
189     public function testGetBreadCrumbSymbolForLTRTheme()
190     {
191         $theme = SugarTestThemeUtilities::createAnonymousTheme();
192         SugarThemeRegistry::set($theme);
193         
194         $this->assertEquals(
195             "<span class='pointer'>&raquo;</span>",
196             $this->_view->getBreadCrumbSymbol()
197             );
198     }
199     
200     public function testGetBreadCrumbSymbolForRTLTheme()
201     {
202         $theme = SugarTestThemeUtilities::createAnonymousRTLTheme();
203         SugarThemeRegistry::set($theme);
204         
205         $this->assertEquals(
206             "<span class='pointer'>&laquo;</span>",
207             $this->_view->getBreadCrumbSymbol()
208             );
209     }
210
211     public function testGetSugarConfigJS()
212     {
213         global $sugar_config;
214
215         $sugar_config['js_available'] = array('default_action');
216
217         $js_array = $this->_view->getSugarConfigJS();
218
219         // this should return 3 objects
220         $this->assertEquals(3, count($js_array));
221
222         $this->assertEquals('SUGAR.config.default_action = "index";', $js_array[2]);
223     }
224 }
225
226 class SugarViewTestMock extends SugarView
227 {
228     public function getModuleTab()
229     {
230         return parent::_getModuleTab();
231     }
232     
233     public function initSmarty()
234     {
235         return parent::_initSmarty();
236     }
237
238     public function getSugarConfigJS()
239     {
240         return parent::getSugarConfigJS();
241     }
242 }