You are here

public function SlickCrudTest::testSlickCrud in Slick Carousel 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/SlickCrudTest.php \Drupal\Tests\slick\Kernel\SlickCrudTest::testSlickCrud()

Tests CRUD operations for Slick optionsets.

File

tests/src/Kernel/SlickCrudTest.php, line 49

Class

SlickCrudTest
Tests creation, loading, updating, deleting of Slick optionsets.

Namespace

Drupal\Tests\slick\Kernel

Code

public function testSlickCrud() {

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

  // Add main Slick optionset with possible properties.
  $main = Slick::create([
    'name' => 'test_main',
    'label' => 'Test main',
  ]);
  $main
    ->save();
  $settings = [
    'arrows' => FALSE,
    'dots' => TRUE,
    'lazyLoad' => 'progressive',
  ] + $main
    ->getSettings();
  $main
    ->set('group', 'main');
  $main
    ->set('optimized', TRUE);
  $main
    ->setSettings($settings);
  $main
    ->save();
  $breakpoints = $main
    ->getBreakpoints();
  $this
    ->assertEmpty($breakpoints);
  $this
    ->assertEquals('main', $main
    ->getGroup());
  $optimized = $main
    ->optimized();
  $this
    ->assertNotEmpty($optimized);
  $this
    ->verifySlickOptionset($main);

  // @todo: Use dataProvider.
  try {
    $responsive_options = $main
      ->getResponsiveOptions();
  } catch (\PHPUnit_Framework_Exception $e) {
  }
  $this
    ->assertTrue(TRUE);
  $responsive_settings = $settings;
  $main
    ->set('breakpoints', 2);
  $breakpoints = [
    481,
    769,
  ];
  foreach ($breakpoints as $key => $breakpoint) {
    $main
      ->setResponsiveSettings($responsive_settings, $key, 'settings');
    $main
      ->setResponsiveSettings($breakpoint, $key, 'breakpoint');
  }
  $main
    ->save();
  $responsive_options = $main
    ->getResponsiveOptions();
  foreach ($responsive_options as $key => $responsive) {
    $this
      ->assertEquals('progressive', $responsive['settings']['lazyLoad']);
    $this
      ->assertEquals($breakpoints[$key], $responsive['breakpoint']);
  }
  $options = $main
    ->getSettings();
  $cleaned = $main
    ->removeDefaultValues($options);
  $this
    ->assertArrayHasKey('responsive', $cleaned);
  foreach ($responsive_options as $key => $responsive) {
    $main
      ->setResponsiveSettings(TRUE, $key, 'unslick');
  }
  $main
    ->save();
  $options = $main
    ->getSettings();
  $cleaned = $main
    ->removeDefaultValues($options);
  foreach ($cleaned['responsive'] as $key => $responsive) {
    $this
      ->assertEquals('unslick', $responsive['settings']);
  }

  // Alter some slick optionset properties and save again.
  $main
    ->set('label', 'Altered slick');
  $main
    ->setSetting('mobileFirst', TRUE);
  $main
    ->save();
  $this
    ->verifySlickOptionset($main);

  // Enable autoplay and save again.
  $main
    ->setSetting('autoplay', TRUE);
  $main
    ->save();
  $this
    ->verifySlickOptionset($main);

  // Add nav Slick optionset with possible properties.
  $nav = Slick::create([
    'name' => 'test_nav',
    'label' => 'Test nav',
  ]);
  $skin = $nav
    ->getSkin();
  $this
    ->assertEmpty($skin);
  $nav
    ->setSetting('cssEaseBezier', 'easeInQuad');
  $nav
    ->save();
  $settings = $nav
    ->getSettings();
  $nav
    ->removeWastedDependentOptions($settings);
  $this
    ->assertArrayNotHasKey('cssEaseBezier', $settings);
  $this
    ->assertEquals('easeInQuad', $settings['cssEase']);
  $this
    ->assertEmpty($nav
    ->getSetting('mobileFirst'));
  $nav
    ->setSetting('mobileFirst', TRUE);
  $nav
    ->save();
  $this
    ->assertNotEmpty($nav
    ->getSetting('mobileFirst'));

  // @todo: Use dataProvider.
  try {
    $mobile_first = $nav
      ->getOptions('settings', 'mobileFirst');
  } catch (\PHPUnit_Framework_Exception $e) {
  }
  $this
    ->assertTrue(!empty($mobile_first));
  try {
    $mobile_first = $nav
      ->getOptions([
      'settings',
      'mobileFirst',
    ]);
  } catch (\PHPUnit_Framework_Exception $e) {
  }
  $this
    ->assertTrue(!empty($mobile_first));
  $settings = $nav
    ->getOptions('settings');
  $this
    ->assertArrayHasKey('mobileFirst', $settings);
  $options = $nav
    ->getOptions();
  $this
    ->assertArrayHasKey('settings', $options);
  $merged = array_merge(Slick::defaultSettings() + Slick::jsSettings(), $settings);
  $nav
    ->setSettings($merged);
  $nav
    ->save();
  $this
    ->assertTrue(!empty($nav
    ->getSetting('mobileFirst')));
  $nav
    ->removeDefaultValues($settings);
  $this
    ->assertArrayNotHasKey('lazyLoad', $settings);

  // Delete the slick optionset.
  $nav
    ->delete();
  $slicks = Slick::loadMultiple();
  $this
    ->assertFalse(isset($slicks[$nav
    ->id()]), 'Slick::loadMultiple: Disabled slick optionset no longer exists.');
  $id1 = Slick::getHtmlId('slick');
  $this
    ->assertNotEmpty($id1);
  $id2 = Slick::getHtmlId('slick', 'slick-image');
  $this
    ->assertEquals($id2, 'slick-image');
}