]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/utils/SugarFileUtilsTest.php
Release 6.2.0
[Github/sugarcrm.git] / tests / include / utils / SugarFileUtilsTest.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/utils/file_utils.php';
39
40 class SugarFileUtilsTest extends Sugar_PHPUnit_Framework_TestCase 
41 {
42     private $_filename;
43     private $_old_default_permissions;
44     
45     public function setUp() 
46     {   
47         if (is_windows())
48             $this->markTestSkipped('Skipping on Windows');
49         
50         $this->_filename = realpath(dirname(__FILE__).'/../../../cache/').'file_utils_override'.mt_rand().'.txt';
51         touch($this->_filename);
52         $this->_old_default_permissions = $GLOBALS['sugar_config']['default_permissions'];
53         $GLOBALS['sugar_config']['default_permissions'] =
54             array (
55                 'dir_mode' => 0777,
56                 'file_mode' => 0660,
57                 'user' => $this->_getCurrentUser(),
58                 'group' => $this->_getCurrentGroup(),
59               );
60     }
61     
62     public function tearDown() 
63     {
64         if(file_exists($this->_filename)) {
65             unlink($this->_filename);
66         }
67         $GLOBALS['sugar_config']['default_permissions'] = $this->_old_default_permissions;
68         SugarConfig::getInstance()->clearCache();
69     }
70     
71     private function _getCurrentUser()
72     {
73         if ( function_exists('posix_getuid') ) {
74             return posix_getuid();
75         }
76         return '';
77     }
78     
79     private function _getCurrentGroup()
80     {
81         if ( function_exists('posix_getgid') ) {
82             return posix_getgid();
83         }
84         return '';
85     }
86     
87     private function _getTestFilePermissions()
88     {
89         return substr(sprintf('%o', fileperms($this->_filename)), -4);
90     }
91     
92     public function testSugarTouch()
93     {
94         $this->assertTrue(sugar_touch($this->_filename));
95     }
96     
97     public function testSugarTouchWithTime()
98     {
99         $time = filemtime($this->_filename);
100         
101         $this->assertTrue(sugar_touch($this->_filename, $time));
102         
103         $this->assertEquals($time,filemtime($this->_filename));
104     }
105     
106     public function testSugarTouchWithAccessTime()
107     {
108         $time  = filemtime($this->_filename);
109         $atime = gmmktime();
110         
111         $this->assertTrue(sugar_touch($this->_filename, $time, $atime));
112         
113         $this->assertEquals($time,filemtime($this->_filename));
114         $this->assertEquals($atime,fileatime($this->_filename));
115     }
116     
117     public function testSugarChmod()
118     {
119         return true;
120         $this->assertTrue(sugar_chmod($this->_filename));
121         $this->assertEquals($this->_getTestFilePermissions(),decoct(get_mode()));
122     }
123     
124     public function testSugarChmodWithMode()
125     {
126         $mode = 0411;
127         $this->assertTrue(sugar_chmod($this->_filename, $mode));
128         
129         $this->assertEquals($this->_getTestFilePermissions(),decoct($mode));
130     }
131     
132     public function testSugarChmodNoDefaultMode()
133     {
134         $GLOBALS['sugar_config']['default_permissions']['file_mode'] = null;
135         $this->assertFalse(sugar_chmod($this->_filename));
136     }
137     
138     public function testSugarChmodDefaultModeNotAnInteger()
139     {
140         $GLOBALS['sugar_config']['default_permissions']['file_mode'] = '';
141         $this->assertFalse(sugar_chmod($this->_filename));
142     }
143     
144     public function testSugarChmodDefaultModeIsZero()
145     {
146         $GLOBALS['sugar_config']['default_permissions']['file_mode'] = 0;
147         $this->assertFalse(sugar_chmod($this->_filename));
148     }
149     
150     public function testSugarChmodWithModeNoDefaultMode()
151     {
152         $GLOBALS['sugar_config']['default_permissions']['file_mode'] = null;
153         $mode = 0411;
154         $this->assertTrue(sugar_chmod($this->_filename, $mode));
155         
156         $this->assertEquals($this->_getTestFilePermissions(),decoct($mode));
157     }
158     
159     public function testSugarChmodWithModeDefaultModeNotAnInteger()
160     {
161         $GLOBALS['sugar_config']['default_permissions']['file_mode'] = '';
162         $mode = 0411;
163         $this->assertTrue(sugar_chmod($this->_filename, $mode));
164         
165         $this->assertEquals($this->_getTestFilePermissions(),decoct($mode));
166     }
167     
168     public function testSugarChown()
169     {
170         $this->assertTrue(sugar_chown($this->_filename));
171         $this->assertEquals(fileowner($this->_filename),$this->_getCurrentUser());
172     }
173     
174     public function testSugarChownWithUser()
175     {
176         $this->assertTrue(sugar_chown($this->_filename,$this->_getCurrentUser()));
177         $this->assertEquals(fileowner($this->_filename),$this->_getCurrentUser());
178     }
179     
180     public function testSugarChownNoDefaultUser()
181     {
182         $GLOBALS['sugar_config']['default_permissions']['user'] = '';
183         
184         $this->assertFalse(sugar_chown($this->_filename));
185     }
186     
187     public function testSugarChownWithUserNoDefaultUser()
188     {
189         $GLOBALS['sugar_config']['default_permissions']['user'] = '';
190         
191         $this->assertTrue(sugar_chown($this->_filename,$this->_getCurrentUser()));
192         
193         $this->assertEquals(fileowner($this->_filename),$this->_getCurrentUser());
194     }
195 }
196 ?>