You are here

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;

/**
 * Tests the GridStack field rendering using the text field type.
 *
 * @coversDefaultClass \Drupal\gridstack\Plugin\Field\FieldFormatter\GridStackFileFormatter
 * @requires module video_embed_media
 * @group gridstack
 */
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);
  }

}

Classes

Namesort descending Description
GridStackFileFormatterTest Tests the GridStack field rendering using the text field type.