View source
<?php
namespace Drupal\Tests\inline_entity_form\FunctionalJavascript;
use Behat\Mink\Element\NodeElement;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\Tests\TestFileCreationTrait;
class ComplexWidgetTest extends InlineEntityFormTestBase {
use TestFileCreationTrait {
getTestFiles as drupalGetTestFiles;
}
protected static $modules = [
'inline_entity_form_test',
'field',
'field_ui',
];
protected $formContentAddUrl;
protected $entityFormDisplayStorage;
protected function setUp() : void {
parent::setUp();
$this->user = $this
->createUser([
'create ief_reference_type content',
'create ief_test_nested1 content',
'create ief_test_nested2 content',
'create ief_test_nested3 content',
'edit any ief_reference_type content',
'delete any ief_reference_type content',
'create ief_test_complex content',
'edit any ief_test_complex content',
'delete any ief_test_complex content',
'edit any ief_test_nested1 content',
'edit any ief_test_nested2 content',
'edit any ief_test_nested3 content',
'view own unpublished content',
'administer content types',
]);
$this
->drupalLogin($this->user);
$this->formContentAddUrl = 'node/add/ief_test_complex';
$this->entityFormDisplayStorage = $this->container
->get('entity_type.manager')
->getStorage('entity_form_display');
}
public function testEmptyField() {
$inner_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 2);
$first_name_field_xpath = $this
->getXpathForNthInputByLabelText('First name', 1);
$last_name_field_xpath = $this
->getXpathForNthInputByLabelText('Last name', 1);
$fieldset_label_all_bundles_xpath = $this
->getXpathForFieldsetLabel('All bundles', 1);
$fieldset_label_multi_xpath = $this
->getXpathForFieldsetLabel('Multiple nodes', 1);
$assert_session = $this
->assertSession();
$this
->updateSetting('allow_existing', FALSE);
$this
->drupalGet($this->formContentAddUrl);
$assert_session
->elementExists('xpath', $fieldset_label_all_bundles_xpath);
$assert_session
->elementExists('xpath', $fieldset_label_multi_xpath);
$assert_session
->elementExists('xpath', $inner_title_field_xpath);
$assert_session
->elementExists('xpath', $first_name_field_xpath);
$assert_session
->elementExists('xpath', $last_name_field_xpath);
$assert_session
->buttonExists('Create node');
$this
->updateSetting('allow_existing', TRUE);
$this
->drupalGet($this->formContentAddUrl);
$multi_fieldset = $assert_session
->elementExists('css', 'fieldset[data-drupal-selector="edit-multi"]');
$assert_session
->elementExists('xpath', $fieldset_label_multi_xpath);
$assert_session
->elementNotExists('xpath', $inner_title_field_xpath);
$assert_session
->elementNotExists('xpath', $first_name_field_xpath);
$assert_session
->elementNotExists('xpath', $last_name_field_xpath);
$assert_session
->buttonExists('Add existing node', $multi_fieldset);
$multi_fieldset
->pressButton('Add new node');
$assert_session
->elementExists('xpath', $fieldset_label_multi_xpath);
$this
->assertNotEmpty($assert_session
->waitForElement('xpath', $inner_title_field_xpath));
$assert_session
->elementExists('xpath', $first_name_field_xpath);
$assert_session
->elementExists('xpath', $last_name_field_xpath);
$assert_session
->buttonExists('Create node');
$assert_session
->buttonExists('Cancel');
$this
->drupalGet($this->formContentAddUrl);
$multi_fieldset = $assert_session
->elementExists('css', 'fieldset[data-drupal-selector="edit-multi"]');
$multi_fieldset
->pressButton('Add existing node');
$this
->assertNotEmpty($assert_session
->waitForElement('xpath', $this
->getXpathForAutoCompleteInput()));
$assert_session
->buttonExists('Add node');
$assert_session
->buttonExists('Cancel');
}
public function testEntityCreation() {
$first_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 1);
$inner_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 2);
$first_name_field_xpath = $this
->getXpathForNthInputByLabelText('First name', 1);
$last_name_field_xpath = $this
->getXpathForNthInputByLabelText('Last name', 1);
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->updateSetting('allow_existing', TRUE);
$this
->drupalGet($this->formContentAddUrl);
$multi_fieldset = $assert_session
->elementExists('css', 'fieldset[data-drupal-selector="edit-multi"]');
$multi_fieldset
->pressButton('Add new node');
$this
->assertNotEmpty($create_node_button = $assert_session
->waitForButton('Create node'));
$create_node_button
->press();
$this
->assertNotEmpty($assert_session
->waitForElement('css', 'div[data-drupal-messages]'));
$assert_session
->pageTextContains('First name field is required.');
$assert_session
->pageTextContains('Last name field is required.');
$assert_session
->pageTextContains('Title field is required.');
$this
->drupalGet($this->formContentAddUrl);
$multi_fieldset = $assert_session
->elementExists('css', 'fieldset[data-drupal-selector="edit-multi"]');
$multi_fieldset
->pressButton('Add new node');
$this
->assertNotEmpty($assert_session
->waitForElement('xpath', $inner_title_field_xpath));
$assert_session
->elementExists('xpath', $inner_title_field_xpath)
->setValue('Some reference');
$assert_session
->elementExists('xpath', $first_name_field_xpath)
->setValue('John');
$assert_session
->elementExists('xpath', $last_name_field_xpath)
->setValue('Doe');
$page
->pressButton('Create node');
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.ief-row-entity'));
$assert_session
->elementTextContains('css', '.ief-row-entity .inline-entity-form-node-label', 'Some reference');
$assert_session
->elementTextContains('css', '.ief-row-entity .inline-entity-form-node-status', 'Published');
$multi_fieldset = $assert_session
->elementExists('css', 'fieldset[data-drupal-selector="edit-multi"]');
$assert_session
->buttonExists('Edit', $multi_fieldset);
$assert_session
->buttonExists('Remove', $multi_fieldset);
$assert_session
->buttonExists('Edit', $multi_fieldset)
->press();
$this
->assertNotEmpty($assert_session
->waitForElement('xpath', $inner_title_field_xpath));
$assert_session
->elementExists('xpath', $inner_title_field_xpath)
->setValue('Some changed reference');
$page
->pressButton('Update node');
$this
->waitForRowByTitle('Some changed reference');
$assert_session
->elementTextContains('css', '.ief-row-entity .inline-entity-form-node-label', 'Some changed reference');
$assert_session
->elementTextContains('css', '.ief-row-entity .inline-entity-form-node-status', 'Published');
$multi_fieldset = $assert_session
->elementExists('css', 'fieldset[data-drupal-selector="edit-multi"]');
$assert_session
->buttonExists('Edit', $multi_fieldset);
$assert_session
->buttonExists('Remove', $multi_fieldset);
$assert_session
->elementExists('xpath', $first_title_field_xpath)
->setValue('Some title');
$page
->pressButton('Save');
$assert_session
->pageTextContains('IEF test complex Some title has been created.');
$node = $this
->drupalGetNodeByTitle('Some changed reference');
$this
->assertNotEmpty($node, 'Created ief_reference_type node ' . $node
->label());
$this
->assertSame('John', $node
->get('first_name')->value, 'First name in reference node set to John');
$this
->assertSame('Doe', $node
->get('last_name')->value, 'Last name in reference node set to Doe');
$parent_node = $this
->drupalGetNodeByTitle('Some title');
$this
->assertNotEmpty($parent_node, 'Created ief_test_complex node ' . $parent_node
->label());
$this
->assertSame($node
->id(), $parent_node->multi->target_id, 'Reference node id set to ' . $node
->id());
}
public function testNestedEntityCreationWithDifferentBundlesAjaxSubmit() {
$top_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 1);
$nested_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 2);
$double_nested_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 3);
$top_fieldset_label_xpath = $this
->getXpathForFieldsetLabel('Multiple nodes', 1);
$nested_fieldset_label_xpath = $this
->getXpathForFieldsetLabel('Multiple nodes', 2);
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
foreach ([
FALSE,
TRUE,
] as $required) {
$this
->setupNestedComplexForm($required);
$assert_session
->elementExists('xpath', $top_fieldset_label_xpath);
$assert_session
->elementExists('xpath', $nested_fieldset_label_xpath);
$required_string = $required ? ' required' : ' unrequired';
$double_nested_title = 'Dream within a dream' . $required_string;
$nested_title = 'Dream' . $required_string;
$top_level_title = 'Reality' . $required_string;
$this
->assertNotEmpty($field = $assert_session
->waitForElement('xpath', $double_nested_title_field_xpath));
$field
->setValue($double_nested_title);
$page
->pressButton('Create node 3');
$this
->waitForRowByTitle($double_nested_title);
$this
->assertNoNodeByTitle($double_nested_title, 'Second nested entity is not saved yet.');
$assert_session
->elementExists('xpath', $nested_title_field_xpath)
->setValue($nested_title);
$page
->pressButton('Create node 2');
$this
->waitForRowByTitle($nested_title);
$this
->assertNoNodeByTitle($nested_title, 'First nested entity is not saved yet.');
$assert_session
->elementExists('xpath', $top_title_field_xpath)
->setValue($top_level_title);
$page
->pressButton('Save');
$assert_session
->pageTextContains("IEF test nested 1 {$top_level_title} has been created.");
$top_level_node = $this
->drupalGetNodeByTitle($top_level_title);
$this
->assertNotEmpty($top_level_node);
$nested_node = $this
->drupalGetNodeByTitle($nested_title);
$this
->assertNotEmpty($nested_node);
$double_nested_node = $this
->drupalGetNodeByTitle($double_nested_title);
$this
->assertNotEmpty($double_nested_node);
$this
->checkNestedNodes($top_level_node, $nested_node, $double_nested_node);
}
}
public function testNestedEntityCreationWithDifferentBundlesNoAjaxSubmit() {
$top_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 1);
$nested_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 2);
$double_nested_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 3);
$top_fieldset_label_xpath = $this
->getXpathForFieldsetLabel('Multiple nodes', 1);
$nested_fieldset_label_xpath = $this
->getXpathForFieldsetLabel('Multiple nodes', 2);
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
foreach ([
FALSE,
TRUE,
] as $required) {
$this
->setupNestedComplexForm($required);
$assert_session
->elementExists('xpath', $top_fieldset_label_xpath);
$assert_session
->elementExists('xpath', $nested_fieldset_label_xpath);
$required_string = $required ? ' required' : ' unrequired';
$double_nested_title = 'Dream within a dream' . $required_string;
$nested_title = 'Dream' . $required_string;
$top_level_title = 'Reality' . $required_string;
$assert_session
->elementExists('xpath', $top_title_field_xpath)
->setValue($top_level_title);
$assert_session
->elementExists('xpath', $nested_title_field_xpath)
->setValue($nested_title);
$assert_session
->elementExists('xpath', $double_nested_title_field_xpath)
->setValue($double_nested_title);
$page
->pressButton('Save');
$assert_session
->pageTextContains("IEF test nested 1 {$top_level_title} has been created.");
$top_level_node = $this
->drupalGetNodeByTitle($top_level_title);
$this
->assertNotEmpty($top_level_node);
$nested_node = $this
->drupalGetNodeByTitle($nested_title);
$this
->assertNotEmpty($nested_node);
$double_nested_node = $this
->drupalGetNodeByTitle($double_nested_title);
$this
->assertNotEmpty($double_nested_node);
$this
->checkNestedNodes($top_level_node, $nested_node, $double_nested_node);
}
}
public function testEntityEditingAndRemoving() {
$inner_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 2);
$first_name_field_xpath = $this
->getXpathForNthInputByLabelText('First name', 1);
$last_name_field_xpath = $this
->getXpathForNthInputByLabelText('Last name', 1);
$first_delete_checkbox_xpath = $this
->getXpathForNthInputByLabelText('Delete this node from the system.', 1);
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->updateSetting('allow_existing', TRUE);
$referenceNodes = $this
->createReferenceContent();
$this
->drupalCreateNode([
'type' => 'ief_test_complex',
'title' => 'Some title',
'multi' => array_values($referenceNodes),
]);
$parent_node = $this
->drupalGetNodeByTitle('Some title');
$this
->drupalGet('node/' . $parent_node
->id() . '/edit');
$assert_session
->elementExists('xpath', '(//input[@value="Edit"])[2]')
->press();
$this
->assertNotEmpty($assert_session
->waitForElement('xpath', $inner_title_field_xpath));
$assert_session
->elementExists('xpath', $first_name_field_xpath)
->setValue('John');
$assert_session
->elementExists('xpath', $last_name_field_xpath)
->setValue('Doe');
$page
->pressButton('Update node');
$this
->assertNotEmpty($assert_session
->waitForElementRemoved('css', 'div[data-drupal-selector="edit-multi-form-inline-entity-form-entities-1-form"]'));
$this
->waitForRowByTitle('Some reference 2');
$page
->pressButton('Save');
$assert_session
->pageTextContains('IEF test complex Some title has been updated.');
$node = $this
->drupalGetNodeByTitle('Some reference 2', TRUE);
$this
->assertSame('John', $node->first_name->value, 'First name in reference node changed to John');
$this
->assertSame('Doe', $node->last_name->value, 'Last name in reference node changed to Doe');
$this
->drupalGet('node/' . $parent_node
->id() . '/edit');
$assert_session
->elementsCount('css', 'tr.ief-row-entity', 3);
$assert_session
->elementExists('xpath', '(//input[@value="Remove"])[2]')
->press();
$this
->assertNotEmpty($confirm_checkbox = $assert_session
->waitForElement('xpath', $first_delete_checkbox_xpath));
$assert_session
->pageTextContains('Are you sure you want to remove Some reference 2?');
$confirm_checkbox
->check();
$assert_session
->elementExists('xpath', '(//input[@value="Remove"])[2]')
->press();
$this
->waitForRowRemovedByTitle('Some reference 2');
$assert_session
->elementsCount('css', 'tr.ief-row-entity', 2);
$page
->pressButton('Save');
$assert_session
->pageTextContains('IEF test complex Some title has been updated.');
$deleted_node = $this
->drupalGetNodeByTitle('Some reference 2');
$this
->assertEmpty($deleted_node, 'The inline entity was deleted from the site.');
$this
->drupalGet('node/' . $parent_node
->id() . '/edit');
$assert_session
->elementsCount('css', 'tr.ief-row-entity', 2);
$this
->assertRowByTitle('Some reference 1');
$this
->assertNoRowByTitle('Some reference 2');
$this
->assertRowByTitle('Some reference 3');
$this
->drupalGet('node/' . $parent_node
->id() . '/edit');
$assert_session
->elementsCount('css', 'tr.ief-row-entity', 2);
$assert_session
->elementExists('xpath', '(//input[@value="Remove"])[2]')
->press();
$this
->assertNotEmpty($assert_session
->waitForElement('xpath', $first_delete_checkbox_xpath));
$assert_session
->pageTextContains('Are you sure you want to remove Some reference 3?');
$assert_session
->elementExists('xpath', '(//input[@value="Remove"])[2]')
->press();
$this
->waitForRowRemovedByTitle('Some reference 3');
$assert_session
->elementsCount('css', 'tr.ief-row-entity', 1);
$this
->assertRowByTitle('Some reference 1');
$this
->assertNoRowByTitle('Some reference 2');
$this
->assertNoRowByTitle('Some reference 3');
$page
->pressButton('Save');
$assert_session
->pageTextContains('IEF test complex Some title has been updated.');
$node = $this
->drupalGetNodeByTitle('Some reference 3');
$this
->assertNotEmpty($node, 'Reference node not deleted');
}
public function testReferencingExistingEntities() {
$node_field_xpath = $this
->getXpathForNthInputByLabelText('Node', 1);
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->updateSetting('allow_existing', TRUE);
$referenceNodes = $this
->createReferenceContent();
$bundle_nodes = $this
->createNodeForEveryBundle();
$this
->drupalCreateNode([
'type' => 'ief_test_complex',
'title' => 'Some title',
'multi' => [
1,
],
'all_bundles' => key($bundle_nodes),
]);
unset($bundle_nodes[key($bundle_nodes)]);
$parent_node = $this
->drupalGetNodeByTitle('Some title', TRUE);
$this
->drupalGet('node/' . $parent_node
->id() . '/edit');
for ($i = 2; $i <= 3; $i++) {
$this
->openMultiExistingForm();
$title = 'Some reference ' . $i;
$assert_session
->elementExists('xpath', $node_field_xpath)
->setValue($title . ' (' . $referenceNodes[$title] . ')');
$page
->pressButton('Add node');
$this
->waitForRowByTitle($title);
}
foreach ($bundle_nodes as $id => $title) {
$all_bundles_fieldset = $assert_session
->elementExists('css', 'fieldset[data-drupal-selector="edit-all-bundles"]');
$assert_session
->buttonExists('Add existing node', $all_bundles_fieldset)
->press();
$this
->assertNotEmpty($assert_session
->waitForElement('xpath', $node_field_xpath));
$assert_session
->elementExists('xpath', $node_field_xpath)
->setValue($title . ' (' . $id . ')');
$page
->pressButton('Add node');
$this
->waitForRowByTitle($title);
}
$page
->pressButton('Save');
$assert_session
->pageTextContains('IEF test complex Some title has been updated.');
$this
->drupalGet('node/' . $parent_node
->id() . '/edit');
$assert_session
->elementsCount('css', 'fieldset[data-drupal-selector="edit-multi"] tr.ief-row-entity', 3);
$this
->assertRowByTitle('Some reference 1');
$this
->assertRowByTitle('Some reference 2');
$this
->assertRowByTitle('Some reference 3');
$assert_session
->elementsCount('css', 'fieldset[data-drupal-selector="edit-all-bundles"] tr.ief-row-entity', 12);
foreach ($bundle_nodes as $title) {
$this
->assertRowByTitle($title);
}
}
public function testReferencingExistingEntitiesNoSubmit() {
$node_field_xpath = $this
->getXpathForNthInputByLabelText('Node', 1);
$title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 1);
$this
->updateSetting('allow_existing', TRUE);
$title = $this
->randomMachineName();
$this
->drupalCreateNode([
'type' => 'ief_reference_type',
'title' => $title,
'first_name' => $this
->randomMachineName(),
'last_name' => $this
->randomMachineName(),
]);
$node = $this
->drupalGetNodeByTitle($title);
$this
->assertNotEmpty($node, 'Created ief_reference_type node "' . $node
->label() . '"');
$this
->drupalGet($this->formContentAddUrl);
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->openMultiExistingForm();
$assert_session
->elementExists('xpath', $node_field_xpath)
->setValue($node
->getTitle() . ' (' . $node
->id() . ')');
$parent_title = $this
->randomMachineName();
$assert_session
->elementExists('xpath', $title_field_xpath)
->setValue($parent_title);
$page
->pressButton('Save');
$assert_session
->pageTextContains("IEF test complex {$parent_title} has been created.");
$assert_session
->pageTextNotContains('This value should not be null.');
$node = $this
->drupalGetNodeByTitle($parent_title);
$this
->assertNotEmpty($node, 'Created ief_reference_type node.');
}
public function testReferenceExistingValidation() {
$node_field_xpath = $this
->getXpathForNthInputByLabelText('Node', 1);
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->updateSetting('allow_existing', TRUE);
$this
->drupalGet('node/add/ief_test_complex');
$this
->checkExistingValidationExpectation('', 'Node field is required.');
$this
->checkExistingValidationExpectation('Fake Title', 'There are no entities matching "Fake Title"');
foreach ($this
->createNodeForEveryBundle() as $id => $title) {
$node = Node::load($id);
if ($node
->bundle() !== 'ief_reference_type') {
$this
->checkExistingValidationExpectation("{$title} ({$id})", "The referenced entity (node: {$id}) does not exist.");
}
}
foreach ($this
->createReferenceContent(2) as $title => $id) {
$this
->openMultiExistingForm();
$current_title = "{$title} ({$id})";
$assert_session
->elementExists('xpath', $node_field_xpath)
->setValue($current_title);
$page
->pressButton('Add node');
$this
->waitForRowByTitle($title);
$assert_session
->elementNotExists('xpath', $node_field_xpath);
$this
->checkExistingValidationExpectation($current_title, 'The selected node has already been added.');
}
}
public function testDuplicatingEntities() {
$title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 2);
$first_name_field_xpath = $this
->getXpathForNthInputByLabelText('First name', 1);
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->updateSetting('allow_duplicate', TRUE);
$referenceNodes = $this
->createReferenceContent(2);
$this
->drupalCreateNode([
'type' => 'ief_test_complex',
'title' => 'Some title',
'multi' => array_values($referenceNodes),
]);
$parent_node = $this
->drupalGetNodeByTitle('Some title');
$this
->drupalGet('node/' . $parent_node
->id() . '/edit');
$this
->assertNotEmpty($multi_fieldset = $assert_session
->waitForElement('css', 'fieldset[data-drupal-selector="edit-multi"]'));
$assert_session
->buttonExists('Duplicate', $multi_fieldset)
->press();
$this
->assertNotEmpty($create_node_button = $assert_session
->waitForButton('Duplicate node'));
$assert_session
->elementExists('xpath', $title_field_xpath)
->setValue('Duplicate!');
$assert_session
->elementExists('xpath', $first_name_field_xpath)
->setValue('Bojan');
$create_node_button
->press();
$this
->waitForRowByTitle('Duplicate!');
$assert_session
->elementsCount('css', 'tr.ief-row-entity', 3);
$this
->assertRowByTitle('Some reference 1');
$this
->assertRowByTitle('Some reference 2');
$this
->assertRowByTitle('Duplicate!');
$page
->pressButton('Save');
$duplicate = $this
->drupalGetNodeByTitle('Duplicate!');
$this
->assertNotEmpty($duplicate, 'Duplicate node created.');
$this
->assertSame('Bojan', $duplicate->first_name->value);
}
public function testEditedInlineEntityValidation() {
$nested_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 2);
$title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 1);
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->updateSetting('allow_existing', TRUE);
$referenced_nodes = $this
->createReferenceContent(1);
$this
->drupalCreateNode([
'type' => 'ief_test_complex',
'title' => 'First referencing node',
'multi' => array_values($referenced_nodes),
]);
$first_node = $this
->drupalGetNodeByTitle('First referencing node');
$this
->drupalCreateNode([
'type' => 'ief_test_complex',
'title' => 'Second referencing node',
'multi' => array_values($referenced_nodes),
]);
$second_node = $this
->drupalGetNodeByTitle('Second referencing node');
$this
->drupalGet('node/' . $first_node
->id() . '/edit');
$page
->pressButton('Edit');
$this
->assertNotEmpty($nested_title = $assert_session
->waitForElement('xpath', $nested_title_field_xpath));
$nested_title
->setValue('Some reference updated');
$page
->pressButton('Update node');
$this
->waitForRowByTitle('Some reference updated');
$assert_session
->elementsCount('css', 'tr.ief-row-entity', 1);
$assert_session
->elementExists('xpath', $title_field_xpath)
->setValue('First node updated');
$page
->pressButton('Save');
$assert_session
->pageTextContains('IEF test complex First node updated has been updated.');
$this
->drupalGet('node/' . $second_node
->id() . '/edit');
$page
->pressButton('Edit');
$this
->assertNotEmpty($nested_title = $assert_session
->waitForElement('xpath', $nested_title_field_xpath));
$nested_title
->setValue('Some reference updated the second time');
$page
->pressButton('Update node');
$this
->waitForRowByTitle('Some reference updated the second time');
$assert_session
->elementsCount('css', 'tr.ief-row-entity', 1);
$assert_session
->elementExists('xpath', $title_field_xpath)
->setValue('Second node updated');
$page
->pressButton('Save');
$assert_session
->pageTextContains('IEF test complex Second node updated has been updated.');
$assert_session
->pageTextNotContains('The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.');
}
protected function checkNestedNodeEditing(NodeInterface $node) {
$double_nested_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 3);
$title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 1);
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$nested_node = $node->test_ref_nested1->entity;
$double_nested_node = $nested_node->test_ref_nested2->entity;
$this
->drupalGet("node/{$node->id()}/edit");
$this
->assertRowByTitle($nested_node
->label());
$page
->pressButton('Edit');
$this
->waitForRowByTitle($double_nested_node
->label());
$page
->pressButton('Edit');
$this
->assertNotEmpty($assert_session
->waitforButton('Update node 3'));
$double_nested_node_update_title = $double_nested_node
->getTitle() . ' - updated';
$assert_session
->elementExists('xpath', $double_nested_title_field_xpath)
->setValue($double_nested_node_update_title);
$page
->pressButton('Update node 3');
$this
->waitForRowByTitle($double_nested_node_update_title);
$page
->pressButton('Update node 2');
$this
->assertNotEmpty($assert_session
->waitForElementRemoved('css', 'div[data-drupal-selector="edit-test-ref-nested1-form-inline-entity-form-entities-0-form"]'));
$this
->waitForRowByTitle($nested_node
->label());
$page
->pressButton('Save');
$assert_session
->pageTextContains('IEF test nested 1 ' . $node
->label() . ' has been updated.');
$this
->drupalGet("node/{$node->id()}/edit");
$page
->pressButton('Edit');
$this
->waitForRowByTitle($double_nested_node_update_title);
$this
->drupalGet("node/{$double_nested_node->id()}/edit");
$this
->assertSame($double_nested_node_update_title, $assert_session
->elementExists('xpath', $title_field_xpath)
->getValue());
}
protected function createReferenceContent($numNodes = 3) {
$retval = [];
for ($i = 1; $i <= $numNodes; $i++) {
$this
->drupalCreateNode([
'type' => 'ief_reference_type',
'title' => 'Some reference ' . $i,
'first_name' => 'First Name ' . $i,
'last_name' => 'Last Name ' . $i,
]);
$node = $this
->drupalGetNodeByTitle('Some reference ' . $i);
$this
->assertNotEmpty($node, 'Created ief_reference_type node "' . $node
->label() . '"');
$retval[$node
->label()] = $node
->id();
}
return $retval;
}
protected function updateSetting(string $name, $value) {
$display = $this->entityFormDisplayStorage
->load('node.ief_test_complex.default');
$component = $display
->getComponent('multi');
$component['settings'][$name] = $value;
$display
->setComponent('multi', $component)
->save();
}
protected function createNodeForEveryBundle() {
$retval = [];
$bundles = $this->container
->get('entity_type.bundle.info')
->getBundleInfo('node');
foreach ($bundles as $id => $value) {
$this
->drupalCreateNode([
'type' => $id,
'title' => $value['label'],
]);
$node = $this
->drupalGetNodeByTitle($value['label']);
$this
->assertNotEmpty($node, 'Created node "' . $node
->label() . '"');
$retval[$node
->id()] = $value['label'];
}
return $retval;
}
protected function setupNestedComplexForm(bool $required) {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this->fieldConfigStorage
->load('node.ief_test_nested1.test_ref_nested1')
->setRequired($required)
->save();
$this->fieldConfigStorage
->load('node.ief_test_nested2.test_ref_nested2')
->setRequired($required)
->save();
$this
->drupalGet('node/add/ief_test_nested1');
if (!$required) {
$page
->pressButton('Add new node 2');
$this
->assertNotEmpty($button = $assert_session
->waitForButton('Add new node 3'));
$button
->press();
$this
->assertNotEmpty($assert_session
->waitForButton('Create node 3'));
}
}
protected function openMultiExistingForm() {
$assert_session = $this
->assertSession();
$this
->assertNotEmpty($multi_fieldset = $assert_session
->waitForElement('css', 'fieldset[data-drupal-selector="edit-multi"]'));
$assert_session
->buttonExists('Add existing node', $multi_fieldset)
->press();
$this
->assertNotEmpty($assert_session
->waitForElement('xpath', $this
->getXpathForAutoCompleteInput()));
}
protected function checkExistingValidationExpectation(string $existing_node_text, string $expected_error) {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->openMultiExistingForm();
$field = $assert_session
->waitForElement('xpath', $this
->getXpathForAutoCompleteInput());
$field
->setValue($existing_node_text);
$page
->pressButton('Add node');
$this
->assertNotNull($assert_session
->waitForText($expected_error));
$assert_session
->buttonExists('Cancel')
->press();
$this
->assertNotEmpty($assert_session
->waitForElementRemoved('xpath', $this
->getXpathForAutoCompleteInput()));
}
public function testComplexEntityCreate() {
$nested_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 2);
$user = $this
->createUser([
'create ief_test_complex content',
]);
$this
->drupalLogin($user);
$this
->drupalGet('node/add/ief_test_complex');
$assert_session = $this
->assertSession();
$assert_session
->fieldNotExists('all_bundles[actions][bundle]');
$assert_session
->elementNotExists('xpath', $nested_title_field_xpath);
$user = $this
->createUser([
'create ief_test_complex content',
'create ief_reference_type content',
]);
$this
->drupalLogin($user);
$this
->drupalGet('node/add/ief_test_complex');
$assert_session
->fieldExists('all_bundles[actions][bundle]');
$this
->assertSession()
->optionExists('edit-all-bundles-actions-bundle', 'ief_reference_type');
$this
->assertSession()
->optionExists('edit-all-bundles-actions-bundle', 'ief_test_complex');
$assert_session
->elementExists('xpath', $nested_title_field_xpath);
}
protected function checkNestedNodes(NodeInterface $top_level_node, NodeInterface $nested_node, NodeInterface $double_nested_node) {
if ($nested_node
->id() == $top_level_node->test_ref_nested1->entity
->id()) {
$this
->assertEquals(1, $top_level_node->test_ref_nested1
->count(), 'Only one nested node created');
$this
->assertSame($top_level_node->test_ref_nested1->entity
->label(), $nested_node
->label(), "Nested node's title is correct.");
$this
->assertSame('ief_test_nested2', $nested_node
->bundle(), "Nested node's type is correct.");
if ($double_nested_node
->id() == $nested_node->test_ref_nested2->entity
->id()) {
$this
->assertEquals(1, $nested_node->test_ref_nested2
->count(), 'Only one node within a node within a node created.');
$this
->assertSame($nested_node->test_ref_nested2->entity
->label(), $double_nested_node
->label(), "Node within a node within a node's title is correct.");
$this
->assertSame('ief_test_nested3', $double_nested_node
->bundle(), "Node within a node within a node's type is correct.");
$this
->checkNestedNodeEditing($top_level_node);
}
}
}
public function testSeparateNestedDataMultiValueFields() {
$top_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 1);
$nested_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 2);
$double_nested_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 3);
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
foreach ([
FALSE,
TRUE,
] as $required) {
$this
->setupNestedComplexForm($required);
$required_string = $required ? ' required' : ' unrequired';
$double_nested_title = 'Dream within a dream' . $required_string;
$nested_title = 'Dream' . $required_string;
$top_level_title = 'Reality' . $required_string;
$assert_session
->elementExists('xpath', $top_title_field_xpath)
->setValue($top_level_title);
$assert_session
->elementExists('xpath', $nested_title_field_xpath)
->setValue($nested_title);
$assert_session
->elementExists('xpath', $double_nested_title_field_xpath)
->setValue($double_nested_title);
$page
->pressButton('Create node 3');
$assert_session
->waitForButton('Add new node 3');
$page
->pressButton('Create node 2');
$assert_session
->waitForButton('Add new node 2');
$page
->pressButton('Add new node 2');
$assert_session
->waitForButton('Add new node 3');
$this
->assertNoRowByTitle($double_nested_title);
}
}
public function testNestedCreateAndEditWontClash(bool $required) {
$top_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 1);
$nested_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 2);
$double_nested_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 3);
$second_edit_button_xpath = $this
->getXpathForButtonWithValue('Edit', 2);
$assert_session = $this
->assertSession();
$this
->setupNestedComplexForm($required);
$required_string = $required ? ' required' : ' unrequired';
$title_1 = 'Rationality' . $required_string;
$title_1_2 = 'Drain' . $required_string;
$title_1_2_3 = 'Drain within a drain' . $required_string;
$title_i_2_3a = "Drone within a drain" . $required_string;
$title_1_2a = 'Drone' . $required_string;
$title_1_2a_3 = 'Drain within a drone' . $required_string;
$title_i_2a_3a = "Drone within a drain" . $required_string;
$this
->elementWithXpath($top_title_field_xpath)
->setValue($title_1);
$this
->elementWithXpath($nested_title_field_xpath)
->setValue($title_1_2);
$this
->elementWithXpath($double_nested_title_field_xpath)
->setValue($title_1_2_3);
$this
->elementWithXpath($this
->buttonXpath('Create node 3'))
->press();
$this
->waitForXpath($this
->buttonXpath('Add new node 3'));
$this
->elementWithXpath($this
->buttonXpath('Create node 2'))
->press();
$this
->waitForXpath($this
->buttonXpath('Add new node 2'));
$this
->elementWithXpath($this
->buttonXpath('Edit'))
->press();
$this
->waitForXpath($this
->buttonXpath('Update node 2'));
$this
->elementWithXpath($this
->buttonXpath('Add new node 3'))
->press();
$this
->assertNotNull($assert_session
->waitForButton('Create node 3'));
$assert_session
->elementExists('xpath', $double_nested_title_field_xpath)
->setValue($title_i_2_3a);
$this
->elementWithXpath($this
->buttonXpath('Create node 3'))
->press();
$this
->waitForXpath($this
->buttonXpath('Add new node 3'));
$this
->elementWithXpath($this
->buttonXpath('Update node 2'))
->press();
$this
->waitForXpathRemoved($this
->buttonXpath('Update node 2'));
$this
->elementWithXpath($this
->buttonXpath('Add new node 2'))
->press();
$this
->waitForXpath($this
->buttonXpath('Create node 2'));
if (!$required) {
$this
->elementWithXpath($this
->buttonXpath('Add new node 3'))
->press();
$this
->waitForXpath($this
->buttonXpath('Create node 3'));
}
$assert_session
->elementExists('xpath', $nested_title_field_xpath)
->setValue($title_1_2a);
$assert_session
->elementExists('xpath', $double_nested_title_field_xpath)
->setValue($title_1_2a_3);
$this
->elementWithXpath($this
->buttonXpath('Create node 3'))
->press();
$this
->waitForXpath($this
->buttonXpath('Add new node 3'));
$this
->elementWithXpath($this
->buttonXpath('Create node 2'))
->press();
$this
->waitForXpathRemoved($this
->buttonXpath('Create node 2'));
$this
->waitForXpath($second_edit_button_xpath)
->press();
$this
->waitForXpath($this
->buttonXpath('Update node 2'));
$this
->elementWithXpath($this
->buttonXpath('Add new node 3'))
->press();
$this
->waitForXpath($this
->buttonXpath('Create node 3'));
$assert_session
->elementExists('xpath', $double_nested_title_field_xpath)
->setValue($title_i_2a_3a);
$this
->elementWithXpath($this
->buttonXpath('Create node 3'))
->press();
$this
->waitForXpath($this
->buttonXpath('Add new node 3'));
$this
->elementWithXpath($this
->buttonXpath('Save'))
->press();
$this
->htmlOutput();
$assert_session
->pageTextContains("IEF test nested 1 {$title_1} has been created.");
}
public function simpleFalseTrueDataProvider() {
return [
[
FALSE,
],
[
TRUE,
],
];
}
public function elementWithXpath(string $xpath) : NodeElement {
return $this
->waitForXpath($xpath, 0);
}
public function waitForXpath(string $xpath, int $timeout = 10000) : NodeElement {
$element = $this
->assertSession()
->waitForElement('xpath', $xpath, $timeout);
if (!$element) {
$this
->htmlOutput();
$this
->assertNotNull($element);
}
return $element;
}
public function waitForXpathRemoved(string $xpath, int $timeout = 10000) : bool {
$removed = $this
->assertSession()
->waitForElementRemoved('xpath', $xpath, $timeout);
if (!$removed) {
$this
->htmlOutput();
$this
->assertTrue($removed);
}
return $removed;
}
protected function buttonXpath(string $label, int $index = 1) : string {
return $this
->getXpathForButtonWithValue($label, $index);
}
}