GridStackFileFormatterTest.php in GridStack 8
File
tests/src/Kernel/GridStackFileFormatterTest.php
View source
<?php
namespace Drupal\Tests\gridstack\Kernel;
use Drupal\Core\Form\FormState;
use Drupal\Tests\blazy\Kernel\BlazyKernelTestBase;
use Drupal\Tests\gridstack\Traits\GridStackUnitTestTrait;
class GridStackFileFormatterTest extends BlazyKernelTestBase {
use GridStackUnitTestTrait;
public static $modules = [
'system',
'user',
'field',
'file',
'image',
'filter',
'node',
'text',
'video_embed_media',
'blazy',
'gridstack',
'gridstack_test',
];
protected function setUp() {
parent::setUp();
$this
->installConfig(static::$modules);
$this
->installEntitySchema('gridstack');
$this->testFieldName = 'field_file_multiple';
$this->testEmptyName = 'field_file_multiple_empty';
$this->testFieldType = 'image';
$this->testPluginId = 'gridstack_file';
$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');
$bundle = $this->bundle;
$data = [
'field_name' => $this->testEmptyName,
'field_type' => 'image',
];
$this
->setUpContentTypeTest($bundle, $data);
$this
->setUpContentWithItems($bundle);
$this->display = $this
->setUpFormatterDisplay($bundle);
$data['plugin_id'] = $this->testPluginId;
$this->displayEmpty = $this
->setUpFormatterDisplay($bundle, $data);
$this->formatterInstance = $this
->getFormatterInstance();
}
public function testGridStackFormatter() {
$entity = $this->entity;
$build = $this->display
->build($entity);
$build_empty = $this->displayEmpty
->build($entity);
$component = $this->display
->getComponent($this->testFieldName);
$this
->assertEquals($this->testPluginId, $component['type']);
$render = $this->gridstackManager
->getRenderer()
->renderRoot($build);
$this
->assertNotEmpty($render);
$render_empty = $this->gridstackManager
->getRenderer()
->renderRoot($build_empty[$this->testEmptyName]);
$this
->assertEmpty($render_empty);
$scopes = $this->formatterInstance
->getScopedFormElements();
$this
->assertEquals($this->testPluginId, $scopes['plugin_id']);
$settings = $this->formatterInstance
->buildSettings();
$this
->assertEquals(TRUE, $settings['blazy']);
$form = [];
$form_state = new FormState();
$element = $this->formatterInstance
->settingsForm($form, $form_state);
$this
->assertArrayHasKey('optionset', $element);
}
}