annotate core/modules/image/tests/src/Kernel/ImageStyleIntegrationTest.php @ 8:50b0d041100e

Further files for download
author Chris Cannam
date Mon, 05 Feb 2018 10:56:40 +0000
parents 4c8ae668cc8c
children 129ea1e6d783
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@0 26 * Tests the dependency between ImageStyle and entity display components.
Chris@0 27 */
Chris@0 28 public function testEntityDisplayDependency() {
Chris@0 29 // Create two image styles.
Chris@0 30 /** @var \Drupal\image\ImageStyleInterface $style */
Chris@0 31 $style = ImageStyle::create(['name' => 'main_style']);
Chris@0 32 $style->save();
Chris@0 33 /** @var \Drupal\image\ImageStyleInterface $replacement */
Chris@0 34 $replacement = ImageStyle::create(['name' => 'replacement_style']);
Chris@0 35 $replacement->save();
Chris@0 36
Chris@0 37 // Create a node-type, named 'note'.
Chris@0 38 $node_type = NodeType::create(['type' => 'note']);
Chris@0 39 $node_type->save();
Chris@0 40
Chris@0 41 // Create an image field and attach it to the 'note' node-type.
Chris@0 42 FieldStorageConfig::create([
Chris@0 43 'entity_type' => 'node',
Chris@0 44 'field_name' => 'sticker',
Chris@0 45 'type' => 'image',
Chris@0 46 ])->save();
Chris@0 47 FieldConfig::create([
Chris@0 48 'entity_type' => 'node',
Chris@0 49 'field_name' => 'sticker',
Chris@0 50 'bundle' => 'note',
Chris@0 51 ])->save();
Chris@0 52
Chris@0 53 // Create the default entity view display and set the 'sticker' field to use
Chris@0 54 // the 'main_style' images style in formatter.
Chris@0 55 /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
Chris@0 56 $view_display = EntityViewDisplay::create([
Chris@0 57 'targetEntityType' => 'node',
Chris@0 58 'bundle' => 'note',
Chris@0 59 'mode' => 'default',
Chris@0 60 'status' => TRUE,
Chris@0 61 ])->setComponent('sticker', ['settings' => ['image_style' => 'main_style']]);
Chris@0 62 $view_display->save();
Chris@0 63
Chris@0 64 // Create the default entity form display and set the 'sticker' field to use
Chris@0 65 // the 'main_style' images style in the widget.
Chris@0 66 /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
Chris@0 67 $form_display = EntityFormDisplay::create([
Chris@0 68 'targetEntityType' => 'node',
Chris@0 69 'bundle' => 'note',
Chris@0 70 'mode' => 'default',
Chris@0 71 'status' => TRUE,
Chris@0 72 ])->setComponent('sticker', ['settings' => ['preview_image_style' => 'main_style']]);
Chris@0 73 $form_display->save();
Chris@0 74
Chris@0 75 // Check that the entity displays exists before dependency removal.
Chris@0 76 $this->assertNotNull(EntityViewDisplay::load($view_display->id()));
Chris@0 77 $this->assertNotNull(EntityFormDisplay::load($form_display->id()));
Chris@0 78
Chris@0 79 // Delete the 'main_style' image style. Before that, emulate the UI process
Chris@0 80 // of selecting a replacement style by setting the replacement image style
Chris@0 81 // ID in the image style storage.
Chris@0 82 /** @var \Drupal\image\ImageStyleStorageInterface $storage */
Chris@0 83 $storage = $this->container->get('entity.manager')->getStorage($style->getEntityTypeId());
Chris@0 84 $storage->setReplacementId('main_style', 'replacement_style');
Chris@0 85 $style->delete();
Chris@0 86
Chris@0 87 // Check that the entity displays exists after dependency removal.
Chris@0 88 $this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id()));
Chris@0 89 $this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id()));
Chris@0 90 // Check that the 'sticker' formatter component exists in both displays.
Chris@0 91 $this->assertNotNull($formatter = $view_display->getComponent('sticker'));
Chris@0 92 $this->assertNotNull($widget = $form_display->getComponent('sticker'));
Chris@0 93 // Check that both displays are using now 'replacement_style' for images.
Chris@0 94 $this->assertSame('replacement_style', $formatter['settings']['image_style']);
Chris@0 95 $this->assertSame('replacement_style', $widget['settings']['preview_image_style']);
Chris@0 96
Chris@0 97 // Delete the 'replacement_style' without setting a replacement image style.
Chris@0 98 $replacement->delete();
Chris@0 99
Chris@0 100 // The entity view and form displays exists after dependency removal.
Chris@0 101 $this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id()));
Chris@0 102 $this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id()));
Chris@0 103 // The 'sticker' formatter component should be hidden in view display.
Chris@0 104 $this->assertNull($view_display->getComponent('sticker'));
Chris@0 105 $this->assertTrue($view_display->get('hidden')['sticker']);
Chris@0 106 // The 'sticker' widget component should be active in form displays, but the
Chris@0 107 // image preview should be disabled.
Chris@0 108 $this->assertNotNull($widget = $form_display->getComponent('sticker'));
Chris@0 109 $this->assertSame('', $widget['settings']['preview_image_style']);
Chris@0 110 }
Chris@0 111
Chris@0 112 }