Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / modules / contrib / blazy / tests / src / Kernel / BlazyManagerTest.php
1 <?php
2
3 namespace Drupal\Tests\blazy\Kernel;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\entity_test\Entity\EntityTest;
7 use Drupal\blazy\Blazy;
8 use Drupal\blazy\Dejavu\BlazyDefault;
9
10 /**
11  * Tests the Blazy manager methods.
12  *
13  * @coversDefaultClass \Drupal\blazy\BlazyManager
14  *
15  * @group blazy
16  */
17 class BlazyManagerTest extends BlazyKernelTestBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   protected function setUp() {
23     parent::setUp();
24
25     $bundle = $this->bundle;
26
27     $settings['fields']['field_text_multiple'] = 'text';
28
29     $this->setUpContentTypeTest($bundle, $settings);
30     $this->setUpContentWithItems($bundle);
31     $this->setUpRealImage();
32   }
33
34   /**
35    * Tests BlazyManager image.
36    *
37    * @param array $settings
38    *   The settings being tested.
39    * @param string $expected_responsive_image_style_id
40    *   The responsive image style ID.
41    *
42    * @covers ::preRenderImage
43    * @covers ::getResponsiveImageCacheTags
44    * @covers \Drupal\blazy\BlazyLightbox::build
45    * @covers \Drupal\blazy\BlazyLightbox::buildCaptions
46    * @dataProvider providerTestPreRenderImage
47    */
48   public function testPreRenderImage(array $settings = [], $expected_responsive_image_style_id = '') {
49     $build             = $this->data;
50     $settings['count'] = $this->maxItems;
51     $settings['uri']   = $this->uri;
52     $build['settings'] = array_merge($build['settings'], $settings);
53
54     $element = $this->doPreRenderImage($build);
55
56     if ($settings['media_switch'] == 'content') {
57       $this->assertEquals($settings['content_url'], $element['#url']);
58       $this->assertArrayHasKey('#url', $element);
59     }
60     else {
61       $this->assertArrayHasKey('data-' . $settings['media_switch'] . '-trigger', $element['#url_attributes']);
62       $this->assertArrayHasKey('#url', $element);
63     }
64
65     $this->assertEquals($expected_responsive_image_style_id, $element['#settings']['responsive_image_style_id']);
66   }
67
68   /**
69    * Provide test cases for ::testPreRenderImage().
70    *
71    * @return array
72    *   An array of tested data.
73    */
74   public function providerTestPreRenderImage() {
75     $data[] = [
76       [
77         'content_url'  => 'node/1',
78         'media_switch' => 'content',
79       ],
80       '',
81     ];
82     $data[] = [
83       [
84         'lightbox'               => TRUE,
85         'media_switch'           => 'photobox',
86         'resimage'               => TRUE,
87         'responsive_image_style' => 'blazy_responsive_test',
88       ],
89       'blazy_responsive_test',
90     ];
91     $data[] = [
92       [
93         'box_style'          => 'blazy_crop',
94         'box_media_style'    => 'large',
95         'box_caption'        => 'custom',
96         'box_caption_custom' => '[node:field_text_multiple]',
97         'embed_url'          => '//www.youtube.com/watch?v=E03HFA923kw',
98         'lightbox'           => TRUE,
99         'media_switch'       => 'blazy_test',
100         'scheme'             => 'youtube',
101         'type'               => 'video',
102       ],
103       '',
104     ];
105
106     return $data;
107   }
108
109   /**
110    * Tests the entity view builder.
111    *
112    * @param string $entity
113    *   The tested entity.
114    * @param string $fallback
115    *   The fallback text.
116    * @param string $message
117    *   The message text.
118    * @param bool $expected
119    *   The expected output.
120    *
121    * @covers ::getEntityView
122    * @dataProvider providerTestGetEntityView
123    */
124   public function testGetEntityView($entity, $fallback, $message, $expected) {
125     if ($entity == 'entity') {
126       $entity_test = EntityTest::create([
127         'name' => $this->randomMachineName(),
128       ]);
129
130       $entity_test->save();
131
132       $entity = $entity_test;
133     }
134     elseif ($entity == 'node') {
135       $entity = empty($this->entity) ? $this->setUpContentWithItems($this->bundle) : $this->entity;
136     }
137     elseif ($entity == 'responsive_image') {
138       $entity = $this->blazyManager->entityLoad('blazy_responsive_test', 'responsive_image_style');
139     }
140     elseif ($entity == 'image') {
141       $entity = $this->testItem;
142     }
143
144     $result = $this->blazyManager->getEntityView($entity, [], $fallback);
145     $this->assertSame($expected, !empty($result), $message);
146   }
147
148   /**
149    * Provide test cases for ::testGetEntityView().
150    *
151    * @return array
152    *   An array of tested data.
153    */
154   public function providerTestGetEntityView() {
155     return [
156       'Entity test' => [
157         'entity',
158         '',
159         'Entity test has no entity_test_view(), yet it has view builder.',
160         TRUE,
161       ],
162       'Node' => [
163         'node',
164         '',
165         'Node has node_view() taking precedence over view builder.',
166         TRUE,
167       ],
168       'Responsive image' => [
169         'responsive_image',
170         'This is some fallback text.',
171         'Responsive image has no view builder. Fallback to text.',
172         TRUE,
173       ],
174       'Image' => [
175         'image',
176         '',
177         'Image is not an instance of EntityInterface, returns false.',
178         FALSE,
179       ],
180     ];
181   }
182
183   /**
184    * Tests building Blazy attributes.
185    *
186    * @param array $settings
187    *   The settings being tested.
188    * @param bool $uri
189    *   Whether to provide image URI, or not.
190    * @param object $item
191    *   Whether to provide image item, or not.
192    * @param bool $iframe
193    *   Whether to expect an iframe, or not.
194    * @param mixed|bool|int $expected
195    *   The expected output.
196    *
197    * @covers \Drupal\blazy\Blazy::buildAttributes
198    * @covers \Drupal\blazy\Blazy::buildBreakpointAttributes
199    * @covers \Drupal\blazy\Blazy::buildUrl
200    * @covers \Drupal\blazy\Dejavu\BlazyDefault::entitySettings
201    * @dataProvider providerBuildAttributes
202    */
203   public function testBuildAttributes(array $settings, $uri, $item, $iframe, $expected) {
204     $content   = [];
205     $variables = ['attributes' => []];
206     $settings  = array_merge($this->getFormatterSettings(), $settings);
207
208     $settings['blazy']           = TRUE;
209     $settings['lazy']            = 'blazy';
210     $settings['image_style']     = 'blazy_crop';
211     $settings['thumbnail_style'] = 'thumbnail';
212     $settings['uri']             = $uri ? $this->uri : '';
213
214     if (!empty($settings['embed_url'])) {
215       $settings = array_merge(BlazyDefault::entitySettings(), $settings);
216     }
217
218     $variables['element']['#item'] = $item ? $this->testItem : NULL;
219     $variables['element']['#settings'] = $settings;
220
221     Blazy::buildAttributes($variables);
222
223     $image  = $expected == TRUE ? !empty($variables['image']) : empty($variables['image']);
224     $iframe = $iframe == TRUE ? !empty($variables['iframe_attributes']) : empty($variables['iframe_attributes']);
225
226     $this->assertTrue($image);
227     $this->assertTrue($iframe);
228   }
229
230   /**
231    * Provider for ::testBuildAttributes.
232    */
233   public function providerBuildAttributes() {
234     $breakpoints = $this->getDataBreakpoints();
235     $breakpoints_cleaned = $this->getDataBreakpoints(TRUE);
236
237     $data[] = [
238       [
239         'background' => FALSE,
240         'breakpoints' => [],
241       ],
242       FALSE,
243       NULL,
244       FALSE,
245       FALSE,
246     ];
247     $data[] = [
248       [
249         'background' => FALSE,
250         'breakpoints' => [],
251       ],
252       FALSE,
253       TRUE,
254       FALSE,
255       TRUE,
256     ];
257     $data[] = [
258       [
259         'background' => TRUE,
260         'breakpoints' => $breakpoints,
261         'ratio' => 'fluid',
262         'sizes' => '100w',
263         'width' => 640,
264         'height' => 360,
265       ],
266       TRUE,
267       TRUE,
268       FALSE,
269       FALSE,
270     ];
271
272     return $data;
273   }
274
275   /**
276    * Tests responsive image integration.
277    *
278    * @param string $responsive_image_style_id
279    *   The responsive_image_style_id.
280    * @param bool $old
281    *   Whether to use Drupal core 8.x-2, or later 8.x-3.
282    * @param bool $expected
283    *   The expected output_image_tag.
284    *
285    * @dataProvider providerResponsiveImage
286    */
287   public function testPreprocessResponsiveImage($responsive_image_style_id, $old, $expected) {
288     $variables = [
289       'item' => $this->testItem,
290       'uri' => $this->uri,
291       'responsive_image_style_id' => $responsive_image_style_id,
292     ];
293
294     template_preprocess_responsive_image($variables);
295
296     if ($old) {
297       $variables['img_element']['#srcset'][0]['uri'] = $this->uri;
298     }
299     else {
300       $variables['img_element']['#uri'] = $this->uri;
301     }
302
303     Blazy::preprocessResponsiveImage($variables);
304
305     $this->assertEquals($expected, $variables['output_image_tag']);
306   }
307
308   /**
309    * Provider for ::testPreprocessResponsiveImage.
310    */
311   public function providerResponsiveImage() {
312     return [
313       'Responsive image with picture 8.x-2' => [
314         'blazy_picture_test',
315         TRUE,
316         FALSE,
317       ],
318       'Responsive image with picture 8.x-3' => [
319         'blazy_picture_test',
320         FALSE,
321         FALSE,
322       ],
323       'Responsive image without picture 8.x-3' => [
324         'blazy_responsive_test',
325         FALSE,
326         TRUE,
327       ],
328     ];
329   }
330
331   /**
332    * Tests isCrop.
333    *
334    * @covers ::isCrop
335    * @dataProvider providerIsCrop
336    */
337   public function testIsCrop($image_style_id, $expected) {
338     $image_style = $this->blazyManager->entityLoad($image_style_id, 'image_style');
339     $is_cropped = $this->blazyManager->isCrop($image_style);
340
341     $this->assertEquals($expected, $is_cropped);
342   }
343
344   /**
345    * Provider for ::testIsCrop.
346    */
347   public function providerIsCrop() {
348     return [
349       'Cropped image style' => [
350         'blazy_crop',
351         TRUE,
352       ],
353       'Non-cropped image style' => [
354         'large',
355         FALSE,
356       ],
357     ];
358   }
359
360   /**
361    * Tests cases for various methods.
362    *
363    * @covers ::attach
364    * @covers ::buildDataBlazy
365    * @covers ::getLightboxes
366    * @covers ::setLightboxes
367    * @covers ::buildSkins
368    * @covers ::getCache
369    *
370    * @todo: Move some to unit tests.
371    */
372   public function testBlazyManagerMethods() {
373     // Tests Blazy attachments.
374     $attach = ['blazy' => TRUE, 'media_switch' => 'blazy_test'];
375
376     $attachments = $this->blazyManager->attach($attach);
377     $this->assertArrayHasKey('blazy', $attachments['drupalSettings']);
378
379     // Tests Blazy [data-blazy] attributes.
380     $build    = $this->data;
381     $settings = &$build['settings'];
382     $item     = $build['item'];
383
384     $settings['item']        = $item;
385     $settings['uri']         = $this->uri;
386     $settings['blazy_data']  = [];
387     $settings['background']  = TRUE;
388     $settings['breakpoints'] = $this->getDataBreakpoints();
389
390     // Ensure Blazy can be activated by breakpoints.
391     $this->blazyManager->buildDataBlazy($settings, $build);
392     $this->assertNotEmpty($settings['blazy']);
393
394     // Tests Blazy lightboxes.
395     $this->blazyManager->setLightboxes('blazy_test');
396     $lightboxes = $this->blazyManager->getLightboxes();
397
398     $this->assertFalse(in_array('nixbox', $lightboxes));
399     $this->assertTrue(in_array('blazy_test', $lightboxes));
400
401     // Tests for skins.
402     // Tests skins with a single expected method BlazySkinTest::skins().
403     $skins = $this->blazyManager->buildSkins('blazy_test', '\Drupal\blazy_test\BlazySkinTest');
404
405     // Verify we have cached skins.
406     $cid = 'blazy_test:skins';
407     $cached_skins = $this->blazyManager->getCache()->get($cid);
408     $this->assertEquals($cid, $cached_skins->cid);
409     $this->assertEquals($skins, $cached_skins->data);
410
411     // Verify multiple skin methods are respected.
412     Cache::invalidateTags([$cid]);
413     drupal_flush_all_caches();
414     $this->assertFalse($this->blazyManager->getCache()->get($cid));
415
416     $skins = $this->blazyManager->buildSkins('blazy_test', '\Drupal\blazy_test\BlazySkinTest', ['skins', 'features']);
417
418     $this->assertArrayHasKey('features', $skins);
419
420     $cached_skins = $this->blazyManager->getCache()->get($cid);
421     $this->assertEquals($skins, $cached_skins->data);
422   }
423
424 }