Chris@0: installEntitySchema('node'); Chris@18: } Chris@18: Chris@18: /** Chris@0: * Tests the dependency between ImageStyle and entity display components. Chris@0: */ Chris@0: public function testEntityDisplayDependency() { Chris@0: // Create two image styles. Chris@0: /** @var \Drupal\image\ImageStyleInterface $style */ Chris@0: $style = ImageStyle::create(['name' => 'main_style']); Chris@0: $style->save(); Chris@0: /** @var \Drupal\image\ImageStyleInterface $replacement */ Chris@0: $replacement = ImageStyle::create(['name' => 'replacement_style']); Chris@0: $replacement->save(); Chris@0: Chris@0: // Create a node-type, named 'note'. Chris@0: $node_type = NodeType::create(['type' => 'note']); Chris@0: $node_type->save(); Chris@0: Chris@0: // Create an image field and attach it to the 'note' node-type. Chris@0: FieldStorageConfig::create([ Chris@0: 'entity_type' => 'node', Chris@0: 'field_name' => 'sticker', Chris@0: 'type' => 'image', Chris@0: ])->save(); Chris@0: FieldConfig::create([ Chris@0: 'entity_type' => 'node', Chris@0: 'field_name' => 'sticker', Chris@0: 'bundle' => 'note', Chris@0: ])->save(); Chris@0: Chris@0: // Create the default entity view display and set the 'sticker' field to use Chris@0: // the 'main_style' images style in formatter. Chris@0: /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */ Chris@0: $view_display = EntityViewDisplay::create([ Chris@0: 'targetEntityType' => 'node', Chris@0: 'bundle' => 'note', Chris@0: 'mode' => 'default', Chris@0: 'status' => TRUE, Chris@0: ])->setComponent('sticker', ['settings' => ['image_style' => 'main_style']]); Chris@0: $view_display->save(); Chris@0: Chris@0: // Create the default entity form display and set the 'sticker' field to use Chris@0: // the 'main_style' images style in the widget. Chris@0: /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */ Chris@0: $form_display = EntityFormDisplay::create([ Chris@0: 'targetEntityType' => 'node', Chris@0: 'bundle' => 'note', Chris@0: 'mode' => 'default', Chris@0: 'status' => TRUE, Chris@0: ])->setComponent('sticker', ['settings' => ['preview_image_style' => 'main_style']]); Chris@0: $form_display->save(); Chris@0: Chris@0: // Check that the entity displays exists before dependency removal. Chris@0: $this->assertNotNull(EntityViewDisplay::load($view_display->id())); Chris@0: $this->assertNotNull(EntityFormDisplay::load($form_display->id())); Chris@0: Chris@0: // Delete the 'main_style' image style. Before that, emulate the UI process Chris@0: // of selecting a replacement style by setting the replacement image style Chris@0: // ID in the image style storage. Chris@0: /** @var \Drupal\image\ImageStyleStorageInterface $storage */ Chris@0: $storage = $this->container->get('entity.manager')->getStorage($style->getEntityTypeId()); Chris@0: $storage->setReplacementId('main_style', 'replacement_style'); Chris@0: $style->delete(); Chris@0: Chris@0: // Check that the entity displays exists after dependency removal. Chris@0: $this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id())); Chris@0: $this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id())); Chris@0: // Check that the 'sticker' formatter component exists in both displays. Chris@0: $this->assertNotNull($formatter = $view_display->getComponent('sticker')); Chris@0: $this->assertNotNull($widget = $form_display->getComponent('sticker')); Chris@0: // Check that both displays are using now 'replacement_style' for images. Chris@0: $this->assertSame('replacement_style', $formatter['settings']['image_style']); Chris@0: $this->assertSame('replacement_style', $widget['settings']['preview_image_style']); Chris@0: Chris@0: // Delete the 'replacement_style' without setting a replacement image style. Chris@0: $replacement->delete(); Chris@0: Chris@0: // The entity view and form displays exists after dependency removal. Chris@0: $this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id())); Chris@0: $this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id())); Chris@0: // The 'sticker' formatter component should be hidden in view display. Chris@0: $this->assertNull($view_display->getComponent('sticker')); Chris@0: $this->assertTrue($view_display->get('hidden')['sticker']); Chris@0: // The 'sticker' widget component should be active in form displays, but the Chris@0: // image preview should be disabled. Chris@0: $this->assertNotNull($widget = $form_display->getComponent('sticker')); Chris@0: $this->assertSame('', $widget['settings']['preview_image_style']); Chris@0: } Chris@0: Chris@17: /** Chris@17: * Tests renaming the ImageStyle. Chris@17: */ Chris@17: public function testEntityDisplayDependencyRename() { Chris@17: // Create an image style. Chris@17: /** @var \Drupal\image\ImageStyleInterface $style */ Chris@17: $style = ImageStyle::create(['name' => 'main_style']); Chris@17: $style->save(); Chris@17: Chris@17: // Create a node-type, named 'note'. Chris@17: $node_type = NodeType::create(['type' => 'note']); Chris@17: $node_type->save(); Chris@17: Chris@17: // Create an image field and attach it to the 'note' node-type. Chris@17: FieldStorageConfig::create([ Chris@17: 'entity_type' => 'node', Chris@17: 'field_name' => 'sticker', Chris@17: 'type' => 'image', Chris@17: ])->save(); Chris@17: FieldConfig::create([ Chris@17: 'entity_type' => 'node', Chris@17: 'field_name' => 'sticker', Chris@17: 'bundle' => 'note', Chris@17: ])->save(); Chris@17: Chris@17: // Create the default entity view display and set the 'sticker' field to use Chris@17: // the 'main_style' images style in formatter. Chris@17: /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */ Chris@17: $view_display = EntityViewDisplay::create([ Chris@17: 'targetEntityType' => 'node', Chris@17: 'bundle' => 'note', Chris@17: 'mode' => 'default', Chris@17: 'status' => TRUE, Chris@17: ])->setComponent('sticker', ['settings' => ['image_style' => 'main_style']]); Chris@17: $view_display->save(); Chris@17: Chris@17: // Create the default entity form display and set the 'sticker' field to use Chris@17: // the 'main_style' images style in the widget. Chris@17: /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */ Chris@17: $form_display = EntityFormDisplay::create([ Chris@17: 'targetEntityType' => 'node', Chris@17: 'bundle' => 'note', Chris@17: 'mode' => 'default', Chris@17: 'status' => TRUE, Chris@17: ])->setComponent('sticker', ['settings' => ['preview_image_style' => 'main_style']]); Chris@17: $form_display->save(); Chris@17: Chris@17: // Check that the entity displays exists before dependency renaming. Chris@17: $this->assertNotNull(EntityViewDisplay::load($view_display->id())); Chris@17: $this->assertNotNull(EntityFormDisplay::load($form_display->id())); Chris@17: Chris@17: // Rename the 'main_style' image style. Chris@17: $style->setName('main_style_renamed'); Chris@17: $style->save(); Chris@17: Chris@17: // Check that the entity displays exists after dependency renaming. Chris@17: $this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id())); Chris@17: $this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id())); Chris@17: // Check that the 'sticker' formatter component exists in both displays. Chris@17: $this->assertNotNull($formatter = $view_display->getComponent('sticker')); Chris@17: $this->assertNotNull($widget = $form_display->getComponent('sticker')); Chris@17: // Check that both displays are using now 'main_style_renamed' for images. Chris@17: $this->assertSame('main_style_renamed', $formatter['settings']['image_style']); Chris@17: $this->assertSame('main_style_renamed', $widget['settings']['preview_image_style']); Chris@17: } Chris@17: Chris@0: }