View source
<?php
namespace Drupal\Tests\map_widget\FunctionalJavascript;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait;
class MapWidgetAjaxTest extends WebDriverTestBase {
use EntityDefinitionTestTrait;
public static $modules = [
'map_widget',
'entity_test',
];
protected $user;
protected $defaultTheme = 'stark';
protected $state;
protected function setUp() {
parent::setUp();
$this->state = $this->container
->get('state');
$definition['map_test_field'] = BaseFieldDefinition::create('map')
->setLabel('Map Widget Test')
->setDisplayOptions('view', [
'region' => 'hidden',
])
->setDisplayOptions('form', [
'type' => 'map_assoc_widget',
'region' => 'content',
'settings' => [
'size' => '40',
'key_placeholder' => 'Key placeholder for map_assoc_widget',
'value_placeholder' => 'Value placeholder for map_assoc_widget',
],
'weight' => 90,
])
->setTranslatable(FALSE)
->setDescription('Map Widget description')
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', FALSE);
$this->state
->set('entity_test.additional_base_field_definitions', $definition);
$this
->applyEntityUpdates('entity_test');
$this->user = $this
->drupalCreateUser([
'administer site configuration',
'administer entity_test content',
]);
$this
->drupalLogin($this->user);
}
public function testMapWidgetAjax() {
$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"]');
$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"]');
$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]);
}
}