You are here

function AddAnotherItemButtonTest::testAddAnotherItemButtonAlter in Custom add another 8

Tests changes of multiple fields buttons labels.

File

src/Tests/AddAnotherItemButtonTest.php, line 80

Class

AddAnotherItemButtonTest
Test case for 'Add another item' button label alter.

Namespace

Drupal\custom_add_another\Tests

Code

function testAddAnotherItemButtonAlter() {
  $field_storage = $this->fieldStorageUnlimited;
  $field_name = $field_storage['field_name'];
  $this->field['field_name'] = $field_name;

  // Creating field with unlimited cardinality
  $this->entityTypeManager
    ->getStorage('field_storage_config')
    ->create($field_storage)
    ->save();

  /** @var \Drupal\field\FieldConfigInterface $field_config_entity */
  $field_config_entity = $this->entityTypeManager
    ->getStorage('field_config')
    ->create($this->field);
  $field_config_entity
    ->save();

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $entity_form_display */
  $entity_form_display = $this->entityTypeManager
    ->getStorage('entity_form_display')
    ->load($this->field['entity_type'] . '.' . $this->field['bundle'] . '.default');
  if (!$entity_form_display) {
    $entity_form_display = $this->entityTypeManager
      ->getStorage('entity_form_display')
      ->create([
      'targetEntityType' => $this->field['entity_type'],
      'bundle' => $this->field['bundle'],
      'mode' => 'default',
      'status' => TRUE,
    ]);
  }
  $entity_form_display
    ->setComponent($field_name)
    ->save();

  // Checking field label.
  $button_name = $field_name . '_add_more';
  $add_more_xpath = '//input[@name="' . $button_name . '"]';
  $default_value = t('Add another item');
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertFieldsByValue($this
    ->xpath('.' . $add_more_xpath), $default_value, 'Found the "add more" button with default value.');

  // Updating label and checking again.
  $updated_value = $this
    ->randomString();
  $field_config_entity
    ->setThirdPartySetting('custom_add_another', 'custom_add_another', $updated_value)
    ->save();
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertFieldsByValue($this
    ->xpath('.' . $add_more_xpath), $updated_value, 'Found the "add more" button with updated value.');
}