View source
<?php
namespace Drupal\Tests\Core\Asset;
use Drupal\Core\Asset\AssetResolver;
use Drupal\Core\Asset\AttachedAssets;
use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\Core\Cache\MemoryBackend;
use Drupal\Tests\UnitTestCase;
class AssetResolverTest extends UnitTestCase {
protected $assetResolver;
protected $libraryDiscovery;
protected $libraryDependencyResolver;
protected $moduleHandler;
protected $themeManager;
protected $languageManager;
protected $cache;
protected function setUp() : void {
parent::setUp();
$this->libraryDiscovery = $this
->getMockBuilder('Drupal\\Core\\Asset\\LibraryDiscovery')
->disableOriginalConstructor()
->getMock();
$this->libraryDependencyResolver = $this
->createMock('\\Drupal\\Core\\Asset\\LibraryDependencyResolverInterface');
$this->libraryDependencyResolver
->expects($this
->any())
->method('getLibrariesWithDependencies')
->willReturnArgument(0);
$this->moduleHandler = $this
->createMock('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
$this->themeManager = $this
->createMock('\\Drupal\\Core\\Theme\\ThemeManagerInterface');
$active_theme = $this
->getMockBuilder('\\Drupal\\Core\\Theme\\ActiveTheme')
->disableOriginalConstructor()
->getMock();
$active_theme
->expects($this
->any())
->method('getName')
->willReturn('bartik');
$this->themeManager
->expects($this
->any())
->method('getActiveTheme')
->willReturn($active_theme);
$this->languageManager = $this
->createMock('\\Drupal\\Core\\Language\\LanguageManagerInterface');
$english = $this
->createMock('\\Drupal\\Core\\Language\\LanguageInterface');
$english
->expects($this
->any())
->method('getId')
->willReturn('en');
$japanese = $this
->createMock('\\Drupal\\Core\\Language\\LanguageInterface');
$japanese
->expects($this
->any())
->method('getId')
->willReturn('jp');
$this->languageManager = $this
->createMock('\\Drupal\\Core\\Language\\LanguageManagerInterface');
$this->languageManager
->expects($this
->any())
->method('getCurrentLanguage')
->will($this
->onConsecutiveCalls($english, $english, $japanese, $japanese));
$this->cache = new TestMemoryBackend();
$this->assetResolver = new AssetResolver($this->libraryDiscovery, $this->libraryDependencyResolver, $this->moduleHandler, $this->themeManager, $this->languageManager, $this->cache);
}
public function testGetCssAssets(AttachedAssetsInterface $assets_a, AttachedAssetsInterface $assets_b, $expected_cache_item_count) {
$this->assetResolver
->getCssAssets($assets_a, FALSE);
$this->assetResolver
->getCssAssets($assets_b, FALSE);
$this
->assertCount($expected_cache_item_count, $this->cache
->getAllCids());
}
public function testGetJsAssets(AttachedAssetsInterface $assets_a, AttachedAssetsInterface $assets_b, $expected_cache_item_count) {
$this->assetResolver
->getJsAssets($assets_a, FALSE);
$this->assetResolver
->getJsAssets($assets_b, FALSE);
$this
->assertCount($expected_cache_item_count, $this->cache
->getAllCids());
$this->assetResolver
->getJsAssets($assets_a, FALSE);
$this->assetResolver
->getJsAssets($assets_b, FALSE);
$this
->assertCount($expected_cache_item_count * 2, $this->cache
->getAllCids());
}
public function providerAttachedAssets() {
$time = time();
return [
'same libraries, different timestamps' => [
(new AttachedAssets())
->setAlreadyLoadedLibraries([])
->setLibraries([
'core/drupal',
])
->setSettings([
'currentTime' => $time,
]),
(new AttachedAssets())
->setAlreadyLoadedLibraries([])
->setLibraries([
'core/drupal',
])
->setSettings([
'currentTime' => $time + 100,
]),
1,
],
'different libraries, same timestamps' => [
(new AttachedAssets())
->setAlreadyLoadedLibraries([])
->setLibraries([
'core/drupal',
])
->setSettings([
'currenttime' => $time,
]),
(new AttachedAssets())
->setAlreadyLoadedLibraries([])
->setLibraries([
'core/drupal',
'core/jquery',
])
->setSettings([
'currentTime' => $time,
]),
2,
],
];
}
}
if (!defined('CSS_AGGREGATE_DEFAULT')) {
define('CSS_AGGREGATE_DEFAULT', 0);
}
if (!defined('JS_DEFAULT')) {
define('JS_DEFAULT', 0);
}
class TestMemoryBackend extends MemoryBackend {
public function getAllCids() {
return array_keys($this->cache);
}
}