6 use Shaarli\Exceptions\IOException
;
11 * Test file utility class.
13 class FileUtilsTest
extends TestCase
16 * @var string Test file path.
18 protected static $file = 'sandbox/flat.db';
20 protected function setUp(): void
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');
34 * Delete test file after every test.
36 protected function tearDown(): void
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');
51 * Test writeDB, then readDB with different data.
53 public function testSimpleWriteRead()
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));
61 $this->assertTrue(FileUtils
::writeFlatDB(self
::$file, $data) > 0);
62 $this->assertEquals($data, FileUtils
::readFlatDB(self
::$file));
65 $this->assertTrue(FileUtils
::writeFlatDB(self
::$file, $data) > 0);
66 $this->assertEquals($data, FileUtils
::readFlatDB(self
::$file));
69 $this->assertTrue(FileUtils
::writeFlatDB(self
::$file, $data) > 0);
70 $this->assertEquals($data, FileUtils
::readFlatDB(self
::$file));
74 * File not writable: raise an exception.
76 public function testWriteWithoutPermission()
78 $this->expectException(\Shaarli\Exceptions\IOException
::class);
79 $this->expectExceptionMessage('Error accessing "sandbox/flat.db"');
82 chmod(self
::$file, 0440);
83 FileUtils
::writeFlatDB(self
::$file, null);
87 * Folder non existent: raise an exception.
89 public function testWriteFolderDoesNotExist()
91 $this->expectException(\Shaarli\Exceptions\IOException
::class);
92 $this->expectExceptionMessage('Error accessing "nopefolder"');
94 FileUtils
::writeFlatDB('nopefolder/file', null);
98 * Folder non writable: raise an exception.
100 public function testWriteFolderPermission()
102 $this->expectException(\Shaarli\Exceptions\IOException
::class);
103 $this->expectExceptionMessage('Error accessing "sandbox"');
105 chmod(dirname(self
::$file), 0555);
107 FileUtils
::writeFlatDB(self
::$file, null);
108 } catch (Exception
$e) {
109 chmod(dirname(self
::$file), 0755);
115 * Read non existent file, use default parameter.
117 public function testReadNotExistentFile()
119 $this->assertEquals(null, FileUtils
::readFlatDB(self
::$file));
120 $this->assertEquals(['test'], FileUtils
::readFlatDB(self
::$file, ['test']));
124 * Read non readable file, use default parameter.
126 public function testReadNotReadable()
129 chmod(self
::$file, 0220);
130 $this->assertEquals(null, FileUtils
::readFlatDB(self
::$file));
131 $this->assertEquals(['test'], FileUtils
::readFlatDB(self
::$file, ['test']));
135 * Test clearFolder with self delete and excluded files
137 public function testClearFolderSelfDeleteWithExclusion(): void
139 FileUtils
::clearFolder('sandbox', true, ['file2']);
141 static::assertFileExists('sandbox/folder1/file2');
142 static::assertFileExists('sandbox/folder1');
143 static::assertFileExists('sandbox/file2');
144 static::assertFileExists('sandbox');
146 static::assertFileNotExists('sandbox/folder1/file1');
147 static::assertFileNotExists('sandbox/file1');
148 static::assertFileNotExists('sandbox/folder3');
152 * Test clearFolder with self delete and excluded files
154 public function testClearFolderSelfDeleteWithoutExclusion(): void
156 FileUtils
::clearFolder('sandbox', true);
158 static::assertFileNotExists('sandbox');
162 * Test clearFolder with self delete and excluded files
164 public function testClearFolderNoSelfDeleteWithoutExclusion(): void
166 FileUtils
::clearFolder('sandbox', false);
168 static::assertFileExists('sandbox');
170 // 2 because '.' and '..'
171 static::assertCount(2, new \
DirectoryIterator('sandbox'));
175 * Test clearFolder on a file instead of a folder
177 public function testClearFolderOnANonDirectory(): void
179 $this->expectException(IOException
::class);
180 $this->expectExceptionMessage('Provided path is not a directory.');
182 FileUtils
::clearFolder('sandbox/file1', false);
186 * Test clearFolder on a file instead of a folder
188 public function testClearFolderOutsideOfShaarliDirectory(): void
190 $this->expectException(IOException
::class);
191 $this->expectExceptionMessage('Trying to delete a folder outside of Shaarli path.');
194 FileUtils
::clearFolder('/tmp/shaarli-to-delete', true);