]>
Commit | Line | Data |
---|---|---|
b2306b0c A |
1 | <?php |
2 | ||
a0c4dbd9 | 3 | namespace Shaarli; |
f3d2f257 | 4 | |
a0c4dbd9 | 5 | use Exception; |
0cf76ccb | 6 | use Shaarli\Exceptions\IOException; |
b2306b0c A |
7 | |
8 | /** | |
9 | * Class FileUtilsTest | |
10 | * | |
11 | * Test file utility class. | |
12 | */ | |
0cf76ccb | 13 | class FileUtilsTest extends TestCase |
b2306b0c A |
14 | { |
15 | /** | |
16 | * @var string Test file path. | |
17 | */ | |
18 | protected static $file = 'sandbox/flat.db'; | |
19 | ||
0cf76ccb A |
20 | protected function setUp(): void |
21 | { | |
22 | @mkdir('sandbox'); | |
23 | mkdir('sandbox/folder2'); | |
24 | touch('sandbox/file1'); | |
25 | touch('sandbox/file2'); | |
26 | mkdir('sandbox/folder1'); | |
27 | touch('sandbox/folder1/file1'); | |
28 | touch('sandbox/folder1/file2'); | |
29 | mkdir('sandbox/folder3'); | |
30 | mkdir('/tmp/shaarli-to-delete'); | |
31 | } | |
32 | ||
b2306b0c A |
33 | /** |
34 | * Delete test file after every test. | |
35 | */ | |
8f60e120 | 36 | protected function tearDown(): void |
b2306b0c A |
37 | { |
38 | @unlink(self::$file); | |
0cf76ccb A |
39 | |
40 | @unlink('sandbox/folder1/file1'); | |
41 | @unlink('sandbox/folder1/file2'); | |
42 | @rmdir('sandbox/folder1'); | |
43 | @unlink('sandbox/file1'); | |
44 | @unlink('sandbox/file2'); | |
45 | @rmdir('sandbox/folder2'); | |
46 | @rmdir('sandbox/folder3'); | |
47 | @rmdir('/tmp/shaarli-to-delete'); | |
b2306b0c A |
48 | } |
49 | ||
50 | /** | |
51 | * Test writeDB, then readDB with different data. | |
52 | */ | |
53 | public function testSimpleWriteRead() | |
54 | { | |
55 | $data = ['blue', 'red']; | |
56 | $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0); | |
57 | $this->assertTrue(startsWith(file_get_contents(self::$file), '<?php /*')); | |
58 | $this->assertEquals($data, FileUtils::readFlatDB(self::$file)); | |
59 | ||
60 | $data = 0; | |
61 | $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0); | |
62 | $this->assertEquals($data, FileUtils::readFlatDB(self::$file)); | |
63 | ||
64 | $data = null; | |
65 | $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0); | |
66 | $this->assertEquals($data, FileUtils::readFlatDB(self::$file)); | |
67 | ||
68 | $data = false; | |
69 | $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0); | |
70 | $this->assertEquals($data, FileUtils::readFlatDB(self::$file)); | |
71 | } | |
72 | ||
73 | /** | |
74 | * File not writable: raise an exception. | |
b2306b0c A |
75 | */ |
76 | public function testWriteWithoutPermission() | |
77 | { | |
b1baca99 A |
78 | $this->expectException(\Shaarli\Exceptions\IOException::class); |
79 | $this->expectExceptionMessage('Error accessing "sandbox/flat.db"'); | |
80 | ||
b2306b0c A |
81 | touch(self::$file); |
82 | chmod(self::$file, 0440); | |
83 | FileUtils::writeFlatDB(self::$file, null); | |
84 | } | |
85 | ||
86 | /** | |
87 | * Folder non existent: raise an exception. | |
b2306b0c A |
88 | */ |
89 | public function testWriteFolderDoesNotExist() | |
90 | { | |
b1baca99 A |
91 | $this->expectException(\Shaarli\Exceptions\IOException::class); |
92 | $this->expectExceptionMessage('Error accessing "nopefolder"'); | |
93 | ||
b2306b0c A |
94 | FileUtils::writeFlatDB('nopefolder/file', null); |
95 | } | |
96 | ||
97 | /** | |
98 | * Folder non writable: raise an exception. | |
b2306b0c A |
99 | */ |
100 | public function testWriteFolderPermission() | |
101 | { | |
b1baca99 A |
102 | $this->expectException(\Shaarli\Exceptions\IOException::class); |
103 | $this->expectExceptionMessage('Error accessing "sandbox"'); | |
104 | ||
b2306b0c A |
105 | chmod(dirname(self::$file), 0555); |
106 | try { | |
107 | FileUtils::writeFlatDB(self::$file, null); | |
108 | } catch (Exception $e) { | |
109 | chmod(dirname(self::$file), 0755); | |
110 | throw $e; | |
111 | } | |
112 | } | |
113 | ||
114 | /** | |
115 | * Read non existent file, use default parameter. | |
116 | */ | |
117 | public function testReadNotExistentFile() | |
118 | { | |
119 | $this->assertEquals(null, FileUtils::readFlatDB(self::$file)); | |
120 | $this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test'])); | |
121 | } | |
122 | ||
123 | /** | |
124 | * Read non readable file, use default parameter. | |
125 | */ | |
126 | public function testReadNotReadable() | |
127 | { | |
128 | touch(self::$file); | |
129 | chmod(self::$file, 0220); | |
130 | $this->assertEquals(null, FileUtils::readFlatDB(self::$file)); | |
131 | $this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test'])); | |
132 | } | |
0cf76ccb A |
133 | |
134 | /** | |
135 | * Test clearFolder with self delete and excluded files | |
136 | */ | |
137 | public function testClearFolderSelfDeleteWithExclusion(): void | |
138 | { | |
139 | FileUtils::clearFolder('sandbox', true, ['file2']); | |
140 | ||
141 | static::assertFileExists('sandbox/folder1/file2'); | |
142 | static::assertFileExists('sandbox/folder1'); | |
143 | static::assertFileExists('sandbox/file2'); | |
144 | static::assertFileExists('sandbox'); | |
145 | ||
146 | static::assertFileNotExists('sandbox/folder1/file1'); | |
147 | static::assertFileNotExists('sandbox/file1'); | |
148 | static::assertFileNotExists('sandbox/folder3'); | |
149 | } | |
150 | ||
151 | /** | |
152 | * Test clearFolder with self delete and excluded files | |
153 | */ | |
154 | public function testClearFolderSelfDeleteWithoutExclusion(): void | |
155 | { | |
156 | FileUtils::clearFolder('sandbox', true); | |
157 | ||
158 | static::assertFileNotExists('sandbox'); | |
159 | } | |
160 | ||
161 | /** | |
162 | * Test clearFolder with self delete and excluded files | |
163 | */ | |
164 | public function testClearFolderNoSelfDeleteWithoutExclusion(): void | |
165 | { | |
166 | FileUtils::clearFolder('sandbox', false); | |
167 | ||
168 | static::assertFileExists('sandbox'); | |
169 | ||
170 | // 2 because '.' and '..' | |
171 | static::assertCount(2, new \DirectoryIterator('sandbox')); | |
172 | } | |
173 | ||
174 | /** | |
175 | * Test clearFolder on a file instead of a folder | |
176 | */ | |
177 | public function testClearFolderOnANonDirectory(): void | |
178 | { | |
179 | $this->expectException(IOException::class); | |
180 | $this->expectExceptionMessage('Provided path is not a directory.'); | |
181 | ||
182 | FileUtils::clearFolder('sandbox/file1', false); | |
183 | } | |
184 | ||
185 | /** | |
186 | * Test clearFolder on a file instead of a folder | |
187 | */ | |
188 | public function testClearFolderOutsideOfShaarliDirectory(): void | |
189 | { | |
190 | $this->expectException(IOException::class); | |
191 | $this->expectExceptionMessage('Trying to delete a folder outside of Shaarli path.'); | |
192 | ||
193 | ||
194 | FileUtils::clearFolder('/tmp/shaarli-to-delete', true); | |
195 | } | |
b2306b0c | 196 | } |