You are here

class BlazyUnitTest in Blazy 8

Same name and namespace in other branches
  1. 8.2 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();
  }

  /**
   * Test \Drupal\blazy\Blazy\widthFromDescriptors.
   *
   * @param string $data
   *   The input data which can be string, or integer.
   * @param mixed|bool|int $expected
   *   The expected output.
   *
   * @covers ::widthFromDescriptors
   * @dataProvider providerTestWidthFromDescriptors
   */
  public function testWidthFromDescriptors($data, $expected) {
    $result = Blazy::widthFromDescriptors($data);
    $this
      ->assertSame($result, $expected);
  }

  /**
   * Provide test cases for ::testWidthFromDescriptors().
   */
  public function providerTestWidthFromDescriptors() {
    return [
      [
        1024,
        1024,
      ],
      [
        '1024',
        1024,
      ],
      [
        '769w',
        769,
      ],
      [
        '640w 2x',
        640,
      ],
      [
        '2x 640w',
        640,
      ],
      [
        'xYz123',
        FALSE,
      ],
    ];
  }

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

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

  /**
   * Tests building Blazy attributes.
   *
   * @param array $settings
   *   The settings being tested.
   * @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::buildAttributes
   * @covers \Drupal\blazy\Blazy::buildBreakpointAttributes
   * @covers \Drupal\blazy\Blazy::buildUrl
   * @covers \Drupal\blazy\Dejavu\BlazyDefault::entitySettings
   * @dataProvider providerBuildAttributes
   */
  public function testBuildAttributes(array $settings, $expected_image, $expected_iframe) {
    $variables = [
      'attributes' => [],
    ];
    $build = $this->data;
    $settings = array_merge($build['settings'], $settings) + BlazyDefault::itemSettings();
    $settings['breakpoints'] = [];
    $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'] = $this->testItem;
    $variables['element']['#settings'] = $settings;
    Blazy::buildAttributes($variables);
    $image = $expected_image == TRUE ? !empty($variables['image']) : empty($variables['image']);
    $iframe = $expected_iframe == TRUE ? !empty($variables['iframe_attributes']) : empty($variables['iframe_attributes']);
    $this
      ->assertTrue($image);
    $this
      ->assertTrue($iframe);
    $this
      ->assertEquals($settings['blazy'], $variables['settings']['blazy']);
  }

  /**
   * Provider for ::testBuildAttributes.
   */
  public function providerBuildAttributes() {
    $uri = 'public://example.jpg';
    $data[] = [
      [
        'background' => FALSE,
        'uri' => '',
      ],
      FALSE,
      FALSE,
    ];
    $data[] = [
      [
        'background' => FALSE,
        'responsive_image_style_id' => 'blazy_responsive_test',
        'uri' => $uri,
      ],
      TRUE,
      FALSE,
    ];
    $data[] = [
      [
        'background' => TRUE,
        'uri' => $uri,
      ],
      FALSE,
      FALSE,
    ];
    $data[] = [
      [
        'background' => FALSE,
        'ratio' => 'fluid',
        'sizes' => '100w',
        'width' => 640,
        'height' => 360,
        'uri' => $uri,
      ],
      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,
      ],
      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::preRenderImage
   * @covers \Drupal\blazy\BlazyLightbox::build
   * @covers \Drupal\blazy\BlazyLightbox::buildCaptions
   * @dataProvider providerTestPreRenderImageLightbox
   */
  public function testPreRenderImageLightbox(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::$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::$formatterDefinition protected property The formatter definition.
BlazyPropertiesTestTrait::$formatterPluginManager protected property The formatter plugin manager.
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::providerBuildAttributes public function Provider for ::testBuildAttributes.
BlazyUnitTest::providerTestBuildIframeAttributes public function Provide test cases for ::testBuildIframeAttributes().
BlazyUnitTest::providerTestPreRenderImageLightbox public function Provide test cases for ::testPreRenderImageLightbox().
BlazyUnitTest::providerTestWidthFromDescriptors public function Provide test cases for ::testWidthFromDescriptors().
BlazyUnitTest::setUp protected function Overrides UnitTestCase::setUp
BlazyUnitTest::testBuildAttributes public function Tests building Blazy attributes.
BlazyUnitTest::testBuildIframeAttributes public function Tests \Drupal\blazy\Blazy\buildIframeAttributes.
BlazyUnitTest::testPreRenderImageLightbox public function Tests BlazyManager image with lightbox support.
BlazyUnitTest::testWidthFromDescriptors public function Test \Drupal\blazy\Blazy\widthFromDescriptors.
BlazyUnitTestTrait::$formatterSettings protected property The formatter settings.
BlazyUnitTestTrait::doPreRenderImage protected function Pre render Blazy image.
BlazyUnitTestTrait::getCacheMetaData protected function Return dummy cache metadata.
BlazyUnitTestTrait::getDataBreakpoints protected function Add partially empty data for breakpoints.
BlazyUnitTestTrait::getDefaulEntityFormatterDefinition protected function Returns the default field formatter definition.
BlazyUnitTestTrait::getDefaultFields protected function Returns dummy fields for an entity reference.
BlazyUnitTestTrait::getDefaultFormatterDefinition protected function Returns the default field formatter definition.
BlazyUnitTestTrait::getEmptyBreakpoints protected function Add empty data for breakpoints.
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.