]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/utils/SugarFileUtilsTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / include / utils / SugarFileUtilsTest.php
1 <?php
2 require_once 'include/utils/file_utils.php';
3
4 class SugarFileUtilsTest extends Sugar_PHPUnit_Framework_TestCase 
5 {
6     private $_filename;
7     private $_old_default_permissions;
8     
9     public function setUp() 
10     {   
11         if (is_windows())
12             $this->markTestSkipped('Skipping on Windows');
13         
14         $this->_filename = realpath(dirname(__FILE__).'/../../../cache/').'file_utils_override'.mt_rand().'.txt';
15         touch($this->_filename);
16         $this->_old_default_permissions = $GLOBALS['sugar_config']['default_permissions'];
17         $GLOBALS['sugar_config']['default_permissions'] =
18             array (
19                 'dir_mode' => 0777,
20                 'file_mode' => 0660,
21                 'user' => $this->_getCurrentUser(),
22                 'group' => $this->_getCurrentGroup(),
23               );
24     }
25     
26     public function tearDown() 
27     {
28         if(file_exists($this->_filename)) {
29             unlink($this->_filename);
30         }
31         $GLOBALS['sugar_config']['default_permissions'] = $this->_old_default_permissions;
32         SugarConfig::getInstance()->clearCache();
33     }
34     
35     private function _getCurrentUser()
36     {
37         if ( function_exists('posix_getuid') ) {
38             return posix_getuid();
39         }
40         return '';
41     }
42     
43     private function _getCurrentGroup()
44     {
45         if ( function_exists('posix_getgid') ) {
46             return posix_getgid();
47         }
48         return '';
49     }
50     
51     private function _getTestFilePermissions()
52     {
53         return substr(sprintf('%o', fileperms($this->_filename)), -4);
54     }
55     
56     public function testSugarTouch()
57     {
58         $this->assertTrue(sugar_touch($this->_filename));
59     }
60     
61     public function testSugarTouchWithTime()
62     {
63         $time = filemtime($this->_filename);
64         
65         $this->assertTrue(sugar_touch($this->_filename, $time));
66         
67         $this->assertEquals($time,filemtime($this->_filename));
68     }
69     
70     public function testSugarTouchWithAccessTime()
71     {
72         $time  = filemtime($this->_filename);
73         $atime = gmmktime();
74         
75         $this->assertTrue(sugar_touch($this->_filename, $time, $atime));
76         
77         $this->assertEquals($time,filemtime($this->_filename));
78         $this->assertEquals($atime,fileatime($this->_filename));
79     }
80     
81     public function testSugarChmod()
82     {
83         return true;
84         $this->assertTrue(sugar_chmod($this->_filename));
85         $this->assertEquals($this->_getTestFilePermissions(),decoct(get_mode()));
86     }
87     
88     public function testSugarChmodWithMode()
89     {
90         $mode = 0411;
91         $this->assertTrue(sugar_chmod($this->_filename, $mode));
92         
93         $this->assertEquals($this->_getTestFilePermissions(),decoct($mode));
94     }
95     
96     public function testSugarChmodNoDefaultMode()
97     {
98         $GLOBALS['sugar_config']['default_permissions']['file_mode'] = null;
99         $this->assertFalse(sugar_chmod($this->_filename));
100     }
101     
102     public function testSugarChmodDefaultModeNotAnInteger()
103     {
104         $GLOBALS['sugar_config']['default_permissions']['file_mode'] = '';
105         $this->assertFalse(sugar_chmod($this->_filename));
106     }
107     
108     public function testSugarChmodDefaultModeIsZero()
109     {
110         $GLOBALS['sugar_config']['default_permissions']['file_mode'] = 0;
111         $this->assertFalse(sugar_chmod($this->_filename));
112     }
113     
114     public function testSugarChmodWithModeNoDefaultMode()
115     {
116         $GLOBALS['sugar_config']['default_permissions']['file_mode'] = null;
117         $mode = 0411;
118         $this->assertTrue(sugar_chmod($this->_filename, $mode));
119         
120         $this->assertEquals($this->_getTestFilePermissions(),decoct($mode));
121     }
122     
123     public function testSugarChmodWithModeDefaultModeNotAnInteger()
124     {
125         $GLOBALS['sugar_config']['default_permissions']['file_mode'] = '';
126         $mode = 0411;
127         $this->assertTrue(sugar_chmod($this->_filename, $mode));
128         
129         $this->assertEquals($this->_getTestFilePermissions(),decoct($mode));
130     }
131     
132     public function testSugarChown()
133     {
134         $this->assertTrue(sugar_chown($this->_filename));
135         $this->assertEquals(fileowner($this->_filename),$this->_getCurrentUser());
136     }
137     
138     public function testSugarChownWithUser()
139     {
140         $this->assertTrue(sugar_chown($this->_filename,$this->_getCurrentUser()));
141         $this->assertEquals(fileowner($this->_filename),$this->_getCurrentUser());
142     }
143     
144     public function testSugarChownNoDefaultUser()
145     {
146         $GLOBALS['sugar_config']['default_permissions']['user'] = '';
147         
148         $this->assertFalse(sugar_chown($this->_filename));
149     }
150     
151     public function testSugarChownWithUserNoDefaultUser()
152     {
153         $GLOBALS['sugar_config']['default_permissions']['user'] = '';
154         
155         $this->assertTrue(sugar_chown($this->_filename,$this->_getCurrentUser()));
156         
157         $this->assertEquals(fileowner($this->_filename),$this->_getCurrentUser());
158     }
159 }
160 ?>