File: FileTest.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (211 lines) | stat: -rw-r--r-- 6,977 bytes parent folder | download
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\CustomJsTracker\tests\Integration;

use Piwik\Plugins\CustomJsTracker\File;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

class CustomTestFile extends File
{
}

/**
 * @group CustomJsTracker
 * @group FileTest
 * @group File
 * @group Plugins
 */
class FileTest extends IntegrationTestCase
{
    public const NOT_EXISTING_FILE_IN_WRITABLE_DIRECTORY = 'notExisTinGFile.js';
    public const NOT_EXISTING_FILE_IN_NON_WRITABLE_DIRECTORY = 'is-not-writable/notExisTinGFile.js';

    /**
     * @var string
     */
    private $dir = '';

    public function setUp(): void
    {
        parent::setUp();
        $this->dir = PIWIK_DOCUMENT_ROOT . '/plugins/CustomJsTracker/tests/resources/';

        // make directory not writable
        $nonWritableDir = dirname($this->dir . self::NOT_EXISTING_FILE_IN_NON_WRITABLE_DIRECTORY);
        @chmod($nonWritableDir, 0444);
        if (is_writable($nonWritableDir)) {
            throw new \Exception("The directory $nonWritableDir should have been made non writable by this test, but it didn't work");
        }
    }

    public function tearDown(): void
    {
        // restore permissions changed by makeNotWritableFile()
        chmod($this->dir, 0777);

        if (file_exists($this->dir . self::NOT_EXISTING_FILE_IN_WRITABLE_DIRECTORY)) {
            unlink($this->dir . self::NOT_EXISTING_FILE_IN_WRITABLE_DIRECTORY);
        }
        parent::tearDown();
    }

    private function makeFile($fileName = 'test.js')
    {
        return new File($this->dir . $fileName);
    }

    private function makeNotWritableFile()
    {
        $path = $this->dir . 'file-made-non-writable.js';
        if (file_exists($path)) {
            chmod($path, 0777);
        }
        $file = new File($path);
        $file->save('will be saved OK, and then we make it non writable.');

        if (!chmod($path, 0444)) {
            throw new \Exception("chmod on the file didn't work");
        }
        if (!chmod(dirname($path), 0755)) {
            throw new \Exception("chmod on the directory didn't work");
        }
        $this->assertTrue(is_writable(dirname($path)));
        $this->assertFalse(is_writable($path));
        $this->assertTrue(file_exists($path));
        return $file;
    }

    private function makeNotReadableFile()
    {
        return $this->makeNotReadableFileInWritableDirectory();
    }

    private function makeNotReadableFileInNonWritableDirectory()
    {
        return $this->makeFile(self::NOT_EXISTING_FILE_IN_NON_WRITABLE_DIRECTORY);
    }

    private function makeNotReadableFileInWritableDirectory()
    {
        return $this->makeFile(self::NOT_EXISTING_FILE_IN_WRITABLE_DIRECTORY);
    }

    public function testGetName()
    {
        $this->assertSame('test.js', $this->makeFile()->getName());
        $this->assertSame('notExisTinGFile.js', $this->makeNotReadableFile()->getName());
    }

    public function testSetFileCreatesNewObjectLeavesOldUnchanged()
    {
        $file = $this->makeFile();
        $file2 = $file->setFile('foo/bar.png');
        $this->assertSame('test.js', $file->getName());
        $this->assertSame('bar.png', $file2->getName());
    }

    public function testSetFileReturnsObjectOfSameType()
    {
        $file = new CustomTestFile('foo/baz.png');
        $file2 = $file->setFile('foo/bar.png');
        $this->assertTrue($file2 instanceof CustomTestFile);
    }

    public function testGetPath()
    {
        $this->assertSame($this->dir . 'notExisTinGFile.js', $this->makeNotReadableFile()->getPath());
    }

    public function testHasReadAccess()
    {
        $this->assertTrue($this->makeFile()->hasReadAccess());
        $this->assertFalse($this->makeNotReadableFile()->hasReadAccess());
    }

    public function testHasWriteAccess()
    {
        $this->assertTrue($this->makeFile()->hasWriteAccess());
        $this->assertTrue($this->makeNotReadableFileInWritableDirectory()->hasWriteAccess());
        $this->assertFalse($this->makeNotReadableFileInNonWritableDirectory()->hasWriteAccess());
    }

    public function testHasWriteAccessWhenFileExistAndIsNotWritable()
    {
        $this->assertFalse($this->makeNotWritableFile()->hasWriteAccess());
    }

    public function testCheckReadableShouldNotThrowExceptionIfIsReadable()
    {
        self::expectNotToPerformAssertions();

        $this->makeFile()->checkReadable();
    }

    public function testCheckWritableShouldNotThrowExceptionIfIsWritable()
    {
        self::expectNotToPerformAssertions();

        $this->makeFile()->checkWritable();
    }

    public function testCheckReadableShouldThrowExceptionIfNotIsReadable()
    {
        $this->expectException(\Piwik\Plugins\CustomJsTracker\Exception\AccessDeniedException::class);
        $this->expectExceptionMessage('not readable');

        $this->makeNotReadableFile()->checkReadable();
    }

    public function testCheckWritableShouldThrowExceptionIfNotIsWritable()
    {
        $this->expectException(\Piwik\Plugins\CustomJsTracker\Exception\AccessDeniedException::class);
        $this->expectExceptionMessage('not writable');

        $this->makeNotReadableFileInNonWritableDirectory()->checkWritable();
    }

    public function testCheckWritableShouldNotThrowExceptionIfDirectoryIsWritable()
    {
        $this->expectNotToPerformAssertions();
        $this->makeNotReadableFileInWritableDirectory()->checkWritable();
    }

    public function testGetContent()
    {
        $this->assertSame("// Hello world\nvar fooBar = 'test';", $this->makeFile()->getContent());
    }

    public function testIsFileContentSame()
    {
        $this->assertTrue($this->makeFile()->isFileContentSame("// Hello world\nvar fooBar = 'test';"));
        $this->assertFalse($this->makeFile()->isFileContentSame("// Hello world\nvar foBar = 'test';"));
        $this->assertFalse($this->makeFile()->isFileContentSame("// Hello world\nvar foBar = 'test'"));
        $this->assertFalse($this->makeFile()->isFileContentSame("Hello world\nvar foBar = 'test'"));
    }

    public function testGetContentReturnsNullIfFileIsNotReadableOrNotExists()
    {
        $this->assertNull($this->makeNotReadableFile()->getContent());
    }

    public function testSave()
    {
        $notExistingFile = $this->makeNotReadableFileInWritableDirectory();
        $this->assertFalse($notExistingFile->hasReadAccess());
        $this->assertTrue($notExistingFile->hasWriteAccess());

        $updatedFile =  $notExistingFile->save('myTestContent');

        $this->assertEquals([$this->dir . 'notExisTinGFile.js'], $updatedFile);
        $this->assertEquals('myTestContent', $notExistingFile->getContent());
        $this->assertTrue($notExistingFile->hasReadAccess());
        $this->assertTrue($notExistingFile->hasWriteAccess());
    }
}