Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / blazy / tests / src / Kernel / BlazyEntityReferenceFormatterTest.php
1 <?php
2
3 namespace Drupal\Tests\blazy\Kernel;
4
5 use Drupal\Core\Form\FormState;
6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\Tests\blazy\Traits\BlazyKernelTestTrait;
8
9 /**
10  * Tests the Blazy entity reference file formatter.
11  *
12  * @coversDefaultClass \Drupal\blazy_test\Plugin\Field\FieldFormatter\BlazyTestEntityReferenceFormatterTest
13  * @group blazy
14  */
15 class BlazyEntityReferenceFormatterTest extends KernelTestBase {
16
17   use BlazyKernelTestTrait;
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = [
25     'system',
26     'user',
27     'field',
28     'field_ui',
29     'filter',
30     'entity_test',
31     'node',
32     'file',
33     'image',
34     'breakpoint',
35     'responsive_image',
36     'link',
37     'text',
38     'options',
39     'blazy',
40     'blazy_ui',
41     'blazy_test',
42   ];
43
44   /**
45    * {@inheritdoc}
46    */
47   protected function setUp() {
48     parent::setUp();
49
50     $this->setUpVariables();
51     $this->setUpKernelInstall();
52     $this->setUpKernelManager();
53
54     $this->blazyAdminTest  = $this->container->get('blazy_test.admin');
55     $this->entityFieldName = 'field_entity_test';
56     $this->entityPluginId  = 'blazy_entity_test';
57     $this->targetBundle    = 'bundle_target_test';
58     $this->targetBundles   = [$this->targetBundle];
59
60     $settings['image_settings'] = [
61       'iframe_lazy'  => TRUE,
62       'lazy'         => 'blazy',
63       'media_switch' => '',
64       'ratio'        => 'fluid',
65       'view_mode'    => 'default',
66     ];
67
68     $settings['entity_field_name'] = $this->entityFieldName;
69     $settings['entity_plugin_id']  = $this->entityPluginId;
70
71     $settings['entity_settings'] = [
72       'grid'      => 4,
73       'optionset' => '',
74     ] + $this->getFormatterSettings();
75
76     $this->setUpContentWithEntityReference($settings);
77     $this->formatterInstance = $this->getFormatterInstance($this->entityPluginId, $this->entityFieldName);
78   }
79
80   /**
81    * Tests the Blazy formatter display.
82    *
83    * @todo: Useful assertions.
84    */
85   public function testFormatterDisplay() {
86     $bundle     = $this->bundle;
87     $field_name = $this->entityFieldName;
88     $plugin_id  = $this->entityPluginId;
89     $formatter  = $this->formatterInstance;
90     $definition = array_merge($formatter->getScopedFormElements(), $this->getFormatterDefinition());
91     $settings   = array_merge($definition['settings'], $this->getDefaultFields(TRUE)) + $formatter::defaultSettings();
92
93     $settings['grid']    = 4;
94     $settings['style']   = 'grid';
95     $settings['overlay'] = 'field_image';
96     $settings['image']   = $this->testFieldName;
97
98     foreach (['field_title', 'field_teaser', 'field_link'] as $key) {
99       $settings['caption'][$key] = $key;
100     }
101
102     $this->referencingDisplay->setComponent($this->entityFieldName, [
103       'type'     => $this->entityPluginId,
104       'settings' => $settings,
105       'label'    => 'hidden',
106     ]);
107
108     $this->referencingDisplay->save();
109
110     // Create referencing entity.
111     $this->referencingEntity = $this->createReferencingEntity();
112
113     // Verify the un-accessible item still exists.
114     $this->assertEquals($this->referencingEntity->{$field_name}->target_id, $this->referencedEntity->id(), format_string('The un-accessible item still exists after @name formatter was executed.', ['@name' => $plugin_id]));
115
116     $entity_type_id = $this->referencingEntity->getEntityTypeId();
117     $component = $this->referencingDisplay->getComponent($this->entityFieldName);
118     $this->assertEquals($this->entityPluginId, $component['type']);
119
120     $array = $this->referencingEntity->get($this->entityFieldName);
121     $value = $array->getValue();
122
123     $list = $this->fieldTypePluginManager->createFieldItemList($this->referencingEntity, $this->entityFieldName, $value);
124     $entities = $list->referencedEntities();
125
126     $elements['settings'] = $settings;
127     $formatter->buildElements($elements, $entities, NULL);
128     $this->assertArrayHasKey('items', $elements);
129
130     $build = $this->referencingDisplay->build($this->referencingEntity);
131
132     $render = $this->blazyManager->getRenderer()->renderRoot($build);
133     $this->assertNotEmpty($render);
134
135     $string = $formatter->getFieldString($this->referencedEntity, '', NULL);
136     $this->assertEmpty($string);
137
138     $data['settings'] = $settings;
139     $data['settings']['delta'] = 0;
140     $data['settings']['vanilla'] = TRUE;
141
142     $formatter->buildElement($data, $this->referencedEntity, NULL);
143     $this->assertArrayHasKey('items', $data);
144
145     $data['settings'] = $settings;
146     $data['settings']['delta'] = 0;
147     $data['settings']['vanilla'] = FALSE;
148     $data['settings']['image'] = $this->testFieldName;
149     $data['settings']['media_switch'] = 'rendered';
150     $data['settings']['nav'] = TRUE;
151     $data['settings']['thumbnail_style'] = 'thumbnail';
152     $data['settings']['thumbnail_caption'] = 'field_text';
153
154     $formatter->buildElement($data, $entities[0], NULL);
155     $this->assertArrayHasKey('items', $data);
156   }
157
158   /**
159    * Tests Blazy preview.
160    *
161    * @param array $settings
162    *   The settings being tested.
163    * @param bool $is_entity
164    *   Tests againts entity or image.
165    * @param bool $is_item
166    *   Tests againts empty image.
167    * @param mixed|bool|array $expected
168    *   The expected output.
169    *
170    * @dataProvider providerTestBuildPreview
171    * @depends testFormatterDisplay
172    */
173   public function testBuildPreview(array $settings, $is_entity, $is_item, $expected) {
174     $formatter  = $this->formatterInstance;
175     $definition = array_merge($formatter->getScopedFormElements(), $this->getFormatterDefinition());
176     $settings   = array_merge($definition['settings'], $settings) + $this->getDefaultFields(TRUE);
177
178     $settings['delta'] = 0;
179
180     $item   = $is_item ? $this->referencedEntity->get($this->testFieldName) : NULL;
181     $entity = $is_entity ? $this->referencedEntity : $this->testItem;
182     $data   = [
183       'item' => $item,
184       'settings' => $settings,
185     ];
186
187     $preview = $formatter->buildPreview($data, $entity, '');
188     $result = $is_entity ? !empty($preview) : $preview;
189
190     $this->assertEquals($expected, $result);
191   }
192
193   /**
194    * Provide test cases for ::testBuildPreview().
195    *
196    * @return array
197    *   An array of tested data.
198    */
199   public function providerTestBuildPreview() {
200     $data[] = [
201       [],
202       FALSE,
203       FALSE,
204       [],
205     ];
206     $data[] = [
207       [
208         '_basic' => FALSE,
209         '_detached' => FALSE,
210       ],
211       TRUE,
212       TRUE,
213       TRUE,
214     ];
215     $data[] = [
216       [
217         '_basic' => TRUE,
218         '_detached' => TRUE,
219       ],
220       TRUE,
221       TRUE,
222       TRUE,
223     ];
224     $data[] = [
225       [],
226       TRUE,
227       FALSE,
228       TRUE,
229     ];
230     return $data;
231   }
232
233   /**
234    * Tests the Blazy formatter settings form.
235    */
236   public function testFormatterSettingsForm() {
237     $formatter  = $this->formatterInstance;
238     $definition = array_merge($formatter->getScopedFormElements(), $this->getFormatterDefinition());
239
240     $definition['settings'] = array_merge($definition['settings'], $this->getDefaultFields(TRUE));
241
242     // Check for setttings form.
243     $form = [];
244     $form_state = new FormState();
245
246     // Verify global option current_view_mode is available.
247     $form['overlay']['#description'] = '';
248     $definition['_views'] = TRUE;
249     $form['disabled_access'] = [
250       '#type'   => 'hidden',
251       '#access' => FALSE,
252     ];
253
254     // Check for formatter buildSettingsForm.
255     $this->blazyAdminFormatter->buildSettingsForm($form, $definition);
256     $this->assertArrayHasKey('current_view_mode', $form);
257
258     // Check for setttings form.
259     $elements = $formatter->settingsForm($form, $form_state);
260     $this->assertArrayHasKey('closing', $elements);
261
262     $default_settings = $formatter::defaultSettings();
263     $this->assertArrayHasKey('image_style', $default_settings);
264
265     $data['settings'] = $definition['settings'];
266
267     // Tests the Blazy admin formatters.
268     $this->assertArrayHasKey('fieldable_form', $definition);
269
270     // Verify summary is working.
271     $summary = $formatter->settingsSummary();
272     foreach ($summary as $markup) {
273       $arguments = $markup->getArguments();
274       $this->assertArrayHasKey('@title', $arguments);
275     }
276
277     $formatter_settings = $formatter->buildSettings();
278     $this->assertArrayHasKey('plugin_id', $formatter_settings);
279   }
280
281 }