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