]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/helper/FileUtilsTest.php
Merge pull request #1597 from ArthurHoaro/feature/share-private-bookmark
[github/shaarli/Shaarli.git] / tests / helper / FileUtilsTest.php
1 <?php
2
3 namespace Shaarli\Helper;
4
5 use Exception;
6 use Shaarli\Exceptions\IOException;
7
8 /**
9 * Class FileUtilsTest
10 *
11 * Test file utility class.
12 */
13 class 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 }