Newer
Older
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Unit;
use PHPUnit_Framework_TestCase;
use Piwik\AssetManager\UIAsset\OnDiskUIAsset;
use Piwik\AssetManager\UIAsset;
use Piwik\AssetManager;
use Piwik\AssetManager\UIAssetFetcher\StaticUIAssetFetcher;
use Piwik\Config;
use Piwik\Plugin;
use Piwik\Plugin\Manager;
use Piwik\Tests\Unit\AssetManager\PluginManagerMock;
use Piwik\Tests\Unit\AssetManager\PluginMock;
use Piwik\Tests\Unit\AssetManager\ThemeMock;
use Piwik\Tests\Unit\AssetManager\UIAssetCacheBusterMock;
class AssetManagerTest extends PHPUnit_Framework_TestCase
{
// todo Theme->rewriteAssetPathIfOverridesFound is not tested
Thomas Steur
a validé
const ASSET_MANAGER_TEST_DIR = 'tests/PHPUnit/Unit/AssetManager/';
const FIRST_CACHE_BUSTER_JS = 'first-cache-buster-js';
const SECOND_CACHE_BUSTER_JS = 'second-cache-buster-js';
const FIRST_CACHE_BUSTER_SS = 'first-cache-buster-stylesheet';
const SECOND_CACHE_BUSTER_SS = 'second-cache-buster-stylesheet';
const CORE_PLUGIN_NAME = 'MockCorePlugin';
const CORE_PLUGIN_WITHOUT_ASSETS_NAME = 'MockCoreWithoutAssetPlugin';
const NON_CORE_PLUGIN_NAME = 'MockNonCorePlugin';
const CORE_THEME_PLUGIN_NAME = 'CoreThemePlugin';
const NON_CORE_THEME_PLUGIN_NAME = 'NonCoreThemePlugin';
* @var AssetManager
*/
private $assetManager;
/**
* @var UIAsset
*/
private $mergedAsset;
/**
* @var \Piwik\Tests\Unit\AssetManager\UIAssetCacheBusterMock
* @var \Piwik\Tests\Unit\AssetManager\PluginManagerMock
*/
private $pluginManager;
public function setUp()
{
$this->activateMergedAssets();
$this->setUpCacheBuster();
$this->setUpAssetManager();
$this->setUpPluginManager();
$this->setUpTheme();
$this->setUpPlugins();
}
public function tearDown()
{
$this->assetManager->removeMergedAssets();
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
}
private function activateMergedAssets()
{
$this->setUpConfig('merged-assets-enabled.ini.php');
}
private function disableMergedAssets()
{
$this->setUpConfig('merged-assets-disabled.ini.php');
}
/**
* @param string $filename
*/
private function setUpConfig($filename)
{
$userFile = PIWIK_INCLUDE_PATH . '/' . self::ASSET_MANAGER_TEST_DIR . 'configs/' . $filename;
$globalFile = PIWIK_INCLUDE_PATH . '/' . self::ASSET_MANAGER_TEST_DIR . 'configs/plugins.ini.php';
$config = Config::getInstance();
$config->setTestEnvironment($userFile, $globalFile);
$config->init();
}
private function setUpCacheBuster()
{
$this->cacheBuster = UIAssetCacheBusterMock::getInstance();
}
private function setUpAssetManager()
{
$this->assetManager = AssetManager::getInstance();
$this->assetManager->removeMergedAssets();
$this->assetManager->setCacheBuster($this->cacheBuster);
}
private function setUpPluginManager()
$this->pluginManager = PluginManagerMock::getInstance();
Manager::setSingletonInstance($this->pluginManager);
EventDispatcher::unsetInstance(); // EventDispatcher stores a reference to Plugin Manager
}
private function setUpPlugins()
{
$this->pluginManager->setPlugins(
array(
$this->getCoreTheme()->getPlugin(),
$this->getNonCoreTheme()->getPlugin(),
$this->getCorePlugin(),
$this->getCorePluginWithoutUIAssets(),
$this->getNonCorePlugin()
)
$this->pluginManager->setLoadedTheme($this->getNonCoreTheme());
}
private function setUpCorePluginOnly()
{
$this->pluginManager->setPlugins(
array(
$this->getCorePlugin(),
)
}
/**
* @return Plugin
*/
private function getCorePlugin()
{
$corePlugin = new PluginMock(self::CORE_PLUGIN_NAME);
$corePlugin->setJsFiles(
array(
self::ASSET_MANAGER_TEST_DIR . 'scripts/SimpleObject.js',
self::ASSET_MANAGER_TEST_DIR . 'scripts/SimpleArray.js',
)
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
$corePlugin->setStylesheetFiles($this->getCorePluginStylesheetFiles());
$corePlugin->setJsCustomization('// customization via event');
$corePlugin->setCssCustomization('/* customization via event */');
return $corePlugin;
}
/**
* @return Plugin
*/
private function getCorePluginWithoutUIAssets()
{
return new PluginMock(self::CORE_PLUGIN_WITHOUT_ASSETS_NAME);
}
/**
* @return Plugin
*/
private function getNonCorePlugin()
{
$nonCorePlugin = new PluginMock(self::NON_CORE_PLUGIN_NAME);
$nonCorePlugin->setJsFiles(array(self::ASSET_MANAGER_TEST_DIR . 'scripts/SimpleAlert.js'));
return $nonCorePlugin;
}
private function setUpTheme()
{
$this->assetManager->setTheme($this->getCoreTheme());
}
/**
* @return ThemeMock
*/
private function getCoreTheme()
{
return $this->createTheme(self::CORE_THEME_PLUGIN_NAME);
}
/**
* @return ThemeMock
*/
private function getNonCoreTheme()
{
return $this->createTheme(self::NON_CORE_THEME_PLUGIN_NAME);
}
/**
* @param string $themeName
* @return ThemeMock
*/
private function createTheme($themeName)
{
$coreThemePlugin = new PluginMock($themeName);
$coreThemePlugin->setIsTheme(true);
$coreTheme = new ThemeMock($coreThemePlugin);
$coreTheme->setStylesheet($this->getCoreThemeStylesheet());
$coreTheme->setJsFiles(array(self::ASSET_MANAGER_TEST_DIR . 'scripts/SimpleComments.js'));
return $coreTheme;
}
/**
* @return string[]
*/
public function getCorePluginStylesheetFiles()
{
return array(
self::ASSET_MANAGER_TEST_DIR . 'stylesheets/SimpleLess.less',
self::ASSET_MANAGER_TEST_DIR . 'stylesheets/CssWithURLs.css',
);
}
private function getAssetContent()
return $this->mergedAsset->getContent();
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
}
/**
* @param string $cacheBuster
*/
private function setJSCacheBuster($cacheBuster)
{
$this->cacheBuster->setPiwikVersionBasedCacheBuster($cacheBuster);
}
/**
* @param string $cacheBuster
*/
private function setStylesheetCacheBuster($cacheBuster)
{
$this->cacheBuster->setMd5BasedCacheBuster($cacheBuster);
}
private function triggerGetMergedCoreJavaScript()
{
$this->mergedAsset = $this->assetManager->getMergedCoreJavaScript();
}
private function triggerGetMergedNonCoreJavaScript()
{
$this->mergedAsset = $this->assetManager->getMergedNonCoreJavaScript();
}
private function triggerGetMergedStylesheet()
{
$this->mergedAsset = $this->assetManager->getMergedStylesheet();
}
private function validateMergedCoreJs()
{
$expectedContent = $this->getExpectedMergedCoreJs();
$this->validateExpectedContent($expectedContent);
}
private function validateMergedNonCoreJs()
{
$expectedContent = $this->getExpectedMergedNonCoreJs();
$this->validateExpectedContent($expectedContent);
}
private function validateMergedStylesheet()
{
$expectedContent = $this->getExpectedMergedStylesheet();
$this->validateExpectedContent($expectedContent);
}
/**
* @param string $expectedContent
*/
private function validateExpectedContent($expectedContent)
{
$this->assertEquals($expectedContent, $this->mergedAsset->getContent());
}
/**
* @return string
*/
private function getExpectedMergedCoreJs()
{
return $this->getExpectedMergedJs('ExpectedMergeResultCore.js');
}
/**
* @return string
*/
private function getExpectedMergedNonCoreJs()
{
return $this->getExpectedMergedJs('ExpectedMergeResultNonCore.js');
}
/**
* @param string $filename
* @return string
*/
private function getExpectedMergedJs($filename)
{
$expectedMergeResult = new OnDiskUIAsset(PIWIK_USER_PATH, self::ASSET_MANAGER_TEST_DIR .'scripts/' . $filename);
$expectedContent = $expectedMergeResult->getContent();
return $this->adjustExpectedJsContent($expectedContent);
}
/**
* @param string $expectedJsContent
* @return string
*/
private function adjustExpectedJsContent($expectedJsContent)
{
$expectedJsContent = str_replace("\n", "\r\n", $expectedJsContent);
$expectedJsContent = $this->specifyCacheBusterInExpectedContent($expectedJsContent, $this->cacheBuster->piwikVersionBasedCacheBuster());
return $expectedJsContent;
}
/**
* @return string
*/
private function getExpectedMergedStylesheet()
{
$expectedMergeResult = new OnDiskUIAsset(PIWIK_USER_PATH, self::ASSET_MANAGER_TEST_DIR .'stylesheets/ExpectedMergeResult.css');
$expectedContent = $expectedMergeResult->getContent();
$expectedContent = $this->specifyCacheBusterInExpectedContent($expectedContent, $this->cacheBuster->md5BasedCacheBuster(''));
return $expectedContent;
}
/**
* @return string
*/
private function getCoreThemeStylesheet()
{
return self::ASSET_MANAGER_TEST_DIR . 'stylesheets/SimpleBody.css';
}
/**
* @param string $content
* @param string $cacheBuster
* @return string
*/
private function specifyCacheBusterInExpectedContent($content, $cacheBuster)
{
return str_replace('{{{CACHE-BUSTER-JS}}}', $cacheBuster, $content);
}
/**
* @param string $previousContent
private function assertAssetContentIsSameAs($previousContent)
$this->assertEquals($previousContent, $this->getAssetContent());
* @param string $previousContent
private function assertAssetContentChanged($previousContent)
$this->assertNotEquals($previousContent, $this->getAssetContent());
}
/**
* @return string
*/
private function getJsTranslationScript()
{
return
'<script type="text/javascript">' . PHP_EOL .
'var translations = [];' . PHP_EOL .
Thomas Steur
a validé
'if (typeof(piwik_translations) == \'undefined\') { var piwik_translations = new Object; }for(var i in translations) { piwik_translations[i] = translations[i];} ' . PHP_EOL .
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
'</script>';
}
/**
* @return UIAsset[]
*/
private function generateAllMergedAssets()
{
$this->triggerGetMergedStylesheet();
$stylesheetAsset = $this->mergedAsset;
$this->triggerGetMergedCoreJavaScript();
$coreJsAsset = $this->mergedAsset;
$this->triggerGetMergedNonCoreJavaScript();
$nonCoreJsAsset = $this->mergedAsset;
$this->assertTrue($stylesheetAsset->exists());
$this->assertTrue($coreJsAsset->exists());
$this->assertTrue($nonCoreJsAsset->exists());
return array($stylesheetAsset, $coreJsAsset, $nonCoreJsAsset);
}
/**
* @group Core
*/
public function test_getMergedCoreJavaScript_NotGenerated()
{
$this->setJSCacheBuster(self::FIRST_CACHE_BUSTER_JS);
$this->triggerGetMergedCoreJavaScript();
$this->validateMergedCoreJs();
}
/**
* @group Core
*/
public function test_getMergedNonCoreJavaScript_NotGenerated()
{
$this->setJSCacheBuster(self::FIRST_CACHE_BUSTER_JS);
$this->triggerGetMergedNonCoreJavaScript();
$this->validateMergedNonCoreJs();
}
/**
* @group Core
*/
public function test_getMergedNonCoreJavaScript_NotGenerated_NoNonCorePlugin()
{
$this->setUpCorePluginOnly();
$this->setJSCacheBuster(self::FIRST_CACHE_BUSTER_JS);
$this->triggerGetMergedNonCoreJavaScript();
$expectedContent = $this->adjustExpectedJsContent('/* Piwik Javascript - cb={{{CACHE-BUSTER-JS}}}*/' . PHP_EOL);
$this->validateExpectedContent($expectedContent);
}
/**
* @group Core
*/
public function test_getMergedCoreJavaScript_AlreadyGenerated_MergedAssetsDisabled_UpToDate()
{
$this->disableMergedAssets();
$this->setJSCacheBuster(self::FIRST_CACHE_BUSTER_JS);
$this->triggerGetMergedCoreJavaScript();
$content = $this->getAssetContent();
$this->assertAssetContentIsSameAs($content);
public function test_getMergedCoreJavaScript_AlreadyGenerated_MergedAssetsDeactivated_Stale()
$this->disableMergedAssets();
$this->setJSCacheBuster(self::FIRST_CACHE_BUSTER_JS);
$this->triggerGetMergedCoreJavaScript();
$content = $this->getAssetContent();
$this->setJSCacheBuster(self::SECOND_CACHE_BUSTER_JS);
$this->triggerGetMergedCoreJavaScript();
$this->assertAssetContentChanged($content);
$this->validateMergedCoreJs();
}
/**
* @group Core
*/
public function test_getMergedStylesheet_NotGenerated()
{
$this->setStylesheetCacheBuster(self::FIRST_CACHE_BUSTER_SS);
$this->triggerGetMergedStylesheet();
$this->validateMergedStylesheet();
}
/**
* We always regenerate if cache buster changes
public function test_getMergedStylesheet_Generated_MergedAssetsEnabled_Stale()
$this->activateMergedAssets();
$this->setStylesheetCacheBuster(self::FIRST_CACHE_BUSTER_SS);
$this->triggerGetMergedStylesheet();
$content = $this->getAssetContent();
$this->setStylesheetCacheBuster(self::SECOND_CACHE_BUSTER_SS);
$this->triggerGetMergedStylesheet();
$this->assertAssetContentChanged($content);
$this->validateMergedStylesheet();
* We always regenerate if cache buster changes
public function test_getMergedStylesheet_Generated_MergedAssetsDisabled_Stale()
$this->disableMergedAssets();
$this->setStylesheetCacheBuster(self::FIRST_CACHE_BUSTER_SS);
$this->triggerGetMergedStylesheet();
$content = $this->getAssetContent();
$this->setStylesheetCacheBuster(self::SECOND_CACHE_BUSTER_SS);
$this->triggerGetMergedStylesheet();
$this->assertAssetContentChanged($content);
$this->validateMergedStylesheet();
}
/**
* @group Core
*/
public function test_getMergedStylesheet_Generated_MergedAssetsDisabled_UpToDate()
{
$this->disableMergedAssets();
$this->setStylesheetCacheBuster(self::FIRST_CACHE_BUSTER_SS);
$this->triggerGetMergedStylesheet();
$content = $this->getAssetContent();
$this->assertAssetContentIsSameAs($content);
}
/**
* @group Core
*/
public function test_getCssInclusionDirective()
{
$expectedCssInclusionDirective = '<link rel="stylesheet" type="text/css" href="index.php?module=Proxy&action=getCss" />' . PHP_EOL;
$this->assertEquals($expectedCssInclusionDirective, $this->assetManager->getCssInclusionDirective());
}
/**
* @group Core
*/
public function test_getJsInclusionDirective_MergedAssetsDisabled()
{
$this->disableMergedAssets();
$expectedJsInclusionDirective =
$this->getJsTranslationScript() .
Thomas Steur
a validé
'<script type="text/javascript" src="tests/PHPUnit/Unit/AssetManager/scripts/SimpleObject.js"></script>' . PHP_EOL .
'<script type="text/javascript" src="tests/PHPUnit/Unit/AssetManager/scripts/SimpleArray.js"></script>' . PHP_EOL .
'<script type="text/javascript" src="tests/PHPUnit/Unit/AssetManager/scripts/SimpleComments.js"></script>' . PHP_EOL .
'<script type="text/javascript" src="tests/PHPUnit/Unit/AssetManager/scripts/SimpleAlert.js"></script>' . PHP_EOL;
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
$this->assertEquals($expectedJsInclusionDirective, $this->assetManager->getJsInclusionDirective());
}
/**
* @group Core
*/
public function test_getJsInclusionDirective_MergedAssetsEnabled()
{
$expectedJsInclusionDirective =
$this->getJsTranslationScript() .
'<script type="text/javascript" src="index.php?module=Proxy&action=getCoreJs"></script>' . PHP_EOL .
'<script type="text/javascript" src="index.php?module=Proxy&action=getNonCoreJs"></script>' . PHP_EOL;
$this->assertEquals($expectedJsInclusionDirective, $this->assetManager->getJsInclusionDirective());
}
/**
* @group Core
*/
public function test_getCompiledBaseCss()
{
$this->setStylesheetCacheBuster(self::FIRST_CACHE_BUSTER_SS);
$staticStylesheetList = array_merge($this->getCorePluginStylesheetFiles(), array($this->getCoreThemeStylesheet()));
$minimalAssetFetcher = new StaticUIAssetFetcher(
array_reverse($staticStylesheetList),
$staticStylesheetList,
$this->getCoreTheme()
);
$this->assetManager->setMinimalStylesheetFetcher($minimalAssetFetcher);
$this->mergedAsset = $this->assetManager->getCompiledBaseCss();
$this->validateMergedStylesheet();
}
/**
* @group Core
*/
public function test_removeMergedAssets()
{
list($stylesheetAsset, $coreJsAsset, $nonCoreJsAsset) = $this->generateAllMergedAssets();
$this->assetManager->removeMergedAssets();
$this->assertFalse($stylesheetAsset->exists());
$this->assertFalse($coreJsAsset->exists());
$this->assertFalse($nonCoreJsAsset->exists());
}
/**
* @group Core
*/
public function test_removeMergedAssets_PluginNameSpecified_PluginWithoutAssets()
{
list($stylesheetAsset, $coreJsAsset, $nonCoreJsAsset) = $this->generateAllMergedAssets();
$this->assetManager->removeMergedAssets(self::CORE_PLUGIN_WITHOUT_ASSETS_NAME);
$this->assertFalse($stylesheetAsset->exists());
$this->assertTrue($coreJsAsset->exists());
$this->assertTrue($nonCoreJsAsset->exists());
}
/**
* @group Core
*/
public function test_removeMergedAssets_PluginNameSpecified_CorePlugin()
{
list($stylesheetAsset, $coreJsAsset, $nonCoreJsAsset) = $this->generateAllMergedAssets();
$this->assetManager->removeMergedAssets(self::CORE_PLUGIN_NAME);
$this->assertFalse($stylesheetAsset->exists());
$this->assertFalse($coreJsAsset->exists());
$this->assertTrue($nonCoreJsAsset->exists());
}
/**
* @group Core
*/
public function test_removeMergedAssets_PluginNameSpecified_NonCoreThemeWithAssets()
{
list($stylesheetAsset, $coreJsAsset, $nonCoreJsAsset) = $this->generateAllMergedAssets();
$this->assetManager->removeMergedAssets(self::NON_CORE_THEME_PLUGIN_NAME);
$this->assertFalse($stylesheetAsset->exists());
$this->assertTrue($coreJsAsset->exists());
$this->assertFalse($nonCoreJsAsset->exists());