You are here

public function FormElementTest::testFormElement in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/FormElementTest.php \Drupal\Tests\entity_browser\Functional\FormElementTest::testFormElement()

Tests the Entity browser form element.

File

tests/src/Functional/FormElementTest.php, line 54

Class

FormElementTest
Tests the entity browser form element.

Namespace

Drupal\Tests\entity_browser\Functional

Code

public function testFormElement() {

  // See \Drupal\entity_browser_test\Form\FormElementTest.
  $this
    ->drupalGet('/test-element');
  $this
    ->assertSession()
    ->linkExists('Select entities', 0, 'Trigger link found.');
  $ids = [
    $this->nodes[0]
      ->getEntityTypeId() . ':' . $this->nodes[0]
      ->id(),
    $this->nodes[1]
      ->getEntityTypeId() . ':' . $this->nodes[1]
      ->id(),
  ];
  $ids = implode(' ', $ids);
  $this
    ->assertSession()
    ->hiddenFieldExists("fancy_entity_browser[entity_ids]")
    ->setValue($ids);
  $this
    ->assertSession()
    ->buttonExists('Submit')
    ->press();
  $expected = 'Selected entities: ' . $this->nodes[0]
    ->label() . ', ' . $this->nodes[1]
    ->label();
  $this
    ->assertSession()
    ->responseContains($expected, 'Selected entities detected.');
  $default_entity = $this->nodes[0]
    ->getEntityTypeId() . ':' . $this->nodes[0]
    ->id();
  $this
    ->drupalGet('/test-element', [
    'query' => [
      'default_entity' => $default_entity,
      'selection_mode' => EntityBrowserElement::SELECTION_MODE_EDIT,
    ],
  ]);
  $this
    ->assertSession()
    ->linkExists('Select entities', 0, 'Trigger link found.');
  $this
    ->assertSession()
    ->hiddenFieldValueEquals("fancy_entity_browser[entity_ids]", $default_entity);
  $hidden_field = $this
    ->assertSession()
    ->hiddenFieldExists("fancy_entity_browser[entity_ids]");
  $new_value = 'node:' . $this->nodes[1]
    ->id() . ' node:' . $this->nodes[0]
    ->id();
  $hidden_field
    ->setValue($new_value);
  $this
    ->submitForm([], 'Submit');
  $expected = 'Selected entities: ' . $this->nodes[1]
    ->label() . ', ' . $this->nodes[0]
    ->label();
  $this
    ->assertSession()
    ->responseContains($expected, 'Selected entities detected.');
}