2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
38 require_once 'include/MVC/View/SugarView.php';
40 class SugarViewTest extends Sugar_PHPUnit_Framework_TestCase
42 private $_backup = array();
45 * @var SugarViewTestMock
49 public function setUp()
51 global $moduleList, $beanList, $beanFiles;
52 require('include/modules.php');
53 $this->_view = new SugarViewTestMock();
54 $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
55 $GLOBALS['mod_strings'] = return_module_language($GLOBALS['current_language'], 'Users');
56 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
57 $this->_backup['currentTheme'] = SugarThemeRegistry::current();
60 public function tearDown()
62 unset($GLOBALS['mod_strings']);
63 unset($GLOBALS['app_strings']);
64 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
65 unset($GLOBALS['current_user']);
66 SugarThemeRegistry::set($this->_backup['currentTheme']->dirName);
69 public function testGetModuleTab()
71 $_REQUEST['module_tab'] = 'ADMIN';
72 $moduleTab = $this->_view->getModuleTab();
73 $this->assertEquals('ADMIN', $moduleTab, 'Module Tab names are not equal from request');
76 public function testGetMetaDataFile()
78 // backup custom file if it already exists
79 if(file_exists('custom/modules/Contacts/metadata/listviewdefs.php')){
80 copy('custom/modules/Contacts/metadata/listviewdefs.php', 'custom/modules/Contacts/metadata/listviewdefs.php.bak');
81 unlink('custom/modules/Contacts/metadata/listviewdefs.php');
83 $this->_view->module = 'Contacts';
84 $this->_view->type = 'list';
85 $metaDataFile = $this->_view->getMetaDataFile();
86 $this->assertEquals('modules/Contacts/metadata/listviewdefs.php', $metaDataFile, 'Did not load the correct metadata file');
89 if(!file_exists('custom/modules/Contacts/metadata/')){
90 sugar_mkdir('custom/modules/Contacts/metadata/', null, true);
92 $customFile = 'custom/modules/Contacts/metadata/listviewdefs.php';
93 if(!file_exists($customFile))
95 sugar_file_put_contents($customFile, array());
96 $customMetaDataFile = $this->_view->getMetaDataFile();
97 $this->assertEquals($customFile, $customMetaDataFile, 'Did not load the correct custom metadata file');
100 // Restore custom file if we backed it up
101 if(file_exists('custom/modules/Contacts/metadata/listviewdefs.php.bak')){
102 rename('custom/modules/Contacts/metadata/listviewdefs.php.bak', 'custom/modules/Contacts/metadata/listviewdefs.php');
106 public function testInit()
108 $bean = new SugarBean;
109 $view_object_map = array('foo'=>'bar');
110 $GLOBALS['action'] = 'barbar';
111 $GLOBALS['module'] = 'foofoo';
113 $this->_view->init($bean,$view_object_map);
115 $this->assertInstanceOf('SugarBean',$this->_view->bean);
116 $this->assertEquals($view_object_map,$this->_view->view_object_map);
117 $this->assertEquals($GLOBALS['action'],$this->_view->action);
118 $this->assertEquals($GLOBALS['module'],$this->_view->module);
119 $this->assertInstanceOf('Sugar_Smarty',$this->_view->ss);
122 public function testInitNoParameters()
124 $GLOBALS['action'] = 'barbar';
125 $GLOBALS['module'] = 'foofoo';
127 $this->_view->init();
129 $this->assertNull($this->_view->bean);
130 $this->assertEquals(array(),$this->_view->view_object_map);
131 $this->assertEquals($GLOBALS['action'],$this->_view->action);
132 $this->assertEquals($GLOBALS['module'],$this->_view->module);
133 $this->assertInstanceOf('Sugar_Smarty',$this->_view->ss);
136 public function testInitSmarty()
138 $this->_view->initSmarty();
140 $this->assertInstanceOf('Sugar_Smarty',$this->_view->ss);
141 $this->assertEquals($this->_view->ss->get_template_vars('MOD'),$GLOBALS['mod_strings']);
142 $this->assertEquals($this->_view->ss->get_template_vars('APP'),$GLOBALS['app_strings']);
146 * @outputBuffering enabled
148 public function testDisplayErrors()
150 $this->_view->errors = array('error1','error2');
151 $this->_view->suppressDisplayErrors = true;
154 '<span class="error">error1</span><br><span class="error">error2</span><br>',
155 $this->_view->displayErrors()
160 * @outputBuffering enabled
162 public function testDisplayErrorsDoNotSupressOutput()
164 $this->_view->errors = array('error1','error2');
165 $this->_view->suppressDisplayErrors = false;
167 $this->assertEmpty($this->_view->displayErrors());
170 public function testGetBrowserTitle()
172 $viewMock = $this->getMock('SugarViewTestMock',array('_getModuleTitleParams'));
173 $viewMock->expects($this->any())
174 ->method('_getModuleTitleParams')
175 ->will($this->returnValue(array('foo','bar')));
178 "bar » foo » {$GLOBALS['app_strings']['LBL_BROWSER_TITLE']}",
179 $viewMock->getBrowserTitle()
183 public function testGetBrowserTitleUserLogin()
185 $this->_view->module = 'Users';
186 $this->_view->action = 'Login';
189 "{$GLOBALS['app_strings']['LBL_BROWSER_TITLE']}",
190 $this->_view->getBrowserTitle()
194 public function testGetBreadCrumbSymbolForLTRTheme()
196 $theme = SugarTestThemeUtilities::createAnonymousTheme();
197 SugarThemeRegistry::set($theme);
200 "<span class='pointer'>»</span>",
201 $this->_view->getBreadCrumbSymbol()
205 public function testGetBreadCrumbSymbolForRTLTheme()
207 $theme = SugarTestThemeUtilities::createAnonymousRTLTheme();
208 SugarThemeRegistry::set($theme);
211 "<span class='pointer'>«</span>",
212 $this->_view->getBreadCrumbSymbol()
216 public function testGetSugarConfigJS()
218 global $sugar_config;
220 $sugar_config['js_available'] = array('default_action');
222 $js_array = $this->_view->getSugarConfigJS();
224 // this should return 3 objects
225 $this->assertEquals(3, count($js_array));
227 $this->assertEquals('SUGAR.config.default_action = "index";', $js_array[2]);
231 class SugarViewTestMock extends SugarView
233 public function getModuleTab()
235 return parent::_getModuleTab();
238 public function initSmarty()
240 return parent::_initSmarty();
243 public function getSugarConfigJS()
245 return parent::getSugarConfigJS();