View source
<?php
namespace Drupal\Tests\slick\Kernel;
use Drupal\Tests\blazy\Kernel\BlazyKernelTestBase;
use Drupal\Tests\slick\Traits\SlickUnitTestTrait;
class SlickFormatterTest extends BlazyKernelTestBase {
use SlickUnitTestTrait;
public static $modules = [
'system',
'user',
'field',
'file',
'image',
'filter',
'node',
'text',
'blazy',
'slick',
'slick_ui',
'slick_test',
];
protected function setUp() {
parent::setUp();
$this
->installConfig(static::$modules);
$this
->installEntitySchema('slick');
$this->testFieldName = 'field_image_multiple';
$this->testEmptyName = 'field_image_multiple_empty';
$this->testPluginId = 'slick_image';
$this->maxItems = 7;
$this->maxParagraphs = 2;
$this->slickAdmin = $this->container
->get('slick.admin');
$this->slickManager = $this->container
->get('slick.manager');
$this->slickFormatter = $this->container
->get('slick.formatter');
$data['fields'] = [
'field_video' => 'text',
'field_image' => 'image',
'field_image_multiple_empty' => 'image',
];
$bundle = $this->bundle;
$this
->setUpContentTypeTest($bundle, $data);
$settings = [
'optionset' => 'test',
'optionset_thumbnail' => 'test_nav',
] + $this
->getFormatterSettings();
$data['settings'] = $settings;
$this->display = $this
->setUpFormatterDisplay($bundle, $data);
$data['plugin_id'] = $this->testPluginId;
$this->displayEmpty = $this
->setUpFormatterDisplay($bundle, $data);
$this->formatterInstance = $this
->getFormatterInstance();
$this->skins = $this->slickManager
->getSkins();
$this
->setUpContentWithItems($bundle);
$this
->setUpRealImage();
}
public function testSlickFormatter() {
$entity = $this->entity;
$build = $this->display
->build($entity);
$build_empty = $this->displayEmpty
->build($entity);
$render = $this->slickManager
->getRenderer()
->renderRoot($build);
$this
->assertNotEmpty($render);
$render_empty = $this->slickManager
->getRenderer()
->renderRoot($build_empty[$this->testEmptyName]);
$this
->assertEmpty($render_empty);
$this
->assertInstanceOf('\\Drupal\\Core\\Field\\FieldItemListInterface', $this->testItems);
$this
->assertInstanceOf('\\Drupal\\slick\\Form\\SlickAdminInterface', $this->formatterInstance
->admin());
$this
->assertInstanceOf('\\Drupal\\slick\\SlickFormatterInterface', $this->formatterInstance
->formatter());
$this
->assertInstanceOf('\\Drupal\\slick\\SlickManagerInterface', $this->formatterInstance
->manager());
$component = $this->display
->getComponent($this->testFieldName);
$this
->assertEquals($this->testPluginId, $component['type']);
$this
->assertEquals($this->testPluginId, $build[$this->testFieldName]['#formatter']);
$scopes = $this->formatterInstance
->getScopedFormElements();
$this
->assertEquals('slick', $scopes['namespace']);
$this
->assertArrayHasKey('optionset', $scopes['settings']);
$summary = $this->formatterInstance
->settingsSummary();
$this
->assertNotEmpty($summary);
}
public function testGetThumbnail($uri, $expected) {
$settings = $this
->getFormatterSettings();
$settings['uri'] = empty($uri) ? '' : $this->uri;
$thumbnail = $this->slickFormatter
->getThumbnail($settings, $this->image);
$this
->assertEquals($expected, !empty($thumbnail));
}
public function providerTestGetThumbnail() {
$data[] = [
'',
FALSE,
];
$data[] = [
'public://example.jpg',
TRUE,
];
return $data;
}
public function testBuildSettings(array $settings, $expected) {
$format['settings'] = array_merge($this
->getFormatterSettings(), $settings);
$this->slickFormatter
->buildSettings($format, $this->testItems);
$this
->assertArrayHasKey('bundle', $format['settings']);
}
public function providerTestBuildSettings() {
$breakpoints = $this
->getDataBreakpoints(TRUE);
$data[] = [
[
'vanilla' => TRUE,
'breakpoints' => [],
],
FALSE,
];
$data[] = [
[
'vanilla' => FALSE,
'breakpoints' => [],
'blazy' => FALSE,
'ratio' => 'fluid',
],
TRUE,
];
$data[] = [
[
'vanilla' => FALSE,
'breakpoints' => $breakpoints,
'blazy' => TRUE,
],
TRUE,
];
return $data;
}
public function testAdminOptions() {
$definition = $this
->getSlickFormatterDefinition();
$form['test'] = [
'#type' => 'hidden',
];
$this->slickAdmin
->buildSettingsForm($form, $definition);
$this
->assertArrayHasKey('optionset', $form);
$options = $this->slickAdmin
->getOverridableOptions();
$this
->assertArrayHasKey('arrows', $options);
$options = $this->slickAdmin
->getLayoutOptions();
$this
->assertArrayHasKey('bottom', $options);
$options = $this->slickAdmin
->getOptionsetsByGroupOptions();
$this
->assertArrayHasKey('default', $options);
$options = $this->slickAdmin
->getOptionsetsByGroupOptions('main');
$this
->assertArrayHasKey('test', $options);
$options = $this->slickAdmin
->getSkinsByGroupOptions('main');
$this
->assertArrayHasKey('classic', $options);
$summary = $this->slickAdmin
->getSettingsSummary($definition);
$this
->assertNotEmpty($summary);
$options = $this->slickAdmin
->getFieldOptions([], [], 'node');
$this
->assertArrayHasKey($this->testFieldName, $options);
}
}