Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / modules / contrib / blazy / tests / src / Unit / BlazyUnitTest.php
1 <?php
2
3 namespace Drupal\Tests\blazy\Unit;
4
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\blazy\Blazy;
7 use Drupal\blazy\Dejavu\BlazyDefault;
8 use Drupal\Tests\blazy\Traits\BlazyUnitTestTrait;
9 use Drupal\Tests\blazy\Traits\BlazyManagerUnitTestTrait;
10
11 /**
12  * @coversDefaultClass \Drupal\blazy\Blazy
13  *
14  * @group blazy
15  */
16 class BlazyUnitTest extends UnitTestCase {
17
18   use BlazyUnitTestTrait;
19   use BlazyManagerUnitTestTrait;
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function setUp() {
25     parent::setUp();
26
27     $this->setUpVariables();
28     $this->setUpUnitServices();
29     $this->setUpUnitContainer();
30     $this->setUpMockImage();
31   }
32
33   /**
34    * Test \Drupal\blazy\Blazy\widthFromDescriptors.
35    *
36    * @param string $data
37    *   The input data which can be string, or integer.
38    * @param mixed|bool|int $expected
39    *   The expected output.
40    *
41    * @covers ::widthFromDescriptors
42    * @dataProvider providerTestWidthFromDescriptors
43    */
44   public function testWidthFromDescriptors($data, $expected) {
45     $result = Blazy::widthFromDescriptors($data);
46     $this->assertSame($result, $expected);
47   }
48
49   /**
50    * Provide test cases for ::testWidthFromDescriptors().
51    */
52   public function providerTestWidthFromDescriptors() {
53     return [
54       [1024, 1024],
55       ['1024', 1024],
56       ['769w', 769],
57       ['640w 2x', 640],
58       ['2x 640w', 640],
59       ['xYz123', FALSE],
60     ];
61   }
62
63   /**
64    * Tests \Drupal\blazy\Blazy\buildIframeAttributes.
65    *
66    * @param array $data
67    *   The input data which can be string, or integer.
68    * @param mixed|bool|int $expected
69    *   The expected output.
70    *
71    * @covers ::buildIframeAttributes
72    * @covers \Drupal\blazy\Dejavu\BlazyDefault::entitySettings
73    * @dataProvider providerTestBuildIframeAttributes
74    */
75   public function testBuildIframeAttributes(array $data, $expected) {
76     $variables = ['attributes' => [], 'image' => []];
77     $settings  = BlazyDefault::entitySettings();
78
79     $settings['embed_url'] = '//www.youtube.com/watch?v=E03HFA923kw';
80     $settings['scheme']    = 'youtube';
81     $settings['type']      = 'video';
82
83     $this->assertArrayHasKey('iframe_lazy', $settings);
84
85     $variables['settings'] = array_merge($settings, $data);
86     Blazy::buildIframeAttributes($variables);
87
88     $this->assertNotEmpty($variables[$expected]);
89   }
90
91   /**
92    * Provide test cases for ::testBuildIframeAttributes().
93    */
94   public function providerTestBuildIframeAttributes() {
95     return [
96       [
97         [
98           'media_switch' => 'media',
99           'ratio' => 'fluid',
100         ],
101         'iframe_attributes',
102       ],
103       [
104         [
105           'media_switch' => '',
106           'ratio' => '',
107           'width' => 640,
108           'height' => 360,
109         ],
110         'iframe_attributes',
111       ],
112     ];
113   }
114
115   /**
116    * Tests building Blazy attributes.
117    *
118    * @param array $settings
119    *   The settings being tested.
120    * @param object $item
121    *   Whether to provide image item, or not.
122    * @param bool $expected_image
123    *   Whether to expect an image, or not.
124    * @param bool $expected_iframe
125    *   Whether to expect an iframe, or not.
126    *
127    * @covers \Drupal\blazy\Blazy::buildAttributes
128    * @covers \Drupal\blazy\Blazy::buildBreakpointAttributes
129    * @covers \Drupal\blazy\Blazy::buildUrl
130    * @covers \Drupal\blazy\Dejavu\BlazyDefault::entitySettings
131    * @dataProvider providerBuildAttributes
132    */
133   public function testBuildAttributes(array $settings, $item, $expected_image, $expected_iframe) {
134     $content   = [];
135     $variables = ['attributes' => []];
136     $build     = $this->data;
137     $settings  = array_merge($build['settings'], $settings);
138
139     $settings['breakpoints']     = [];
140     $settings['blazy']           = TRUE;
141     $settings['lazy']            = 'blazy';
142     $settings['image_style']     = '';
143     $settings['thumbnail_style'] = '';
144
145     if (!empty($settings['embed_url'])) {
146       $settings = array_merge(BlazyDefault::entitySettings(), $settings);
147     }
148
149     $variables['element']['#item'] = $item == TRUE ? $this->testItem : NULL;
150     $variables['element']['#settings'] = $settings;
151
152     Blazy::buildAttributes($variables);
153
154     $image = $expected_image == TRUE ? !empty($variables['image']) : empty($variables['image']);
155     $iframe = $expected_iframe == TRUE ? !empty($variables['iframe_attributes']) : empty($variables['iframe_attributes']);
156
157     $this->assertTrue($image);
158     $this->assertTrue($iframe);
159
160     $this->assertEquals($settings['blazy'], $variables['settings']['blazy']);
161   }
162
163   /**
164    * Provider for ::testBuildAttributes.
165    */
166   public function providerBuildAttributes() {
167     $breakpoints = $this->getDataBreakpoints();
168     $uri = 'public://example.jpg';
169
170     $data[] = [
171       [
172         'background' => FALSE,
173         'uri' => '',
174       ],
175       TRUE,
176       FALSE,
177       FALSE,
178     ];
179     $data[] = [
180       [
181         'background' => FALSE,
182         'responsive_image_style_id' => 'blazy_responsive_test',
183         'uri' => $uri,
184       ],
185       TRUE,
186       TRUE,
187       FALSE,
188     ];
189     $data[] = [
190       [
191         'background' => TRUE,
192         'uri' => $uri,
193       ],
194       TRUE,
195       FALSE,
196       FALSE,
197     ];
198     $data[] = [
199       [
200         'background' => FALSE,
201         'ratio' => 'fluid',
202         'sizes' => '100w',
203         'width' => 640,
204         'height' => 360,
205         'uri' => $uri,
206       ],
207       TRUE,
208       TRUE,
209       FALSE,
210     ];
211     $data[] = [
212       [
213         'background' => FALSE,
214         'embed_url' => '//www.youtube.com/watch?v=E03HFA923kw',
215         'media_switch' => 'media',
216         'ratio' => 'fluid',
217         'sizes' => '100w',
218         'scheme' => 'youtube',
219         'type' => 'video',
220         'uri' => $uri,
221       ],
222       TRUE,
223       TRUE,
224       TRUE,
225     ];
226
227     return $data;
228   }
229
230   /**
231    * Tests BlazyManager image with lightbox support.
232    *
233    * This is here as we need file_create_url() for both Blazy and its lightbox.
234    *
235    * @param array $settings
236    *   The settings being tested.
237    *
238    * @covers \Drupal\blazy\BlazyManager::preRenderImage
239    * @covers \Drupal\blazy\BlazyLightbox::build
240    * @covers \Drupal\blazy\BlazyLightbox::buildCaptions
241    * @dataProvider providerTestPreRenderImageLightbox
242    */
243   public function testPreRenderImageLightbox(array $settings = []) {
244     $build                       = $this->data;
245     $settings['count']           = $this->maxItems;
246     $settings['uri']             = $this->uri;
247     $settings['box_style']       = '';
248     $settings['box_media_style'] = '';
249     $build['settings']           = array_merge($build['settings'], $settings);
250
251     foreach (['caption', 'media', 'wrapper'] as $key) {
252       $build['settings'][$key . '_attributes']['class'][] = $key . '-test';
253     }
254
255     $element = $this->doPreRenderImage($build);
256
257     if ($settings['media_switch'] == 'content') {
258       $this->assertEquals($settings['content_url'], $element['#url']);
259       $this->assertArrayHasKey('#url', $element);
260     }
261     else {
262       $this->assertArrayHasKey('data-' . $settings['media_switch'] . '-trigger', $element['#url_attributes']);
263       $this->assertArrayHasKey('#url', $element);
264     }
265   }
266
267   /**
268    * Provide test cases for ::testPreRenderImageLightbox().
269    *
270    * @return array
271    *   An array of tested data.
272    */
273   public function providerTestPreRenderImageLightbox() {
274     $data[] = [
275       [
276         'box_caption' => '',
277         'content_url' => 'node/1',
278         'dimension' => '',
279         'lightbox' => FALSE,
280         'media_switch' => 'content',
281         'type' => 'image',
282       ],
283     ];
284     $data[] = [
285       [
286         'box_caption' => 'auto',
287         'lightbox' => TRUE,
288         'media_switch' => 'colorbox',
289         'type' => 'image',
290       ],
291     ];
292     $data[] = [
293       [
294         'box_caption' => 'alt',
295         'lightbox' => TRUE,
296         'media_switch' => 'colorbox',
297         'type' => 'image',
298       ],
299     ];
300     $data[] = [
301       [
302         'box_caption' => 'title',
303         'lightbox' => TRUE,
304         'media_switch' => 'photobox',
305         'type' => 'image',
306       ],
307     ];
308     $data[] = [
309       [
310         'box_caption' => 'alt_title',
311         'lightbox' => TRUE,
312         'media_switch' => 'colorbox',
313         'type' => 'image',
314       ],
315     ];
316     $data[] = [
317       [
318         'box_caption' => 'title_alt',
319         'lightbox' => TRUE,
320         'media_switch' => 'photobox',
321         'type' => 'image',
322       ],
323     ];
324     $data[] = [
325       [
326         'box_caption' => 'entity_title',
327         'lightbox' => TRUE,
328         'media_switch' => 'photobox',
329         'type' => 'image',
330       ],
331     ];
332     $data[] = [
333       [
334         'box_caption' => 'custom',
335         'box_caption_custom' => '[node:field_text_multiple]',
336         'dimension' => '640x360',
337         'embed_url' => '//www.youtube.com/watch?v=E03HFA923kw',
338         'lightbox' => TRUE,
339         'media_switch' => 'photobox',
340         'scheme' => 'youtube',
341         'type' => 'video',
342       ],
343     ];
344
345     return $data;
346   }
347
348 }
349
350 namespace Drupal\blazy;
351
352 if (!function_exists('file_create_url')) {
353
354   /**
355    * Dummy function.
356    */
357   function file_create_url() {
358     // Empty block to satisfy coder.
359   }
360
361 }