]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/MVC/View/LoadMenuTest.php
Release 6.2.3
[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         /**
84          * @ticket 43497
85          */
86         public function testMenuExistsCanFindModuleMenu()
87         {
88             // Create module menu
89         if( $fh = @fopen("modules/{$this->_moduleName}/Menu.php", 'w+') ) {
90                 $string = <<<EOQ
91 <?php
92 \$module_menu[]=Array("index.php?module=Import&action=bar&import_module=Accounts&return_module=Accounts&return_action=index","Foo","Foo", 'Accounts');
93 ?>
94 EOQ;
95             fputs( $fh, $string);
96             fclose( $fh );
97         }
98
99         $view = new SugarView;
100         $module_menu = $view->getMenu($this->_moduleName);
101         $found_menu = false;
102         $found_menu_twice = false;
103         foreach ($module_menu as $menu_entry) {
104                 foreach ($menu_entry as $menu_item) {
105                         if (preg_match('/action=bar/', $menu_item)) {
106                             if ( $found_menu ) {
107                                 $found_menu_twice = true;
108                             }
109                             $found_menu = true;
110                         }
111                 }
112         }
113         
114         $this->assertTrue($found_menu, "Assert that menu was detected");
115         $this->assertFalse($found_menu_twice, "Assert that menu item wasn't listed twice");
116         }
117
118     /**
119      * @ticket 29114
120      * @ticket 43497
121      */
122     public function testMenuExistsCanFindModuleExtMenu()
123     {
124         // Create module ext menu
125         sugar_mkdir("custom/modules/{$this->_moduleName}/Ext/Menus/",null,true);
126         if( $fh = @fopen("custom/modules/{$this->_moduleName}/Ext/Menus/menu.ext.php", 'w+') ) {
127                 $string = <<<EOQ
128 <?php
129 \$module_menu[]=Array("index.php?module=Import&action=foo&import_module=Accounts&return_module=Accounts&return_action=index","Foo","Foo", 'Accounts');
130 ?>
131 EOQ;
132             fputs( $fh, $string);
133             fclose( $fh );
134         }
135
136         $view = new SugarView;
137         $module_menu = $view->getMenu($this->_moduleName);
138         $found_custom_menu = false;
139         $found_custom_menu_twice = false;
140         foreach ($module_menu as $key => $menu_entry) {
141                 foreach ($menu_entry as $id => $menu_item) {
142                         if (preg_match('/action=foo/', $menu_item)) {
143                             if ( $found_custom_menu ) {
144                                 $found_custom_menu_twice = true;
145                             }
146                             $found_custom_menu = true;
147                         }
148                 }
149         }
150         $this->assertTrue($found_custom_menu, "Assert that custom menu was detected");
151         $this->assertFalse($found_custom_menu_twice, "Assert that custom menu item wasn't listed twice");
152     }
153
154     /**
155      * @ticket 38935
156      * @ticket 43497
157      */
158     public function testMenuExistsCanFindModuleExtMenuWhenModuleMenuDefinedGlobal()
159     {
160         // Create module ext menu
161         sugar_mkdir("custom/modules/{$this->_moduleName}/Ext/Menus/",null,true);
162         if( $fh = @fopen("custom/modules/{$this->_moduleName}/Ext/Menus/menu.ext.php", 'w+') ) {
163                 $string = <<<EOQ
164 <?php
165 global \$module_menu;
166 \$module_menu[]=Array("index.php?module=Import&action=foo&import_module=Accounts&return_module=Accounts&return_action=index","Foo","Foo", 'Accounts');
167 ?>
168 EOQ;
169             fputs( $fh, $string);
170             fclose( $fh );
171         }
172
173         $view = new SugarView;
174         $module_menu = $view->getMenu($this->_moduleName);
175         $found_custom_menu = false;
176         $found_custom_menu_twice = false;
177         foreach ($module_menu as $key => $menu_entry) {
178                 foreach ($menu_entry as $id => $menu_item) {
179                         if (preg_match('/action=foo/', $menu_item)) {
180                             if ( $found_custom_menu ) {
181                                 $found_custom_menu_twice = true;
182                             }
183                             $found_custom_menu = true;
184                         }
185                 }
186         }
187         
188         $this->assertTrue($found_custom_menu, "Assert that custom menu was detected");
189         $this->assertFalse($found_custom_menu_twice, "Assert that custom menu item wasn't listed twice");
190     }    
191     
192     /**
193      * @ticket 43497
194      */
195     public function testMenuExistsCanFindApplicationExtMenu()
196         {
197             // Create module ext menu
198             $backupCustomMenu = false;
199             if ( !is_dir("custom/application/Ext/Menus/") )
200                 sugar_mkdir("custom/application/Ext/Menus/",null,true);
201         if (file_exists('custom/application/Ext/Menus/menu.ext.php')) {
202                 copy('custom/application/Ext/Menus/menu.ext.php', 'custom/application/Ext/Menus/menu.ext.php.backup');
203                 $backupCustomMenu = true;
204             }
205
206         if ( $fh = @fopen("custom/application/Ext/Menus/menu.ext.php", 'w+') ) {
207                 $string = <<<EOQ
208 <?php
209 \$module_menu[]=Array("index.php?module=Import&action=foobar&import_module=Accounts&return_module=Accounts&return_action=index","Foo","Foo", 'Accounts');
210 ?>
211 EOQ;
212             fputs( $fh, $string);
213             fclose( $fh );
214         }
215
216         $view = new SugarView;
217         $module_menu = $view->getMenu($this->_moduleName);
218         $found_application_custom_menu = false;
219         $found_application_custom_menu_twice = false;
220         foreach ($module_menu as $key => $menu_entry) {
221                 foreach ($menu_entry as $id => $menu_item) {
222                         if (preg_match('/action=foobar/', $menu_item)) {
223                             if ( $found_application_custom_menu ) {
224                                 $found_application_custom_menu_twice = true;
225                             }
226                             $found_application_custom_menu = true;
227                         }
228                 }
229         }
230         
231         $this->assertTrue($found_application_custom_menu, "Assert that application custom menu was detected");
232         $this->assertFalse($found_application_custom_menu_twice, "Assert that application custom menu item wasn't duplicated");
233         
234         if($backupCustomMenu) {
235             copy('custom/application/Ext/Menus/menu.ext.php.backup', 'custom/application/Ext/Menus/menu.ext.php');
236             unlink('custom/application/Ext/Menus/menu.ext.php.backup');
237         }
238         else
239             unlink('custom/application/Ext/Menus/menu.ext.php');
240         }
241
242         /**
243          * @ticket 43497
244          */
245         public function testMenuExistsCanFindModuleMenuAndModuleExtMenu()
246         {
247             // Create module menu
248         if( $fh = @fopen("modules/{$this->_moduleName}/Menu.php", 'w+') ) {
249                 $string = <<<EOQ
250 <?php
251 \$module_menu[]=Array("index.php?module=Import&action=foo&import_module=Accounts&return_module=Accounts&return_action=index","Foo","Foo", 'Accounts');
252 ?>
253 EOQ;
254             fputs( $fh, $string);
255             fclose( $fh );
256         }
257
258         // Create module ext menu
259         sugar_mkdir("custom/modules/{$this->_moduleName}/Ext/Menus/",null,true);
260         if( $fh = @fopen("custom/modules/{$this->_moduleName}/Ext/Menus/menu.ext.php", 'w+') ) {
261                 $string = <<<EOQ
262 <?php
263 \$module_menu[]=Array("index.php?module=Import&action=bar&import_module=Accounts&return_module=Accounts&return_action=index","Foo","Foo", 'Accounts');
264 ?>
265 EOQ;
266             fputs( $fh, $string);
267             fclose( $fh );
268         }
269
270         $view = new SugarView;
271         $module_menu = $view->getMenu($this->_moduleName);
272         $found_custom_menu = false;
273         $found_custom_menu_twice = false;
274         $found_menu = false;
275         $found_menu_twice = false;
276         foreach ($module_menu as $key => $menu_entry) {
277                 foreach ($menu_entry as $id => $menu_item) {
278                         if (preg_match('/action=foo/', $menu_item)) {
279                             if ( $found_menu ) {
280                                 $found_menu_twice = true;
281                             }
282                             $found_menu = true;
283                         }
284                         if (preg_match('/action=bar/', $menu_item)) {
285                             if ( $found_custom_menu ) {
286                                 $found_custom_menu_twice = true;
287                             }
288                             $found_custom_menu = true;
289                         }
290                 }
291         }
292         $this->assertTrue($found_menu, "Assert that menu was detected");
293         $this->assertFalse($found_menu_twice, "Assert that menu item wasn't duplicated");
294         $this->assertTrue($found_custom_menu, "Assert that custom menu was detected");
295         $this->assertFalse($found_custom_menu_twice, "Assert that custom menu item wasn't duplicated");
296         }
297 }
298
299 class ViewLoadMenuTest extends SugarView
300 {
301     public function menuExists(
302         $module
303         )
304     {
305         return $this->_menuExists($module);
306     }
307 }