View source
<?php
namespace Drupal\Tests\gridstack\Kernel;
use Drupal\Tests\blazy\Kernel\BlazyKernelTestBase;
use Drupal\Tests\gridstack\Traits\GridStackUnitTestTrait;
class GridStackFormatterTest extends BlazyKernelTestBase {
use GridStackUnitTestTrait;
public static $modules = [
'system',
'user',
'field',
'file',
'image',
'filter',
'node',
'text',
'blazy',
'gridstack',
'gridstack_ui',
'gridstack_test',
];
protected function setUp() {
parent::setUp();
$this
->installConfig(static::$modules);
$this
->installEntitySchema('gridstack');
$this->testFieldName = 'field_image_multiple';
$this->testEmptyName = 'field_image_multiple_empty';
$this->testPluginId = 'gridstack_image';
$this->maxItems = 7;
$this->maxParagraphs = 2;
$this->gridstackAdmin = $this->container
->get('gridstack.admin');
$this->gridstackManager = $this->container
->get('gridstack.manager');
$this->gridstackFormatter = $this->container
->get('gridstack.formatter');
$data['fields'] = [
'field_video' => 'text',
'field_image' => 'image',
'field_image_multiple_empty' => 'image',
];
$bundle = $this->bundle;
$this
->setUpContentTypeTest($bundle, $data);
$settings = [
'optionset' => 'frontend',
] + $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->gridstackManager
->getSkins();
$this
->setUpContentWithItems($bundle);
$this
->setUpRealImage();
$this->blazyManager
->getConfigFactory()
->getEditable('gridstack.settings')
->set('framework', 'bootstrap')
->save();
}
public function testGridStackFormatter() {
$entity = $this->entity;
$build = $this->display
->build($entity);
$build_empty = $this->displayEmpty
->build($entity);
$render = $this->gridstackManager
->getRenderer()
->renderRoot($build);
$this
->assertNotEmpty($render);
$render_empty = $this->gridstackManager
->getRenderer()
->renderRoot($build_empty[$this->testEmptyName]);
$this
->assertEmpty($render_empty);
$this
->assertInstanceOf('\\Drupal\\Core\\Field\\FieldItemListInterface', $this->testItems);
$this
->assertInstanceOf('\\Drupal\\gridstack\\Form\\GridStackAdminInterface', $this->formatterInstance
->admin());
$this
->assertInstanceOf('\\Drupal\\gridstack\\GridStackFormatterInterface', $this->formatterInstance
->formatter());
$this
->assertInstanceOf('\\Drupal\\gridstack\\GridStackManagerInterface', $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('gridstack', $scopes['namespace']);
$this
->assertArrayHasKey('optionset', $scopes['settings']);
$summary = $this->formatterInstance
->settingsSummary();
$this
->assertNotEmpty($summary);
}
public function testBuildSettings(array $settings, $expected) {
$format['settings'] = array_merge($this
->getFormatterSettings(), $settings);
$this->gridstackFormatter
->buildSettings($format, $this->testItems);
$this
->assertArrayHasKey('bundle', $format['settings']);
}
public function providerTestBuildSettings() {
$breakpoints = $this
->getDataBreakpoints(TRUE);
$data[] = [
[
'breakpoints' => [],
],
FALSE,
];
$data[] = [
[
'breakpoints' => [],
'blazy' => FALSE,
],
TRUE,
];
$data[] = [
[
'breakpoints' => $breakpoints,
'blazy' => TRUE,
],
TRUE,
];
return $data;
}
public function testAdminOptions() {
$definition = $this
->getGridStackFormatterDefinition();
$form['test'] = [
'#type' => 'hidden',
];
$this->gridstackAdmin
->buildSettingsForm($form, $definition);
$this
->assertArrayHasKey('optionset', $form);
$this->gridstackAdmin
->finalizeForm($form, $definition);
$this
->assertArrayHasKey('closing', $form);
$options = $this->gridstackAdmin
->getLayoutOptions();
$this
->assertArrayHasKey('bottom', $options);
$options = $this->gridstackAdmin
->getSkinOptions();
$this
->assertArrayHasKey('selena', $options);
$summary = $this->gridstackAdmin
->getSettingsSummary($definition);
$this
->assertNotEmpty($summary);
$options = $this->gridstackAdmin
->getFieldOptions([], [], 'node');
$this
->assertArrayHasKey($this->testFieldName, $options);
}
}