You are here

public function MapWidgetAjaxTest::testMapWidgetAjax in Map Widget 8

Tests that the widget displays properly.

File

tests/src/FunctionalJavascript/MapWidgetAjaxTest.php, line 98

Class

MapWidgetAjaxTest
Test widget with ajax behavior.

Namespace

Drupal\Tests\map_widget\FunctionalJavascript

Code

public function testMapWidgetAjax() {

  // Display creation form.
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertFieldByName("map_test_field[0][value][0][key]", '');
  $this
    ->assertFieldByName("map_test_field[0][value][0][value]", '');
  $this
    ->assertElementPresent('input[name="map_test_field_0_add_more"]');

  // Ajax test.
  $button = $this
    ->getSession()
    ->getPage()
    ->find('css', 'input[name="map_test_field_0_add_more"]');
  $button
    ->click();
  $this
    ->assertSession()
    ->waitForElement('css', 'input[name="map_test_field_1_add_more"]');
  $button
    ->click();
  $this
    ->assertSession()
    ->waitForElement('css', 'input[name="map_test_field_2_add_more"]');
  $button
    ->click();
  $this
    ->assertSession()
    ->waitForElement('css', 'input[name="map_test_field_3_add_more"]');

  // Submit with some value.
  $keys = [
    $this
      ->randomMachineName(),
    $this
      ->randomMachineName(),
  ];
  $values = [
    $this
      ->randomMachineName(),
    $this
      ->randomMachineName(),
  ];
  $edit = [
    "map_test_field[0][value][0][key]" => $keys[0],
    "map_test_field[0][value][0][value]" => $values[0],
    "map_test_field[0][value][1][key]" => $keys[1],
    "map_test_field[0][value][1][value]" => $values[1],
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');
  preg_match('|entity_test/manage/(\\d+)|', $this
    ->getUrl(), $match);
  $id = $match[1];
  $testEntity = EntityTest::load($id);
  $storedValue = $testEntity
    ->get('map_test_field')->value;
  $this
    ->assertTrue(is_array($storedValue), 'map_test_field does not contain an array');
  $this
    ->assertEqual(count($storedValue), 2, 'Returned array does not have exactly two elements.');
  for ($index = 0; $index < 2; $index++) {
    $this
      ->assertTrue(isset($storedValue[$keys[$index]]), "Test key {$index} not present in value array.");
    $this
      ->assertEqual($storedValue[$keys[$index]], $values[$index], "The value stored for the test key {$index} in the MapItem does not match the test value.");
  }
  $this
    ->assertSession()
    ->fieldValueEquals('map_test_field[0][value][0][key]', $keys[0]);
  $this
    ->assertSession()
    ->fieldValueEquals('map_test_field[0][value][0][value]', $values[0]);
  $this
    ->assertSession()
    ->fieldValueEquals('map_test_field[0][value][1][key]', $keys[1]);
  $this
    ->assertSession()
    ->fieldValueEquals('map_test_field[0][value][1][value]', $values[1]);
}