You are here

class ResponsiveImageStyleConfigEntityUnitTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/responsive_image/tests/src/Unit/ResponsiveImageStyleConfigEntityUnitTest.php \Drupal\Tests\responsive_image\Unit\ResponsiveImageStyleConfigEntityUnitTest
  2. 10 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 15

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 type manager used for testing.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityTypeManager;

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

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->entityType = $this
      ->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
    $this->entityType
      ->expects($this
      ->any())
      ->method('getProvider')
      ->will($this
      ->returnValue('responsive_image'));
    $this->entityTypeManager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $this->entityTypeManager
      ->expects($this
      ->any())
      ->method('getDefinition')
      ->with('responsive_image_style')
      ->will($this
      ->returnValue($this->entityType));
    $this->breakpointManager = $this
      ->createMock('\\Drupal\\breakpoint\\BreakpointManagerInterface');
    $container = new ContainerBuilder();
    $container
      ->set('entity_type.manager', $this->entityTypeManager);
    $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
        ->createMock('Drupal\\Core\\Config\\Entity\\ConfigEntityInterface');
      $mock
        ->expects($this
        ->any())
        ->method('getConfigDependencyName')
        ->willReturn('image.style.' . $style);
      $styles[$style] = $mock;
    }
    $storage = $this
      ->createMock('\\Drupal\\Core\\Config\\Entity\\ConfigEntityStorageInterface');
    $storage
      ->expects($this
      ->any())
      ->method('loadMultiple')
      ->with(array_keys($styles))
      ->willReturn($styles);
    $this->entityTypeManager
      ->expects($this
      ->any())
      ->method('getStorage')
      ->with('image_style')
      ->willReturn($storage);
    $entity_type_repository = $this
      ->getMockForAbstractClass(EntityTypeRepositoryInterface::class);
    $entity_type_repository
      ->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([
      'bartik' => 'theme',
      'toolbar' => 'module',
    ]);
    \Drupal::getContainer()
      ->set('entity_type.repository', $entity_type_repository);
    $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([]);
    $this
      ->assertFalse($entity
      ->hasImageStyleMappings());
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', [
      'image_mapping_type' => 'image_style',
      'image_mapping' => '',
    ]);
    $this
      ->assertFalse($entity
      ->hasImageStyleMappings());
    $entity
      ->removeImageStyleMappings();
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', [
      'image_mapping_type' => 'sizes',
      'image_mapping' => [
        'sizes' => '(min-width:700px) 700px, 100vw',
        'sizes_image_styles' => [],
      ],
    ]);
    $this
      ->assertFalse($entity
      ->hasImageStyleMappings());
    $entity
      ->removeImageStyleMappings();
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', [
      'image_mapping_type' => 'sizes',
      'image_mapping' => [
        'sizes' => '',
        'sizes_image_styles' => [
          'large' => 'large',
        ],
      ],
    ]);
    $this
      ->assertFalse($entity
      ->hasImageStyleMappings());
    $entity
      ->removeImageStyleMappings();
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', [
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'large',
    ]);
    $this
      ->assertTrue($entity
      ->hasImageStyleMappings());
    $entity
      ->removeImageStyleMappings();
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', [
      'image_mapping_type' => 'sizes',
      'image_mapping' => [
        'sizes' => '(min-width:700px) 700px, 100vw',
        'sizes_image_styles' => [
          'large' => 'large',
        ],
      ],
    ]);
    $this
      ->assertTrue($entity
      ->hasImageStyleMappings());
  }

  /**
   * @covers ::addImageStyleMapping
   * @covers ::getImageStyleMapping
   */
  public function testGetImageStyleMapping() {
    $entity = new ResponsiveImageStyle([
      '',
    ]);
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', [
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'large',
    ]);
    $expected = [
      '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([
      '',
    ]);
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', [
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'large',
    ]);
    $entity
      ->addImageStyleMapping('test_breakpoint', '2x', [
      'image_mapping_type' => 'sizes',
      'image_mapping' => [
        'sizes' => '(min-width:700px) 700px, 100vw',
        'sizes_image_styles' => [
          'large' => 'large',
        ],
      ],
    ]);
    $entity
      ->addImageStyleMapping('test_breakpoint2', '1x', [
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'thumbnail',
    ]);
    $entity
      ->addImageStyleMapping('test_breakpoint2', '2x', [
      'image_mapping_type' => 'image_style',
      'image_mapping' => '_original image_',
    ]);
    $expected = [
      'test_breakpoint' => [
        '1x' => [
          'breakpoint_id' => 'test_breakpoint',
          'multiplier' => '1x',
          'image_mapping_type' => 'image_style',
          'image_mapping' => 'large',
        ],
        '2x' => [
          'breakpoint_id' => 'test_breakpoint',
          'multiplier' => '2x',
          'image_mapping_type' => 'sizes',
          'image_mapping' => [
            'sizes' => '(min-width:700px) 700px, 100vw',
            'sizes_image_styles' => [
              'large' => 'large',
            ],
          ],
        ],
      ],
      'test_breakpoint2' => [
        '1x' => [
          'breakpoint_id' => 'test_breakpoint2',
          'multiplier' => '1x',
          'image_mapping_type' => 'image_style',
          'image_mapping' => 'thumbnail',
        ],
        '2x' => [
          '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', [
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'medium',
    ]);
    $expected['test_breakpoint2']['2x'] = [
      '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', [
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'large',
    ]);
    $expected['test_breakpoint2']['2x'] = [
      '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([
      '',
    ]);
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', [
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'large',
    ]);
    $entity
      ->addImageStyleMapping('test_breakpoint', '2x', [
      'image_mapping_type' => 'sizes',
      'image_mapping' => [
        'sizes' => '(min-width:700px) 700px, 100vw',
        'sizes_image_styles' => [
          'large' => 'large',
        ],
      ],
    ]);
    $entity
      ->addImageStyleMapping('test_breakpoint2', '1x', [
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'thumbnail',
    ]);
    $expected = [
      [
        'breakpoint_id' => 'test_breakpoint',
        'multiplier' => '1x',
        'image_mapping_type' => 'image_style',
        'image_mapping' => 'large',
      ],
      [
        'breakpoint_id' => 'test_breakpoint',
        'multiplier' => '2x',
        'image_mapping_type' => 'sizes',
        'image_mapping' => [
          'sizes' => '(min-width:700px) 700px, 100vw',
          'sizes_image_styles' => [
            'large' => 'large',
          ],
        ],
      ],
      [
        '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([
      '',
    ]);
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', [
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'large',
    ]);
    $entity
      ->addImageStyleMapping('test_breakpoint', '2x', [
      'image_mapping_type' => 'sizes',
      'image_mapping' => [
        'sizes' => '(min-width:700px) 700px, 100vw',
        'sizes_image_styles' => [
          'large' => 'large',
        ],
      ],
    ]);
    $entity
      ->addImageStyleMapping('test_breakpoint2', '1x', [
      '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([
      'breakpoint_group' => 'test_group',
    ]);
    $entity
      ->addImageStyleMapping('test_breakpoint', '1x', [
      'image_mapping_type' => 'image_style',
      'image_mapping' => 'large',
    ]);
    $entity
      ->addImageStyleMapping('test_breakpoint', '2x', [
      'image_mapping_type' => 'sizes',
      'image_mapping' => [
        'sizes' => '(min-width:700px) 700px, 100vw',
        'sizes_image_styles' => [
          'large' => 'large',
        ],
      ],
    ]);
    $entity
      ->addImageStyleMapping('test_breakpoint2', '1x', [
      '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
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.
ResponsiveImageStyleConfigEntityUnitTest::$breakpointManager protected property The breakpoint manager used for testing.
ResponsiveImageStyleConfigEntityUnitTest::$entityType protected property The entity type used for testing.
ResponsiveImageStyleConfigEntityUnitTest::$entityTypeManager protected property The entity type manager 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. 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.