You are here

class ResponsiveImageStyleConfigEntityUnitTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/responsive_image/tests/src/Unit/ResponsiveImageStyleConfigEntityUnitTest.php \Drupal\Tests\responsive_image\Unit\ResponsiveImageStyleConfigEntityUnitTest

@coversDefaultClass \Drupal\responsive_image\Entity\ResponsiveImageStyle @group block

Hierarchy

Expanded class hierarchy of ResponsiveImageStyleConfigEntityUnitTest

File

core/modules/responsive_image/tests/src/Unit/ResponsiveImageStyleConfigEntityUnitTest.php, line 18
Contains \Drupal\Tests\responsive_image\Unit\ResponsiveImageStyleConfigEntityUnitTest.

Namespace

Drupal\Tests\responsive_image\Unit
View source
class ResponsiveImageStyleConfigEntityUnitTest extends UnitTestCase {

  /**
   * The entity type used for testing.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityType;

  /**
   * The entity manager used for testing.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityManager;

  /**
   * The breakpoint manager used for testing.
   *
   * @var \Drupal\breakpoint\BreakpointManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $breakpointManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->entityType = $this
      ->getMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
    $this->entityType
      ->expects($this
      ->any())
      ->method('getProvider')
      ->will($this
      ->returnValue('responsive_image'));
    $this->entityManager = $this
      ->getMock('\\Drupal\\Core\\Entity\\EntityManagerInterface');
    $this->entityManager
      ->expects($this
      ->any())
      ->method('getDefinition')
      ->with('responsive_image_style')
      ->will($this
      ->returnValue($this->entityType));
    $this->breakpointManager = $this
      ->getMock('\\Drupal\\breakpoint\\BreakpointManagerInterface');
    $container = new ContainerBuilder();
    $container
      ->set('entity.manager', $this->entityManager);
    $container
      ->set('breakpoint.manager', $this->breakpointManager);
    \Drupal::setContainer($container);
  }

  /**
   * @covers ::calculateDependencies
   */
  public function testCalculateDependencies() {

    // Set up image style loading mock.
    $styles = [];
    foreach ([
      'fallback',
      'small',
      'medium',
      'large',
    ] as $style) {
      $mock = $this
        ->getMock('Drupal\\Core\\Config\\Entity\\ConfigEntityInterface');
      $mock
        ->expects($this
        ->any())
        ->method('getConfigDependencyName')
        ->willReturn('image.style.' . $style);
      $styles[$style] = $mock;
    }
    $storage = $this
      ->getMock('\\Drupal\\Core\\Config\\Entity\\ConfigEntityStorageInterface');
    $storage
      ->expects($this
      ->any())
      ->method('loadMultiple')
      ->with(array_keys($styles))
      ->willReturn($styles);
    $this->entityManager
      ->expects($this
      ->any())
      ->method('getStorage')
      ->with('image_style')
      ->willReturn($storage);
    $this->entityManager
      ->expects($this
      ->any())
      ->method('getEntityTypeFromClass')
      ->with('Drupal\\image\\Entity\\ImageStyle')
      ->willReturn('image_style');
    $entity = new ResponsiveImageStyle([
      'breakpoint_group' => 'test_group',
    ]);
    $entity
      ->setBreakpointGroup('test_group');
    $entity
      ->setFallbackImageStyle('fallback');
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', [
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'small',
    ]);
    $entity
      ->addImageStyleMapping('test_breakpoint', '2x', [
      'image_mapping_type' => 'sizes',
      'image_mapping' => [
        'sizes' => '(min-width:700px) 700px, 100vw',
        'sizes_image_styles' => [
          'medium' => 'medium',
          'large' => 'large',
        ],
      ],
    ]);
    $this->breakpointManager
      ->expects($this
      ->any())
      ->method('getGroupProviders')
      ->with('test_group')
      ->willReturn(array(
      'bartik' => 'theme',
      'toolbar' => 'module',
    ));
    $dependencies = $entity
      ->calculateDependencies()
      ->getDependencies();
    $this
      ->assertEquals([
      'toolbar',
    ], $dependencies['module']);
    $this
      ->assertEquals([
      'bartik',
    ], $dependencies['theme']);
    $this
      ->assertEquals([
      'image.style.fallback',
      'image.style.large',
      'image.style.medium',
      'image.style.small',
    ], $dependencies['config']);
  }

  /**
   * @covers ::addImageStyleMapping
   * @covers ::hasImageStyleMappings
   */
  public function testHasImageStyleMappings() {
    $entity = new ResponsiveImageStyle(array());
    $this
      ->assertFalse($entity
      ->hasImageStyleMappings());
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', array(
      'image_mapping_type' => 'image_style',
      'image_mapping' => '',
    ));
    $this
      ->assertFalse($entity
      ->hasImageStyleMappings());
    $entity
      ->removeImageStyleMappings();
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', array(
      'image_mapping_type' => 'sizes',
      'image_mapping' => array(
        'sizes' => '(min-width:700px) 700px, 100vw',
        'sizes_image_styles' => array(),
      ),
    ));
    $this
      ->assertFalse($entity
      ->hasImageStyleMappings());
    $entity
      ->removeImageStyleMappings();
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', array(
      'image_mapping_type' => 'sizes',
      'image_mapping' => array(
        'sizes' => '',
        'sizes_image_styles' => array(
          'large' => 'large',
        ),
      ),
    ));
    $this
      ->assertFalse($entity
      ->hasImageStyleMappings());
    $entity
      ->removeImageStyleMappings();
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', array(
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'large',
    ));
    $this
      ->assertTrue($entity
      ->hasImageStyleMappings());
    $entity
      ->removeImageStyleMappings();
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', array(
      'image_mapping_type' => 'sizes',
      'image_mapping' => array(
        'sizes' => '(min-width:700px) 700px, 100vw',
        'sizes_image_styles' => array(
          'large' => 'large',
        ),
      ),
    ));
    $this
      ->assertTrue($entity
      ->hasImageStyleMappings());
  }

  /**
   * @covers ::addImageStyleMapping
   * @covers ::getImageStyleMapping
   */
  public function testGetImageStyleMapping() {
    $entity = new ResponsiveImageStyle(array(
      '',
    ));
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', array(
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'large',
    ));
    $expected = array(
      'breakpoint_id' => 'test_breakpoint',
      'multiplier' => '1x',
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'large',
    );
    $this
      ->assertEquals($expected, $entity
      ->getImageStyleMapping('test_breakpoint', '1x'));
    $this
      ->assertNull($entity
      ->getImageStyleMapping('test_unknown_breakpoint', '1x'));
  }

  /**
   * @covers ::addImageStyleMapping
   * @covers ::getKeyedImageStyleMappings
   */
  public function testGetKeyedImageStyleMappings() {
    $entity = new ResponsiveImageStyle(array(
      '',
    ));
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', array(
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'large',
    ));
    $entity
      ->addImageStyleMapping('test_breakpoint', '2x', array(
      'image_mapping_type' => 'sizes',
      'image_mapping' => array(
        'sizes' => '(min-width:700px) 700px, 100vw',
        'sizes_image_styles' => array(
          'large' => 'large',
        ),
      ),
    ));
    $entity
      ->addImageStyleMapping('test_breakpoint2', '1x', array(
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'thumbnail',
    ));
    $entity
      ->addImageStyleMapping('test_breakpoint2', '2x', array(
      'image_mapping_type' => 'image_style',
      'image_mapping' => '_original image_',
    ));
    $expected = array(
      'test_breakpoint' => array(
        '1x' => array(
          'breakpoint_id' => 'test_breakpoint',
          'multiplier' => '1x',
          'image_mapping_type' => 'image_style',
          'image_mapping' => 'large',
        ),
        '2x' => array(
          'breakpoint_id' => 'test_breakpoint',
          'multiplier' => '2x',
          'image_mapping_type' => 'sizes',
          'image_mapping' => array(
            'sizes' => '(min-width:700px) 700px, 100vw',
            'sizes_image_styles' => array(
              'large' => 'large',
            ),
          ),
        ),
      ),
      'test_breakpoint2' => array(
        '1x' => array(
          'breakpoint_id' => 'test_breakpoint2',
          'multiplier' => '1x',
          'image_mapping_type' => 'image_style',
          'image_mapping' => 'thumbnail',
        ),
        '2x' => array(
          'breakpoint_id' => 'test_breakpoint2',
          'multiplier' => '2x',
          'image_mapping_type' => 'image_style',
          'image_mapping' => '_original image_',
        ),
      ),
    );
    $this
      ->assertEquals($expected, $entity
      ->getKeyedImageStyleMappings());

    // Add another mapping to ensure keyed mapping static cache is rebuilt.
    $entity
      ->addImageStyleMapping('test_breakpoint2', '2x', array(
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'medium',
    ));
    $expected['test_breakpoint2']['2x'] = array(
      'breakpoint_id' => 'test_breakpoint2',
      'multiplier' => '2x',
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'medium',
    );
    $this
      ->assertEquals($expected, $entity
      ->getKeyedImageStyleMappings());

    // Overwrite a mapping to ensure keyed mapping static cache is rebuilt.
    $entity
      ->addImageStyleMapping('test_breakpoint2', '2x', array(
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'large',
    ));
    $expected['test_breakpoint2']['2x'] = array(
      'breakpoint_id' => 'test_breakpoint2',
      'multiplier' => '2x',
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'large',
    );
    $this
      ->assertEquals($expected, $entity
      ->getKeyedImageStyleMappings());
  }

  /**
   * @covers ::addImageStyleMapping
   * @covers ::getImageStyleMappings
   */
  public function testGetImageStyleMappings() {
    $entity = new ResponsiveImageStyle(array(
      '',
    ));
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', array(
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'large',
    ));
    $entity
      ->addImageStyleMapping('test_breakpoint', '2x', array(
      'image_mapping_type' => 'sizes',
      'image_mapping' => array(
        'sizes' => '(min-width:700px) 700px, 100vw',
        'sizes_image_styles' => array(
          'large' => 'large',
        ),
      ),
    ));
    $entity
      ->addImageStyleMapping('test_breakpoint2', '1x', array(
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'thumbnail',
    ));
    $expected = array(
      array(
        'breakpoint_id' => 'test_breakpoint',
        'multiplier' => '1x',
        'image_mapping_type' => 'image_style',
        'image_mapping' => 'large',
      ),
      array(
        'breakpoint_id' => 'test_breakpoint',
        'multiplier' => '2x',
        'image_mapping_type' => 'sizes',
        'image_mapping' => array(
          'sizes' => '(min-width:700px) 700px, 100vw',
          'sizes_image_styles' => array(
            'large' => 'large',
          ),
        ),
      ),
      array(
        'breakpoint_id' => 'test_breakpoint2',
        'multiplier' => '1x',
        'image_mapping_type' => 'image_style',
        'image_mapping' => 'thumbnail',
      ),
    );
    $this
      ->assertEquals($expected, $entity
      ->getImageStyleMappings());
  }

  /**
   * @covers ::addImageStyleMapping
   * @covers ::removeImageStyleMappings
   */
  public function testRemoveImageStyleMappings() {
    $entity = new ResponsiveImageStyle(array(
      '',
    ));
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', array(
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'large',
    ));
    $entity
      ->addImageStyleMapping('test_breakpoint', '2x', array(
      'image_mapping_type' => 'sizes',
      'image_mapping' => array(
        'sizes' => '(min-width:700px) 700px, 100vw',
        'sizes_image_styles' => array(
          'large' => 'large',
        ),
      ),
    ));
    $entity
      ->addImageStyleMapping('test_breakpoint2', '1x', array(
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'thumbnail',
    ));
    $this
      ->assertTrue($entity
      ->hasImageStyleMappings());
    $entity
      ->removeImageStyleMappings();
    $this
      ->assertEmpty($entity
      ->getImageStyleMappings());
    $this
      ->assertEmpty($entity
      ->getKeyedImageStyleMappings());
    $this
      ->assertFalse($entity
      ->hasImageStyleMappings());
  }

  /**
   * @covers ::setBreakpointGroup
   * @covers ::getBreakpointGroup
   */
  public function testSetBreakpointGroup() {
    $entity = new ResponsiveImageStyle(array(
      'breakpoint_group' => 'test_group',
    ));
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', array(
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'large',
    ));
    $entity
      ->addImageStyleMapping('test_breakpoint', '2x', array(
      'image_mapping_type' => 'sizes',
      'image_mapping' => array(
        'sizes' => '(min-width:700px) 700px, 100vw',
        'sizes_image_styles' => array(
          'large' => 'large',
        ),
      ),
    ));
    $entity
      ->addImageStyleMapping('test_breakpoint2', '1x', array(
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'thumbnail',
    ));

    // Ensure that setting to same group does not remove mappings.
    $entity
      ->setBreakpointGroup('test_group');
    $this
      ->assertTrue($entity
      ->hasImageStyleMappings());
    $this
      ->assertEquals('test_group', $entity
      ->getBreakpointGroup());

    // Ensure that changing the group removes mappings.
    $entity
      ->setBreakpointGroup('test_group2');
    $this
      ->assertEquals('test_group2', $entity
      ->getBreakpointGroup());
    $this
      ->assertFalse($entity
      ->hasImageStyleMappings());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ResponsiveImageStyleConfigEntityUnitTest::$breakpointManager protected property The breakpoint manager used for testing.
