View source
<?php
namespace Drupal\Tests\map_widget\Functional;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait;
use Drupal\entity_test\Entity\EntityTest;
class MapWidgetTest extends BrowserTestBase {
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 testMapWidget() {
$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');
$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);
}
}