3 namespace Drupal\Tests\blazy\Traits;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\blazy\Dejavu\BlazyDefault;
9 * A Trait common for Blazy Unit tests.
11 trait BlazyUnitTestTrait {
13 use BlazyPropertiesTestTrait;
16 * The formatter settings.
20 protected $formatterSettings = [];
23 * Add empty data for breakpoints.
26 * The dummy breakpoints.
28 protected function getEmptyBreakpoints() {
31 foreach (BlazyDefault::getConstantBreakpoints() as $breakpoint) {
32 $build[$breakpoint]['image_style'] = '';
33 $build[$breakpoint]['width'] = '';
40 * Add partially empty data for breakpoints.
42 * @param string $clean
43 * The flag for clean breakpoints.
46 * The dummy breakpoints.
48 protected function getDataBreakpoints($clean = FALSE) {
50 $widths = ['xs' => 210, 'sm' => 1024, 'md' => 1900];
51 $styles = ['xs' => 'blazy_crop', 'sm' => 'blazy_crop', 'md' => 'blazy_crop'];
53 foreach (BlazyDefault::getConstantBreakpoints() as $breakpoint) {
54 if ($clean && (!isset($styles[$breakpoint]) || !isset($widths[$breakpoint]))) {
57 $build[$breakpoint]['image_style'] = isset($styles[$breakpoint]) ? $styles[$breakpoint] : '';
58 $build[$breakpoint]['width'] = isset($widths[$breakpoint]) ? $widths[$breakpoint] : '';
65 * Returns sensible formatter settings for testing purposes.
68 * The formatter settings.
70 protected function getFormatterSettings() {
72 'box_caption' => 'custom',
73 'box_style' => 'large',
74 'breakpoints' => $this->getDataBreakpoints(),
76 'image_style' => 'blazy_crop',
77 'media_switch' => 'blazy_test',
78 'thumbnail_style' => 'thumbnail',
80 'caption' => ['alt' => 'alt', 'title' => 'title'],
82 ] + BlazyDefault::extendedSettings();
84 return empty($this->formatterSettings) ? $defaults : array_merge($defaults, $this->formatterSettings);
88 * Sets formatter settings.
90 * @param array $settings
94 * The class instance that this method is called on.
96 protected function setFormatterSettings(array $settings = []) {
97 $this->formatterSettings = array_merge($this->getFormatterSettings(), $settings);
102 * Sets formatter setting.
104 * @param string $setting
106 * @param mixed|bool|string $value
110 * The class instance that this method is called on.
112 protected function setFormatterSetting($setting, $value) {
113 $this->formatterSettings[$setting] = $value;
118 * Returns the default field formatter definition.
121 * The default field formatter settings.
123 protected function getDefaultFormatterDefinition() {
124 // @deprecated: Will be replaced by `form` array below.
127 'image_style_form' => TRUE,
128 'fieldable_form' => TRUE,
129 'media_switch_form' => TRUE,
133 'background' => TRUE,
134 'box_captions' => TRUE,
135 'breakpoints' => BlazyDefault::getConstantBreakpoints(),
136 'captions' => ['alt' => 'Alt', 'title' => 'Title'],
137 'classes' => ['field_class' => 'Classes'],
138 'current_view_mode' => 'default',
139 'entity_type' => $this->entityType,
140 'field_name' => $this->testFieldName,
141 'field_type' => 'image',
142 'multimedia' => TRUE,
143 'images' => [$this->testFieldName => $this->testFieldName],
144 'layouts' => ['top' => 'Top'],
145 'links' => ['field_link' => 'Link'],
146 'namespace' => 'blazy',
147 'responsive_image' => TRUE,
148 'thumbnail_style' => TRUE,
149 'skins' => ['classic' => 'Classic'],
151 'target_type' => 'file',
152 'titles' => ['field_text' => 'Text'],
153 'view_mode' => 'default',
154 'settings' => $this->getFormatterSettings(),
165 * Returns the default field formatter definition.
168 * The default field formatter settings.
170 protected function getDefaulEntityFormatterDefinition() {
173 'overlays' => ['field_image' => 'Image'],
175 'optionsets' => ['default' => 'Default'],
176 'thumbnails' => TRUE,
177 'thumbnail_effect' => ['grid', 'hover'],
178 'thumbnail_style' => TRUE,
179 'thumb_captions' => ['field_text' => 'Text'],
180 'thumb_positions' => TRUE,
186 * Returns the field formatter definition along with settings.
189 * The field formatter settings.
191 protected function getFormatterDefinition() {
192 $defaults = $this->getDefaultFormatterDefinition();
193 return empty($this->formatterDefinition) ? $defaults : array_merge($defaults, $this->formatterDefinition);
197 * Sets the field formatter definition.
199 * @param string $definition
200 * The key definition defining scope for form elements.
201 * @param mixed|string|bool $value
205 * The class instance that this method is called on.
207 protected function setFormatterDefinition($definition, $value) {
208 $this->formatterDefinition[$definition] = $value;
213 * Return dummy cache metadata.
215 protected function getCacheMetaData() {
218 foreach (['contexts', 'keys', 'tags'] as $key) {
219 if ($key == 'contexts') {
220 $cache = ['languages'];
222 elseif ($key == 'keys') {
223 $cache = ['blazy_image'];
225 elseif ($key == 'tags') {
226 $cache = Cache::buildTags('file:123', $suffixes, '.');
229 $build['cache_' . $key] = $cache;
235 * Pre render Blazy image.
237 * @param array $build
238 * The data containing: settings and image item.
241 * The pre_render element.
243 protected function doPreRenderImage(array $build = []) {
244 $image = $this->blazyManager->getImage($build);
246 $image['#build']['settings'] = array_merge($this->getCacheMetaData(), $build['settings']);
247 $image['#build']['item'] = $build['item'];
248 return $this->blazyManager->preRenderImage($image);
252 * Returns dummy fields for an entity reference.
255 * A common field array for Blazy related entity reference formatter.
257 protected function getDefaultFields($select = FALSE) {
259 'field_class' => 'text',
260 'field_id' => 'text',
261 'field_image' => 'image',
262 'field_layout' => 'list_string',
263 'field_link' => 'link',
264 'field_title' => 'text',
265 'field_teaser' => 'text',
269 foreach (array_keys($fields) as $key) {
270 if (in_array($key, ['field_id', 'field_teaser'])) {
273 $option = str_replace('field_', '', $key);
274 $options[$option] = $key;
277 return $select ? $options : $fields;
281 * Set up Blazy variables.
283 protected function setUpVariables() {
284 $this->entityType = 'node';
285 $this->bundle = 'bundle_test';
286 $this->testFieldName = 'field_image_multiple';
287 $this->testFieldType = 'image';
288 $this->testPluginId = 'blazy';
290 $this->maxParagraphs = 20;
294 * Setup the unit images.
296 protected function setUpUnitImages() {
297 $item = new \stdClass();
298 $item->uri = 'public://example.jpg';
299 $item->entity = NULL;
300 $item->alt = $this->randomMachineName();
301 $item->title = $this->randomMachineName();
303 $settings = $this->getFormatterSettings();
305 $this->uri = $settings['uri'] = $item->uri;
308 'settings' => $settings,
312 $this->testItem = $item;
316 * Setup the unit images.
318 protected function setUpMockImage() {
319 $entity = $this->getMock('\Drupal\Core\Entity\ContentEntityInterface');
320 $entity->expects($this->any())
322 ->willReturn($this->randomMachineName());
323 $entity->expects($this->any())
324 ->method('getEntityTypeId')
325 ->will($this->returnValue('node'));
327 $item = $this->getMock('\Drupal\Core\Field\FieldItemListInterface');
328 $item->expects($this->any())
329 ->method('getEntity')
330 ->willReturn($entity);
332 $this->setUpUnitImages();
334 $this->testItem = $item;
335 $this->data['item'] = $item;
336 $item->entity = $entity;