2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
38 require_once 'include/utils/file_utils.php';
40 class SugarFileUtilsTest extends Sugar_PHPUnit_Framework_TestCase
43 private $_old_default_permissions;
45 public function setUp()
48 $this->markTestSkipped('Skipping on Windows');
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'] =
57 'user' => $this->_getCurrentUser(),
58 'group' => $this->_getCurrentGroup(),
62 public function tearDown()
64 if(file_exists($this->_filename)) {
65 unlink($this->_filename);
67 $GLOBALS['sugar_config']['default_permissions'] = $this->_old_default_permissions;
68 SugarConfig::getInstance()->clearCache();
71 private function _getCurrentUser()
73 if ( function_exists('posix_getuid') ) {
74 return posix_getuid();
79 private function _getCurrentGroup()
81 if ( function_exists('posix_getgid') ) {
82 return posix_getgid();
87 private function _getTestFilePermissions()
89 return substr(sprintf('%o', fileperms($this->_filename)), -4);
92 public function testSugarTouch()
94 $this->assertTrue(sugar_touch($this->_filename));
97 public function testSugarTouchWithTime()
99 $time = filemtime($this->_filename);
101 $this->assertTrue(sugar_touch($this->_filename, $time));
103 $this->assertEquals($time,filemtime($this->_filename));
106 public function testSugarTouchWithAccessTime()
108 $time = filemtime($this->_filename);
111 $this->assertTrue(sugar_touch($this->_filename, $time, $atime));
113 $this->assertEquals($time,filemtime($this->_filename));
114 $this->assertEquals($atime,fileatime($this->_filename));
117 public function testSugarChmod()
120 $this->assertTrue(sugar_chmod($this->_filename));
121 $this->assertEquals($this->_getTestFilePermissions(),decoct(get_mode()));
124 public function testSugarChmodWithMode()
127 $this->assertTrue(sugar_chmod($this->_filename, $mode));
129 $this->assertEquals($this->_getTestFilePermissions(),decoct($mode));
132 public function testSugarChmodNoDefaultMode()
134 $GLOBALS['sugar_config']['default_permissions']['file_mode'] = null;
135 $this->assertFalse(sugar_chmod($this->_filename));
138 public function testSugarChmodDefaultModeNotAnInteger()
140 $GLOBALS['sugar_config']['default_permissions']['file_mode'] = '';
141 $this->assertFalse(sugar_chmod($this->_filename));
144 public function testSugarChmodDefaultModeIsZero()
146 $GLOBALS['sugar_config']['default_permissions']['file_mode'] = 0;
147 $this->assertFalse(sugar_chmod($this->_filename));
150 public function testSugarChmodWithModeNoDefaultMode()
152 $GLOBALS['sugar_config']['default_permissions']['file_mode'] = null;
154 $this->assertTrue(sugar_chmod($this->_filename, $mode));
156 $this->assertEquals($this->_getTestFilePermissions(),decoct($mode));
159 public function testSugarChmodWithModeDefaultModeNotAnInteger()
161 $GLOBALS['sugar_config']['default_permissions']['file_mode'] = '';
163 $this->assertTrue(sugar_chmod($this->_filename, $mode));
165 $this->assertEquals($this->_getTestFilePermissions(),decoct($mode));
168 public function testSugarChown()
170 if ($GLOBALS['sugar_config']['default_permissions']['user'] == '')
172 $this->markTestSkipped('Can not get UID. Posix extension is required.');
174 $this->assertTrue(sugar_chown($this->_filename));
175 $this->assertEquals(fileowner($this->_filename),$this->_getCurrentUser());
178 public function testSugarChownWithUser()
180 if ($this->_getCurrentUser() == '')
182 $this->markTestSkipped('Can not get UID. Posix extension is required.');
184 $this->assertTrue(sugar_chown($this->_filename,$this->_getCurrentUser()));
185 $this->assertEquals(fileowner($this->_filename),$this->_getCurrentUser());
188 public function testSugarChownNoDefaultUser()
190 $GLOBALS['sugar_config']['default_permissions']['user'] = '';
192 $this->assertFalse(sugar_chown($this->_filename));
195 public function testSugarChownWithUserNoDefaultUser()
197 if ($this->_getCurrentUser() == '')
199 $this->markTestSkipped('Can not get UID. Posix extension is required.');
202 $GLOBALS['sugar_config']['default_permissions']['user'] = '';
204 $this->assertTrue(sugar_chown($this->_filename,$this->_getCurrentUser()));
206 $this->assertEquals(fileowner($this->_filename),$this->_getCurrentUser());