function ResponsiveImageFormatterTest::setUp

Same name and namespace in other branches
  1. main core/modules/responsive_image/tests/src/Kernel/ResponsiveImageFormatterTest.php \Drupal\Tests\responsive_image\Kernel\ResponsiveImageFormatterTest::setUp()

Set the default field storage backend for fields created during tests.

Overrides FieldKernelTestBase::setUp

File

core/modules/responsive_image/tests/src/Kernel/ResponsiveImageFormatterTest.php, line 54

Class

ResponsiveImageFormatterTest
Tests the responsive image field rendering.

Namespace

Drupal\Tests\responsive_image\Kernel

Code

protected function setUp() : void {
  parent::setUp();
  $this->installConfig([
    'field',
  ]);
  $this->installEntitySchema('entity_test');
  $this->installEntitySchema('file');
  $this->installSchema('file', [
    'file_usage',
  ]);
  $this->entityType = 'entity_test';
  $bundle = 'entity_test';
  $this->fieldName = $this->randomMachineName();
  // Create a responsive image style.
  ResponsiveImageStyle::create([
    'id' => 'foo',
    'label' => 'Foo',
  ])->save();
  // Create an image field to be used with a responsive image formatter.
  FieldStorageConfig::create([
    'entity_type' => $this->entityType,
    'field_name' => $this->fieldName,
    'type' => 'image',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ])
    ->save();
  FieldConfig::create([
    'entity_type' => $this->entityType,
    'field_name' => $this->fieldName,
    'bundle' => $bundle,
    'settings' => [
      'file_extensions' => 'jpg',
    ],
  ])
    ->save();
  /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
  $this->display = EntityViewDisplay::create([
    'targetEntityType' => 'entity_test',
    'bundle' => 'entity_test',
    'mode' => 'default',
  ]);
  $this->display
    ->setComponent($this->fieldName, [
    'type' => 'responsive_image',
    'label' => 'hidden',
    'settings' => [
      'responsive_image_style' => 'foo',
      'image_link' => 'content',
    ],
  ])
    ->save();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.