3 namespace Shaarli\Helper
;
6 use Shaarli\Exceptions\IOException
;
12 * Test file utility class.
14 class FileUtilsTest
extends TestCase
17 * @var string Test file path.
19 protected static $file = 'sandbox/flat.db';
21 protected function setUp(): void
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');
35 * Delete test file after every test.
37 protected function tearDown(): void
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');
52 * Test writeDB, then readDB with different data.
54 public function testSimpleWriteRead()
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));
62 $this->assertTrue(FileUtils
::writeFlatDB(self
::$file, $data) > 0);
63 $this->assertEquals($data, FileUtils
::readFlatDB(self
::$file));
66 $this->assertTrue(FileUtils
::writeFlatDB(self
::$file, $data) > 0);
67 $this->assertEquals($data, FileUtils
::readFlatDB(self
::$file));
70 $this->assertTrue(FileUtils
::writeFlatDB(self
::$file, $data) > 0);
71 $this->assertEquals($data, FileUtils
::readFlatDB(self
::$file));
75 * File not writable: raise an exception.
77 public function testWriteWithoutPermission()
79 $this->expectException(\Shaarli\Exceptions\IOException
::class);
80 $this->expectExceptionMessage('Error accessing "sandbox/flat.db"');
83 chmod(self
::$file, 0440);
84 FileUtils
::writeFlatDB(self
::$file, null);
88 * Folder non existent: raise an exception.
90 public function testWriteFolderDoesNotExist()
92 $this->expectException(\Shaarli\Exceptions\IOException
::class);
93 $this->expectExceptionMessage('Error accessing "nopefolder"');
95 FileUtils
::writeFlatDB('nopefolder/file', null);
99 * Folder non writable: raise an exception.
101 public function testWriteFolderPermission()
103 $this->expectException(\Shaarli\Exceptions\IOException
::class);
104 $this->expectExceptionMessage('Error accessing "sandbox"');
106 chmod(dirname(self
::$file), 0555);
108 FileUtils
::writeFlatDB(self
::$file, null);
109 } catch (Exception
$e) {
110 chmod(dirname(self
::$file), 0755);
116 * Read non existent file, use default parameter.
118 public function testReadNotExistentFile()
120 $this->assertEquals(null, FileUtils
::readFlatDB(self
::$file));
121 $this->assertEquals(['test'], FileUtils
::readFlatDB(self
::$file, ['test']));
125 * Read non readable file, use default parameter.
127 public function testReadNotReadable()
130 chmod(self
::$file, 0220);
131 $this->assertEquals(null, FileUtils
::readFlatDB(self
::$file));
132 $this->assertEquals(['test'], FileUtils
::readFlatDB(self
::$file, ['test']));
136 * Test clearFolder with self delete and excluded files
138 public function testClearFolderSelfDeleteWithExclusion(): void
140 FileUtils
::clearFolder('sandbox', true, ['file2']);
142 static::assertFileExists('sandbox/folder1/file2');
143 static::assertFileExists('sandbox/folder1');
144 static::assertFileExists('sandbox/file2');
145 static::assertFileExists('sandbox');
147 static::assertFileNotExists('sandbox/folder1/file1');
148 static::assertFileNotExists('sandbox/file1');
149 static::assertFileNotExists('sandbox/folder3');
153 * Test clearFolder with self delete and excluded files
155 public function testClearFolderSelfDeleteWithoutExclusion(): void
157 FileUtils
::clearFolder('sandbox', true);
159 static::assertFileNotExists('sandbox');
163 * Test clearFolder with self delete and excluded files
165 public function testClearFolderNoSelfDeleteWithoutExclusion(): void
167 FileUtils
::clearFolder('sandbox', false);
169 static::assertFileExists('sandbox');
171 // 2 because '.' and '..'
172 static::assertCount(2, new \
DirectoryIterator('sandbox'));
176 * Test clearFolder on a file instead of a folder
178 public function testClearFolderOnANonDirectory(): void
180 $this->expectException(IOException
::class);
181 $this->expectExceptionMessage('Provided path is not a directory.');
183 FileUtils
::clearFolder('sandbox/file1', false);
187 * Test clearFolder on a file instead of a folder
189 public function testClearFolderOutsideOfShaarliDirectory(): void
191 $this->expectException(IOException
::class);
192 $this->expectExceptionMessage('Trying to delete a folder outside of Shaarli path.');
195 FileUtils
::clearFolder('/tmp/shaarli-to-delete', true);