View source
<?php
namespace Drupal\Tests\blazy\Kernel;
use Drupal\blazy\Blazy;
use Drupal\blazy\BlazyDefault;
class BlazyManagerTest extends BlazyKernelTestBase {
protected function setUp() {
parent::setUp();
$bundle = $this->bundle;
$settings['fields']['field_text_multiple'] = 'text';
$this
->setUpContentTypeTest($bundle, $settings);
$this
->setUpContentWithItems($bundle);
$this
->setUpRealImage();
}
public function testPreRenderImage(array $settings = [], $expected_has_responsive_image = FALSE) {
$build = $this->data;
$settings['count'] = $this->maxItems;
$settings['uri'] = $this->uri;
$settings['resimage'] = $expected_has_responsive_image ? $this->blazyManager
->entityLoad('blazy_responsive_test', 'responsive_image_style') : NULL;
$build['settings'] = array_merge($build['settings'], $settings);
$switch_css = str_replace('_', '-', $settings['media_switch']);
$element = $this
->doPreRenderImage($build);
if ($settings['media_switch'] == 'content') {
$this
->assertEquals($settings['content_url'], $element['#url']);
$this
->assertArrayHasKey('#url', $element);
}
else {
$this
->assertArrayHasKey('data-' . $switch_css . '-trigger', $element['#url_attributes']);
$this
->assertArrayHasKey('#url', $element);
}
$this
->assertEquals($expected_has_responsive_image, !empty($element['#settings']['responsive_image_style_id']));
}
public function providerTestPreRenderImage() {
$data[] = [
[
'content_url' => 'node/1',
'media_switch' => 'content',
],
FALSE,
];
$data[] = [
[
'lightbox' => TRUE,
'media_switch' => 'photobox',
'responsive_image_style' => 'blazy_responsive_test',
],
TRUE,
];
$data[] = [
[
'box_style' => 'blazy_crop',
'box_media_style' => 'large',
'box_caption' => 'custom',
'box_caption_custom' => '[node:field_text_multiple]',
'embed_url' => '//www.youtube.com/watch?v=E03HFA923kw',
'lightbox' => TRUE,
'media_switch' => 'blazy_test',
'scheme' => 'youtube',
'type' => 'video',
],
FALSE,
];
return $data;
}
public function testPreprocessBlazy(array $settings, $use_uri, $iframe, $expected) {
$variables = [
'attributes' => [],
];
$settings = array_merge($this
->getFormatterSettings(), $settings);
$settings += BlazyDefault::itemSettings();
$settings['blazy'] = TRUE;
$settings['lazy'] = 'blazy';
$settings['image_style'] = 'blazy_crop';
$settings['thumbnail_style'] = 'thumbnail';
$settings['uri'] = $use_uri ? $this->uri : '';
if (!empty($settings['embed_url'])) {
$settings = array_merge(BlazyDefault::entitySettings(), $settings);
}
$variables['element']['#item'] = $this->testItem;
$variables['element']['#settings'] = $settings;
Blazy::preprocessBlazy($variables);
$image = $expected == TRUE ? !empty($variables['image']) : empty($variables['image']);
$iframe = $iframe == TRUE ? !empty($variables['iframe']) : empty($variables['iframe']);
$this
->assertTrue($image);
$this
->assertTrue($iframe);
}
public function providerPreprocessBlazy() {
$data[] = [
[
'background' => FALSE,
],
FALSE,
FALSE,
FALSE,
];
$data[] = [
[
'background' => FALSE,
],
TRUE,
FALSE,
TRUE,
];
$data[] = [
[
'background' => TRUE,
'ratio' => 'fluid',
'sizes' => '100w',
'width' => 640,
'height' => 360,
],
TRUE,
FALSE,
FALSE,
];
return $data;
}
public function testPreprocessResponsiveImage($responsive_image_style_id, $expected) {
$variables = [
'item' => $this->testItem,
'uri' => $this->uri,
'responsive_image_style_id' => $responsive_image_style_id,
'width' => 600,
'height' => 480,
];
template_preprocess_responsive_image($variables);
$variables['img_element']['#uri'] = $this->uri;
Blazy::preprocessResponsiveImage($variables);
$this
->assertEquals($expected, $variables['output_image_tag']);
}
public function providerResponsiveImage() {
return [
'Responsive image with picture 8.x-3' => [
'blazy_picture_test',
FALSE,
],
'Responsive image without picture 8.x-3' => [
'blazy_responsive_test',
TRUE,
],
];
}
public function testBlazyManagerMethods() {
$attach = [
'blazy' => TRUE,
'media_switch' => 'blazy_test',
];
$attachments = $this->blazyManager
->attach($attach);
$this
->assertArrayHasKey('blazy', $attachments['drupalSettings']);
}
}