Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Asset / LibraryDiscoveryCollectorTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Asset;
4
5 use Drupal\Core\Asset\LibraryDiscoveryCollector;
6 use Drupal\Core\Cache\Cache;
7 use Drupal\Tests\UnitTestCase;
8
9 /**
10  * @coversDefaultClass \Drupal\Core\Asset\LibraryDiscoveryCollector
11  * @group Asset
12  */
13 class LibraryDiscoveryCollectorTest extends UnitTestCase {
14
15   /**
16    * The mock cache backend.
17    *
18    * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
19    */
20   protected $cache;
21
22   /**
23    * The mock lock backend.
24    *
25    * @var \Drupal\Core\Lock\LockBackendInterface|\PHPUnit_Framework_MockObject_MockObject
26    */
27   protected $lock;
28
29   /**
30    * The mock library discovery parser.
31    *
32    * @var \Drupal\Core\Asset\LibraryDiscoveryParser|\PHPUnit_Framework_MockObject_MockObject
33    */
34   protected $libraryDiscoveryParser;
35
36   /**
37    * The library discovery collector under test.
38    *
39    * @var \Drupal\Core\Asset\LibraryDiscoveryCollector
40    */
41   protected $libraryDiscoveryCollector;
42
43   /**
44    * The mocked theme manager.
45    *
46    * @var \Drupal\Core\Theme\ThemeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
47    */
48   protected $themeManager;
49
50   /**
51    * Test library data.
52    *
53    * @var array
54    */
55   protected $libraryData = [
56     'test_1' => [
57       'js' => [],
58       'css' => [],
59     ],
60     'test_2' => [
61       'js' => [],
62       'css' => [],
63     ],
64   ];
65
66   protected $activeTheme;
67
68   /**
69    * {@inheritdoc}
70    */
71   protected function setUp() {
72     $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
73     $this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface');
74     $this->themeManager = $this->getMockBuilder('Drupal\Core\Theme\ThemeManagerInterface')
75       ->disableOriginalConstructor()
76       ->getMock();
77     $this->libraryDiscoveryParser = $this->getMockBuilder('Drupal\Core\Asset\LibraryDiscoveryParser')
78       ->disableOriginalConstructor()
79       ->getMock();
80
81   }
82
83   /**
84    * Tests the resolve cache miss function.
85    *
86    * @covers ::resolveCacheMiss
87    */
88   public function testResolveCacheMiss() {
89     $this->activeTheme = $this->getMockBuilder('Drupal\Core\Theme\ActiveTheme')
90       ->disableOriginalConstructor()
91       ->getMock();
92     $this->themeManager->expects($this->exactly(3))
93       ->method('getActiveTheme')
94       ->willReturn($this->activeTheme);
95     $this->activeTheme->expects($this->once())
96       ->method('getName')
97       ->willReturn('kitten_theme');
98     $this->libraryDiscoveryCollector = new LibraryDiscoveryCollector($this->cache, $this->lock, $this->libraryDiscoveryParser, $this->themeManager);
99
100     $this->libraryDiscoveryParser->expects($this->once())
101       ->method('buildByExtension')
102       ->with('test')
103       ->willReturn($this->libraryData);
104
105     $this->assertSame($this->libraryData, $this->libraryDiscoveryCollector->get('test'));
106     $this->assertSame($this->libraryData, $this->libraryDiscoveryCollector->get('test'));
107   }
108
109   /**
110    * Tests the destruct method.
111    *
112    * @covers ::destruct
113    */
114   public function testDestruct() {
115     $this->activeTheme = $this->getMockBuilder('Drupal\Core\Theme\ActiveTheme')
116       ->disableOriginalConstructor()
117       ->getMock();
118     $this->themeManager->expects($this->exactly(3))
119       ->method('getActiveTheme')
120       ->willReturn($this->activeTheme);
121     $this->activeTheme->expects($this->once())
122       ->method('getName')
123       ->willReturn('kitten_theme');
124     $this->libraryDiscoveryCollector = new LibraryDiscoveryCollector($this->cache, $this->lock, $this->libraryDiscoveryParser, $this->themeManager);
125
126     $this->libraryDiscoveryParser->expects($this->once())
127       ->method('buildByExtension')
128       ->with('test')
129       ->willReturn($this->libraryData);
130
131     $lock_key = 'library_info:kitten_theme:Drupal\Core\Cache\CacheCollector';
132
133     $this->lock->expects($this->once())
134       ->method('acquire')
135       ->with($lock_key)
136       ->will($this->returnValue(TRUE));
137     $this->cache->expects($this->exactly(2))
138       ->method('get')
139       ->with('library_info:kitten_theme')
140       ->willReturn(FALSE);
141     $this->cache->expects($this->once())
142       ->method('set')
143       ->with('library_info:kitten_theme', ['test' => $this->libraryData], Cache::PERMANENT, ['library_info']);
144     $this->lock->expects($this->once())
145       ->method('release')
146       ->with($lock_key);
147
148     // This should get data and persist the key.
149     $this->libraryDiscoveryCollector->get('test');
150     $this->libraryDiscoveryCollector->destruct();
151   }
152
153 }