3 namespace Drupal\Tests\video_embed_field\Kernel;
5 use Drupal\image\Entity\ImageStyle;
8 * Test the configuration dependencies are created correctly.
10 * @group video_embed_field
12 class FormatterDependenciesTest extends KernelTestBase {
17 * @var \Drupal\image\ImageStyleInterface
24 * @var \Drupal\image\ImageStyleInterface
26 protected $replacementStyle;
29 * The entity type manager.
31 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
33 protected $entityTypeManager;
38 protected function setUp() {
41 $this->style = ImageStyle::create(['name' => 'style_foo', 'label' => $this->randomString()]);
43 $this->replacementStyle = ImageStyle::create(['name' => 'style_bar', 'label' => $this->randomString()]);
44 $this->replacementStyle->save();
46 $this->entityTypeManager = \Drupal::entityTypeManager();
50 * Test dependencies are created correctly added for the image formatter.
52 public function testThumbnailConfigDependencies() {
53 $this->assertFormatterDependencyBehavior([
54 'type' => 'video_embed_field_thumbnail',
56 'image_style' => $this->style->id(),
62 * Test dependencies are created correctly added for the colorbox formatter.
64 public function testColorboxConfigDependencies() {
65 $this->assertFormatterDependencyBehavior([
66 'type' => 'video_embed_field_colorbox',
68 'image_style' => $this->style->id(),
74 * Assert the behavior of the formatter dependencies.
76 * @param array $formatter_settings
77 * The formatter settings to apply to the entity dispaly.
79 protected function assertFormatterDependencyBehavior($formatter_settings) {
80 // Assert the image style becomes a dependency of the entity display.
81 $this->loadEntityDisplay()->setComponent($this->fieldName, $formatter_settings)->save();
82 $this->assertTrue(in_array('image.style.' . $this->style->id(), $this->loadEntityDisplay()->getDependencies()['config']), 'The image style was correctly added as a dependency the entity display config object.');
83 // Delete the image style.
84 $storage = $this->entityTypeManager->getStorage('image_style');
85 $storage->setReplacementId($this->style->id(), $this->replacementStyle->id());
86 $this->style->delete();
87 // Ensure the replacement is now a dependency.
88 $this->assertTrue(in_array('image.style.' . $this->replacementStyle->id(), $this->loadEntityDisplay()->getDependencies()['config']), 'The replacement style was added to the entity display.');
92 * Load the entity display for the test entity.
94 * @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface
95 * The entity display for the test entity.
97 protected function loadEntityDisplay() {
98 return $this->entityTypeManager->getStorage('entity_view_display')->load('entity_test.entity_test.default');