b60841a71ecd12219f8593bb103c6a0b5f84a198
[yaffs-website] / web / modules / contrib / blazy / tests / src / Unit / BlazyManagerUnitTest.php
1 <?php
2
3 namespace Drupal\Tests\blazy\Unit;
4
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\Tests\blazy\Traits\BlazyUnitTestTrait;
7 use Drupal\Tests\blazy\Traits\BlazyManagerUnitTestTrait;
8
9 /**
10  * @coversDefaultClass \Drupal\blazy\BlazyManager
11  *
12  * @group blazy
13  */
14 class BlazyManagerUnitTest extends UnitTestCase {
15
16   use BlazyUnitTestTrait;
17   use BlazyManagerUnitTestTrait;
18
19   /**
20    * {@inheritdoc}
21    */
22   protected function setUp() {
23     parent::setUp();
24
25     $this->setUpUnitServices();
26     $this->setUpUnitContainer();
27     $this->setUpUnitImages();
28
29     $this->blazyManager->setLightboxes('blazy_test');
30   }
31
32   /**
33    * Tests cases for various methods.
34    *
35    * @covers ::getEntityTypeManager
36    * @covers ::getModuleHandler
37    * @covers ::getRenderer
38    * @covers ::getCache
39    * @covers ::getConfigFactory
40    */
41   public function testBlazyManagerServiceInstances() {
42     $this->assertInstanceOf('\Drupal\Core\Entity\EntityTypeManagerInterface', $this->blazyManager->getEntityTypeManager());
43     $this->assertInstanceOf('\Drupal\Core\Extension\ModuleHandlerInterface', $this->blazyManager->getModuleHandler());
44     $this->assertInstanceOf('\Drupal\Core\Render\RendererInterface', $this->blazyManager->getRenderer());
45     $this->assertInstanceOf('\Drupal\Core\Config\ConfigFactoryInterface', $this->blazyManager->getConfigFactory());
46     $this->assertInstanceOf('\Drupal\Core\Cache\CacheBackendInterface', $this->blazyManager->getCache());
47   }
48
49   /**
50    * Tests cases for config.
51    *
52    * @covers ::configLoad
53    */
54   public function testConfigLoad() {
55     $blazy = $this->blazyManager->configLoad('blazy');
56     $this->assertArrayHasKey('loadInvisible', $blazy);
57
58     $admin_css = $this->blazyManager->configLoad('admin_css', 'blazy.settings');
59     $this->assertTrue($admin_css, 'Blazy admin CSS is enabled by default.');
60
61     $responsive_image = $this->blazyManager->configLoad('responsive_image');
62     $this->assertTrue($responsive_image, 'Responsive image was disabled by default, yet enabled now.');
63   }
64
65   /**
66    * Tests cases for config.
67    *
68    * @covers ::entityLoad
69    * @covers ::entityLoadMultiple
70    */
71   public function testEntityLoadImageStyle() {
72     $styles = $this->setUpImageStyle();
73
74     $ids = array_keys($styles);
75     $multiple = $this->blazyManager->entityLoadMultiple('image_style', $ids);
76     $this->assertArrayHasKey('large', $multiple);
77
78     $expected = $this->blazyManager->entityLoad('large', 'image_style');
79     $this->assertEquals($expected, $multiple['large']);
80   }
81
82   /**
83    * Tests cases for config.
84    *
85    * @covers ::entityLoad
86    * @covers ::entityLoadMultiple
87    */
88   public function testEntityLoadResponsiveImageStyle() {
89     $styles = $this->setUpResponsiveImageStyle();
90
91     $ids = array_keys($styles);
92     $multiple = $this->blazyManager->entityLoadMultiple('responsive_image_style', $ids);
93     $this->assertArrayHasKey('blazy_picture_test', $multiple);
94
95     $expected = $this->blazyManager->entityLoad('blazy_picture_test', 'responsive_image_style');
96     $this->assertEquals($expected, $multiple['blazy_picture_test']);
97   }
98
99   /**
100    * Test \Drupal\blazy\BlazyManager::cleanUpBreakpoints().
101    *
102    * @covers ::cleanUpBreakpoints
103    * @dataProvider providerTestCleanUpBreakpoints
104    */
105   public function testCleanUpBreakpoints($breakpoints, $expected_breakpoints, $blazy, $expected_blazy) {
106     $settings['blazy'] = $blazy;
107     $settings['breakpoints'] = $breakpoints;
108
109     $this->blazyManager->cleanUpBreakpoints($settings);
110     $this->assertEquals($expected_breakpoints, $settings['breakpoints']);
111
112     // Verify that Blazy is activated by breakpoints.
113     $this->assertEquals($expected_blazy, $settings['blazy']);
114   }
115
116   /**
117    * Provider for ::testCleanUpBreakpoints().
118    */
119   public function providerTestCleanUpBreakpoints() {
120     return [
121       'empty' => [
122         [],
123         [],
124         FALSE,
125         FALSE,
126       ],
127       'not so empty' => [
128         $this->getEmptyBreakpoints(),
129         [],
130         FALSE,
131         FALSE,
132       ],
133       'mixed empty' => [
134         $this->getDataBreakpoints(),
135         $this->getDataBreakpoints(TRUE),
136         FALSE,
137         TRUE,
138       ],
139       'mixed empty blazy enabled first' => [
140         $this->getDataBreakpoints(),
141         $this->getDataBreakpoints(TRUE),
142         FALSE,
143         TRUE,
144       ],
145     ];
146   }
147
148   /**
149    * Tests for \Drupal\blazy\BlazyManager::preRenderImage().
150    *
151    * @covers ::getImage
152    * @covers ::preRenderImage
153    * @dataProvider providerTestPreRenderImage
154    */
155   public function testPreRenderImage($item, $uri, $content, $expected_image, $expected_render) {
156     $build = [];
157
158     $build['item'] = $item ? $this->testItem : [];
159     $build['content'] = $content;
160     $build['settings']['uri'] = $uri;
161
162     if ($item) {
163       $build['item']->_attributes['data-blazy-test'] = TRUE;
164     }
165
166     $image = $this->blazyManager->getImage($build);
167
168     $build_image['#build']['settings'] = array_merge($this->getCacheMetaData(), $build['settings']);
169     $build_image['#build']['item'] = $build['item'];
170
171     $pre_render = $this->blazyManager->preRenderImage($build_image);
172
173     $check_image = !$expected_image ? empty($image) : !empty($image);
174     $this->assertTrue($check_image);
175
176     $check_pre_render = !$expected_render ? TRUE : !empty($pre_render);
177     $this->assertTrue($check_pre_render);
178   }
179
180   /**
181    * Provide test cases for ::testPreRenderImage().
182    *
183    * @return array
184    *   An array of tested data.
185    */
186   public function providerTestPreRenderImage() {
187     $data[] = [
188       FALSE,
189       '',
190       '',
191       FALSE,
192       FALSE,
193     ];
194     $data[] = [
195       TRUE,
196       '',
197       '',
198       TRUE,
199       TRUE,
200     ];
201     $data[] = [
202       TRUE,
203       'core/misc/druplicon.png',
204       '',
205       TRUE,
206       TRUE,
207     ];
208     $data[] = [
209       TRUE,
210       'core/misc/druplicon.png',
211       '<iframe src="//www.youtube.com/watch?v=E03HFA923kw" class="b-lazy"></iframe>',
212       TRUE,
213       FALSE,
214     ];
215
216     return $data;
217   }
218
219   /**
220    * Tests cases for attachments.
221    *
222    * @covers ::attach
223    * @depends testConfigLoad
224    */
225   public function testAttach() {
226     $attach = [
227       'blazy'        => TRUE,
228       'grid'         => 0,
229       'media'        => TRUE,
230       'media_switch' => 'media',
231       'ratio'        => 'fluid',
232       'style'        => 'column',
233     ];
234
235     $attachments = $this->blazyManager->attach($attach);
236
237     $this->assertArrayHasKey('library', $attachments);
238     $this->assertArrayHasKey('blazy', $attachments['drupalSettings']);
239
240     $this->assertContains('blazy/media', $attachments['library']);
241     $this->assertContains('blazy/ratio', $attachments['library']);
242   }
243
244   /**
245    * Tests cases for lightboxes.
246    *
247    * @covers ::getLightboxes
248    * @covers ::setLightboxes
249    */
250   public function testGetLightboxes() {
251     $lightboxes = $this->blazyManager->getLightboxes();
252
253     $this->assertNotContains('nixbox', $lightboxes);
254   }
255
256 }
257
258 namespace Drupal\blazy;
259
260 if (!function_exists('blazy_test_theme')) {
261
262   /**
263    * Dummy function.
264    */
265   function blazy_test_theme() {
266     // Empty block to satisfy coder.
267   }
268
269 }
270
271 if (!function_exists('colorbox_theme')) {
272
273   /**
274    * Dummy function.
275    */
276   function colorbox_theme() {
277     // Empty block to satisfy coder.
278   }
279
280 }
281
282 if (!function_exists('photobox_theme')) {
283
284   /**
285    * Dummy function.
286    */
287   function photobox_theme() {
288     // Empty block to satisfy coder.
289   }
290
291 }