You are here

class BlazyUnitTest in Blazy 8.2

Same name and namespace in other branches
  1. 8 tests/src/Unit/BlazyUnitTest.php \Drupal\Tests\blazy\Unit\BlazyUnitTest

@coversDefaultClass \Drupal\blazy\Blazy

@group blazy

Hierarchy

Expanded class hierarchy of BlazyUnitTest

File

tests/src/Unit/BlazyUnitTest.php, line 16

Namespace

Drupal\Tests\blazy\Unit
View source
class BlazyUnitTest extends UnitTestCase {
  use BlazyUnitTestTrait;
  use BlazyManagerUnitTestTrait;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this
      ->setUpVariables();
    $this
      ->setUpUnitServices();
    $this
      ->setUpUnitContainer();
    $this
      ->setUpMockImage();
  }

  /**
   * Tests \Drupal\blazy\Blazy::buildIframe.
   *
   * @param array $data
   *   The input data which can be string, or integer.
   * @param mixed|bool|int $expected
   *   The expected output.
   *
   * @covers ::buildIframe
   * @covers \Drupal\blazy\BlazyDefault::entitySettings
   * @dataProvider providerTestBuildIframe
   */
  public function testBuildIframe(array $data, $expected) {
    $variables = [
      'attributes' => [],
      'image' => [],
    ];
    $settings = BlazyDefault::entitySettings();
    $settings['embed_url'] = '//www.youtube.com/watch?v=E03HFA923kw';
    $settings['type'] = 'video';
    $settings['bundle'] = 'remote_video';
    $variables['settings'] = array_merge($settings, $data);
    Blazy::buildIframe($variables);
    $this
      ->assertNotEmpty($variables[$expected]);
  }

  /**
   * Provide test cases for ::testBuildIframe().
   */
  public function providerTestBuildIframe() {
    return [
      [
        [
          'media_switch' => 'media',
          'ratio' => 'fluid',
        ],
        'iframe',
      ],
      [
        [
          'media_switch' => '',
          'ratio' => '',
          'width' => 640,
          'height' => 360,
        ],
        'iframe',
      ],
    ];
  }

  /**
   * Tests \Drupal\blazy\Blazy::preprocessBlazy.
   *
   * @param array $settings
   *   The settings being tested.
   * @param object $item
   *   Whether to provide image item, or not.
   * @param bool $expected_image
   *   Whether to expect an image, or not.
   * @param bool $expected_iframe
   *   Whether to expect an iframe, or not.
   *
   * @covers \Drupal\blazy\Blazy::preprocessBlazy
   * @covers \Drupal\blazy\Blazy::urlAndDimensions
   * @covers \Drupal\blazy\BlazyDefault::entitySettings
   * @dataProvider providerPreprocessBlazy
   */
  public function testPreprocessBlazy(array $settings, $item, $expected_image, $expected_iframe) {
    $variables = [
      'attributes' => [],
    ];
    $build = $this->data;
    $settings = array_merge($build['settings'], $settings);
    $settings += BlazyDefault::itemSettings();
    $settings['blazy'] = TRUE;
    $settings['lazy'] = 'blazy';
    $settings['image_style'] = '';
    $settings['thumbnail_style'] = '';
    if (!empty($settings['embed_url'])) {
      $settings = array_merge(BlazyDefault::entitySettings(), $settings);
    }
    $variables['element']['#item'] = $item == TRUE ? $this->testItem : NULL;
    $variables['element']['#settings'] = $settings;
    Blazy::preprocessBlazy($variables);
    $image = $expected_image == TRUE ? !empty($variables['image']) : empty($variables['image']);
    $iframe = $expected_iframe == TRUE ? !empty($variables['iframe']) : empty($variables['iframe']);
    $this
      ->assertTrue($image);
    $this
      ->assertTrue($iframe);
    $this
      ->assertEquals($settings['blazy'], $variables['settings']['blazy']);
  }

  /**
   * Provider for ::testPreprocessBlazy.
   */
  public function providerPreprocessBlazy() {
    $uri = 'public://example.jpg';
    $data[] = [
      [
        'background' => FALSE,
        'uri' => '',
      ],
      TRUE,
      FALSE,
      FALSE,
    ];
    $data[] = [
      [
        'background' => TRUE,
        'uri' => $uri,
      ],
      TRUE,
      FALSE,
      FALSE,
    ];
    $data[] = [
      [
        'background' => FALSE,
        'ratio' => 'fluid',
        'sizes' => '100w',
        'width' => 640,
        'height' => 360,
        'uri' => $uri,
      ],
      TRUE,
      TRUE,
      FALSE,
    ];
    $data[] = [
      [
        'background' => FALSE,
        'embed_url' => '//www.youtube.com/watch?v=E03HFA923kw',
        'media_switch' => 'media',
        'ratio' => 'fluid',
        'sizes' => '100w',
        'scheme' => 'youtube',
        'type' => 'video',
        'uri' => $uri,
        'use_media' => TRUE,
      ],
      TRUE,
      TRUE,
      TRUE,
    ];
    return $data;
  }

  /**
   * Tests BlazyManager image with lightbox support.
   *
   * This is here as we need file_create_url() for both Blazy and its lightbox.
   *
   * @param array $settings
   *   The settings being tested.
   *
   * @covers \Drupal\blazy\BlazyManager::preRenderBlazy
   * @covers \Drupal\blazy\BlazyLightbox::build
   * @covers \Drupal\blazy\BlazyLightbox::buildCaptions
   * @dataProvider providerTestPreRenderImageLightbox
   */
  public function todoTestPreRenderImageLightbox(array $settings = []) {
    $build = $this->data;
    $settings += BlazyDefault::itemSettings();
    $settings['count'] = $this->maxItems;
    $settings['uri'] = $this->uri;
    $settings['box_style'] = '';
    $settings['box_media_style'] = '';
    $build['settings'] = array_merge($build['settings'], $settings);
    $switch_css = str_replace('_', '-', $settings['media_switch']);
    foreach ([
      'caption',
      'media',
      'wrapper',
    ] as $key) {
      $build['settings'][$key . '_attributes']['class'][] = $key . '-test';
    }
    $element = $this
      ->doPreRenderImage($build);
    if ($settings['media_switch'] == 'content') {
      $this
        ->assertEquals($settings['content_url'], $element['#url']);
      $this
        ->assertArrayHasKey('#url', $element);
    }
    else {
      $this
        ->assertArrayHasKey('data-' . $switch_css . '-trigger', $element['#url_attributes']);
      $this
        ->assertArrayHasKey('#url', $element);
    }
  }

  /**
   * Provide test cases for ::testPreRenderImageLightbox().
   *
   * @return array
   *   An array of tested data.
   */
  public function providerTestPreRenderImageLightbox() {
    $data[] = [
      [
        'box_caption' => '',
        'content_url' => 'node/1',
        'dimension' => '',
        'lightbox' => FALSE,
        'media_switch' => 'content',
        'type' => 'image',
      ],
    ];
    $data[] = [
      [
        'box_caption' => 'auto',
        'lightbox' => TRUE,
        'media_switch' => 'colorbox',
        'type' => 'image',
      ],
    ];
    $data[] = [
      [
        'box_caption' => 'alt',
        'lightbox' => TRUE,
        'media_switch' => 'colorbox',
        'type' => 'image',
      ],
    ];
    $data[] = [
      [
        'box_caption' => 'title',
        'lightbox' => TRUE,
        'media_switch' => 'photobox',
        'type' => 'image',
      ],
    ];
    $data[] = [
      [
        'box_caption' => 'alt_title',
        'lightbox' => TRUE,
        'media_switch' => 'colorbox',
        'type' => 'image',
      ],
    ];
    $data[] = [
      [
        'box_caption' => 'title_alt',
        'lightbox' => TRUE,
        'media_switch' => 'photobox',
        'type' => 'image',
      ],
    ];
    $data[] = [
      [
        'box_caption' => 'entity_title',
        'lightbox' => TRUE,
        'media_switch' => 'photobox',
        'type' => 'image',
      ],
    ];
    $data[] = [
      [
        'box_caption' => 'custom',
        'box_caption_custom' => '[node:field_text_multiple]',
        'dimension' => '640x360',
        'embed_url' => '//www.youtube.com/watch?v=E03HFA923kw',
        'lightbox' => TRUE,
        'media_switch' => 'photobox',
        'scheme' => 'youtube',
        'type' => 'video',
      ],
    ];
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlazyManagerUnitTestTrait::setUpImageStyle protected function Prepare image styles.
BlazyManagerUnitTestTrait::setUpResponsiveImageStyle protected function Prepare Responsive image styles.
BlazyManagerUnitTestTrait::setUpUnitContainer protected function Setup the unit manager.
BlazyManagerUnitTestTrait::setUpUnitServices protected function Setup the unit manager.
BlazyPropertiesTestTrait::$blazyAdmin protected property The blazy admin service.
BlazyPropertiesTestTrait::$blazyAdminFormatter protected property The blazy admin service.
BlazyPropertiesTestTrait::$blazyEntity protected property The blazy entity service.
BlazyPropertiesTestTrait::$blazyManager protected property The blazy manager service.
BlazyPropertiesTestTrait::$bundle protected property The bundle name.
BlazyPropertiesTestTrait::$display protected property The entity display.
BlazyPropertiesTestTrait::$entities protected property The entity.
BlazyPropertiesTestTrait::$entity protected property The entity.
BlazyPropertiesTestTrait::$entityFieldManager protected property The entity manager.
BlazyPropertiesTestTrait::$entityPluginId protected property The tested entity reference formatter ID.
BlazyPropertiesTestTrait::$entityType protected property The tested entity type.
BlazyPropertiesTestTrait::$filterFormatFull protected property The filter format.
BlazyPropertiesTestTrait::$filterFormatRestricted protected property The filter format.
BlazyPropertiesTestTrait::$formatterDefinition protected property The formatter definition.
BlazyPropertiesTestTrait::$formatterPluginManager protected property The formatter plugin manager.
BlazyPropertiesTestTrait::$image protected property The created image item.
BlazyPropertiesTestTrait::$maxItems protected property The maximum number of created images.
BlazyPropertiesTestTrait::$maxParagraphs protected property The maximum number of created paragraphs.
BlazyPropertiesTestTrait::$node protected property The node entity.
BlazyPropertiesTestTrait::$referencedEntity protected property The referenced node entity.
BlazyPropertiesTestTrait::$referencingEntity protected property The node entity.
BlazyPropertiesTestTrait::$skins protected property The tested skins.
BlazyPropertiesTestTrait::$targetBundles protected property The target bundle names.
BlazyPropertiesTestTrait::$testEmptyName protected property The tested empty field name.
BlazyPropertiesTestTrait::$testEmptyType protected property The tested empty field type.
BlazyPropertiesTestTrait::$testFieldName protected property The tested field name.
BlazyPropertiesTestTrait::$testFieldType protected property The tested field type.
BlazyPropertiesTestTrait::$testItem protected property The created item.
BlazyPropertiesTestTrait::$testItems protected property The created items.
BlazyPropertiesTestTrait::$testPluginId protected property The tested formatter ID.
BlazyPropertiesTestTrait::$typeDefinition protected property The tested type definitions.
BlazyUnitTest::providerPreprocessBlazy public function Provider for ::testPreprocessBlazy.
BlazyUnitTest::providerTestBuildIframe public function Provide test cases for ::testBuildIframe().
BlazyUnitTest::providerTestPreRenderImageLightbox public function Provide test cases for ::testPreRenderImageLightbox().
BlazyUnitTest::setUp protected function Overrides UnitTestCase::setUp
BlazyUnitTest::testBuildIframe public function Tests \Drupal\blazy\Blazy::buildIframe.
BlazyUnitTest::testPreprocessBlazy public function Tests \Drupal\blazy\Blazy::preprocessBlazy.
BlazyUnitTest::todoTestPreRenderImageLightbox public function Tests BlazyManager image with lightbox support.
BlazyUnitTestTrait::$formatterSettings protected property The formatter settings.
BlazyUnitTestTrait::doPreRenderImage protected function Pre render Blazy image.
BlazyUnitTestTrait::getCacheMetaData protected function Return dummy cache metadata.
BlazyUnitTestTrait::getDefaulEntityFormatterDefinition protected function Returns the default field formatter definition.
BlazyUnitTestTrait::getDefaultFieldDefinition protected function Returns the default field definition.
BlazyUnitTestTrait::getDefaultFields protected function Returns dummy fields for an entity reference.
BlazyUnitTestTrait::getDefaultFormatterDefinition protected function Returns the default field formatter definition.
BlazyUnitTestTrait::getFormatterDefinition protected function Returns the field formatter definition along with settings.
BlazyUnitTestTrait::getFormatterSettings protected function Returns sensible formatter settings for testing purposes.
BlazyUnitTestTrait::setFormatterDefinition protected function Sets the field formatter definition.
BlazyUnitTestTrait::setFormatterSetting protected function Sets formatter setting.
BlazyUnitTestTrait::setFormatterSettings protected function Sets formatter settings.
BlazyUnitTestTrait::setUpMockImage protected function Setup the unit images.
BlazyUnitTestTrait::setUpUnitImages protected function Setup the unit images.
BlazyUnitTestTrait::setUpVariables protected function Set up Blazy variables.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.