3 namespace Drupal\Tests\blazy\Unit;
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;
12 * @coversDefaultClass \Drupal\blazy\Blazy
16 class BlazyUnitTest extends UnitTestCase {
18 use BlazyUnitTestTrait;
19 use BlazyManagerUnitTestTrait;
24 protected function setUp() {
27 $this->setUpVariables();
28 $this->setUpUnitServices();
29 $this->setUpUnitContainer();
30 $this->setUpMockImage();
34 * Test \Drupal\blazy\Blazy\widthFromDescriptors.
37 * The input data which can be string, or integer.
38 * @param mixed|bool|int $expected
39 * The expected output.
41 * @covers ::widthFromDescriptors
42 * @dataProvider providerTestWidthFromDescriptors
44 public function testWidthFromDescriptors($data, $expected) {
45 $result = Blazy::widthFromDescriptors($data);
46 $this->assertSame($result, $expected);
50 * Provide test cases for ::testWidthFromDescriptors().
52 public function providerTestWidthFromDescriptors() {
64 * Tests \Drupal\blazy\Blazy\buildIframeAttributes.
67 * The input data which can be string, or integer.
68 * @param mixed|bool|int $expected
69 * The expected output.
71 * @covers ::buildIframeAttributes
72 * @covers \Drupal\blazy\Dejavu\BlazyDefault::entitySettings
73 * @dataProvider providerTestBuildIframeAttributes
75 public function testBuildIframeAttributes(array $data, $expected) {
76 $variables = ['attributes' => [], 'image' => []];
77 $settings = BlazyDefault::entitySettings();
79 $settings['embed_url'] = '//www.youtube.com/watch?v=E03HFA923kw';
80 $settings['scheme'] = 'youtube';
81 $settings['type'] = 'video';
83 $this->assertArrayHasKey('iframe_lazy', $settings);
85 $variables['settings'] = array_merge($settings, $data);
86 Blazy::buildIframeAttributes($variables);
88 $this->assertNotEmpty($variables[$expected]);
92 * Provide test cases for ::testBuildIframeAttributes().
94 public function providerTestBuildIframeAttributes() {
98 'media_switch' => 'media',
105 'media_switch' => '',
116 * Tests building Blazy attributes.
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.
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
133 public function testBuildAttributes(array $settings, $item, $expected_image, $expected_iframe) {
135 $variables = ['attributes' => []];
136 $build = $this->data;
137 $settings = array_merge($build['settings'], $settings);
139 $settings['breakpoints'] = [];
140 $settings['blazy'] = TRUE;
141 $settings['lazy'] = 'blazy';
142 $settings['image_style'] = '';
143 $settings['thumbnail_style'] = '';
145 if (!empty($settings['embed_url'])) {
146 $settings = array_merge(BlazyDefault::entitySettings(), $settings);
149 $variables['element']['#item'] = $item == TRUE ? $this->testItem : NULL;
150 $variables['element']['#settings'] = $settings;
152 Blazy::buildAttributes($variables);
154 $image = $expected_image == TRUE ? !empty($variables['image']) : empty($variables['image']);
155 $iframe = $expected_iframe == TRUE ? !empty($variables['iframe_attributes']) : empty($variables['iframe_attributes']);
157 $this->assertTrue($image);
158 $this->assertTrue($iframe);
160 $this->assertEquals($settings['blazy'], $variables['settings']['blazy']);
164 * Provider for ::testBuildAttributes.
166 public function providerBuildAttributes() {
167 $breakpoints = $this->getDataBreakpoints();
168 $uri = 'public://example.jpg';
172 'background' => FALSE,
181 'background' => FALSE,
182 'responsive_image_style_id' => 'blazy_responsive_test',
191 'background' => TRUE,
200 'background' => FALSE,
213 'background' => FALSE,
214 'embed_url' => '//www.youtube.com/watch?v=E03HFA923kw',
215 'media_switch' => 'media',
218 'scheme' => 'youtube',
231 * Tests BlazyManager image with lightbox support.
233 * This is here as we need file_create_url() for both Blazy and its lightbox.
235 * @param array $settings
236 * The settings being tested.
238 * @covers \Drupal\blazy\BlazyManager::preRenderImage
239 * @covers \Drupal\blazy\BlazyLightbox::build
240 * @covers \Drupal\blazy\BlazyLightbox::buildCaptions
241 * @dataProvider providerTestPreRenderImageLightbox
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);
251 foreach (['caption', 'media', 'wrapper'] as $key) {
252 $build['settings'][$key . '_attributes']['class'][] = $key . '-test';
255 $element = $this->doPreRenderImage($build);
257 if ($settings['media_switch'] == 'content') {
258 $this->assertEquals($settings['content_url'], $element['#url']);
259 $this->assertArrayHasKey('#url', $element);
262 $this->assertArrayHasKey('data-' . $settings['media_switch'] . '-trigger', $element['#url_attributes']);
263 $this->assertArrayHasKey('#url', $element);
268 * Provide test cases for ::testPreRenderImageLightbox().
271 * An array of tested data.
273 public function providerTestPreRenderImageLightbox() {
277 'content_url' => 'node/1',
280 'media_switch' => 'content',
286 'box_caption' => 'auto',
288 'media_switch' => 'colorbox',
294 'box_caption' => 'alt',
296 'media_switch' => 'colorbox',
302 'box_caption' => 'title',
304 'media_switch' => 'photobox',
310 'box_caption' => 'alt_title',
312 'media_switch' => 'colorbox',
318 'box_caption' => 'title_alt',
320 'media_switch' => 'photobox',
326 'box_caption' => 'entity_title',
328 'media_switch' => 'photobox',
334 'box_caption' => 'custom',
335 'box_caption_custom' => '[node:field_text_multiple]',
336 'dimension' => '640x360',
337 'embed_url' => '//www.youtube.com/watch?v=E03HFA923kw',
339 'media_switch' => 'photobox',
340 'scheme' => 'youtube',
350 namespace Drupal\blazy;
352 if (!function_exists('file_create_url')) {
357 function file_create_url() {
358 // Empty block to satisfy coder.