You are here

class GridStackManagerTest in GridStack 8.2

Same name and namespace in other branches
  1. 8 tests/src/Kernel/GridStackManagerTest.php \Drupal\Tests\gridstack\Kernel\GridStackManagerTest

Tests the GridStack manager methods.

@coversDefaultClass \Drupal\gridstack\GridStackManager

@group gridstack

Hierarchy

Expanded class hierarchy of GridStackManagerTest

File

tests/src/Kernel/GridStackManagerTest.php, line 18

Namespace

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

  /**
   * The messenger service.
   *
   * @var \Drupal\Core\Messenger\Messenger
   */
  protected $messenger;

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

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this
      ->installConfig([
      'field',
      'node',
      'views',
      'blazy',
      'gridstack',
    ]);
    $bundle = $this->bundle;
    $this->fileSystem = $this->container
      ->get('file_system');
    $this->messenger = $this->container
      ->get('messenger');
    $this->gridstackAdmin = $this->container
      ->get('gridstack.admin');
    $this->blazyAdminFormatter = $this->gridstackAdmin;
    $this->gridstackFormatter = $this->container
      ->get('gridstack.formatter');
    $this->gridstackManager = $this->container
      ->get('gridstack.manager');
    $this->gridstackForm = GridStackForm::create($this->container);
    $this->testPluginId = 'gridstack_image';
    $this->testFieldName = 'field_gridstack_image';
    $this->maxItems = 7;
    $this->maxParagraphs = 2;
    $settings['fields']['field_text_multiple'] = 'text';
    $this
      ->setUpContentTypeTest($bundle, $settings);
    $this
      ->setUpContentWithItems($bundle);
    $this
      ->setUpRealImage();
    $this->display = $this
      ->setUpFormatterDisplay($bundle);
    $this->formatterInstance = $this
      ->getFormatterInstance();

    // Enable Boostrap support.
    $this->gridstackManager
      ->getConfigFactory()
      ->getEditable('gridstack.settings')
      ->set('framework', 'bootstrap')
      ->save();
  }

  /**
   * Tests cases for various methods.
   *
   * @covers ::attach
   */
  public function testGridStackManagerMethods() {
    $manager = $this->gridstackManager;
    $settings = [
      'use_js' => TRUE,
      'skin' => 'selena',
      'width' => 11,
      'breakpoints' => [
        'lg' => [
          'column' => 11,
        ],
      ],
    ] + $this
      ->getFormatterSettings();

    // The gridstack is moved into plugin, it is no longer loaded by default.
    $attachments = $manager
      ->attach($settings);
    $this
      ->assertArrayNotHasKey('gridstack', $attachments['drupalSettings']);
  }

  /**
   * Tests for GridStack build.
   *
   * @param bool $items
   *   Whether to provide items, or not.
   * @param array $settings
   *   The settings being tested.
   * @param mixed|bool|string $expected
   *   The expected output.
   *
   * @covers ::build
   * @covers ::preRenderGridStack
   * @dataProvider providerTestGridStackBuild
   */
  public function testBuild($items, array $settings, $expected) {
    $manager = $this->gridstackManager;
    $defaults = $this
      ->getFormatterSettings() + GridStackDefault::htmlSettings();
    $settings = array_merge($defaults, $settings) + GridStackDefault::imageSettings();
    $settings['optionset'] = 'test';
    $build = $this->display
      ->build($this->entity);
    $items = !$items ? [] : $build[$this->testFieldName]['#build']['items'];
    $build = [
      'items' => $items,
      'settings' => $settings,
      'optionset' => GridStack::loadWithFallback($settings['optionset']),
    ];
    $gridstack = $manager
      ->build($build);
    $this
      ->assertEquals($expected, !empty($gridstack));
  }

  /**
   * Provide test cases for ::testBuild().
   *
   * @return array
   *   An array of tested data.
   */
  public function providerTestGridStackBuild() {
    $data[] = [
      FALSE,
      [],
      FALSE,
    ];
    $data[] = [
      TRUE,
      [
        'skin' => 'selena',
      ],
      TRUE,
    ];
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GridStackManagerTest::$messenger protected property The messenger service.
GridStackManagerTest::$modules public static property
GridStackManagerTest::providerTestGridStackBuild public function Provide test cases for ::testBuild().
GridStackManagerTest::setUp protected function
GridStackManagerTest::testBuild public function Tests for GridStack build.
GridStackManagerTest::testGridStackManagerMethods public function Tests cases for various methods.
GridStackUnitTestTrait::getGridStackFormatterDefinition protected function Defines scoped definition.