aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/helper/FileUtilsTest.php
blob: 948e46d1a404971addb7a658d199707a9f9d4783 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php

namespace Shaarli\Helper;

use Exception;
use Shaarli\Exceptions\IOException;

/**
 * Class FileUtilsTest
 *
 * Test file utility class.
 */
class FileUtilsTest extends TestCase
{
    /**
     * @var string Test file path.
     */
    protected static $file = 'sandbox/flat.db';

    protected function setUp(): void
    {
        @mkdir('sandbox');
        mkdir('sandbox/folder2');
        touch('sandbox/file1');
        touch('sandbox/file2');
        mkdir('sandbox/folder1');
        touch('sandbox/folder1/file1');
        touch('sandbox/folder1/file2');
        mkdir('sandbox/folder3');
        mkdir('/tmp/shaarli-to-delete');
    }

    /**
     * Delete test file after every test.
     */
    protected function tearDown(): void
    {
        @unlink(self::$file);

        @unlink('sandbox/folder1/file1');
        @unlink('sandbox/folder1/file2');
        @rmdir('sandbox/folder1');
        @unlink('sandbox/file1');
        @unlink('sandbox/file2');
        @rmdir('sandbox/folder2');
        @rmdir('sandbox/folder3');
        @rmdir('/tmp/shaarli-to-delete');
    }

    /**
     * Test writeDB, then readDB with different data.
     */
    public function testSimpleWriteRead()
    {
        $data = ['blue', 'red'];
        $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
        $this->assertTrue(startsWith(file_get_contents(self::$file), '<?php /*'));
        $this->assertEquals($data, FileUtils::readFlatDB(self::$file));

        $data = 0;
        $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
        $this->assertEquals($data, FileUtils::readFlatDB(self::$file));

        $data = null;
        $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
        $this->assertEquals($data, FileUtils::readFlatDB(self::$file));

        $data = false;
        $this->assertTrue(FileUtils::writeFlatDB(self::$file, $data) > 0);
        $this->assertEquals($data, FileUtils::readFlatDB(self::$file));
    }

    /**
     * File not writable: raise an exception.
     */
    public function testWriteWithoutPermission()
    {
        $this->expectException(\Shaarli\Exceptions\IOException::class);
        $this->expectExceptionMessage('Error accessing "sandbox/flat.db"');

        touch(self::$file);
        chmod(self::$file, 0440);
        FileUtils::writeFlatDB(self::$file, null);
    }

    /**
     * Folder non existent: raise an exception.
     */
    public function testWriteFolderDoesNotExist()
    {
        $this->expectException(\Shaarli\Exceptions\IOException::class);
        $this->expectExceptionMessage('Error accessing "nopefolder"');

        FileUtils::writeFlatDB('nopefolder/file', null);
    }

    /**
     * Folder non writable: raise an exception.
     */
    public function testWriteFolderPermission()
    {
        $this->expectException(\Shaarli\Exceptions\IOException::class);
        $this->expectExceptionMessage('Error accessing "sandbox"');

        chmod(dirname(self::$file), 0555);
        try {
            FileUtils::writeFlatDB(self::$file, null);
        } catch (Exception $e) {
            chmod(dirname(self::$file), 0755);
            throw $e;
        }
    }

    /**
     * Read non existent file, use default parameter.
     */
    public function testReadNotExistentFile()
    {
        $this->assertEquals(null, FileUtils::readFlatDB(self::$file));
        $this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test']));
    }

    /**
     * Read non readable file, use default parameter.
     */
    public function testReadNotReadable()
    {
        touch(self::$file);
        chmod(self::$file, 0220);
        $this->assertEquals(null, FileUtils::readFlatDB(self::$file));
        $this->assertEquals(['test'], FileUtils::readFlatDB(self::$file, ['test']));
    }

    /**
     * Test clearFolder with self delete and excluded files
     */
    public function testClearFolderSelfDeleteWithExclusion(): void
    {
        FileUtils::clearFolder('sandbox', true, ['file2']);

        static::assertFileExists('sandbox/folder1/file2');
        static::assertFileExists('sandbox/folder1');
        static::assertFileExists('sandbox/file2');
        static::assertFileExists('sandbox');

        static::assertFileNotExists('sandbox/folder1/file1');
        static::assertFileNotExists('sandbox/file1');
        static::assertFileNotExists('sandbox/folder3');
    }

    /**
     * Test clearFolder with self delete and excluded files
     */
    public function testClearFolderSelfDeleteWithoutExclusion(): void
    {
        FileUtils::clearFolder('sandbox', true);

        static::assertFileNotExists('sandbox');
    }

    /**
     * Test clearFolder with self delete and excluded files
     */
    public function testClearFolderNoSelfDeleteWithoutExclusion(): void
    {
        FileUtils::clearFolder('sandbox', false);

        static::assertFileExists('sandbox');

        // 2 because '.' and '..'
        static::assertCount(2, new \DirectoryIterator('sandbox'));
    }

    /**
     * Test clearFolder on a file instead of a folder
     */
    public function testClearFolderOnANonDirectory(): void
    {
        $this->expectException(IOException::class);
        $this->expectExceptionMessage('Provided path is not a directory.');

        FileUtils::clearFolder('sandbox/file1', false);
    }

    /**
     * Test clearFolder on a file instead of a folder
     */
    public function testClearFolderOutsideOfShaarliDirectory(): void
    {
        $this->expectException(IOException::class);
        $this->expectExceptionMessage('Trying to delete a folder outside of Shaarli path.');


        FileUtils::clearFolder('/tmp/shaarli-to-delete', true);
    }
}