3 namespace Drupal\Tests\blazy\Kernel;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\entity_test\Entity\EntityTest;
7 use Drupal\blazy\Blazy;
8 use Drupal\blazy\Dejavu\BlazyDefault;
11 * Tests the Blazy manager methods.
13 * @coversDefaultClass \Drupal\blazy\BlazyManager
17 class BlazyManagerTest extends BlazyKernelTestBase {
22 protected function setUp() {
25 $bundle = $this->bundle;
27 $settings['fields']['field_text_multiple'] = 'text';
29 $this->setUpContentTypeTest($bundle, $settings);
30 $this->setUpContentWithItems($bundle);
31 $this->setUpRealImage();
35 * Tests BlazyManager image.
37 * @param array $settings
38 * The settings being tested.
39 * @param string $expected_responsive_image_style_id
40 * The responsive image style ID.
42 * @covers ::preRenderImage
43 * @covers ::getResponsiveImageCacheTags
44 * @covers \Drupal\blazy\BlazyLightbox::build
45 * @covers \Drupal\blazy\BlazyLightbox::buildCaptions
46 * @dataProvider providerTestPreRenderImage
48 public function testPreRenderImage(array $settings = [], $expected_responsive_image_style_id = '') {
50 $settings['count'] = $this->maxItems;
51 $settings['uri'] = $this->uri;
52 $build['settings'] = array_merge($build['settings'], $settings);
54 $element = $this->doPreRenderImage($build);
56 if ($settings['media_switch'] == 'content') {
57 $this->assertEquals($settings['content_url'], $element['#url']);
58 $this->assertArrayHasKey('#url', $element);
61 $this->assertArrayHasKey('data-' . $settings['media_switch'] . '-trigger', $element['#url_attributes']);
62 $this->assertArrayHasKey('#url', $element);
65 $this->assertEquals($expected_responsive_image_style_id, $element['#settings']['responsive_image_style_id']);
69 * Provide test cases for ::testPreRenderImage().
72 * An array of tested data.
74 public function providerTestPreRenderImage() {
77 'content_url' => 'node/1',
78 'media_switch' => 'content',
85 'media_switch' => 'photobox',
87 'responsive_image_style' => 'blazy_responsive_test',
89 'blazy_responsive_test',
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',
99 'media_switch' => 'blazy_test',
100 'scheme' => 'youtube',
110 * Tests the entity view builder.
112 * @param string $entity
114 * @param string $fallback
116 * @param string $message
118 * @param bool $expected
119 * The expected output.
121 * @covers ::getEntityView
122 * @dataProvider providerTestGetEntityView
124 public function testGetEntityView($entity, $fallback, $message, $expected) {
125 if ($entity == 'entity') {
126 $entity_test = EntityTest::create([
127 'name' => $this->randomMachineName(),
130 $entity_test->save();
132 $entity = $entity_test;
134 elseif ($entity == 'node') {
135 $entity = empty($this->entity) ? $this->setUpContentWithItems($this->bundle) : $this->entity;
137 elseif ($entity == 'responsive_image') {
138 $entity = $this->blazyManager->entityLoad('blazy_responsive_test', 'responsive_image_style');
140 elseif ($entity == 'image') {
141 $entity = $this->testItem;
144 $result = $this->blazyManager->getEntityView($entity, [], $fallback);
145 $this->assertSame($expected, !empty($result), $message);
149 * Provide test cases for ::testGetEntityView().
152 * An array of tested data.
154 public function providerTestGetEntityView() {
159 'Entity test has no entity_test_view(), yet it has view builder.',
165 'Node has node_view() taking precedence over view builder.',
168 'Responsive image' => [
170 'This is some fallback text.',
171 'Responsive image has no view builder. Fallback to text.',
177 'Image is not an instance of EntityInterface, returns false.',
184 * Tests building Blazy attributes.
186 * @param array $settings
187 * The settings being tested.
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.
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
203 public function testBuildAttributes(array $settings, $uri, $item, $iframe, $expected) {
205 $variables = ['attributes' => []];
206 $settings = array_merge($this->getFormatterSettings(), $settings);
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 : '';
214 if (!empty($settings['embed_url'])) {
215 $settings = array_merge(BlazyDefault::entitySettings(), $settings);
218 $variables['element']['#item'] = $item ? $this->testItem : NULL;
219 $variables['element']['#settings'] = $settings;
221 Blazy::buildAttributes($variables);
223 $image = $expected == TRUE ? !empty($variables['image']) : empty($variables['image']);
224 $iframe = $iframe == TRUE ? !empty($variables['iframe_attributes']) : empty($variables['iframe_attributes']);
226 $this->assertTrue($image);
227 $this->assertTrue($iframe);
231 * Provider for ::testBuildAttributes.
233 public function providerBuildAttributes() {
234 $breakpoints = $this->getDataBreakpoints();
235 $breakpoints_cleaned = $this->getDataBreakpoints(TRUE);
239 'background' => FALSE,
249 'background' => FALSE,
259 'background' => TRUE,
260 'breakpoints' => $breakpoints,
276 * Tests responsive image integration.
278 * @param string $responsive_image_style_id
279 * The responsive_image_style_id.
281 * Whether to use Drupal core 8.x-2, or later 8.x-3.
282 * @param bool $expected
283 * The expected output_image_tag.
285 * @dataProvider providerResponsiveImage
287 public function testPreprocessResponsiveImage($responsive_image_style_id, $old, $expected) {
289 'item' => $this->testItem,
291 'responsive_image_style_id' => $responsive_image_style_id,
294 template_preprocess_responsive_image($variables);
297 $variables['img_element']['#srcset'][0]['uri'] = $this->uri;
300 $variables['img_element']['#uri'] = $this->uri;
303 Blazy::preprocessResponsiveImage($variables);
305 $this->assertEquals($expected, $variables['output_image_tag']);
309 * Provider for ::testPreprocessResponsiveImage.
311 public function providerResponsiveImage() {
313 'Responsive image with picture 8.x-2' => [
314 'blazy_picture_test',
318 'Responsive image with picture 8.x-3' => [
319 'blazy_picture_test',
323 'Responsive image without picture 8.x-3' => [
324 'blazy_responsive_test',
335 * @dataProvider providerIsCrop
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);
341 $this->assertEquals($expected, $is_cropped);
345 * Provider for ::testIsCrop.
347 public function providerIsCrop() {
349 'Cropped image style' => [
353 'Non-cropped image style' => [
361 * Tests cases for various methods.
364 * @covers ::buildDataBlazy
365 * @covers ::getLightboxes
366 * @covers ::setLightboxes
367 * @covers ::buildSkins
370 * @todo: Move some to unit tests.
372 public function testBlazyManagerMethods() {
373 // Tests Blazy attachments.
374 $attach = ['blazy' => TRUE, 'media_switch' => 'blazy_test'];
376 $attachments = $this->blazyManager->attach($attach);
377 $this->assertArrayHasKey('blazy', $attachments['drupalSettings']);
379 // Tests Blazy [data-blazy] attributes.
380 $build = $this->data;
381 $settings = &$build['settings'];
382 $item = $build['item'];
384 $settings['item'] = $item;
385 $settings['uri'] = $this->uri;
386 $settings['blazy_data'] = [];
387 $settings['background'] = TRUE;
388 $settings['breakpoints'] = $this->getDataBreakpoints();
390 // Ensure Blazy can be activated by breakpoints.
391 $this->blazyManager->buildDataBlazy($settings, $build);
392 $this->assertNotEmpty($settings['blazy']);
394 // Tests Blazy lightboxes.
395 $this->blazyManager->setLightboxes('blazy_test');
396 $lightboxes = $this->blazyManager->getLightboxes();
398 $this->assertFalse(in_array('nixbox', $lightboxes));
399 $this->assertTrue(in_array('blazy_test', $lightboxes));
402 // Tests skins with a single expected method BlazySkinTest::skins().
403 $skins = $this->blazyManager->buildSkins('blazy_test', '\Drupal\blazy_test\BlazySkinTest');
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);
411 // Verify multiple skin methods are respected.
412 Cache::invalidateTags([$cid]);
413 drupal_flush_all_caches();
414 $this->assertFalse($this->blazyManager->getCache()->get($cid));
416 $skins = $this->blazyManager->buildSkins('blazy_test', '\Drupal\blazy_test\BlazySkinTest', ['skins', 'features']);
418 $this->assertArrayHasKey('features', $skins);
420 $cached_skins = $this->blazyManager->getCache()->get($cid);
421 $this->assertEquals($skins, $cached_skins->data);