]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/ModuleInstall/ExtTest.php
Release 6.4.0
[Github/sugarcrm.git] / tests / ModuleInstall / ExtTest.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('ModuleInstall/ModuleInstaller.php');
39
40 class ExtTest extends Sugar_PHPUnit_Framework_TestCase
41 {
42     protected $module_installer;
43
44     public static function setUpBeforeClass()
45     {
46         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
47         $GLOBALS['current_user']->is_admin = "1";
48         $GLOBALS['current_language'] = "en_us";
49         $GLOBALS['app_strings'] = return_application_language($GLOBALS['current_language']);
50         $GLOBALS['mod_strings'] = return_module_language($GLOBALS['current_language'], 'Administration');
51         mkdir_recursive("cache/ExtTest");
52     }
53
54         public function setUp()
55         {
56         $this->module_installer = new ModuleInstaller();
57         $this->module_installer->silent = true;
58         $this->module_installer->base_dir = "cache/ExtTest";
59         $this->module_installer->id_name = 'ExtFrameworkTest';
60         $this->testvalue = uniqid("ext", true);
61         file_put_contents($this->module_installer->base_dir."/test.ext.php", "<?php \$testvalue = '$this->testvalue';");
62         }
63
64         public function tearDown()
65         {
66             if($this->module_installer) {
67                 $this->module_installer->uninstall_extensions();
68             }
69             if(file_exists($this->module_installer->base_dir."/test.ext.php")) {
70                 @unlink($this->module_installer->base_dir."/test.ext.php");
71             }
72             SugarCache::$isCacheReset = false;
73         }
74
75         public static function tearDownAfterClass()
76         {
77                 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
78         unset($GLOBALS['current_user']);
79         unset($GLOBALS['current_language']);
80         unset($GLOBALS['app_strings']);
81         unset($GLOBALS['mod_strings']);
82             if(file_exists("cache/ExtTest/test.ext.php")) {
83                 @unlink("cache/ExtTest/test.ext.php");
84             }
85         rmdir_recursive("cache/ExtTest");
86         }
87
88     public function getExt()
89     {
90         include 'ModuleInstall/extensions.php';
91         $params = array();
92         foreach($extensions as $name => $ext) {
93             if($name == 'modules') continue;
94             $params[] = array($name, $ext['section'], $ext['extdir'], $ext['file'], isset($ext['module'])?$ext['module']:'');
95         }
96         return $params;
97     }
98
99     /**
100      * @dataProvider getExt
101      * @param string $extname
102      * @param string $section
103      * @param string $dir
104      * @param string $file
105      * @param string $module
106      */
107     public function testExtFramework($extname, $section, $extdir, $file, $module = '')
108     {
109         if(empty($module)) {
110             $module = 'application';
111         }
112         $this->module_installer->installdefs[$section] = array(
113             array("from" => '<basepath>/test.ext.php', 'to_module' => $module)
114         );
115         $prefix = '';
116         $srcFileName = "test.ext.php";
117         if($extname == 'languages') {
118             $this->module_installer->installdefs[$section][0]['language'] = 'en_us';
119             $prefix = 'en_us.';
120             $file = 'lang.ext.php';
121             $srcFileName = "ExtFrameworkTest.php";
122         }
123             if($module == 'application') {
124             $srcfile = "custom/Extension/application/Ext/$extdir/{$prefix}{$srcFileName}";
125             $dstfile = "custom/application/Ext/$extdir/{$prefix}$file";
126         } else {
127             $srcfile = "custom/Extension/modules/$module/Ext/$extdir/{$prefix}{$srcFileName}";
128             $dstfile = "custom/modules/$module/Ext/$extdir/{$prefix}$file";
129         }
130         $this->module_installer->install_extensions();
131         // check file is there
132         $this->assertFileExists($srcfile);
133         $testvalue = null;
134         // check it works
135         include($dstfile);
136         $this->assertEquals($this->testvalue, $testvalue);
137         $testvalue = null;
138         // check disable
139         $this->module_installer->disable_extensions();
140         if(file_exists($dstfile)) include($dstfile);
141         $this->assertNull($testvalue);
142         // check enable
143         $this->module_installer->enable_extensions();
144         $this->assertFileExists($srcfile);
145         include($dstfile);
146         $this->assertEquals($this->testvalue, $testvalue);
147         $testvalue = null;
148         // check uninstall
149         $this->module_installer->uninstall_extensions();
150         if(file_exists($dstfile)) include($dstfile);
151         $this->assertNull($testvalue);
152     }
153
154     public function testExtModules()
155     {
156         $this->module_installer->installdefs['beans'] = array(
157             array(
158                 'module' => 'ExtFrameworkTest',
159                 'class' =>  'ExtFrameworkTest',
160                 'path' =>  'ExtFrameworkTest',
161                 'tab' => true
162             )
163         );
164         $srcfile = "custom/Extension/application/Ext/Include/ExtFrameworkTest.php";
165         $dstfile = "custom/application/Ext/Include/modules.ext.php";
166         $this->module_installer->install_extensions();
167         // check file is there
168         $this->assertFileExists($srcfile);
169         $beanList = null;
170         // check it works
171         include($dstfile);
172         $this->assertEquals('ExtFrameworkTest', $beanList['ExtFrameworkTest']);
173         // check disable
174         $this->module_installer->disable_extensions();
175         $beanList = array();
176         if(file_exists($dstfile)) include($dstfile);
177         $this->assertArrayNotHasKey('ExtFrameworkTest', $beanList);
178         // check enable
179         $beanList = array();
180         $this->module_installer->enable_extensions();
181         $this->assertFileExists($srcfile);
182         include($dstfile);
183         $this->assertEquals('ExtFrameworkTest', $beanList['ExtFrameworkTest']);
184         $beanList = array();
185         // check uninstall
186         $this->module_installer->uninstall_extensions();
187         if(file_exists($dstfile)) include($dstfile);
188         $this->assertArrayNotHasKey('ExtFrameworkTest', $beanList);
189     }
190 }