ResponsiveImageStyleConfigEntityUnitTest::$entityManager protected property The entity manager used for testing.
ResponsiveImageStyleConfigEntityUnitTest::$entityType protected property The entity type used for testing.
ResponsiveImageStyleConfigEntityUnitTest::setUp protected function Overrides UnitTestCase::setUp
ResponsiveImageStyleConfigEntityUnitTest::testCalculateDependencies public function @covers ::calculateDependencies
ResponsiveImageStyleConfigEntityUnitTest::testGetImageStyleMapping public function @covers ::addImageStyleMapping @covers ::getImageStyleMapping
ResponsiveImageStyleConfigEntityUnitTest::testGetImageStyleMappings public function @covers ::addImageStyleMapping @covers ::getImageStyleMappings
ResponsiveImageStyleConfigEntityUnitTest::testGetKeyedImageStyleMappings public function @covers ::addImageStyleMapping @covers ::getKeyedImageStyleMappings
ResponsiveImageStyleConfigEntityUnitTest::testHasImageStyleMappings public function @covers ::addImageStyleMapping @covers ::hasImageStyleMappings
ResponsiveImageStyleConfigEntityUnitTest::testRemoveImageStyleMappings public function @covers ::addImageStyleMapping @covers ::removeImageStyleMappings
ResponsiveImageStyleConfigEntityUnitTest::testSetBreakpointGroup public function @covers ::setBreakpointGroup @covers ::getBreakpointGroup
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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.