You are here

public function GridStackCrudTest::testGridStackCrud in GridStack 8.2

Same name and namespace in other branches
  1. 8 tests/src/Kernel/GridStackCrudTest.php \Drupal\Tests\gridstack\Kernel\GridStackCrudTest::testGridStackCrud()

Tests CRUD operations for GridStack optionsets.

File

tests/src/Kernel/GridStackCrudTest.php, line 47

Class

GridStackCrudTest
Tests creation, loading, updating, deleting of GridStack optionsets.

Namespace

Drupal\Tests\gridstack\Kernel

Code

public function testGridStackCrud() {

  // Add a GridStack optionset with minimum data only.
  $empty = GridStack::create([
    'name' => 'test_empty',
    'label' => 'Empty gridstack',
    'json' => [],
  ]);
  $empty
    ->save();
  $this
    ->verifyGridStackOptionset($empty);

  // Add main GridStack optionset with possible properties.
  $main = GridStack::create([
    'name' => 'test_main',
    'label' => 'Test main',
    'json' => [],
  ]);
  $main
    ->save();
  $settings = [
    'cellHeight' => 80,
    'minWidth' => 480,
    'verticalMargin' => 15,
  ] + $main
    ->getSettings();
  $main
    ->setSettings($settings);
  $main
    ->getSetting('float');
  $main
    ->setSetting('float', TRUE);
  $main
    ->getOption('use_framework');
  $main
    ->setOption('use_framework', TRUE);
  $main
    ->save();
  $this
    ->assertEquals(TRUE, $main
    ->getSetting('float'));
  $this
    ->assertEquals(TRUE, $main
    ->getOption('use_framework'));
  $this
    ->verifyGridStackOptionset($main);

  // Alter some gridstack optionset properties and save again.
  $main
    ->set('label', 'Altered gridstack');
  $main
    ->setOption('use_framework', TRUE);
  $main
    ->save();
  $this
    ->verifyGridStackOptionset($main);

  // Disable auto and save again.
  $main
    ->setSetting('auto', FALSE);
  $main
    ->save();
  $this
    ->verifyGridStackOptionset($main);

  // Delete the gridstack optionset.
  $main
    ->delete();
  $gridstacks = GridStack::loadMultiple();
  $this
    ->assertFalse(isset($gridstacks[$main
    ->id()]), 'GridStack::loadMultiple: Deleted gridstack optionset no longer exists.');

  // Tests for optionset Frontend JS.
  $frontend = GridStack::load('frontend');
  $icon_uri = $frontend
    ->getIconUri();
  $this
    ->assertTrue(strpos($icon_uri, 'frontend.png') !== FALSE);
  $icon_url = $frontend
    ->getIconUrl();
  $this
    ->assertTrue(strpos($icon_url, 'frontend.png') !== FALSE);

  // @todo move to GridStackEnginePluginBase tests.
  // @todo $string = 'data-role|complimentary';
  // @todo $attributes = $frontend->parseAttributes($string);
  // @todo $this->assertEquals('complimentary', $attributes['data-role']);
  $breakpoints = $frontend
    ->getJson('breakpoints');
  $this
    ->assertTrue(is_string($breakpoints));
  $end = $frontend
    ->getLastBreakpoint('grids');
  $this
    ->assertEquals(0, $end[0][0]);
  $lg = $frontend
    ->getBreakpoints('lg');
  $this
    ->assertEquals(12, $lg['column']);
  $grid = $frontend
    ->getBreakpointItem('lg', 0, 'x', 'grids');
  $this
    ->assertEquals(0, $grid);
  $auto = $frontend
    ->getOptions([
    'settings',
    'float',
  ]);
  $this
    ->assertEquals(FALSE, $auto);
  $options = $frontend
    ->getOptions();
  $this
    ->assertArrayHasKey('settings', $options);
  $summary = $frontend
    ->getJsonSummaryBreakpoints('lg');
  $this
    ->assertTrue(strpos($summary, 'width') === FALSE);
  $settings = [
    '_root' => 'grids',
  ];
  $breakpoints = $frontend
    ->breakpointsToArray();
  $this
    ->assertNotEmpty($breakpoints);

  // Tests for optionset Test which is a clone of Bootstrap.
  $bootstrap = GridStack::load('test');
  $this
    ->assertNotEmpty($bootstrap);
  $nested = $bootstrap
    ->getNestedGridsByDelta(1);
  $this
    ->assertNotEmpty($nested);
  $settings = [
    '_root' => 'nested',
  ];
  $breakpoints = $bootstrap
    ->breakpointsToArray();
  $this
    ->assertNotEmpty($breakpoints);
}