]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/MVC/View/LoadMenuTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / include / MVC / View / LoadMenuTest.php
1 <?php 
2 require_once('include/MVC/View/SugarView.php');
3
4 class LoadMenuTest extends Sugar_PHPUnit_Framework_TestCase
5 {   
6     protected $_moduleName;
7     
8     public function setUp() 
9         {
10                 global $mod_strings, $app_strings;
11                 $mod_strings = return_module_language($GLOBALS['current_language'], 'Accounts');
12                 $app_strings = return_application_language($GLOBALS['current_language']);       
13                 
14                 // create a dummy module directory
15                 $this->_moduleName = 'TestModule'.mt_rand();
16                 
17         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
18         
19         sugar_mkdir("modules/{$this->_moduleName}",null,true);
20         }
21         
22         public function tearDown() 
23         {
24                 unset($GLOBALS['mod_strings']);
25                 unset($GLOBALS['app_strings']);
26         
27                 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
28                 unset($GLOBALS['current_user']);
29         
30                 if ( is_dir("modules/{$this->_moduleName}") )
31                     rmdir_recursive("modules/{$this->_moduleName}");
32                 if ( is_dir("custom/modules/{$this->_moduleName}") )
33                     rmdir_recursive("custom/modules/{$this->_moduleName}");
34         }
35         
36         public function testMenuDoesNotExists()
37         {
38         $view = new SugarView;
39         $module_menu = $view->getMenu($this->_moduleName);
40         $this->assertTrue(empty($module_menu),'Assert the module menu array is empty');
41         }
42         
43         public function testMenuExistsCanFindModuleMenu()
44         {
45             // Create module menu
46         if( $fh = @fopen("modules/{$this->_moduleName}/Menu.php", 'w+') ) {
47                 $string = <<<EOQ
48 <?php
49 \$module_menu[]=Array("index.php?module=Import&action=bar&import_module=Accounts&return_module=Accounts&return_action=index","Foo","Foo", 'Accounts');
50 ?>
51 EOQ;
52             fputs( $fh, $string);
53             fclose( $fh );
54         }
55         
56         $view = new SugarView;
57         $module_menu = $view->getMenu($this->_moduleName);
58         $found_custom_menu = false;
59         foreach ($module_menu as $menu_entry) {
60                 foreach ($menu_entry as $menu_item) {
61                         if (preg_match('/action=bar/', $menu_item)) {
62                            $found_custom_menu = true;
63                         }
64                 }
65         }
66         $this->assertTrue($found_custom_menu, "Assert that menu was detected");
67         }
68
69     /**
70      * @group bug29114
71      */
72     public function testMenuExistsCanFindModuleExtMenu()
73     {
74         // Create module ext menu
75         sugar_mkdir("custom/modules/{$this->_moduleName}/Ext/Menus/",null,true);
76         if( $fh = @fopen("custom/modules/{$this->_moduleName}/Ext/Menus/menu.ext.php", 'w+') ) {
77                 $string = <<<EOQ
78 <?php
79 \$module_menu[]=Array("index.php?module=Import&action=foo&import_module=Accounts&return_module=Accounts&return_action=index","Foo","Foo", 'Accounts');
80 ?>
81 EOQ;
82             fputs( $fh, $string);
83             fclose( $fh );
84         }
85         
86         $view = new SugarView;
87         $module_menu = $view->getMenu($this->_moduleName);
88         $found_custom_menu = false;
89         foreach ($module_menu as $key => $menu_entry) {
90                 foreach ($menu_entry as $id => $menu_item) {
91                         if (preg_match('/action=foo/', $menu_item)) {
92                            $found_custom_menu = true;
93                         }
94                 }
95         }
96         $this->assertTrue($found_custom_menu, "Assert that custom menu was detected");
97     }
98
99     /**
100      * @group bug38935
101      */
102     public function testMenuExistsCanFindModuleExtMenuWhenModuleMenuDefinedGlobal()
103     {
104         // Create module ext menu
105         sugar_mkdir("custom/modules/{$this->_moduleName}/Ext/Menus/",null,true);
106         if( $fh = @fopen("custom/modules/{$this->_moduleName}/Ext/Menus/menu.ext.php", 'w+') ) {
107                 $string = <<<EOQ
108 <?php
109 global \$module_menu;
110 \$module_menu[]=Array("index.php?module=Import&action=foo&import_module=Accounts&return_module=Accounts&return_action=index","Foo","Foo", 'Accounts');
111 ?>
112 EOQ;
113             fputs( $fh, $string);
114             fclose( $fh );
115         }
116         
117         $view = new SugarView;
118         $module_menu = $view->getMenu($this->_moduleName);
119         $found_custom_menu = false;
120         foreach ($module_menu as $key => $menu_entry) {
121                 foreach ($menu_entry as $id => $menu_item) {
122                         if (preg_match('/action=foo/', $menu_item)) {
123                            $found_custom_menu = true;
124                         }
125                 }
126         }
127         $this->assertTrue($found_custom_menu, "Assert that custom menu was detected");
128     }    
129     
130     public function testMenuExistsCanFindApplicationExtMenu()
131         {
132             // Create module ext menu
133             $backupCustomMenu = false;
134             if ( !is_dir("custom/application/Ext/Menus/") )
135                 sugar_mkdir("custom/application/Ext/Menus/",null,true);
136         if (file_exists('custom/application/Ext/Menus/menu.ext.php')) {
137                 copy('custom/application/Ext/Menus/menu.ext.php', 'custom/application/Ext/Menus/menu.ext.php.backup');
138                 $backupCustomMenu = true;
139             }
140             
141         if ( $fh = @fopen("custom/application/Ext/Menus/menu.ext.php", 'w+') ) {
142                 $string = <<<EOQ
143 <?php
144 \$module_menu[]=Array("index.php?module=Import&action=foobar&import_module=Accounts&return_module=Accounts&return_action=index","Foo","Foo", 'Accounts');
145 ?>
146 EOQ;
147             fputs( $fh, $string);
148             fclose( $fh );
149         }
150         
151         $view = new SugarView;
152         $module_menu = $view->getMenu($this->_moduleName);
153         $found_application_custom_menu = false;
154         foreach ($module_menu as $key => $menu_entry) {
155                 foreach ($menu_entry as $id => $menu_item) {
156                         if (preg_match('/action=foobar/', $menu_item)) {
157                            $found_application_custom_menu = true;
158                         }
159                 }
160         }
161         $this->assertTrue($found_application_custom_menu, "Assert that application custom menu was detected");
162         
163         if($backupCustomMenu) {
164             copy('custom/application/Ext/Menus/menu.ext.php.backup', 'custom/application/Ext/Menus/menu.ext.php');
165             unlink('custom/application/Ext/Menus/menu.ext.php.backup');
166         }       
167         else
168             unlink('custom/application/Ext/Menus/menu.ext.php');
169         }
170
171         public function testMenuExistsCanFindModuleMenuAndModuleExtMenu()
172         {
173             // Create module menu
174         if( $fh = @fopen("modules/{$this->_moduleName}/Menu.php", 'w+') ) {
175                 $string = <<<EOQ
176 <?php
177 \$module_menu[]=Array("index.php?module=Import&action=foo&import_module=Accounts&return_module=Accounts&return_action=index","Foo","Foo", 'Accounts');
178 ?>
179 EOQ;
180             fputs( $fh, $string);
181             fclose( $fh );
182         }
183         
184         // Create module ext menu
185         sugar_mkdir("custom/modules/{$this->_moduleName}/Ext/Menus/",null,true);
186         if( $fh = @fopen("custom/modules/{$this->_moduleName}/Ext/Menus/menu.ext.php", 'w+') ) {
187                 $string = <<<EOQ
188 <?php
189 \$module_menu[]=Array("index.php?module=Import&action=bar&import_module=Accounts&return_module=Accounts&return_action=index","Foo","Foo", 'Accounts');
190 ?>
191 EOQ;
192             fputs( $fh, $string);
193             fclose( $fh );
194         }
195         
196         $view = new SugarView;
197         $module_menu = $view->getMenu($this->_moduleName);
198         $found_custom_menu = false;
199         $found_menu = false;
200         foreach ($module_menu as $key => $menu_entry) {
201                 foreach ($menu_entry as $id => $menu_item) {
202                         if (preg_match('/action=foo/', $menu_item)) {
203                            $found_menu = true;
204                         }
205                         if (preg_match('/action=bar/', $menu_item)) {
206                            $found_custom_menu = true;
207                         }
208                 }
209         }
210         $this->assertTrue($found_menu, "Assert that menu was detected");
211         $this->assertTrue($found_custom_menu, "Assert that custom menu was detected");
212         }
213 }
214
215 class ViewLoadMenuTest extends SugarView
216 {
217     public function menuExists(
218         $module
219         )
220     {
221         return $this->_menuExists($module);
222     }
223 }