View source
<?php
namespace Drupal\Tests\blazy\Traits;
use Drupal\Core\Cache\Cache;
use Drupal\blazy\BlazyDefault;
trait BlazyUnitTestTrait {
use BlazyPropertiesTestTrait;
protected $formatterSettings = [];
protected function getEmptyBreakpoints() {
$build = [];
foreach (BlazyDefault::getConstantBreakpoints() as $breakpoint) {
$build[$breakpoint]['image_style'] = '';
$build[$breakpoint]['width'] = '';
}
return $build;
}
protected function getDataBreakpoints($clean = FALSE) {
$build = [];
$widths = [
'xs' => 210,
'sm' => 1024,
'md' => 1900,
];
$styles = [
'xs' => 'blazy_crop',
'sm' => 'blazy_crop',
'md' => 'blazy_crop',
];
foreach (BlazyDefault::getConstantBreakpoints() as $breakpoint) {
if ($clean && (!isset($styles[$breakpoint]) || !isset($widths[$breakpoint]))) {
continue;
}
$build[$breakpoint]['image_style'] = isset($styles[$breakpoint]) ? $styles[$breakpoint] : '';
$build[$breakpoint]['width'] = isset($widths[$breakpoint]) ? $widths[$breakpoint] : '';
}
return $build;
}
protected function getFormatterSettings() {
$defaults = [
'box_caption' => 'custom',
'box_style' => 'large',
'breakpoints' => $this
->getDataBreakpoints(),
'cache' => 0,
'image_style' => 'blazy_crop',
'media_switch' => 'blazy_test',
'thumbnail_style' => 'thumbnail',
'ratio' => 'fluid',
'caption' => [
'alt' => 'alt',
'title' => 'title',
],
'sizes' => '100w',
] + BlazyDefault::extendedSettings();
return empty($this->formatterSettings) ? $defaults : array_merge($defaults, $this->formatterSettings);
}
protected function setFormatterSettings(array $settings = []) {
$this->formatterSettings = array_merge($this
->getFormatterSettings(), $settings);
return $this;
}
protected function setFormatterSetting($setting, $value) {
$this->formatterSettings[$setting] = $value;
return $this;
}
protected function getDefaultFormatterDefinition() {
$deprecated = [
'grid_form' => TRUE,
'image_style_form' => TRUE,
'fieldable_form' => TRUE,
'media_switch_form' => TRUE,
];
return [
'background' => TRUE,
'box_captions' => TRUE,
'breakpoints' => BlazyDefault::getConstantBreakpoints(),
'captions' => [
'alt' => 'Alt',
'title' => 'Title',
],
'classes' => [
'field_class' => 'Classes',
],
'current_view_mode' => 'default',
'entity_type' => $this->entityType,
'field_name' => $this->testFieldName,
'field_type' => 'image',
'multimedia' => TRUE,
'images' => [
$this->testFieldName => $this->testFieldName,
],
'layouts' => [
'top' => 'Top',
],
'links' => [
'field_link' => 'Link',
],
'namespace' => 'blazy',
'responsive_image' => TRUE,
'thumbnail_style' => TRUE,
'skins' => [
'classic' => 'Classic',
],
'style' => 'grid',
'target_type' => 'file',
'titles' => [
'field_text' => 'Text',
],
'view_mode' => 'default',
'settings' => $this
->getFormatterSettings(),
'form' => [
'fieldable',
'grid',
'image_style',
'media_switch',
],
] + $deprecated;
}
protected function getDefaulEntityFormatterDefinition() {
return [
'nav' => TRUE,
'overlays' => [
'field_image' => 'Image',
],
'vanilla' => TRUE,
'optionsets' => [
'default' => 'Default',
],
'thumbnails' => TRUE,
'thumbnail_effect' => [
'grid',
'hover',
],
'thumbnail_style' => TRUE,
'thumb_captions' => [
'field_text' => 'Text',
],
'thumb_positions' => TRUE,
'caches' => TRUE,
];
}
protected function getFormatterDefinition() {
$defaults = $this
->getDefaultFormatterDefinition();
return empty($this->formatterDefinition) ? $defaults : array_merge($defaults, $this->formatterDefinition);
}
protected function setFormatterDefinition($definition, $value) {
$this->formatterDefinition[$definition] = $value;
return $this;
}
protected function getCacheMetaData() {
$build = [];
$suffixes[] = 3;
foreach ([
'contexts',
'keys',
'tags',
] as $key) {
if ($key == 'contexts') {
$cache = [
'languages',
];
}
elseif ($key == 'keys') {
$cache = [
'blazy_image',
];
}
elseif ($key == 'tags') {
$cache = Cache::buildTags('file:123', $suffixes, '.');
}
$build['cache_' . $key] = $cache;
}
return $build;
}
protected function doPreRenderImage(array $build = []) {
$image = $this->blazyManager
->getImage($build);
$image['#build']['settings'] = array_merge($this
->getCacheMetaData(), $build['settings']);
$image['#build']['item'] = $build['item'];
return $this->blazyManager
->preRenderImage($image);
}
protected function getDefaultFields($select = FALSE) {
$fields = [
'field_class' => 'text',
'field_id' => 'text',
'field_image' => 'image',
'field_layout' => 'list_string',
'field_link' => 'link',
'field_title' => 'text',
'field_teaser' => 'text',
];
$options = [];
foreach (array_keys($fields) as $key) {
if (in_array($key, [
'field_id',
'field_teaser',
])) {
continue;
}
$option = str_replace('field_', '', $key);
$options[$option] = $key;
}
return $select ? $options : $fields;
}
protected function setUpVariables() {
$this->entityType = 'node';
$this->bundle = 'bundle_test';
$this->testFieldName = 'field_image_multiple';
$this->testFieldType = 'image';
$this->testPluginId = 'blazy';
$this->maxItems = 3;
$this->maxParagraphs = 20;
}
protected function setUpUnitImages() {
$item = new \stdClass();
$item->uri = 'public://example.jpg';
$item->entity = NULL;
$item->alt = $this
->randomMachineName();
$item->title = $this
->randomMachineName();
$settings = $this
->getFormatterSettings();
$this->uri = $settings['uri'] = $item->uri;
$this->data = [
'settings' => $settings,
'item' => $item,
];
$this->testItem = $item;
}
protected function setUpMockImage() {
$entity = $this
->createMock('\\Drupal\\Core\\Entity\\ContentEntityInterface');
$entity
->expects($this
->any())
->method('label')
->willReturn($this
->randomMachineName());
$entity
->expects($this
->any())
->method('getEntityTypeId')
->will($this
->returnValue('node'));
$item = $this
->createMock('\\Drupal\\Core\\Field\\FieldItemListInterface');
$item
->expects($this
->any())
->method('getEntity')
->willReturn($entity);
$this
->setUpUnitImages();
$this->testItem = $item;
$this->data['item'] = $item;
$item->entity = $entity;
}
}
namespace Drupal\blazy;
if (!function_exists('blazy_alterable_settings')) {
function blazy_alterable_settings() {
}
}