3 namespace Drupal\Tests\entity_reference_revisions\Kernel;
5 use Drupal\entity_composite_relationship_test\Entity\EntityTestCompositeRelationship;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\KernelTests\KernelTestBase;
9 use Drupal\node\Entity\Node;
10 use Drupal\node\Entity\NodeType;
11 use Drupal\simpletest\UserCreationTrait;
14 * @coversDefaultClass \Drupal\entity_reference_revisions\Plugin\Field\FieldFormatter\EntityReferenceRevisionsEntityFormatter
15 * @group entity_reference_revisions
17 class EntityReferenceRevisionsFormatterTest extends KernelTestBase {
19 use UserCreationTrait;
26 public static $modules = [
31 'entity_reference_revisions',
32 'entity_composite_relationship_test',
38 protected function setUp() {
40 // Create article content type.
41 $values = ['type' => 'article', 'name' => 'Article'];
42 $node_type = NodeType::create($values);
44 $this->installEntitySchema('user');
45 $this->installEntitySchema('node');
46 $this->installEntitySchema('entity_test_composite');
47 $this->installSchema('system', ['sequences']);
48 $this->installSchema('node', ['node_access']);
50 // Add the entity_reference_revisions field to article.
51 $field_storage = FieldStorageConfig::create([
52 'field_name' => 'composite_reference',
53 'entity_type' => 'node',
54 'type' => 'entity_reference_revisions',
56 'target_type' => 'entity_test_composite'
59 $field_storage->save();
60 $field = FieldConfig::create([
61 'field_storage' => $field_storage,
62 'bundle' => 'article',
66 $user = $this->createUser(['administer entity_test composite relationship']);
67 \Drupal::currentUser()->setAccount($user);
70 public function testFormatterWithDeletedReference() {
71 // Create the test composite entity.
73 $entity_test = EntityTestCompositeRelationship::create([
79 $text = 'Clever text';
80 // Set the name to a new text.
81 /** @var \Drupal\entity_composite_relationship_test\Entity\EntityTestCompositeRelationship $entity_test */
82 $entity_test->name = $text;
83 $entity_test->setNeedsSave(TRUE);
85 $node = Node::create([
86 'title' => $this->randomMachineName(),
88 'composite_reference' => $entity_test,
92 // entity_reference_revisions_entity_view
93 $result = $node->composite_reference->view(['type' => 'entity_reference_revisions_entity_view']);
94 $this->setRawContent($this->render($result));
95 $this->assertText('Clever text');
97 // Remove the referenced entity.
98 $entity_test->delete();
100 $node = Node::load($node->id());
101 $result = $node->composite_reference->view(['type' => 'entity_reference_revisions_entity_view']);
102 $this->render($result);
103 $this->assertNoText('Clever text');