aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/helper/FileUtilsTest.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-10-16 13:34:59 +0200
committerArthurHoaro <arthur@hoa.ro>2020-10-27 19:41:38 +0100
commitc2cd15dac2bfaebe6d32f7649fbdedc07400fa08 (patch)
treec450feedcf731a5fc74ea26375e6e4861b545f37 /tests/helper/FileUtilsTest.php
parent977db7eabc30cd9d84f22330a114cb9d904cb514 (diff)
downloadShaarli-c2cd15dac2bfaebe6d32f7649fbdedc07400fa08.tar.gz
Shaarli-c2cd15dac2bfaebe6d32f7649fbdedc07400fa08.tar.zst
Shaarli-c2cd15dac2bfaebe6d32f7649fbdedc07400fa08.zip
Move utils classes to Shaarli\Helper namespace and folder
Diffstat (limited to 'tests/helper/FileUtilsTest.php')
-rw-r--r--tests/helper/FileUtilsTest.php196
1 files changed, 196 insertions, 0 deletions
diff --git a/tests/helper/FileUtilsTest.php b/tests/helper/FileUtilsTest.php
new file mode 100644
index 00000000..948e46d1
--- /dev/null
+++ b/tests/helper/FileUtilsTest.php
@@ -0,0 +1,196 @@
1<?php
2
3namespace Shaarli\Helper;
4
5use Exception;
6use Shaarli\Exceptions\IOException;
7
8/**
9 * Class FileUtilsTest
10 *
11 * Test file utility class.
12 */
13class FileUtilsTest extends TestCase
14{
15 /**
16 * @var string Test file path.
17 */
18 protected static $file = 'sandbox/flat.db';
19
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
33 /**
34 * Delete test file after every test.
35 */
36 protected function tearDown(): void
37 {
38 @unlink(self::$file);
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');
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.
75 */
76 public function testWriteWithoutPermission()
77 {
78 $this->expectException(\Shaarli\Exceptions\IOException::class);
79 $this->expectExceptionMessage('Error accessing "sandbox/flat.db"');
80
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.
88 */
89 public function testWriteFolderDoesNotExist()
90 {
91 $this->expectException(\Shaarli\Exceptions\IOException::class);
92 $this->expectExceptionMessage('Error accessing "nopefolder"');
93
94 FileUtils::writeFlatDB('nopefolder/file', null);
95 }
96
97 /**
98 * Folder non writable: raise an exception.
99 */
100 public function testWriteFolderPermission()
101 {
102 $this->expectException(\Shaarli\Exceptions\IOException::class);
103 $this->expectExceptionMessage('Error accessing "sandbox"');
104
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 }
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 }
196}