annotate core/modules/image/tests/src/Kernel/ImageStyleIntegrationTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\image\Kernel;
Chris@0 4
Chris@0 5 use Drupal\Core\Entity\Entity\EntityFormDisplay;
Chris@0 6 use Drupal\Core\Entity\Entity\EntityViewDisplay;
Chris@0 7 use Drupal\field\Entity\FieldConfig;
Chris@0 8 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 9 use Drupal\image\Entity\ImageStyle;
Chris@0 10 use Drupal\KernelTests\KernelTestBase;
Chris@0 11 use Drupal\node\Entity\NodeType;
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Tests the integration of ImageStyle with the core.
Chris@0 15 *
Chris@0 16 * @group image
Chris@0 17 */
Chris@0 18 class ImageStyleIntegrationTest extends KernelTestBase {
Chris@0 19
Chris@0 20 /**
Chris@0 21 * {@inheritdoc}
Chris@0 22 */
Chris@0 23 public static $modules = ['image', 'file', 'field', 'system', 'user', 'node'];
Chris@0 24
Chris@0 25 /**
Chris@18 26 * {@inheritdoc}
Chris@18 27 */
Chris@18 28 protected function setUp() {
Chris@18 29 parent::setUp();
Chris@18 30 $this->installEntitySchema('node');
Chris@18 31 }
Chris@18 32
Chris@18 33 /**
Chris@0 34 * Tests the dependency between ImageStyle and entity display components.
Chris@0 35 */
Chris@0 36 public function testEntityDisplayDependency() {
Chris@0 37 // Create two image styles.
Chris@0 38 /** @var \Drupal\image\ImageStyleInterface $style */
Chris@0 39 $style = ImageStyle::create(['name' => 'main_style']);
Chris@0 40 $style->save();
Chris@0 41 /** @var \Drupal\image\ImageStyleInterface $replacement */
Chris@0 42 $replacement = ImageStyle::create(['name' => 'replacement_style']);
Chris@0 43 $replacement->save();
Chris@0 44
Chris@0 45 // Create a node-type, named 'note'.
Chris@0 46 $node_type = NodeType::create(['type' => 'note']);
Chris@0 47 $node_type->save();
Chris@0 48
Chris@0 49 // Create an image field and attach it to the 'note' node-type.
Chris@0 50 FieldStorageConfig::create([
Chris@0 51 'entity_type' => 'node',
Chris@0 52 'field_name' => 'sticker',
Chris@0 53 'type' => 'image',
Chris@0 54 ])->save();
Chris@0 55 FieldConfig::create([
Chris@0 56 'entity_type' => 'node',
Chris@0 57 'field_name' => 'sticker',
Chris@0 58 'bundle' => 'note',
Chris@0 59 ])->save();
Chris@0 60
Chris@0 61 // Create the default entity view display and set the 'sticker' field to use
Chris@0 62 // the 'main_style' images style in formatter.
Chris@0 63 /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
Chris@0 64 $view_display = EntityViewDisplay::create([
Chris@0 65 'targetEntityType' => 'node',
Chris@0 66 'bundle' => 'note',
Chris@0 67 'mode' => 'default',
Chris@0 68 'status' => TRUE,
Chris@0 69 ])->setComponent('sticker', ['settings' => ['image_style' => 'main_style']]);
Chris@0 70 $view_display->save();
Chris@0 71
Chris@0 72 // Create the default entity form display and set the 'sticker' field to use
Chris@0 73 // the 'main_style' images style in the widget.
Chris@0 74 /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
Chris@0 75 $form_display = EntityFormDisplay::create([
Chris@0 76 'targetEntityType' => 'node',
Chris@0 77 'bundle' => 'note',
Chris@0 78 'mode' => 'default',
Chris@0 79 'status' => TRUE,
Chris@0 80 ])->setComponent('sticker', ['settings' => ['preview_image_style' => 'main_style']]);
Chris@0 81 $form_display->save();
Chris@0 82
Chris@0 83 // Check that the entity displays exists before dependency removal.
Chris@0 84 $this->assertNotNull(EntityViewDisplay::load($view_display->id()));
Chris@0 85 $this->assertNotNull(EntityFormDisplay::load($form_display->id()));
Chris@0 86
Chris@0 87 // Delete the 'main_style' image style. Before that, emulate the UI process
Chris@0 88 // of selecting a replacement style by setting the replacement image style
Chris@0 89 // ID in the image style storage.
Chris@0 90 /** @var \Drupal\image\ImageStyleStorageInterface $storage */
Chris@0 91 $storage = $this->container->get('entity.manager')->getStorage($style->getEntityTypeId());
Chris@0 92 $storage->setReplacementId('main_style', 'replacement_style');
Chris@0 93 $style->delete();
Chris@0 94
Chris@0 95 // Check that the entity displays exists after dependency removal.
Chris@0 96 $this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id()));
Chris@0 97 $this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id()));
Chris@0 98 // Check that the 'sticker' formatter component exists in both displays.
Chris@0 99 $this->assertNotNull($formatter = $view_display->getComponent('sticker'));
Chris@0 100 $this->assertNotNull($widget = $form_display->getComponent('sticker'));
Chris@0 101 // Check that both displays are using now 'replacement_style' for images.
Chris@0 102 $this->assertSame('replacement_style', $formatter['settings']['image_style']);
Chris@0 103 $this->assertSame('replacement_style', $widget['settings']['preview_image_style']);
Chris@0 104
Chris@0 105 // Delete the 'replacement_style' without setting a replacement image style.
Chris@0 106 $replacement->delete();
Chris@0 107
Chris@0 108 // The entity view and form displays exists after dependency removal.
Chris@0 109 $this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id()));
Chris@0 110 $this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id()));
Chris@0 111 // The 'sticker' formatter component should be hidden in view display.
Chris@0 112 $this->assertNull($view_display->getComponent('sticker'));
Chris@0 113 $this->assertTrue($view_display->get('hidden')['sticker']);
Chris@0 114 // The 'sticker' widget component should be active in form displays, but the
Chris@0 115 // image preview should be disabled.
Chris@0 116 $this->assertNotNull($widget = $form_display->getComponent('sticker'));
Chris@0 117 $this->assertSame('', $widget['settings']['preview_image_style']);
Chris@0 118 }
Chris@0 119
Chris@17 120 /**
Chris@17 121 * Tests renaming the ImageStyle.
Chris@17 122 */
Chris@17 123 public function testEntityDisplayDependencyRename() {
Chris@17 124 // Create an image style.
Chris@17 125 /** @var \Drupal\image\ImageStyleInterface $style */
Chris@17 126 $style = ImageStyle::create(['name' => 'main_style']);
Chris@17 127 $style->save();
Chris@17 128
Chris@17 129 // Create a node-type, named 'note'.
Chris@17 130 $node_type = NodeType::create(['type' => 'note']);
Chris@17 131 $node_type->save();
Chris@17 132
Chris@17 133 // Create an image field and attach it to the 'note' node-type.
Chris@17 134 FieldStorageConfig::create([
Chris@17 135 'entity_type' => 'node',
Chris@17 136 'field_name' => 'sticker',
Chris@17 137 'type' => 'image',
Chris@17 138 ])->save();
Chris@17 139 FieldConfig::create([
Chris@17 140 'entity_type' => 'node',
Chris@17 141 'field_name' => 'sticker',
Chris@17 142 'bundle' => 'note',
Chris@17 143 ])->save();
Chris@17 144
Chris@17 145 // Create the default entity view display and set the 'sticker' field to use
Chris@17 146 // the 'main_style' images style in formatter.
Chris@17 147 /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
Chris@17 148 $view_display = EntityViewDisplay::create([
Chris@17 149 'targetEntityType' => 'node',
Chris@17 150 'bundle' => 'note',
Chris@17 151 'mode' => 'default',
Chris@17 152 'status' => TRUE,
Chris@17 153 ])->setComponent('sticker', ['settings' => ['image_style' => 'main_style']]);
Chris@17 154 $view_display->save();
Chris@17 155
Chris@17 156 // Create the default entity form display and set the 'sticker' field to use
Chris@17 157 // the 'main_style' images style in the widget.
Chris@17 158 /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
Chris@17 159 $form_display = EntityFormDisplay::create([
Chris@17 160 'targetEntityType' => 'node',
Chris@17 161 'bundle' => 'note',
Chris@17 162 'mode' => 'default',
Chris@17 163 'status' => TRUE,
Chris@17 164 ])->setComponent('sticker', ['settings' => ['preview_image_style' => 'main_style']]);
Chris@17 165 $form_display->save();
Chris@17 166
Chris@17 167 // Check that the entity displays exists before dependency renaming.
Chris@17 168 $this->assertNotNull(EntityViewDisplay::load($view_display->id()));
Chris@17 169 $this->assertNotNull(EntityFormDisplay::load($form_display->id()));
Chris@17 170
Chris@17 171 // Rename the 'main_style' image style.
Chris@17 172 $style->setName('main_style_renamed');
Chris@17 173 $style->save();
Chris@17 174
Chris@17 175 // Check that the entity displays exists after dependency renaming.
Chris@17 176 $this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id()));
Chris@17 177 $this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id()));
Chris@17 178 // Check that the 'sticker' formatter component exists in both displays.
Chris@17 179 $this->assertNotNull($formatter = $view_display->getComponent('sticker'));
Chris@17 180 $this->assertNotNull($widget = $form_display->getComponent('sticker'));
Chris@17 181 // Check that both displays are using now 'main_style_renamed' for images.
Chris@17 182 $this->assertSame('main_style_renamed', $formatter['settings']['image_style']);
Chris@17 183 $this->assertSame('main_style_renamed', $widget['settings']['preview_image_style']);
Chris@17 184 }
Chris@17 185
Chris@0 186 }