private function ComponentAttributeTest::verifyAllowedAttributes in Layout Builder Component Attributes 1.2.x
Same name and namespace in other branches
- 1.0.x tests/src/FunctionalJavascript/ComponentAttributeTest.php \Drupal\Tests\layout_builder_component_attributes\FunctionalJavascript\ComponentAttributeTest::verifyAllowedAttributes()
- 1.1.x tests/src/FunctionalJavascript/ComponentAttributeTest.php \Drupal\Tests\layout_builder_component_attributes\FunctionalJavascript\ComponentAttributeTest::verifyAllowedAttributes()
Verifies form rendering and on-page rendering of allowed attributes.
Parameters
string $group: A group of fields: 'block_attributes', 'block_title_attributes', or 'block_content_attributes'.
array $attributes: An array of attributes with the attribute name as the key and a test value as the value. Only one data-* attribute can be passed per group.
1 call to ComponentAttributeTest::verifyAllowedAttributes()
- ComponentAttributeTest::testAllowedAttributes in tests/
src/ FunctionalJavascript/ ComponentAttributeTest.php - Tests allowed attributes (both form render and page render).
File
- tests/
src/ FunctionalJavascript/ ComponentAttributeTest.php, line 343
Class
- ComponentAttributeTest
- Tests UI and rendering of component attributes.
Namespace
Drupal\Tests\layout_builder_component_attributes\FunctionalJavascriptCode
private function verifyAllowedAttributes($group, array $attributes) {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
// Create an array to keep track of attributes' statuses during test loops.
// Initially, set all attributes as allowed.
$attribute_fields = [];
foreach ($attributes as $attribute => $test_value) {
// Replace 'data-*' attribute with 'data' to match expected FAPI key.
if (substr($attribute, 0, 5) === "data-") {
$attribute = 'data';
}
$attribute_fields[$attribute] = TRUE;
}
// Load config.
$config = \Drupal::service('config.factory')
->getEditable('layout_builder_component_attributes.settings');
// Load contextual menu and observe all fields are rendered.
$this
->drupalGet(static::FIELD_UI_PREFIX . '/display/default/layout');
$this
->clickContextualLink('.layout-builder-block', 'Manage attributes');
$assert_session
->assertWaitOnAjaxRequest();
// Loop through fields.
foreach ($attribute_fields as $attribute => $attribute_status) {
$this
->assertTrue($page
->hasField($group . '[' . $attribute . ']'), "Attribute field " . $attribute . " is rendered for " . $group . " group");
}
// Loop through attributes and disable one attribute per time.
foreach ($attribute_fields as $attribute => $attribute_status) {
$attribute_fields[$attribute] = FALSE;
$config
->set('allowed_' . $group, $attribute_fields)
->save();
$this
->drupalGet(static::FIELD_UI_PREFIX . '/display/default/layout');
$this
->clickContextualLink('.layout-builder-block', 'Manage attributes');
$assert_session
->assertWaitOnAjaxRequest();
// Verify only fields for allowed attributes are rendered.
foreach ($attribute_fields as $attribute_inner => $attribute_status) {
if ($attribute_fields[$attribute_inner]) {
$this
->assertTrue($page
->hasField($group . '[' . $attribute_inner . ']'), "Attribute field " . $attribute_inner . " is rendered for " . $group . " group");
}
else {
$this
->assertFalse($page
->hasField($group . '[' . $attribute_inner . ']'), "Attribute field " . $attribute_inner . " is not rendered for " . $group . " group");
}
}
// Create and load a test node.
$page
->pressButton('Update');
$assert_session
->assertWaitOnAjaxRequest();
$page
->pressButton('Save layout');
$this
->drupalGet('node/add/bundle_with_section_field');
$page
->fillField('Title', 'Test Node Title');
$page
->pressButton('Save');
$this
->drupalGet('node/1');
// Load page and verify only allowed attributes are rendered in markup.
foreach ($attributes as $attribute_inner => $test_value) {
// Replace 'data-*' attribute with 'data' to match expected FAPI key.
$attribute_field = substr($attribute_inner, 0, 5) === "data-" ? 'data' : $attribute_inner;
if ($attribute_fields[$attribute_field]) {
$element = $page
->find('xpath', '//*[contains(@' . $attribute_inner . ', "' . $test_value . '")]');
$this
->assertNotEmpty($element, "Attribute " . $attribute_inner . " rendered in " . $group . " group");
}
else {
$element = $page
->find('xpath', '//*[contains(@' . $attribute_inner . ', "' . $test_value . '")]');
$this
->assertEmpty($element, "Attribute " . $attribute_inner . " not rendered in " . $group . " group");
}
}
}
// After last loop, verify details element is no longer rendered.
$element = $page
->find('xpath', '//details[contains(@id, "edit-' . $group . '")]');
$this
->assertEmpty($element, "Details element not rendered");
}