Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TrackerUpdaterTest.php « Integration « tests « CustomJsTracker « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bec258cdee39c46869f1a55290f19fbe3d0a843b (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license http://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\Plugins\CustomJsTracker\tests\Framework\Mock\PluginTrackerFilesMock;
use Piwik\Plugins\CustomJsTracker\TrackerUpdater;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
 * @group CustomJsTracker
 * @group PiwikJsManipulatorTest
 * @group PiwikJsManipulator
 * @group Plugins
 */
class TrackerUpdaterTest extends IntegrationTestCase
{
    private $dir;
    private $trackerJsChangedEventPath = null;

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

        $this->cleanUp();
    }

    public function tearDown(): void
    {
        parent::tearDown();

        $this->cleanUp();
    }

    private function cleanUp()
    {
        $target = $this->dir . 'MyTestTarget.js';
        if (file_exists($target)) {
            unlink($target);
        }

        $nonExistentFile = $this->dir . 'MyNotExisIngFilessss.js';
        if (file_exists($nonExistentFile)) {
            unlink($nonExistentFile);
        }
    }

    private function makeUpdater($from = null, $to = null)
    {
        return new TrackerUpdater($from, $to);
    }

    public function test_construct_setsDefaults()
    {
        $updater = $this->makeUpdater();
        $fromFile = $updater->getFromFile();
        $toFile = $updater->getToFile();
        $this->assertTrue($fromFile instanceof File);
        $this->assertTrue($toFile instanceof File);

        $this->assertSame(basename(TrackerUpdater::ORIGINAL_PIWIK_JS), $fromFile->getName());
        $this->assertSame(basename(TrackerUpdater::TARGET_MATOMO_JS), $toFile->getName());
    }

    public function test_setFormFile_getFromFile()
    {
        $updater = $this->makeUpdater();
        $testFile = new File('foobar');
        $updater->setFromFile($testFile);

        $this->assertSame($testFile, $updater->getFromFile());
    }

    public function test_setFormFile_CanBeString()
    {
        $updater = $this->makeUpdater();
        $updater->setFromFile('foobar');

        $this->assertSame('foobar', $updater->getFromFile()->getName());
    }

    public function test_setToFile_getToFile()
    {
        $updater = $this->makeUpdater();
        $testFile = new File('foobar');
        $updater->setToFile($testFile);

        $this->assertSame($testFile, $updater->getToFile());
    }

    public function test_setToFile_CanBeString()
    {
        $updater = $this->makeUpdater();
        $updater->setToFile('foobar');

        $this->assertSame('foobar', $updater->getToFile()->getName());
    }

    public function test_checkWillSucceed_shouldNotThrowExceptionIfPiwikJsTargetIsWritable()
    {
        $updater = $this->makeUpdater();
        $updater->checkWillSucceed();

        $this->assertTrue(true);
    }

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

        $updater = $this->makeUpdater(null, $this->dir . 'not-writable/MyNotExisIngFilessss.js');
        $updater->checkWillSucceed();
    }

    public function test_checkWillSucceed_shouldNotThrowExceptionIfTargetIsWritable()
    {
        $this->expectNotToPerformAssertions();
        $updater = $this->makeUpdater(null, $this->dir . 'MyNotExisIngFilessss.js');
        $updater->checkWillSucceed();
    }

    public function test_getCurrentTrackerFileContent()
    {
        $targetFile = $this->dir . 'testpiwik.js';

        $updater = $this->makeUpdater(null, $targetFile);
        $content = $updater->getCurrentTrackerFileContent();

        $this->assertSame(file_get_contents($targetFile), $content);
    }

    public function test_getUpdatedTrackerFileContent_returnsGeneratedPiwikJsWithMergedTrackerFiles_WhenTheyExist()
    {
        $source = $this->dir . 'testpiwik.js';
        $target = $this->dir . 'MyTestTarget.js';

        $updater = $this->makeUpdater($source, $target);
        $updater->setTrackerFiles(new PluginTrackerFilesMock(array(
            $this->dir . 'tracker.js', $this->dir . 'tracker.min.js'
        )));
        $content = $updater->getUpdatedTrackerFileContent();

        $this->assertSame('/** MyHeader*/
var PiwikJs = "mytest";

/*!!! pluginTrackerHook */

/* GENERATED: tracker.min.js */

/* END GENERATED: tracker.min.js */


/* GENERATED: tracker.js */

/* END GENERATED: tracker.js */


var myArray = [];
', $content);
    }

    public function test_getUpdatedTrackerFileContent_returnsSourceFile_IfNoTrackerFilesFound()
    {
        $source = $this->dir . 'testpiwik.js';
        $target = $this->dir . 'MyTestTarget.js';

        $updater = $this->makeUpdater($source, $target);
        $updater->setTrackerFiles(new PluginTrackerFilesMock(array()));
        $content = $updater->getUpdatedTrackerFileContent();

        $this->assertSame(file_get_contents($source), $content);
    }

    public function test_update_shouldNotThrowAnError_IfTargetFileIsNotWritable()
    {
        $updater = $this->makeUpdater(null, $this->dir . 'not-writable/MyNotExisIngFilessss.js');
        $updater->update();
        $this->assertTrue(true);
        $this->assertNull($this->trackerJsChangedEventPath);
    }

    public function test_update_shouldNotWriteToFileIfThereIsNothingToChange()
    {
        $source = $this->dir . 'testpiwik.js';
        $target = $this->dir . 'MyTestTarget.js';
        file_put_contents($target, file_get_contents($source));
        $updater = $this->makeUpdater($this->dir . 'testpiwik.js', $target);
        $updater->setTrackerFiles(new PluginTrackerFilesMock(array()));
        // mock that does not find any files . therefore there is nothing to di
        $updater->update();

        $this->assertSame(file_get_contents($source), file_get_contents($target));
        $this->assertNull($this->trackerJsChangedEventPath);
    }

    public function test_update_targetFileIfPluginsDefineDifferentFiles()
    {
        $target = $this->dir . 'MyTestTarget.js';
        file_put_contents($target, ''); // file has to exist in order to work

        $updater = $this->makeUpdater($this->dir . 'testpiwik.js', $target);
        $updater->setTrackerFiles(new PluginTrackerFilesMock(array(
            $this->dir . 'tracker.js', $this->dir . 'tracker.min.js'
        )));
        $updater->update();

        $this->assertSame('/** MyHeader*/
var PiwikJs = "mytest";

/*!!! pluginTrackerHook */

/* GENERATED: tracker.min.js */

/* END GENERATED: tracker.min.js */


/* GENERATED: tracker.js */

/* END GENERATED: tracker.js */


var myArray = [];
', file_get_contents($target));
        $this->assertEquals($target, $this->trackerJsChangedEventPath);
    }

    public function provideContainerConfig()
    {
        return [
            'observers.global' => \DI\add([
                ['CustomJsTracker.trackerJsChanged', \DI\value(function ($path) {
                    $this->trackerJsChangedEventPath = $path;
                })],
            ]),
        ];
    }
}