public function ComponentFormTest::testPropertyDefaults in Module Builder 8.3
Tests the handling of property default values.
File
- tests/
src/ Functional/ ComponentFormTest.php, line 85
Class
- ComponentFormTest
- Tests the component edit form.
Namespace
Drupal\Tests\module_builder\FunctionalCode
public function testPropertyDefaults() {
$web_assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
// Get to the misc tab where our stuff is.
$this
->drupalGet('/admin/config/development/test_component/manage/my_component/misc');
// Check all the elements have their default values.
$web_assert
->fieldValueEquals('module[string_empty]', '');
$web_assert
->fieldValueEquals('module[string_default]', 'default value');
$web_assert
->fieldValueEquals('module[checkbox_empty]', FALSE);
$web_assert
->fieldValueEquals('module[checkbox_default]', TRUE);
$web_assert
->fieldValueEquals('module[array_empty]', '');
$web_assert
->fieldValueEquals('module[array_default]', "value 1\nvalue 2");
// Submit the form with no changes: defaults should remain the same.
$page
->pressButton('Save');
$web_assert
->fieldValueEquals('module[string_empty]', '');
$web_assert
->fieldValueEquals('module[string_default]', 'default value');
$web_assert
->fieldValueEquals('module[checkbox_empty]', FALSE);
$web_assert
->fieldValueEquals('module[checkbox_default]', TRUE);
$web_assert
->fieldValueEquals('module[array_empty]', '');
$web_assert
->fieldValueEquals('module[array_default]', "value 1\nvalue 2");
$component = $this->testComponentStorage
->load('my_component');
// The component has the values from the defaults.
$this
->assertEquals('default value', $component->data['string_default']);
$this
->assertEquals(TRUE, $component->data['checkbox_default']);
$this
->assertEquals([
0 => "value 1",
1 => "value 2",
], $component->data['array_default']);
// Empty properties stayed empty.
$this
->assertEquals('', $component->data['string_empty']);
$this
->assertEquals(FALSE, $component->data['checkbox_empty']);
$this
->assertEquals([], $component->data['array_empty']);
// Submit the form with empty values for everything: defaults should be
// zapped.
$page
->fillField('module[string_empty]', '');
$page
->fillField('module[string_default]', '');
$page
->uncheckField('module[checkbox_empty]');
$page
->uncheckField('module[checkbox_default]');
$page
->fillField('module[array_empty]', '');
$page
->fillField('module[array_default]', '');
$page
->pressButton('Save');
// The updated form shows the correct values.
$web_assert
->fieldValueEquals('module[string_empty]', '');
$web_assert
->fieldValueEquals('module[string_default]', '');
$web_assert
->fieldValueEquals('module[checkbox_empty]', FALSE);
$web_assert
->fieldValueEquals('module[checkbox_default]', FALSE);
$web_assert
->fieldValueEquals('module[array_empty]', '');
$web_assert
->fieldValueEquals('module[array_default]', "");
// Aaaaaaaaaaaaaargh.
// Having to reach directly into config storage here, because doing:
// $component = $this->testComponentStorage->load('my_component');
// gets a stale version of the entity. WHY???
// Extra weirdness: if we load the edit form first, then the entity load
// works ok:
// $this->drupalGet('/admin/config/development/test_component/manage/my_component/misc');
// $component = $this->testComponentStorage->load('my_component');
// WTF?
$configs = $this->container
->get('config.factory')
->loadMultiple([
'module_builder_test_component_type.test_component.my_component',
]);
$config = reset($configs);
$read = $config
->getStorage()
->read('module_builder_test_component_type.test_component.my_component');
// $component = new \Drupal\module_builder_test_component_type\Entity\($read, $this->entityTypeId);
$entity_class = $this->entityTypeManager
->getDefinition('test_component')
->getClass();
$component = new $entity_class($read, 'test_component');
// The component's values are now all empty.
$this
->assertEquals('', $component->data['string_empty']);
$this
->assertEquals('', $component->data['string_default']);
$this
->assertEquals(FALSE, $component->data['checkbox_empty']);
$this
->assertEquals(FALSE, $component->data['checkbox_default']);
$this
->assertEquals([], $component->data['array_empty']);
$this
->assertEquals([], $component->data['array_default']);
}