]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/utils/Bug46850Test.php
Release 6.5.6
[Github/sugarcrm.git] / tests / include / utils / Bug46850Test.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/utils/LogicHook.php');
39
40 class Bug46850Test extends Sugar_PHPUnit_Framework_TestCase
41 {
42     protected $renames = array();
43     protected $deletes = array();
44
45     protected $hook = array(
46         'test_logic_hook' => array(array(1, 'test_logic_hook', __FILE__, 'LogicHookTest', 'testLogicHook')),
47     );
48
49     public function setUp()
50     {
51         LogicHookTest::$called = false;
52         unset($GLOBALS['logic_hook']);
53         $GLOBALS['logic_hook'] = LogicHook::initialize();
54         LogicHook::refreshHooks();
55     }
56
57     public function tearDown()
58     {
59         foreach($this->renames as $file) {
60             rename($file.".bak", $file);
61         }
62         foreach($this->deletes as $file) {
63             unlink($file);
64         }
65         unset($GLOBALS['logic_hook']);
66         LogicHook::refreshHooks();
67     }
68
69     protected function saveHook($file)
70     {
71         if(file_exists($file)) {
72             rename($file, $file.".bak");
73             $this->renames[] = $file;
74         } else {
75             $this->deletes[] = $file;
76         }
77     }
78
79     public function getModules()
80     {
81         return array(
82             array(''),
83             array('Contacts'),
84             array('Accounts'),
85         );
86     }
87
88     /**
89      * @dataProvider getModules
90      */
91     public function testHooksDirect($module)
92     {
93         $dir = rtrim("custom/modules/$module", "/");
94         $file = "$dir/logic_hooks.php";
95         $this->saveHook($file);
96         if(!is_dir($dir)) {
97             mkdir($dir, 0755, true);
98         }
99         write_array_to_file('hook_array', $this->hook, $file);
100         $GLOBALS['logic_hook']->getHooks($module, true); // manually refresh
101         $GLOBALS['logic_hook']->call_custom_logic($module, 'test_logic_hook');
102         $this->assertTrue(LogicHookTest::$called);
103     }
104
105     /**
106      * @dataProvider getModules
107      */
108     public function testHooksExtDirect($module)
109     {
110         if(empty($module)) {
111             $dir = "custom/application/Ext/LogicHooks";
112         } else {
113             $dir = "custom/modules/$module/Ext/LogicHooks";
114         }
115         if(!is_dir($dir)) {
116             mkdir($dir, 0755, true);
117         }
118         $file = "$dir/logichooks.ext.php";
119         $this->saveHook($file);
120         write_array_to_file('hook_array', $this->hook, $file);
121         $GLOBALS['logic_hook']->getHooks($module, true); // manually refresh
122         $GLOBALS['logic_hook']->call_custom_logic($module, 'test_logic_hook');
123         $this->assertTrue(LogicHookTest::$called);
124     }
125
126     /**
127      * @dataProvider getModules
128      */
129     public function testHooksUtils($module)
130     {
131         $dir = rtrim("custom/modules/$module", "/");
132         $file = "$dir/logic_hooks.php";
133         if(!is_dir($dir)) {
134             mkdir($dir, 0755, true);
135         }
136         $this->saveHook($file);
137         check_logic_hook_file($module, 'test_logic_hook', $this->hook['test_logic_hook'][0]);
138         $GLOBALS['logic_hook']->getHooks($module, true); // manually refresh
139         $GLOBALS['logic_hook']->call_custom_logic($module, 'test_logic_hook');
140         $this->assertTrue(LogicHookTest::$called);
141     }
142
143
144     /**
145      * @dataProvider getModules
146      */
147     public function testGeHookArray($module)
148     {
149         $dir = rtrim("custom/modules/$module", "/");
150         $file = "$dir/logic_hooks.php";
151         if(!is_dir($dir)) {
152             mkdir($dir, 0755, true);
153         }
154         $this->saveHook($file);
155         check_logic_hook_file($module, 'test_logic_hook', $this->hook['test_logic_hook'][0]);
156         $array = get_hook_array($module);
157         $this->assertEquals($this->hook, $array);
158     }
159 }
160
161 class LogicHookTest {
162     public static $called = false;
163     function testLogicHook() {
164         self::$called = true;
165     }
166 }