eaf822e9801db2f05310e95bd09f062c84cf1820
[yaffs-website] / web / modules / contrib / slick / tests / src / Kernel / SlickFormatterTest.php
1 <?php
2
3 namespace Drupal\Tests\slick\Kernel;
4
5 use Drupal\Tests\blazy\Kernel\BlazyKernelTestBase;
6 use Drupal\Tests\slick\Traits\SlickUnitTestTrait;
7
8 /**
9  * Tests the Slick field rendering using the image field type.
10  *
11  * @coversDefaultClass \Drupal\slick\Plugin\Field\FieldFormatter\SlickImageFormatter
12  * @group slick
13  */
14 class SlickFormatterTest extends BlazyKernelTestBase {
15
16   use SlickUnitTestTrait;
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = [
24     'system',
25     'user',
26     'field',
27     'file',
28     'image',
29     'filter',
30     'node',
31     'text',
32     'blazy',
33     'slick',
34     'slick_ui',
35     'slick_test',
36   ];
37
38   /**
39    * {@inheritdoc}
40    */
41   protected function setUp() {
42     parent::setUp();
43
44     $this->installConfig(static::$modules);
45     $this->installEntitySchema('slick');
46
47     $this->testFieldName  = 'field_image_multiple';
48     $this->testEmptyName  = 'field_image_multiple_empty';
49     $this->testPluginId   = 'slick_image';
50     $this->maxItems       = 7;
51     $this->maxParagraphs  = 2;
52     $this->slickAdmin     = $this->container->get('slick.admin');
53     $this->slickManager   = $this->container->get('slick.manager');
54     $this->slickFormatter = $this->container->get('slick.formatter');
55
56     $data['fields'] = [
57       'field_video'                => 'text',
58       'field_image'                => 'image',
59       'field_image_multiple_empty' => 'image',
60     ];
61
62     // Create contents.
63     $bundle = $this->bundle;
64     $this->setUpContentTypeTest($bundle, $data);
65
66     $settings = [
67       'optionset' => 'test',
68       'optionset_thumbnail' => 'test_nav',
69     ] + $this->getFormatterSettings();
70
71     $data['settings'] = $settings;
72     $this->display = $this->setUpFormatterDisplay($bundle, $data);
73
74     $data['plugin_id'] = $this->testPluginId;
75     $this->displayEmpty = $this->setUpFormatterDisplay($bundle, $data);
76
77     $this->formatterInstance = $this->getFormatterInstance();
78     $this->skins = $this->slickManager->getSkins();
79
80     $this->setUpContentWithItems($bundle);
81     $this->setUpRealImage();
82   }
83
84   /**
85    * Tests the Slick formatters.
86    */
87   public function testSlickFormatter() {
88     $bundle = $this->bundle;
89     $entity = $this->entity;
90
91     // Generate the render array to verify if the cache tags are as expected.
92     $build = $this->display->build($entity);
93     $build_empty = $this->displayEmpty->build($entity);
94
95     $render = $this->slickManager->getRenderer()->renderRoot($build);
96     $this->assertNotEmpty($render);
97
98     $render_empty = $this->slickManager->getRenderer()->renderRoot($build_empty[$this->testEmptyName]);
99     $this->assertEmpty($render_empty);
100
101     $this->assertInstanceOf('\Drupal\Core\Field\FieldItemListInterface', $this->testItems);
102     $this->assertInstanceOf('\Drupal\slick\Form\SlickAdminInterface', $this->formatterInstance->admin());
103     $this->assertInstanceOf('\Drupal\slick\SlickFormatterInterface', $this->formatterInstance->formatter());
104     $this->assertInstanceOf('\Drupal\slick\SlickManagerInterface', $this->formatterInstance->manager());
105
106     $component = $this->display->getComponent($this->testFieldName);
107     $this->assertEquals($this->testPluginId, $component['type']);
108     $this->assertEquals($this->testPluginId, $build[$this->testFieldName]['#formatter']);
109
110     $scopes = $this->formatterInstance->getScopedFormElements();
111     $this->assertEquals('slick', $scopes['namespace']);
112     $this->assertArrayHasKey('optionset', $scopes['settings']);
113
114     $summary = $this->formatterInstance->settingsSummary();
115     $this->assertNotEmpty($summary);
116   }
117
118   /**
119    * Tests for \Drupal\slick\SlickFormatter::testGetThumbnail().
120    *
121    * @param string $uri
122    *   The uri being tested.
123    * @param bool $expected
124    *   The expected output.
125    *
126    * @covers \Drupal\slick\SlickFormatter::getThumbnail
127    * @dataProvider providerTestGetThumbnail
128    */
129   public function testGetThumbnail($uri, $expected) {
130     $settings = $this->getFormatterSettings();
131     $settings['uri'] = empty($uri) ? '' : $this->uri;
132
133     $thumbnail = $this->slickFormatter->getThumbnail($settings, $this->image);
134     $this->assertEquals($expected, !empty($thumbnail));
135   }
136
137   /**
138    * Provide test cases for ::testGetThumbnail().
139    *
140    * @return array
141    *   An array of tested data.
142    */
143   public function providerTestGetThumbnail() {
144     $data[] = [
145       '',
146       FALSE,
147     ];
148     $data[] = [
149       'public://example.jpg',
150       TRUE,
151     ];
152
153     return $data;
154   }
155
156   /**
157    * Tests for \Drupal\slick\SlickFormatter.
158    *
159    * @param array $settings
160    *   The settings being tested.
161    * @param mixed|bool|string $expected
162    *   The expected output.
163    *
164    * @covers \Drupal\slick\SlickFormatter::buildSettings
165    * @dataProvider providerTestBuildSettings
166    */
167   public function testBuildSettings(array $settings, $expected) {
168     $format['settings'] = array_merge($this->getFormatterSettings(), $settings);
169
170     $this->slickFormatter->buildSettings($format, $this->testItems);
171     $this->assertArrayHasKey('bundle', $format['settings']);
172   }
173
174   /**
175    * Provide test cases for ::testBuildSettings().
176    *
177    * @return array
178    *   An array of tested data.
179    */
180   public function providerTestBuildSettings() {
181     $breakpoints = $this->getDataBreakpoints(TRUE);
182
183     $data[] = [
184       [
185         'vanilla'     => TRUE,
186         'breakpoints' => [],
187       ],
188       FALSE,
189     ];
190     $data[] = [
191       [
192         'vanilla'     => FALSE,
193         'breakpoints' => [],
194         'blazy'       => FALSE,
195         'ratio'       => 'fluid',
196       ],
197       TRUE,
198     ];
199     $data[] = [
200       [
201         'vanilla'     => FALSE,
202         'breakpoints' => $breakpoints,
203         'blazy'       => TRUE,
204       ],
205       TRUE,
206     ];
207
208     return $data;
209   }
210
211   /**
212    * Tests for \Drupal\slick\Form\SlickAdmin.
213    *
214    * @covers \Drupal\slick\Form\SlickAdmin::buildSettingsForm
215    * @covers \Drupal\slick\Form\SlickAdmin::openingForm
216    * @covers \Drupal\slick\Form\SlickAdmin::imageStyleForm
217    * @covers \Drupal\slick\Form\SlickAdmin::fieldableForm
218    * @covers \Drupal\slick\Form\SlickAdmin::mediaSwitchForm
219    * @covers \Drupal\slick\Form\SlickAdmin::gridForm
220    * @covers \Drupal\slick\Form\SlickAdmin::closingForm
221    * @covers \Drupal\slick\Form\SlickAdmin::finalizeForm
222    * @covers \Drupal\slick\Form\SlickAdmin::getOverridableOptions
223    * @covers \Drupal\slick\Form\SlickAdmin::getLayoutOptions
224    * @covers \Drupal\slick\Form\SlickAdmin::getOptionsetsByGroupOptions
225    * @covers \Drupal\slick\Form\SlickAdmin::getSkinsByGroupOptions
226    * @covers \Drupal\slick\Form\SlickAdmin::getSettingsSummary
227    * @covers \Drupal\slick\Form\SlickAdmin::getFieldOptions
228    */
229   public function testAdminOptions() {
230     $definition = $this->getSlickFormatterDefinition();
231     $form['test'] = ['#type' => 'hidden'];
232
233     $this->slickAdmin->buildSettingsForm($form, $definition);
234     $this->assertArrayHasKey('optionset', $form);
235
236     $options = $this->slickAdmin->getOverridableOptions();
237     $this->assertArrayHasKey('arrows', $options);
238
239     $options = $this->slickAdmin->getLayoutOptions();
240     $this->assertArrayHasKey('bottom', $options);
241
242     $options = $this->slickAdmin->getOptionsetsByGroupOptions();
243     $this->assertArrayHasKey('default', $options);
244
245     $options = $this->slickAdmin->getOptionsetsByGroupOptions('main');
246     $this->assertArrayHasKey('test', $options);
247
248     $options = $this->slickAdmin->getSkinsByGroupOptions('main');
249     $this->assertArrayHasKey('classic', $options);
250
251     $summary = $this->slickAdmin->getSettingsSummary($definition);
252     $this->assertNotEmpty($summary);
253
254     $options = $this->slickAdmin->getFieldOptions([], [], 'node');
255     $this->assertArrayHasKey($this->testFieldName, $options);
256   }
257
258 }