3 namespace Drupal\Tests\slick\Kernel;
5 use Drupal\Tests\blazy\Kernel\BlazyKernelTestBase;
6 use Drupal\Tests\slick\Traits\SlickUnitTestTrait;
9 * Tests the Slick field rendering using the image field type.
11 * @coversDefaultClass \Drupal\slick\Plugin\Field\FieldFormatter\SlickImageFormatter
14 class SlickFormatterTest extends BlazyKernelTestBase {
16 use SlickUnitTestTrait;
23 public static $modules = [
41 protected function setUp() {
44 $this->installConfig(static::$modules);
45 $this->installEntitySchema('slick');
47 $this->testFieldName = 'field_image_multiple';
48 $this->testEmptyName = 'field_image_multiple_empty';
49 $this->testPluginId = 'slick_image';
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');
57 'field_video' => 'text',
58 'field_image' => 'image',
59 'field_image_multiple_empty' => 'image',
63 $bundle = $this->bundle;
64 $this->setUpContentTypeTest($bundle, $data);
67 'optionset' => 'test',
68 'optionset_thumbnail' => 'test_nav',
69 ] + $this->getFormatterSettings();
71 $data['settings'] = $settings;
72 $this->display = $this->setUpFormatterDisplay($bundle, $data);
74 $data['plugin_id'] = $this->testPluginId;
75 $this->displayEmpty = $this->setUpFormatterDisplay($bundle, $data);
77 $this->formatterInstance = $this->getFormatterInstance();
78 $this->skins = $this->slickManager->getSkins();
80 $this->setUpContentWithItems($bundle);
81 $this->setUpRealImage();
85 * Tests the Slick formatters.
87 public function testSlickFormatter() {
88 $bundle = $this->bundle;
89 $entity = $this->entity;
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);
95 $render = $this->slickManager->getRenderer()->renderRoot($build);
96 $this->assertNotEmpty($render);
98 $render_empty = $this->slickManager->getRenderer()->renderRoot($build_empty[$this->testEmptyName]);
99 $this->assertEmpty($render_empty);
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());
106 $component = $this->display->getComponent($this->testFieldName);
107 $this->assertEquals($this->testPluginId, $component['type']);
108 $this->assertEquals($this->testPluginId, $build[$this->testFieldName]['#formatter']);
110 $scopes = $this->formatterInstance->getScopedFormElements();
111 $this->assertEquals('slick', $scopes['namespace']);
112 $this->assertArrayHasKey('optionset', $scopes['settings']);
114 $summary = $this->formatterInstance->settingsSummary();
115 $this->assertNotEmpty($summary);
119 * Tests for \Drupal\slick\SlickFormatter::testGetThumbnail().
122 * The uri being tested.
123 * @param bool $expected
124 * The expected output.
126 * @covers \Drupal\slick\SlickFormatter::getThumbnail
127 * @dataProvider providerTestGetThumbnail
129 public function testGetThumbnail($uri, $expected) {
130 $settings = $this->getFormatterSettings();
131 $settings['uri'] = empty($uri) ? '' : $this->uri;
133 $thumbnail = $this->slickFormatter->getThumbnail($settings, $this->image);
134 $this->assertEquals($expected, !empty($thumbnail));
138 * Provide test cases for ::testGetThumbnail().
141 * An array of tested data.
143 public function providerTestGetThumbnail() {
149 'public://example.jpg',
157 * Tests for \Drupal\slick\SlickFormatter.
159 * @param array $settings
160 * The settings being tested.
161 * @param mixed|bool|string $expected
162 * The expected output.
164 * @covers \Drupal\slick\SlickFormatter::buildSettings
165 * @dataProvider providerTestBuildSettings
167 public function testBuildSettings(array $settings, $expected) {
168 $format['settings'] = array_merge($this->getFormatterSettings(), $settings);
170 $this->slickFormatter->buildSettings($format, $this->testItems);
171 $this->assertArrayHasKey('bundle', $format['settings']);
175 * Provide test cases for ::testBuildSettings().
178 * An array of tested data.
180 public function providerTestBuildSettings() {
181 $breakpoints = $this->getDataBreakpoints(TRUE);
202 'breakpoints' => $breakpoints,
212 * Tests for \Drupal\slick\Form\SlickAdmin.
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
229 public function testAdminOptions() {
230 $definition = $this->getSlickFormatterDefinition();
231 $form['test'] = ['#type' => 'hidden'];
233 $this->slickAdmin->buildSettingsForm($form, $definition);
234 $this->assertArrayHasKey('optionset', $form);
236 $options = $this->slickAdmin->getOverridableOptions();
237 $this->assertArrayHasKey('arrows', $options);
239 $options = $this->slickAdmin->getLayoutOptions();
240 $this->assertArrayHasKey('bottom', $options);
242 $options = $this->slickAdmin->getOptionsetsByGroupOptions();
243 $this->assertArrayHasKey('default', $options);
245 $options = $this->slickAdmin->getOptionsetsByGroupOptions('main');
246 $this->assertArrayHasKey('test', $options);
248 $options = $this->slickAdmin->getSkinsByGroupOptions('main');
249 $this->assertArrayHasKey('classic', $options);
251 $summary = $this->slickAdmin->getSettingsSummary($definition);
252 $this->assertNotEmpty($summary);
254 $options = $this->slickAdmin->getFieldOptions([], [], 'node');
255 $this->assertArrayHasKey($this->testFieldName, $options);