Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / modules / contrib / blazy / tests / src / Traits / BlazyUnitTestTrait.php
1 <?php
2
3 namespace Drupal\Tests\blazy\Traits;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\blazy\Dejavu\BlazyDefault;
7
8 /**
9  * A Trait common for Blazy Unit tests.
10  */
11 trait BlazyUnitTestTrait {
12
13   use BlazyPropertiesTestTrait;
14
15   /**
16    * The formatter settings.
17    *
18    * @var array
19    */
20   protected $formatterSettings = [];
21
22   /**
23    * Add empty data for breakpoints.
24    *
25    * @return array
26    *   The dummy breakpoints.
27    */
28   protected function getEmptyBreakpoints() {
29     $build = [];
30
31     foreach (BlazyDefault::getConstantBreakpoints() as $breakpoint) {
32       $build[$breakpoint]['image_style'] = '';
33       $build[$breakpoint]['width'] = '';
34     }
35
36     return $build;
37   }
38
39   /**
40    * Add partially empty data for breakpoints.
41    *
42    * @param string $clean
43    *   The flag for clean breakpoints.
44    *
45    * @return array
46    *   The dummy breakpoints.
47    */
48   protected function getDataBreakpoints($clean = FALSE) {
49     $build  = [];
50     $widths = ['xs' => 210, 'sm' => 1024, 'md' => 1900];
51     $styles = ['xs' => 'blazy_crop', 'sm' => 'blazy_crop', 'md' => 'blazy_crop'];
52
53     foreach (BlazyDefault::getConstantBreakpoints() as $breakpoint) {
54       if ($clean && (!isset($styles[$breakpoint]) || !isset($widths[$breakpoint]))) {
55         continue;
56       }
57       $build[$breakpoint]['image_style'] = isset($styles[$breakpoint]) ? $styles[$breakpoint] : '';
58       $build[$breakpoint]['width'] = isset($widths[$breakpoint]) ? $widths[$breakpoint] : '';
59     }
60
61     return $build;
62   }
63
64   /**
65    * Returns sensible formatter settings for testing purposes.
66    *
67    * @return array
68    *   The formatter settings.
69    */
70   protected function getFormatterSettings() {
71     $defaults = [
72       'box_caption'     => 'custom',
73       'box_style'       => 'large',
74       'breakpoints'     => $this->getDataBreakpoints(),
75       'cache'           => 0,
76       'image_style'     => 'blazy_crop',
77       'media_switch'    => 'blazy_test',
78       'thumbnail_style' => 'thumbnail',
79       'ratio'           => 'fluid',
80       'caption'         => ['alt' => 'alt', 'title' => 'title'],
81       'sizes'           => '100w',
82     ] + BlazyDefault::extendedSettings();
83
84     return empty($this->formatterSettings) ? $defaults : array_merge($defaults, $this->formatterSettings);
85   }
86
87   /**
88    * Sets formatter settings.
89    *
90    * @param array $settings
91    *   The given settings.
92    *
93    * @return $this
94    *   The class instance that this method is called on.
95    */
96   protected function setFormatterSettings(array $settings = []) {
97     $this->formatterSettings = array_merge($this->getFormatterSettings(), $settings);
98     return $this;
99   }
100
101   /**
102    * Sets formatter setting.
103    *
104    * @param string $setting
105    *   The given setting.
106    * @param mixed|bool|string $value
107    *   The given value.
108    *
109    * @return $this
110    *   The class instance that this method is called on.
111    */
112   protected function setFormatterSetting($setting, $value) {
113     $this->formatterSettings[$setting] = $value;
114     return $this;
115   }
116
117   /**
118    * Returns the default field formatter definition.
119    *
120    * @return array
121    *   The default field formatter settings.
122    */
123   protected function getDefaultFormatterDefinition() {
124     // @deprecated: Will be replaced by `form` array below.
125     $deprecated = [
126       'grid_form'         => TRUE,
127       'image_style_form'  => TRUE,
128       'fieldable_form'    => TRUE,
129       'media_switch_form' => TRUE,
130     ];
131
132     return [
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'],
150       'style'             => 'grid',
151       'target_type'       => 'file',
152       'titles'            => ['field_text' => 'Text'],
153       'view_mode'         => 'default',
154       'settings'          => $this->getFormatterSettings(),
155       'form'              => [
156         'fieldable',
157         'grid',
158         'image_style',
159         'media_switch',
160       ],
161     ] + $deprecated;
162   }
163
164   /**
165    * Returns the default field formatter definition.
166    *
167    * @return array
168    *   The default field formatter settings.
169    */
170   protected function getDefaulEntityFormatterDefinition() {
171     return [
172       'nav'              => TRUE,
173       'overlays'         => ['field_image' => 'Image'],
174       'vanilla'          => TRUE,
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,
181       'caches'           => TRUE,
182     ];
183   }
184
185   /**
186    * Returns the field formatter definition along with settings.
187    *
188    * @return array
189    *   The field formatter settings.
190    */
191   protected function getFormatterDefinition() {
192     $defaults = $this->getDefaultFormatterDefinition();
193     return empty($this->formatterDefinition) ? $defaults : array_merge($defaults, $this->formatterDefinition);
194   }
195
196   /**
197    * Sets the field formatter definition.
198    *
199    * @param string $definition
200    *   The key definition defining scope for form elements.
201    * @param mixed|string|bool $value
202    *   The defined value.
203    *
204    * @return $this
205    *   The class instance that this method is called on.
206    */
207   protected function setFormatterDefinition($definition, $value) {
208     $this->formatterDefinition[$definition] = $value;
209     return $this;
210   }
211
212   /**
213    * Return dummy cache metadata.
214    */
215   protected function getCacheMetaData() {
216     $build = [];
217     $suffixes[] = 3;
218     foreach (['contexts', 'keys', 'tags'] as $key) {
219       if ($key == 'contexts') {
220         $cache = ['languages'];
221       }
222       elseif ($key == 'keys') {
223         $cache = ['blazy_image'];
224       }
225       elseif ($key == 'tags') {
226         $cache = Cache::buildTags('file:123', $suffixes, '.');
227       }
228
229       $build['cache_' . $key] = $cache;
230     }
231     return $build;
232   }
233
234   /**
235    * Pre render Blazy image.
236    *
237    * @param array $build
238    *   The data containing: settings and image item.
239    *
240    * @return array
241    *   The pre_render element.
242    */
243   protected function doPreRenderImage(array $build = []) {
244     $image = $this->blazyManager->getImage($build);
245
246     $image['#build']['settings'] = array_merge($this->getCacheMetaData(), $build['settings']);
247     $image['#build']['item'] = $build['item'];
248     return $this->blazyManager->preRenderImage($image);
249   }
250
251   /**
252    * Returns dummy fields for an entity reference.
253    *
254    * @return array
255    *   A common field array for Blazy related entity reference formatter.
256    */
257   protected function getDefaultFields($select = FALSE) {
258     $fields = [
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',
266     ];
267
268     $options = [];
269     foreach (array_keys($fields) as $key) {
270       if (in_array($key, ['field_id', 'field_teaser'])) {
271         continue;
272       }
273       $option = str_replace('field_', '', $key);
274       $options[$option] = $key;
275     }
276
277     return $select ? $options : $fields;
278   }
279
280   /**
281    * Set up Blazy variables.
282    */
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';
289     $this->maxItems      = 3;
290     $this->maxParagraphs = 20;
291   }
292
293   /**
294    * Setup the unit images.
295    */
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();
302
303     $settings = $this->getFormatterSettings();
304
305     $this->uri = $settings['uri'] = $item->uri;
306
307     $this->data = [
308       'settings' => $settings,
309       'item' => $item,
310     ];
311
312     $this->testItem = $item;
313   }
314
315   /**
316    * Setup the unit images.
317    */
318   protected function setUpMockImage() {
319     $entity = $this->getMock('\Drupal\Core\Entity\ContentEntityInterface');
320     $entity->expects($this->any())
321       ->method('label')
322       ->willReturn($this->randomMachineName());
323     $entity->expects($this->any())
324       ->method('getEntityTypeId')
325       ->will($this->returnValue('node'));
326
327     $item = $this->getMock('\Drupal\Core\Field\FieldItemListInterface');
328     $item->expects($this->any())
329       ->method('getEntity')
330       ->willReturn($entity);
331
332     $this->setUpUnitImages();
333
334     $this->testItem = $item;
335     $this->data['item'] = $item;
336     $item->entity = $entity;
337   }
338
339 }