You are here

public function MapWidgetTest::testMapWidget in Map Widget 8

Tests that the widget displays properly.

File

tests/src/Functional/MapWidgetTest.php, line 98

Class

MapWidgetTest
Test the actual widget for map_widget.

Namespace

Drupal\Tests\map_widget\Functional

Code

public function testMapWidget() {

  // 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
    ->assertRaw('placeholder="Key placeholder for map_assoc_widget"');
  $this
    ->assertRaw('placeholder="Value placeholder for map_assoc_widget"');
  $this
    ->assertRaw('Map Widget description');

  // Submit with some value.
  $key = $this
    ->randomMachineName();
  $value = $this
    ->randomMachineName();
  $edit = [
    "map_test_field[0][value][0][key]" => $key,
    "map_test_field[0][value][0][value]" => $value,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');
  preg_match('|entity_test/manage/(\\d+)|', $this
    ->getUrl(), $match);
  $id = $match[1];
  $this
    ->assertText("entity_test {$id} has been created.");
  $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), 1, 'Returned array does not have exactly one element.');
  $this
    ->assertTrue(isset($storedValue[$key]), 'Test key not present in value array.');
  $this
    ->assertEqual($storedValue[$key], $value, 'The value stored for the test key in the MapItem does not match the test value.');
  $this
    ->assertSession()
    ->fieldValueEquals('map_test_field[0][value][0][key]', $key);
  $this
    ->assertSession()
    ->fieldValueEquals('map_test_field[0][value][0][value]', $value);
}