]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/FileUtilsTest.php
Security: fix multiple XSS vulnerabilities + fix search tags with special chars
[github/shaarli/Shaarli.git] / tests / FileUtilsTest.php
CommitLineData
b2306b0c
A
1<?php
2
a0c4dbd9 3namespace Shaarli;
f3d2f257 4
a0c4dbd9 5use Exception;
b2306b0c
A
6
7/**
8 * Class FileUtilsTest
9 *
10 * Test file utility class.
11 */
a5a9cf23 12class FileUtilsTest extends \Shaarli\TestCase
b2306b0c
A
13{
14 /**
15 * @var string Test file path.
16 */
17 protected static $file = 'sandbox/flat.db';
18
19 /**
20 * Delete test file after every test.
21 */
8f60e120 22 protected function tearDown(): void
b2306b0c
A
23 {
24 @unlink(self::$file);
25 }
26
27 /**
28 * Test writeDB, then readDB with different data.
29 */
30 public function testSimpleWriteRead()
31 {
32 $data = ['blue', 'red'];
33 $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
34 $this->assertTrue(startsWith(file_get_contents(self::$file), '<?php /*'));
35 $this->assertEquals($data, FileUtils::readFlatDB(self::$file));
36
37 $data = 0;
38 $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
39 $this->assertEquals($data, FileUtils::readFlatDB(self::$file));
40
41 $data = null;
42 $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
43 $this->assertEquals($data, FileUtils::readFlatDB(self::$file));
44
45 $data = false;
46 $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
47 $this->assertEquals($data, FileUtils::readFlatDB(self::$file));
48 }
49
50 /**
51 * File not writable: raise an exception.
b2306b0c
A
52 */
53 public function testWriteWithoutPermission()
54 {
b1baca99
A
55 $this->expectException(\Shaarli\Exceptions\IOException::class);
56 $this->expectExceptionMessage('Error accessing "sandbox/flat.db"');
57
b2306b0c
A
58 touch(self::$file);
59 chmod(self::$file, 0440);
60 FileUtils::writeFlatDB(self::$file, null);
61 }
62
63 /**
64 * Folder non existent: raise an exception.
b2306b0c
A
65 */
66 public function testWriteFolderDoesNotExist()
67 {
b1baca99
A
68 $this->expectException(\Shaarli\Exceptions\IOException::class);
69 $this->expectExceptionMessage('Error accessing "nopefolder"');
70
b2306b0c
A
71 FileUtils::writeFlatDB('nopefolder/file', null);
72 }
73
74 /**
75 * Folder non writable: raise an exception.
b2306b0c
A
76 */
77 public function testWriteFolderPermission()
78 {
b1baca99
A
79 $this->expectException(\Shaarli\Exceptions\IOException::class);
80 $this->expectExceptionMessage('Error accessing "sandbox"');
81
b2306b0c
A
82 chmod(dirname(self::$file), 0555);
83 try {
84 FileUtils::writeFlatDB(self::$file, null);
85 } catch (Exception $e) {
86 chmod(dirname(self::$file), 0755);
87 throw $e;
88 }
89 }
90
91 /**
92 * Read non existent file, use default parameter.
93 */
94 public function testReadNotExistentFile()
95 {
96 $this->assertEquals(null, FileUtils::readFlatDB(self::$file));
97 $this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test']));
98 }
99
100 /**
101 * Read non readable file, use default parameter.
102 */
103 public function testReadNotReadable()
104 {
105 touch(self::$file);
106 chmod(self::$file, 0220);
107 $this->assertEquals(null, FileUtils::readFlatDB(self::$file));
108 $this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test']));
109 }
110}