Interim commit.
[yaffs-website] / web / modules / contrib / video_embed_field / tests / src / Kernel / FormatterDependenciesTest.php
1 <?php
2
3 namespace Drupal\Tests\video_embed_field\Kernel;
4
5 use Drupal\image\Entity\ImageStyle;
6
7 /**
8  * Test the configuration dependencies are created correctly.
9  *
10  * @group video_embed_field
11  */
12 class FormatterDependenciesTest extends KernelTestBase {
13
14   /**
15    * A test image style.
16    *
17    * @var \Drupal\image\ImageStyleInterface
18    */
19   protected $style;
20
21   /**
22    * A test image style.
23    *
24    * @var \Drupal\image\ImageStyleInterface
25    */
26   protected $replacementStyle;
27
28   /**
29    * The entity type manager.
30    *
31    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
32    */
33   protected $entityTypeManager;
34
35   /**
36    * {@inheritdoc}
37    */
38   protected function setUp() {
39     parent::setUp();
40
41     $this->style = ImageStyle::create(['name' => 'style_foo', 'label' => $this->randomString()]);
42     $this->style->save();
43     $this->replacementStyle = ImageStyle::create(['name' => 'style_bar', 'label' => $this->randomString()]);
44     $this->replacementStyle->save();
45
46     $this->entityTypeManager = \Drupal::entityTypeManager();
47   }
48
49   /**
50    * Test dependencies are created correctly added for the image formatter.
51    */
52   public function testThumbnailConfigDependencies() {
53     $this->assertFormatterDependencyBehavior([
54       'type' => 'video_embed_field_thumbnail',
55       'settings' => [
56         'image_style' => $this->style->id(),
57       ],
58     ]);
59   }
60
61   /**
62    * Test dependencies are created correctly added for the colorbox formatter.
63    */
64   public function testColorboxConfigDependencies() {
65     $this->assertFormatterDependencyBehavior([
66       'type' => 'video_embed_field_colorbox',
67       'settings' => [
68         'image_style' => $this->style->id(),
69       ],
70     ]);
71   }
72
73   /**
74    * Assert the behavior of the formatter dependencies.
75    *
76    * @param array $formatter_settings
77    *   The formatter settings to apply to the entity dispaly.
78    */
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.');
89   }
90
91   /**
92    * Load the entity display for the test entity.
93    *
94    * @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface
95    *   The entity display for the test entity.
96    */
97   protected function loadEntityDisplay() {
98     return $this->entityTypeManager->getStorage('entity_view_display')->load('entity_test.entity_test.default');
99   }
100
101 }