You are here

class GridStackFileFormatterTest in GridStack 8

Tests the GridStack field rendering using the text field type.

@coversDefaultClass \Drupal\gridstack\Plugin\Field\FieldFormatter\GridStackFileFormatter @requires module video_embed_media @group gridstack

Hierarchy

Expanded class hierarchy of GridStackFileFormatterTest

File

tests/src/Kernel/GridStackFileFormatterTest.php, line 16

Namespace

Drupal\Tests\gridstack\Kernel
View source
class GridStackFileFormatterTest extends BlazyKernelTestBase {
  use GridStackUnitTestTrait;

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'system',
    'user',
    'field',
    'file',
    'image',
    'filter',
    'node',
    'text',
    'video_embed_media',
    'blazy',
    'gridstack',
    'gridstack_test',
  ];

  /**
   * {@inheritdoc}
   */
  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');

    // Create contents.
    $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();
  }

  /**
   * Tests the GridStack formatters.
   */
  public function testGridStackFormatter() {
    $entity = $this->entity;

    // Generate the render array to verify if the cache tags are as expected.
    $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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GridStackFileFormatterTest::$modules public static property
GridStackFileFormatterTest::setUp protected function
GridStackFileFormatterTest::testGridStackFormatter public function Tests the GridStack formatters.
GridStackUnitTestTrait::getGridStackFormatterDefinition protected function Defines scoped definition.