View source
<?php
namespace Drupal\Tests\blazy\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\blazy\Blazy;
use Drupal\blazy\BlazyDefault;
use Drupal\Tests\blazy\Traits\BlazyUnitTestTrait;
use Drupal\Tests\blazy\Traits\BlazyManagerUnitTestTrait;
class BlazyUnitTest extends UnitTestCase {
use BlazyUnitTestTrait;
use BlazyManagerUnitTestTrait;
protected function setUp() {
parent::setUp();
$this
->setUpVariables();
$this
->setUpUnitServices();
$this
->setUpUnitContainer();
$this
->setUpMockImage();
}
public function testBuildIframe(array $data, $expected) {
$variables = [
'attributes' => [],
'image' => [],
];
$settings = BlazyDefault::entitySettings();
$settings['embed_url'] = '//www.youtube.com/watch?v=E03HFA923kw';
$settings['type'] = 'video';
$settings['bundle'] = 'remote_video';
$variables['settings'] = array_merge($settings, $data);
Blazy::buildIframe($variables);
$this
->assertNotEmpty($variables[$expected]);
}
public function providerTestBuildIframe() {
return [
[
[
'media_switch' => 'media',
'ratio' => 'fluid',
],
'iframe',
],
[
[
'media_switch' => '',
'ratio' => '',
'width' => 640,
'height' => 360,
],
'iframe',
],
];
}
public function testPreprocessBlazy(array $settings, $item, $expected_image, $expected_iframe) {
$variables = [
'attributes' => [],
];
$build = $this->data;
$settings = array_merge($build['settings'], $settings);
$settings += BlazyDefault::itemSettings();
$settings['blazy'] = TRUE;
$settings['lazy'] = 'blazy';
$settings['image_style'] = '';
$settings['thumbnail_style'] = '';
if (!empty($settings['embed_url'])) {
$settings = array_merge(BlazyDefault::entitySettings(), $settings);
}
$variables['element']['#item'] = $item == TRUE ? $this->testItem : NULL;
$variables['element']['#settings'] = $settings;
Blazy::preprocessBlazy($variables);
$image = $expected_image == TRUE ? !empty($variables['image']) : empty($variables['image']);
$iframe = $expected_iframe == TRUE ? !empty($variables['iframe']) : empty($variables['iframe']);
$this
->assertTrue($image);
$this
->assertTrue($iframe);
$this
->assertEquals($settings['blazy'], $variables['settings']['blazy']);
}
public function providerPreprocessBlazy() {
$uri = 'public://example.jpg';
$data[] = [
[
'background' => FALSE,
'uri' => '',
],
TRUE,
FALSE,
FALSE,
];
$data[] = [
[
'background' => TRUE,
'uri' => $uri,
],
TRUE,
FALSE,
FALSE,
];
$data[] = [
[
'background' => FALSE,
'ratio' => 'fluid',
'sizes' => '100w',
'width' => 640,
'height' => 360,
'uri' => $uri,
],
TRUE,
TRUE,
FALSE,
];
$data[] = [
[
'background' => FALSE,
'embed_url' => '//www.youtube.com/watch?v=E03HFA923kw',
'media_switch' => 'media',
'ratio' => 'fluid',
'sizes' => '100w',
'scheme' => 'youtube',
'type' => 'video',
'uri' => $uri,
'use_media' => TRUE,
],
TRUE,
TRUE,
TRUE,
];
return $data;
}
public function todoTestPreRenderImageLightbox(array $settings = []) {
$build = $this->data;
$settings += BlazyDefault::itemSettings();
$settings['count'] = $this->maxItems;
$settings['uri'] = $this->uri;
$settings['box_style'] = '';
$settings['box_media_style'] = '';
$build['settings'] = array_merge($build['settings'], $settings);
$switch_css = str_replace('_', '-', $settings['media_switch']);
foreach ([
'caption',
'media',
'wrapper',
] as $key) {
$build['settings'][$key . '_attributes']['class'][] = $key . '-test';
}
$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);
}
}
public function providerTestPreRenderImageLightbox() {
$data[] = [
[
'box_caption' => '',
'content_url' => 'node/1',
'dimension' => '',
'lightbox' => FALSE,
'media_switch' => 'content',
'type' => 'image',
],
];
$data[] = [
[
'box_caption' => 'auto',
'lightbox' => TRUE,
'media_switch' => 'colorbox',
'type' => 'image',
],
];
$data[] = [
[
'box_caption' => 'alt',
'lightbox' => TRUE,
'media_switch' => 'colorbox',
'type' => 'image',
],
];
$data[] = [
[
'box_caption' => 'title',
'lightbox' => TRUE,
'media_switch' => 'photobox',
'type' => 'image',
],
];
$data[] = [
[
'box_caption' => 'alt_title',
'lightbox' => TRUE,
'media_switch' => 'colorbox',
'type' => 'image',
],
];
$data[] = [
[
'box_caption' => 'title_alt',
'lightbox' => TRUE,
'media_switch' => 'photobox',
'type' => 'image',
],
];
$data[] = [
[
'box_caption' => 'entity_title',
'lightbox' => TRUE,
'media_switch' => 'photobox',
'type' => 'image',
],
];
$data[] = [
[
'box_caption' => 'custom',
'box_caption_custom' => '[node:field_text_multiple]',
'dimension' => '640x360',
'embed_url' => '//www.youtube.com/watch?v=E03HFA923kw',
'lightbox' => TRUE,
'media_switch' => 'photobox',
'scheme' => 'youtube',
'type' => 'video',
],
];
return $data;
}
}