public function SimpleWidgetTest::testSimpleCardinalityOptions in Inline Entity Form 8
Tests simple IEF widget with different cardinality options.
File
- tests/
src/ FunctionalJavascript/ SimpleWidgetTest.php, line 43
Class
- SimpleWidgetTest
- Tests the IEF simple widget.
Namespace
Drupal\Tests\inline_entity_form\FunctionalJavascriptCode
public function testSimpleCardinalityOptions() {
// Get the xpath selectors for the fields in this test.
$title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 1);
$first_nested_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 2);
$second_nested_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 3);
$third_nested_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 4);
$fourth_nested_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 5);
$first_positive_int_field_xpath = $this
->getXpathForNthInputByLabelText('Positive int', 1);
$second_positive_int_field_xpath = $this
->getXpathForNthInputByLabelText('Positive int', 2);
$third_positive_int_field_xpath = $this
->getXpathForNthInputByLabelText('Positive int', 3);
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->drupalLogin($this->user);
$cardinality_options = [
1 => 1,
2 => 2,
FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED => 3,
];
/** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
$field_storage = $this->fieldStorageConfigStorage
->load('node.single');
foreach ($cardinality_options as $cardinality => $number_of_items) {
$field_storage
->setCardinality($cardinality);
$field_storage
->save();
$this
->drupalGet('node/add/ief_simple_single');
$assert_session
->elementTextContains('css', 'span.fieldset-legend', 'Single node');
$assert_session
->elementTextContains('css', 'div.description', 'Reference a single node.');
if ($cardinality === 1) {
// With cardinality 1, one item should already be on the page.
$assert_session
->buttonNotExists('Add another item');
$assert_session
->elementExists('xpath', $title_field_xpath)
->setValue('Host node');
$assert_session
->elementExists('xpath', $first_nested_title_field_xpath)
->setValue('Nested single node');
$assert_session
->elementExists('xpath', $first_positive_int_field_xpath)
->setValue(42);
$page
->pressButton('Save');
$assert_session
->pageTextContains('IEF simple single Host node has been created.');
$host_node = $this
->getNodeByTitle('Host node');
}
elseif ($cardinality === 2) {
// With cardinality 2, two items should already be on the page.
$assert_session
->buttonNotExists('Add another item');
$assert_session
->elementExists('xpath', $title_field_xpath)
->setValue('Host node 2');
$assert_session
->elementExists('xpath', $first_nested_title_field_xpath)
->setValue('Nested single node 2');
$assert_session
->elementExists('xpath', $first_positive_int_field_xpath)
->setValue(42);
$assert_session
->elementExists('xpath', $second_nested_title_field_xpath)
->setValue('Nested single node 3');
$assert_session
->elementExists('xpath', $second_positive_int_field_xpath)
->setValue(42);
$page
->pressButton('Save');
$assert_session
->pageTextContains('IEF simple single Host node 2 has been created.');
$host_node = $this
->getNodeByTitle('Host node 2');
}
elseif ($cardinality === FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
// With unlimited cardinality, one item should already be on the page,
// and an 'Add another item' button should appear.
$assert_session
->elementExists('xpath', $title_field_xpath)
->setValue('Host node 3');
$assert_session
->elementExists('xpath', $first_nested_title_field_xpath)
->setValue('Nested single node 4');
$assert_session
->elementExists('xpath', $first_positive_int_field_xpath)
->setValue(42);
$assert_session
->elementNotExists('xpath', $second_positive_int_field_xpath);
// Press the 'add another item' button and add a second item.
$assert_session
->buttonExists('Add another item')
->press();
$this
->assertNotEmpty($assert_session
->waitForElement('xpath', $second_nested_title_field_xpath));
// Assert an extra item isn't added at the same time.
$assert_session
->elementNotExists('xpath', $third_nested_title_field_xpath);
$assert_session
->elementExists('xpath', $second_nested_title_field_xpath)
->setValue('Nested single node 5');
$assert_session
->elementExists('xpath', $second_positive_int_field_xpath)
->setValue(42);
// Press the 'add another item' button and add a third item.
$assert_session
->buttonExists('Add another item')
->press();
$this
->assertNotEmpty($assert_session
->waitForElement('xpath', $third_nested_title_field_xpath));
// Assert an extra item isn't added at the same time.
$assert_session
->elementNotExists('xpath', $fourth_nested_title_field_xpath);
$assert_session
->elementExists('xpath', $third_nested_title_field_xpath)
->setValue('Nested single node 6');
$assert_session
->elementExists('xpath', $third_positive_int_field_xpath)
->setValue(42);
$page
->pressButton('Save');
$assert_session
->pageTextContains('IEF simple single Host node 3 has been created.');
$host_node = $this
->getNodeByTitle('Host node 3');
}
$this
->checkEditAccess($host_node, $number_of_items, $cardinality);
}
}