You are here

public function BlazyManagerTest::testBlazyManagerMethods in Blazy 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/BlazyManagerTest.php \Drupal\Tests\blazy\Kernel\BlazyManagerTest::testBlazyManagerMethods()

Tests cases for various methods.

@covers ::attach @covers ::buildDataBlazy @covers ::getLightboxes @covers ::buildSkins @covers ::getCache

@todo: Move some to unit tests.

File

tests/src/Kernel/BlazyManagerTest.php, line 373

Class

BlazyManagerTest
Tests the Blazy manager methods.

Namespace

Drupal\Tests\blazy\Kernel

Code

public function testBlazyManagerMethods() {

  // Tests Blazy attachments.
  $attach = [
    'blazy' => TRUE,
    'media_switch' => 'blazy_test',
  ];
  $attachments = $this->blazyManager
    ->attach($attach);
  $this
    ->assertArrayHasKey('blazy', $attachments['drupalSettings']);

  // Tests Blazy [data-blazy] attributes.
  $build = $this->data;
  $settings =& $build['settings'];
  $settings += BlazyDefault::itemSettings();
  $item = $build['item'];
  $settings['item'] = $item;
  $settings['uri'] = $this->uri;
  $settings['blazy_data'] = [];
  $settings['background'] = TRUE;
  $settings['breakpoints'] = $this
    ->getDataBreakpoints();

  // Ensure Blazy can be activated by breakpoints.
  $this->blazyManager
    ->buildDataBlazy($settings, $build);
  $this
    ->assertNotEmpty($settings['blazy']);

  // Tests Blazy lightboxes.
  $lightboxes = $this->blazyManager
    ->getLightboxes();
  $this
    ->assertFalse(in_array('nixbox', $lightboxes));

  // Tests for skins.
  // Tests skins with a single expected method BlazySkinTest::skins().
  $skins = $this->blazyManager
    ->buildSkins('blazy_test', '\\Drupal\\blazy_test\\BlazySkinTest');

  // Verify we have cached skins.
  $cid = 'blazy_test:skins';
  $cached_skins = $this->blazyManager
    ->getCache()
    ->get($cid);
  $this
    ->assertEquals($cid, $cached_skins->cid);
  $this
    ->assertEquals($skins, $cached_skins->data);

  // Verify multiple skin methods are respected.
  Cache::invalidateTags([
    $cid,
  ]);
  drupal_flush_all_caches();
  $this
    ->assertFalse($this->blazyManager
    ->getCache()
    ->get($cid));
  $skins = $this->blazyManager
    ->buildSkins('blazy_test', '\\Drupal\\blazy_test\\BlazySkinTest', [
    'skins',
    'features',
  ]);
  $this
    ->assertArrayHasKey('features', $skins);
  $cached_skins = $this->blazyManager
    ->getCache()
    ->get($cid);
  $this
    ->assertEquals($skins, $cached_skins->data);
}