You are here

public function FieldCollectionAJAXTest::testEmptyFields in Field collection 8

Tests how Field Collections manage empty fields.

See also

\Drupal\field_collection\Plugin\Field\FieldWidget\FieldCollectionEmbedWidget::formMultipleElements()

File

tests/src/FunctionalJavascript/FieldCollectionAjaxTest.php, line 38

Class

FieldCollectionAJAXTest
Tests that AJAX functionality works.

Namespace

Drupal\Tests\field_collection\FunctionalJavascriptTests

Code

public function testEmptyFields() {
  $user_privileged = $this
    ->drupalCreateUser([
    'access content',
    'edit any article content',
    'create article content',
  ]);
  $this
    ->drupalLogin($user_privileged);

  // First, set the field collection cardinality to unlimited.
  $field_config = FieldStorageConfig::loadByName('node', $this->field_collection_name);
  $field_config
    ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
  $field_config
    ->save();

  // Check that we can see field collection fields when creating content.
  $this
    ->drupalGet('node/add/article');
  $this
    ->assertFieldById('edit-field-test-collection-0-field-inner-0-value');

  // Check that the "Add another item" button works as expected.

  //$this->drupalPostAjaxForm('node/add/article', [], ['field_test_collection_add_more' => t('Add another item')]);
  $this
    ->click('.field-add-more-submit');

  // The AJAX request changes field identifiers, so we need to find them by name.
  $this
    ->assertJsCondition('jQuery("[name=\'field_test_collection[0][field_inner][0][value]\']").length', 10000);
  $this
    ->assertJsCondition('jQuery("[name=\'field_test_collection[1][field_inner][0][value]\']").length', 10000);

  // Check that we can see an empty field collection when editing content
  // that did not have values for it.
  $node = $this
    ->drupalCreateNode([
    'type' => 'article',
  ]);
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->assertFieldById('edit-field-test-collection-0-field-inner-0-value');
}