Chris@0: '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@0: }