View source
<?php
namespace Drupal\Tests\module_builder\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\RoleInterface;
class ComponentFormTest extends BrowserTestBase {
protected $strictConfigSchema = FALSE;
public static $modules = [
'system',
'user',
'block',
'module_builder',
'module_builder_test_component_type',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$settings = [];
$settings['config']['system.logging']['error_level'] = (object) [
'value' => ERROR_REPORTING_DISPLAY_VERBOSE,
'required' => TRUE,
];
$this
->writeSettings($settings);
$this
->drupalPlaceBlock('local_actions_block');
$this
->drupalPlaceBlock('local_tasks_block');
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this->testComponentStorage = $this->entityTypeManager
->getStorage('test_component');
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
'create modules' => TRUE,
]);
$component_entity = $this->testComponentStorage
->create([
'id' => 'my_component',
'name' => 'My Component',
]);
$component_entity
->save();
}
public function testPropertyDefaults() {
$web_assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->drupalGet('/admin/config/development/test_component/manage/my_component/misc');
$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");
$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');
$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']);
$this
->assertEquals('', $component->data['string_empty']);
$this
->assertEquals(FALSE, $component->data['checkbox_empty']);
$this
->assertEquals([], $component->data['array_empty']);
$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');
$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]', "");
$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');
$entity_class = $this->entityTypeManager
->getDefinition('test_component')
->getClass();
$component = new $entity_class($read, 'test_component');
$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']);
}
public function testCompoundProperties() {
$web_assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->drupalGet('/admin/config/development/test_component/manage/my_component/misc');
$page
->pressButton('Add a Compound empty item');
$web_assert
->elementExists('named_exact', [
'id_or_name',
'module[compound_empty][0][one]',
]);
$web_assert
->elementExists('named_exact', [
'id_or_name',
'module[compound_empty][0][two]',
]);
$page
->pressButton('Add another Compound empty item');
$web_assert
->elementExists('named_exact', [
'id_or_name',
'module[compound_empty][1][one]',
]);
$web_assert
->elementExists('named_exact', [
'id_or_name',
'module[compound_empty][1][two]',
]);
$page
->pressButton('Remove last item');
$web_assert
->elementExists('named_exact', [
'id_or_name',
'module[compound_empty][0][one]',
]);
$web_assert
->elementExists('named_exact', [
'id_or_name',
'module[compound_empty][0][two]',
]);
$web_assert
->elementNotExists('named_exact', [
'id_or_name',
'module[compound_empty][1][one]',
]);
$web_assert
->elementNotExists('named_exact', [
'id_or_name',
'module[compound_empty][1][two]',
]);
$page
->fillField('module[compound_empty][0][one]', 'value one');
$page
->fillField('module[compound_empty][0][two]', 'value two');
$page
->pressButton('Save');
$this->testComponentStorage
->resetCache();
$component = $this->testComponentStorage
->load('my_component');
$this
->assertEquals('value one', $component->data['compound_empty'][0]['one']);
$this
->assertEquals('value two', $component->data['compound_empty'][0]['two']);
$this
->assertArrayNotHasKey(1, $component->data['compound_empty']);
$page
->pressButton('Remove last item');
$page
->pressButton('Save');
$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');
$entity_class = $this->entityTypeManager
->getDefinition('test_component')
->getClass();
$component = new $entity_class($read, 'test_component');
$this
->assertArrayNotHasKey('compound_empty', $component->data);
}
}