function EntityReferenceAutoCreateTest::testAutoCreatedNodeNewNodePreview
Test previewing autocreated node in a new node is not flagged as recursion.
Entity render recursion is prevented by pushing/popping entities being rendered on to a stack keyed by a string made up of the entity type ID, entity ID, and the view mode. In the case of new entities, the entity ID is NULL, so this test checks that unique identifiers are used in the stack keys for new entities to prevent false positive recursion protection.
File
-
core/
modules/ field/ tests/ src/ Functional/ EntityReference/ EntityReferenceAutoCreateTest.php, line 318
Class
- EntityReferenceAutoCreateTest
- Tests creating new entity (e.g. taxonomy-term) from an autocomplete widget.
Namespace
Drupal\Tests\field\Functional\EntityReferenceCode
public function testAutoCreatedNodeNewNodePreview() : void {
// Set the referenced node to be displayed as a rendered entity in the same
// view mode as the referencing node.
$display_repository = \Drupal::service('entity_display.repository');
$display_repository->getViewDisplay('node', $this->referencingType)
->setComponent('test_field', [
'type' => 'entity_reference_entity_view',
'settings' => [
'view_mode' => 'teaser',
],
])
->save();
// Remove the links component from the referenced node's display because
// there would be errors rendering links on the referenced entity without an
// ID.
$display_repository->getViewDisplay('node', $this->referencedType, 'teaser')
->removeComponent('links')
->save();
// Assert referenced node does not exist.
$referenced_title = $this->randomMachineName();
$result = \Drupal::entityQuery('node')->accessCheck(FALSE)
->condition('type', $this->referencedType)
->condition('title', $referenced_title)
->execute();
$this->assertEmpty($result, 'Referenced node does not exist yet.');
// Preview a new referencing node from the node add form.
$edit = [
'title[0][value]' => $this->randomMachineName(),
'test_field[0][target_id]' => $referenced_title,
];
$this->drupalGet("node/add/{$this->referencingType}");
$this->submitForm($edit, 'Preview');
$this->assertSession()
->statusCodeEquals(200);
// Referenced node title should appear if not blocked by recursion
// protection.
$this->assertSession()
->pageTextContains($referenced_title);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.