]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/MVC/View/LoadMenuTest.php
Release 6.2.0
[Github/sugarcrm.git] / tests / include / MVC / View / LoadMenuTest.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 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 LoadMenuTest extends Sugar_PHPUnit_Framework_TestCase
41 {
42     protected $_moduleName;
43
44     public function setUp()
45         {
46                 global $mod_strings, $app_strings;
47                 $mod_strings = return_module_language($GLOBALS['current_language'], 'Accounts');
48                 $app_strings = return_application_language($GLOBALS['current_language']);
49
50                 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
51
52                 // create a dummy module directory
53                 $this->_moduleName = 'TestModule'.mt_rand();
54
55         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
56
57         sugar_mkdir("modules/{$this->_moduleName}",null,true);
58         }
59
60         public function tearDown()
61         {
62                 unset($GLOBALS['mod_strings']);
63                 unset($GLOBALS['app_strings']);
64
65                 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
66                 unset($GLOBALS['current_user']);
67         if(!empty($this->_moduleName)) {
68                 if ( is_dir("modules/{$this->_moduleName}") )
69                     rmdir_recursive("modules/{$this->_moduleName}");
70                 if ( is_dir("custom/modules/{$this->_moduleName}") )
71                     rmdir_recursive("custom/modules/{$this->_moduleName}");
72         }
73                 unset($GLOBALS['current_user']);
74         }
75
76         public function testMenuDoesNotExists()
77         {
78         $view = new SugarView;
79         $module_menu = $view->getMenu($this->_moduleName);
80         $this->assertTrue(empty($module_menu),'Assert the module menu array is empty');
81         }
82
83         public function testMenuExistsCanFindModuleMenu()
84         {
85             // Create module menu
86         if( $fh = @fopen("modules/{$this->_moduleName}/Menu.php", 'w+') ) {
87                 $string = <<<EOQ
88 <?php
89 \$module_menu[]=Array("index.php?module=Import&action=bar&import_module=Accounts&return_module=Accounts&return_action=index","Foo","Foo", 'Accounts');
90 ?>
91 EOQ;
92             fputs( $fh, $string);
93             fclose( $fh );
94         }
95
96         $view = new SugarView;
97         $module_menu = $view->getMenu($this->_moduleName);
98         $found_custom_menu = false;
99         foreach ($module_menu as $menu_entry) {
100                 foreach ($menu_entry as $menu_item) {
101                         if (preg_match('/action=bar/', $menu_item)) {
102                            $found_custom_menu = true;
103                         }
104                 }
105         }
106         $this->assertTrue($found_custom_menu, "Assert that menu was detected");
107         }
108
109     /**
110      * @ticket 29114
111      */
112     public function testMenuExistsCanFindModuleExtMenu()
113     {
114         // Create module ext menu
115         sugar_mkdir("custom/modules/{$this->_moduleName}/Ext/Menus/",null,true);
116         if( $fh = @fopen("custom/modules/{$this->_moduleName}/Ext/Menus/menu.ext.php", 'w+') ) {
117                 $string = <<<EOQ
118 <?php
119 \$module_menu[]=Array("index.php?module=Import&action=foo&import_module=Accounts&return_module=Accounts&return_action=index","Foo","Foo", 'Accounts');
120 ?>
121 EOQ;
122             fputs( $fh, $string);
123             fclose( $fh );
124         }
125
126         $view = new SugarView;
127         $module_menu = $view->getMenu($this->_moduleName);
128         $found_custom_menu = false;
129         foreach ($module_menu as $key => $menu_entry) {
130                 foreach ($menu_entry as $id => $menu_item) {
131                         if (preg_match('/action=foo/', $menu_item)) {
132                            $found_custom_menu = true;
133                         }
134                 }
135         }
136         $this->assertTrue($found_custom_menu, "Assert that custom menu was detected");
137     }
138
139     /**
140      * @ticket 38935
141      */
142     public function testMenuExistsCanFindModuleExtMenuWhenModuleMenuDefinedGlobal()
143     {
144         // Create module ext menu
145         sugar_mkdir("custom/modules/{$this->_moduleName}/Ext/Menus/",null,true);
146         if( $fh = @fopen("custom/modules/{$this->_moduleName}/Ext/Menus/menu.ext.php", 'w+') ) {
147                 $string = <<<EOQ
148 <?php
149 global \$module_menu;
150 \$module_menu[]=Array("index.php?module=Import&action=foo&import_module=Accounts&return_module=Accounts&return_action=index","Foo","Foo", 'Accounts');
151 ?>
152 EOQ;
153             fputs( $fh, $string);
154             fclose( $fh );
155         }
156
157         $view = new SugarView;
158         $module_menu = $view->getMenu($this->_moduleName);
159         $found_custom_menu = false;
160         foreach ($module_menu as $key => $menu_entry) {
161                 foreach ($menu_entry as $id => $menu_item) {
162                         if (preg_match('/action=foo/', $menu_item)) {
163                            $found_custom_menu = true;
164                         }
165                 }
166         }
167         $this->assertTrue($found_custom_menu, "Assert that custom menu was detected");
168     }
169
170     public function testMenuExistsCanFindApplicationExtMenu()
171         {
172             // Create module ext menu
173             $backupCustomMenu = false;
174             if ( !is_dir("custom/application/Ext/Menus/") )
175                 sugar_mkdir("custom/application/Ext/Menus/",null,true);
176         if (file_exists('custom/application/Ext/Menus/menu.ext.php')) {
177                 copy('custom/application/Ext/Menus/menu.ext.php', 'custom/application/Ext/Menus/menu.ext.php.backup');
178                 $backupCustomMenu = true;
179             }
180
181         if ( $fh = @fopen("custom/application/Ext/Menus/menu.ext.php", 'w+') ) {
182                 $string = <<<EOQ
183 <?php
184 \$module_menu[]=Array("index.php?module=Import&action=foobar&import_module=Accounts&return_module=Accounts&return_action=index","Foo","Foo", 'Accounts');
185 ?>
186 EOQ;
187             fputs( $fh, $string);
188             fclose( $fh );
189         }
190
191         $view = new SugarView;
192         $module_menu = $view->getMenu($this->_moduleName);
193         $found_application_custom_menu = false;
194         foreach ($module_menu as $key => $menu_entry) {
195                 foreach ($menu_entry as $id => $menu_item) {
196                         if (preg_match('/action=foobar/', $menu_item)) {
197                            $found_application_custom_menu = true;
198                         }
199                 }
200         }
201         $this->assertTrue($found_application_custom_menu, "Assert that application custom menu was detected");
202
203         if($backupCustomMenu) {
204             copy('custom/application/Ext/Menus/menu.ext.php.backup', 'custom/application/Ext/Menus/menu.ext.php');
205             unlink('custom/application/Ext/Menus/menu.ext.php.backup');
206         }
207         else
208             unlink('custom/application/Ext/Menus/menu.ext.php');
209         }
210
211         public function testMenuExistsCanFindModuleMenuAndModuleExtMenu()
212         {
213             // Create module menu
214         if( $fh = @fopen("modules/{$this->_moduleName}/Menu.php", 'w+') ) {
215                 $string = <<<EOQ
216 <?php
217 \$module_menu[]=Array("index.php?module=Import&action=foo&import_module=Accounts&return_module=Accounts&return_action=index","Foo","Foo", 'Accounts');
218 ?>
219 EOQ;
220             fputs( $fh, $string);
221             fclose( $fh );
222         }
223
224         // Create module ext menu
225         sugar_mkdir("custom/modules/{$this->_moduleName}/Ext/Menus/",null,true);
226         if( $fh = @fopen("custom/modules/{$this->_moduleName}/Ext/Menus/menu.ext.php", 'w+') ) {
227                 $string = <<<EOQ
228 <?php
229 \$module_menu[]=Array("index.php?module=Import&action=bar&import_module=Accounts&return_module=Accounts&return_action=index","Foo","Foo", 'Accounts');
230 ?>
231 EOQ;
232             fputs( $fh, $string);
233             fclose( $fh );
234         }
235
236         $view = new SugarView;
237         $module_menu = $view->getMenu($this->_moduleName);
238         $found_custom_menu = false;
239         $found_menu = false;
240         foreach ($module_menu as $key => $menu_entry) {
241                 foreach ($menu_entry as $id => $menu_item) {
242                         if (preg_match('/action=foo/', $menu_item)) {
243                            $found_menu = true;
244                         }
245                         if (preg_match('/action=bar/', $menu_item)) {
246                            $found_custom_menu = true;
247                         }
248                 }
249         }
250         $this->assertTrue($found_menu, "Assert that menu was detected");
251         $this->assertTrue($found_custom_menu, "Assert that custom menu was detected");
252         }
253 }
254
255 class ViewLoadMenuTest extends SugarView
256 {
257     public function menuExists(
258         $module
259         )
260     {
261         return $this->_menuExists($module);
262     }
263 }