You are here

public function ViewsConfigDependenciesIntegrationTest::testImage in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Kernel/ViewsConfigDependenciesIntegrationTest.php \Drupal\Tests\views\Kernel\ViewsConfigDependenciesIntegrationTest::testImage()
  2. 9 core/modules/views/tests/src/Kernel/ViewsConfigDependenciesIntegrationTest.php \Drupal\Tests\views\Kernel\ViewsConfigDependenciesIntegrationTest::testImage()

Tests integration with image module.

File

core/modules/views/tests/src/Kernel/ViewsConfigDependenciesIntegrationTest.php, line 49

Class

ViewsConfigDependenciesIntegrationTest
Tests integration of views with other modules.

Namespace

Drupal\Tests\views\Kernel

Code

public function testImage() {

  /** @var \Drupal\image\ImageStyleInterface $style */
  $style = ImageStyle::create([
    'name' => 'foo',
  ]);
  $style
    ->save();

  // Create a new image field 'bar' to be used in 'entity_test_fields' view.
  FieldStorageConfig::create([
    'entity_type' => 'entity_test',
    'field_name' => 'bar',
    'type' => 'image',
  ])
    ->save();
  FieldConfig::create([
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
    'field_name' => 'bar',
  ])
    ->save();

  /** @var \Drupal\views\ViewEntityInterface $view */
  $view = View::load('entity_test_fields');
  $display =& $view
    ->getDisplay('default');

  // Add the 'bar' image field to 'entity_test_fields' view.
  $display['display_options']['fields']['bar'] = [
    'id' => 'bar',
    'field' => 'bar',
    'plugin_id' => 'field',
    'table' => 'entity_test__bar',
    'entity_type' => 'entity_test',
    'entity_field' => 'bar',
    'type' => 'image',
    'settings' => [
      'image_style' => 'foo',
      'image_link' => '',
    ],
  ];
  $view
    ->save();
  $dependencies = $view
    ->getDependencies() + [
    'config' => [],
  ];

  // Checks that style 'foo' is a dependency of view 'entity_test_fields'.
  $this
    ->assertContains('image.style.foo', $dependencies['config']);

  // Delete the 'foo' image style.
  $style
    ->delete();
  $view = View::load('entity_test_fields');

  // Checks that the view has not been deleted too.
  $this
    ->assertNotNull(View::load('entity_test_fields'));

  // Checks that the image field was removed from the View.
  $display = $view
    ->getDisplay('default');
  $this
    ->assertFalse(isset($display['display_options']['fields']['bar']));

  // Checks that the view has been disabled.
  $this
    ->assertFalse($view
    ->status());
  $dependencies = $view
    ->getDependencies() + [
    'config' => [],
  ];

  // Checks that the dependency on style 'foo' has been removed.
  $this
    ->assertNotContains('image.style.foo', $dependencies['config']);
}