Commit 3ce319d6 authored by catch's avatar catch

Issue #3187309 by longwave, Spokje, ayushmishra206, adityasingh, mondrake:...

Issue #3187309 by longwave, Spokje, ayushmishra206, adityasingh, mondrake: Convert assertions involving use of xpath on select and option elements to WebAssert
parent 47344caf
......@@ -64,15 +64,13 @@ public function testExistingFormat() {
$this->assertNull($editor, 'No Editor config entity exists yet.');
// Verify the "Text Editor" <select> when a text editor is available.
$select = $this->xpath('//select[@name="editor[editor]"]');
$select_is_disabled = $this->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
$options = $this->xpath('//select[@name="editor[editor]"]/option');
$this->assertCount(1, $select, 'The Text Editor select exists.');
$this->assertCount(0, $select_is_disabled, 'The Text Editor select is not disabled.');
$this->assertCount(2, $options, 'The Text Editor select has two options.');
$this->assertSame('None', $options[0]->getText(), 'Option 1 in the Text Editor select is "None".');
$this->assertSame('CKEditor', $options[1]->getText(), 'Option 2 in the Text Editor select is "CKEditor".');
$this->assertSame('selected', $options[0]->getAttribute('selected'), 'Option 1 ("None") is selected.');
$select = $this->assertSession()->selectExists('editor[editor]');
$this->assertFalse($select->hasAttribute('disabled'));
$options = $select->findAll('css', 'option');
$this->assertCount(2, $options);
$this->assertSame('None', $options[0]->getText());
$this->assertSame('CKEditor', $options[1]->getText());
$this->assertTrue($options[0]->isSelected());
// Select the "CKEditor" editor and click the "Save configuration" button.
$edit = [
......@@ -247,15 +245,13 @@ public function testNewFormat() {
$this->drupalGet('admin/config/content/formats/add');
// Verify the "Text Editor" <select> when a text editor is available.
$select = $this->xpath('//select[@name="editor[editor]"]');
$select_is_disabled = $this->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
$options = $this->xpath('//select[@name="editor[editor]"]/option');
$this->assertCount(1, $select, 'The Text Editor select exists.');
$this->assertCount(0, $select_is_disabled, 'The Text Editor select is not disabled.');
$this->assertCount(2, $options, 'The Text Editor select has two options.');
$this->assertSame('None', $options[0]->getText(), 'Option 1 in the Text Editor select is "None".');
$this->assertSame('CKEditor', $options[1]->getText(), 'Option 2 in the Text Editor select is "CKEditor".');
$this->assertSame('selected', $options[0]->getAttribute('selected'), 'Option 1 ("None") is selected.');
$select = $this->assertSession()->selectExists('editor[editor]');
$this->assertFalse($select->hasAttribute('disabled'));
$options = $select->findAll('css', 'option');
$this->assertCount(2, $options);
$this->assertSame('None', $options[0]->getText());
$this->assertSame('CKEditor', $options[1]->getText());
$this->assertTrue($options[0]->isSelected());
// Name our fancy new text format, select the "CKEditor" editor and click
// the "Configure" button.
......
......@@ -93,11 +93,11 @@ public function testLoading() {
// - doesn't have access to the filtered_html text format, so: no text editor.
$this->drupalLogin($this->untrustedUser);
$this->drupalGet('node/add/article');
list($settings, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this->getThingsToCheck();
list($settings, $editor_settings_present, $editor_js_present, $body) = $this->getThingsToCheck();
$this->assertFalse($editor_settings_present, 'No Text Editor module settings.');
$this->assertFalse($editor_js_present, 'No Text Editor JavaScript.');
$this->assertCount(1, $body, 'A body field exists.');
$this->assertCount(0, $format_selector, 'No text format selector exists on the page.');
$this->assertSession()->elementNotExists('css', 'select.js-filter-list');
$hidden_input = $this->xpath('//input[@type="hidden" and contains(@class, "editor")]');
$this->assertCount(0, $hidden_input, 'A single text format hidden input does not exist on the page.');
// Verify that CKEditor glue JS is absent.
......@@ -112,7 +112,7 @@ public function testLoading() {
// - does have access to the filtered_html text format, so: CKEditor.
$this->drupalLogin($this->normalUser);
$this->drupalGet('node/add/article');
list($settings, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this->getThingsToCheck();
list($settings, $editor_settings_present, $editor_js_present, $body) = $this->getThingsToCheck();
$ckeditor_plugin = $this->container->get('plugin.manager.editor')->createInstance('ckeditor');
$editor = Editor::load('filtered_html');
$expected = [
......@@ -130,9 +130,11 @@ public function testLoading() {
$this->assertEquals($expected, $settings['editor'], "Text Editor module's JavaScript settings on the page are correct.");
$this->assertTrue($editor_js_present, 'Text Editor JavaScript is present.');
$this->assertCount(1, $body, 'A body field exists.');
$this->assertCount(1, $format_selector, 'A single text format selector exists on the page.');
$specific_format_selector = $this->xpath('//select[contains(@class, "filter-list") and @data-editor-for="edit-body-0-value"]');
$this->assertCount(1, $specific_format_selector, 'A single text format selector exists on the page and has a "data-editor-for" attribute with the correct value.');
// Verify that a single text format selector exists on the page and has a
// "data-editor-for" attribute with the correct value.
$this->assertSession()->elementsCount('css', 'select.js-filter-list', 1);
$select = $this->assertSession()->elementExists('css', 'select.js-filter-list');
$this->assertSame('edit-body-0-value', $select->getAttribute('data-editor-for'));
$this->assertContains('ckeditor/drupal.ckeditor', explode(',', $settings['ajaxPageState']['libraries']), 'CKEditor glue library is present.');
// Enable the ckeditor_test module, customize configuration. In this case,
......@@ -147,7 +149,7 @@ public function testLoading() {
$editor->setSettings($editor_settings);
$editor->save();
$this->drupalGet('node/add/article');
list($settings, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this->getThingsToCheck();
list($settings, $editor_settings_present, $editor_js_present, $body) = $this->getThingsToCheck();
$expected = [
'formats' => [
'filtered_html' => [
......@@ -266,8 +268,6 @@ protected function getThingsToCheck() {
isset($settings['ajaxPageState']['libraries']) && in_array('ckeditor/drupal.ckeditor', explode(',', $settings['ajaxPageState']['libraries'])),
// Body field.
$this->xpath('//textarea[@id="edit-body-0-value"]'),
// Format selector.
$this->xpath('//select[contains(@class, "filter-list")]'),
];
}
......
......@@ -249,13 +249,8 @@ public function testExport() {
$option_node = $this->assertSession()->optionExists("config_type", 'Simple configuration');
$this->assertTrue($option_node->isSelected());
// Spot check several known simple configuration files.
$element = $this->xpath('//select[@name="config_name"]')[0];
$options = $element->findAll('css', 'option');
$expected_options = ['system.site', 'user.settings'];
foreach ($options as &$option) {
$option = $option->getValue();
}
$this->assertSame($expected_options, array_intersect($expected_options, $options), 'The expected configuration files are listed.');
$this->assertSession()->optionExists('config_name', 'system.site');
$this->assertSession()->optionExists('config_name', 'user.settings');
$this->drupalGet('admin/config/development/configuration/single/export/system.simple/system.image');
$this->assertEquals("toolkit: gd\n_core:\n default_config_hash: durWHaKeBaq4d9Wpi4RqwADj1OufDepcnJuhVLmKN24\n", $this->xpath('//textarea[@name="export"]')[0]->getValue(), 'The expected system configuration is displayed.');
......
......@@ -215,11 +215,11 @@ public function testSettingsUI() {
'und',
'zxx',
];
$elements = $this->xpath('//select[@id="edit-settings-node-article-settings-language-langcode"]/option');
// Compare values inside the option elements with expected values.
for ($i = 0; $i < count($elements); $i++) {
$this->assertEqual($expected_elements[$i], $elements[$i]->getValue());
}
$options = $this->assertSession()->selectExists('edit-settings-node-article-settings-language-langcode')->findAll('css', 'option');
$options = array_map(function ($item) {
return $item->getValue();
}, $options);
$this->assertSame($expected_elements, $options);
}
/**
......
......@@ -66,12 +66,10 @@ public function testNoEditorAvailable() {
$this->assertLessThan($filters_pos, $editor_pos);
// Verify the <select>.
$select = $this->xpath('//select[@name="editor[editor]"]');
$select_is_disabled = $this->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
$options = $this->xpath('//select[@name="editor[editor]"]/option');
$this->assertCount(1, $select, 'The Text Editor select exists.');
$this->assertCount(1, $select_is_disabled, 'The Text Editor select is disabled.');
$this->assertCount(1, $options, 'The Text Editor select has only one option.');
$select = $this->assertSession()->selectExists('editor[editor]');
$this->assertSame('disabled', $select->getAttribute('disabled'));
$options = $select->findAll('css', 'option');
$this->assertCount(1, $options);
$this->assertSame('None', $options[0]->getText(), 'Option 1 in the Text Editor select is "None".');
$this->assertRaw('This option is disabled because no modules that provide a text editor are currently enabled.');
}
......@@ -211,12 +209,10 @@ protected function enableUnicornEditor() {
*/
protected function selectUnicornEditor() {
// Verify the <select> when a text editor is available.
$select = $this->xpath('//select[@name="editor[editor]"]');
$select_is_disabled = $this->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
$options = $this->xpath('//select[@name="editor[editor]"]/option');
$this->assertCount(1, $select, 'The Text Editor select exists.');
$this->assertCount(0, $select_is_disabled, 'The Text Editor select is not disabled.');
$this->assertCount(2, $options, 'The Text Editor select has two options.');
$select = $this->assertSession()->selectExists('editor[editor]');
$this->assertFalse($select->hasAttribute('disabled'));
$options = $select->findAll('css', 'option');
$this->assertCount(2, $options);
$this->assertSame('None', $options[0]->getText(), 'Option 1 in the Text Editor select is "None".');
$this->assertSame('Unicorn Editor', $options[1]->getText(), 'Option 2 in the Text Editor select is "Unicorn Editor".');
$this->assertTrue($options[0]->hasAttribute('selected'), 'Option 1 ("None") is selected.');
......@@ -248,13 +244,11 @@ protected function verifyUnicornEditorConfiguration($format_id, $ponies_too = TR
$this->assertSame('unicorn', $editor->getEditor(), 'The text editor is configured correctly.');
$this->assertSame($ponies_too, $settings['ponies_too'], 'The text editor settings are stored correctly.');
$this->drupalGet('admin/config/content/formats/manage/' . $format_id);
$select = $this->xpath('//select[@name="editor[editor]"]');
$select_is_disabled = $this->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
$options = $this->xpath('//select[@name="editor[editor]"]/option');
$this->assertCount(1, $select, 'The Text Editor select exists.');
$this->assertCount(0, $select_is_disabled, 'The Text Editor select is not disabled.');
$this->assertCount(2, $options, 'The Text Editor select has two options.');
$this->assertTrue($options[1]->hasAttribute('selected'), 'Option 2 ("Unicorn Editor") is selected.');
$select = $this->assertSession()->selectExists('editor[editor]');
$this->assertFalse($select->hasAttribute('disabled'));
$options = $select->findAll('css', 'option');
$this->assertCount(2, $options);
$this->assertTrue($options[1]->isSelected(), 'Option 2 ("Unicorn Editor") is selected.');
}
}
......@@ -147,11 +147,11 @@ public function testLoading() {
// - doesn't have access to the full_html text format, so: no text editor.
$this->drupalLogin($this->normalUser);
$this->drupalGet('node/add/article');
list(, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this->getThingsToCheck('body');
list(, $editor_settings_present, $editor_js_present, $body) = $this->getThingsToCheck('body');
$this->assertFalse($editor_settings_present, 'No Text Editor module settings.');
$this->assertFalse($editor_js_present, 'No Text Editor JavaScript.');
$this->assertCount(1, $body, 'A body field exists.');
$this->assertCount(0, $format_selector, 'No text format selector exists on the page because the user only has access to a single format.');
$this->assertSession()->elementNotExists('css', 'select.js-filter-list');
$this->drupalLogout($this->normalUser);
// The privileged user:
......@@ -159,7 +159,7 @@ public function testLoading() {
// - does have access to the full_html text format, so: Unicorn text editor.
$this->drupalLogin($this->privilegedUser);
$this->drupalGet('node/add/article');
list($settings, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this->getThingsToCheck('body');
list($settings, $editor_settings_present, $editor_js_present, $body) = $this->getThingsToCheck('body');
$expected = [
'formats' => [
'full_html' => [
......@@ -175,9 +175,9 @@ public function testLoading() {
$this->assertSame($expected, $settings['editor'], "Text Editor module's JavaScript settings on the page are correct.");
$this->assertTrue($editor_js_present, 'Text Editor JavaScript is present.');
$this->assertCount(1, $body, 'A body field exists.');
$this->assertCount(1, $format_selector, 'A single text format selector exists on the page.');
$specific_format_selector = $this->xpath('//select[contains(@class, "filter-list") and @data-editor-for="edit-body-0-value"]');
$this->assertCount(1, $specific_format_selector, 'A single text format selector exists on the page and has a "data-editor-for" attribute with the correct value.');
$this->assertSession()->elementsCount('css', 'select.js-filter-list', 1);
$select = $this->assertSession()->elementExists('css', 'select.js-filter-list');
$this->assertSame('edit-body-0-value', $select->getAttribute('data-editor-for'));
// Load the editor image dialog form and make sure it does not fatal.
$this->drupalGet('editor/dialog/image/full_html');
......@@ -197,7 +197,7 @@ public function testLoading() {
// - has access to the plain_text text format, so: Unicorn text editor.
$this->drupalLogin($this->untrustedUser);
$this->drupalGet('node/add/article');
list($settings, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this->getThingsToCheck('body');
list($settings, $editor_settings_present, $editor_js_present, $body) = $this->getThingsToCheck('body');
$expected = [
'formats' => [
'plain_text' => [
......@@ -213,7 +213,7 @@ public function testLoading() {
$this->assertSame($expected, $settings['editor'], "Text Editor module's JavaScript settings on the page are correct.");
$this->assertTrue($editor_js_present, 'Text Editor JavaScript is present.');
$this->assertCount(1, $body, 'A body field exists.');
$this->assertCount(0, $format_selector, 'No text format selector exists on the page.');
$this->assertSession()->elementNotExists('css', 'select.js-filter-list');
$hidden_input = $this->xpath('//input[@type="hidden" and @value="plain_text" and @data-editor-for="edit-body-0-value"]');
$this->assertCount(1, $hidden_input, 'A single text format hidden input exists on the page and has a "data-editor-for" attribute with the correct value.');
......@@ -230,13 +230,13 @@ public function testLoading() {
// that they are not allowed to use. The editor is still loaded. CKEditor,
// for example, supports being loaded in a disabled state.
$this->drupalGet('node/1/edit');
list(, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this->getThingsToCheck('body');
list(, $editor_settings_present, $editor_js_present, $body) = $this->getThingsToCheck('body');
$this->assertTrue($editor_settings_present, 'Text Editor module settings.');
$this->assertTrue($editor_js_present, 'Text Editor JavaScript.');
$this->assertCount(1, $body, 'A body field exists.');
$this->assertSession()->fieldDisabled("edit-body-0-value");
$this->assertSession()->fieldValueEquals("edit-body-0-value", 'This field has been disabled because you do not have sufficient permissions to edit it.');
$this->assertCount(0, $format_selector, 'No text format selector exists on the page.');
$this->assertSession()->elementNotExists('css', 'select.js-filter-list');
$hidden_input = $this->xpath('//input[@type="hidden" and contains(@class, "editor")]');
$this->assertCount(0, $hidden_input, 'A single text format hidden input does not exist on the page.');
}
......@@ -270,13 +270,16 @@ public function testSupportedElementTypes() {
// Assert the unicorn editor works with textfields.
$this->drupalLogin($this->privilegedUser);
$this->drupalGet('node/1/edit');
list(, $editor_settings_present, $editor_js_present, $field, $format_selector) = $this->getThingsToCheck('field-text', 'input');
list(, $editor_settings_present, $editor_js_present, $field) = $this->getThingsToCheck('field-text', 'input');
$this->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page.");
$this->assertTrue($editor_js_present, 'Text Editor JavaScript is present.');
$this->assertCount(1, $field, 'A text field exists.');
$this->assertCount(1, $format_selector, 'A single text format selector exists on the page.');
$specific_format_selector = $this->xpath('//select[contains(@class, "filter-list") and contains(@class, "editor") and @data-editor-for="edit-field-text-0-value"]');
$this->assertCount(1, $specific_format_selector, 'A single text format selector exists on the page and has the "editor" class and a "data-editor-for" attribute with the correct value.');
// Verify that a single text format selector exists on the page and has the
// "editor" class and a "data-editor-for" attribute with the correct value.
$this->assertSession()->elementsCount('css', 'select.js-filter-list', 1);
$select = $this->assertSession()->elementExists('css', 'select.js-filter-list');
$this->assertStringContainsString('editor', $select->getAttribute('class'));
$this->assertSame('edit-field-text-0-value', $select->getAttribute('data-editor-for'));
// Associate the trex text editor with the "Full HTML" text format.
$editor->delete();
......@@ -286,13 +289,17 @@ public function testSupportedElementTypes() {
])->save();
$this->drupalGet('node/1/edit');
list(, $editor_settings_present, $editor_js_present, $field, $format_selector) = $this->getThingsToCheck('field-text', 'input');
list(, $editor_settings_present, $editor_js_present, $field) = $this->getThingsToCheck('field-text', 'input');
$this->assertFalse($editor_settings_present, "Text Editor module's JavaScript settings are not on the page.");
$this->assertFalse($editor_js_present, 'Text Editor JavaScript is not present.');
$this->assertCount(1, $field, 'A text field exists.');
$this->assertCount(1, $format_selector, 'A single text format selector exists on the page.');
$specific_format_selector = $this->xpath('//select[contains(@class, "filter-list") and contains(@class, "editor") and @data-editor-for="edit-field-text-0-value"]');
$this->assertCount(0, $specific_format_selector, 'No text format selector exists on the page with the "editor" class and a "data-editor-for" attribute with the expected value.');
// Verify that a single text format selector exists on the page but without
// the "editor" class or a "data-editor-for" attribute with the expected
// value.
$this->assertSession()->elementsCount('css', 'select.js-filter-list', 1);
$select = $this->assertSession()->elementExists('css', 'select.js-filter-list');
$this->assertStringNotContainsString('editor', $select->getAttribute('class'));
$this->assertNotSame('edit-field-text-0-value', $select->getAttribute('data-editor-for'));
}
protected function getThingsToCheck($field_name, $type = 'textarea') {
......@@ -306,8 +313,6 @@ protected function getThingsToCheck($field_name, $type = 'textarea') {
strpos($this->getSession()->getPage()->getContent(), drupal_get_path('module', 'editor') . '/js/editor.js') !== FALSE,
// Body field.
$this->xpath('//' . $type . '[@id="edit-' . $field_name . '-0-value"]'),
// Format selector.
$this->xpath('//select[contains(@class, "filter-list")]'),
];
}
......
......@@ -390,21 +390,11 @@ protected function createEntityReferenceField($target_type, $bundles = []) {
* An array of expected options.
*/
protected function assertFieldSelectOptions($name, array $expected_options) {
$xpath = $this->assertSession()->buildXPathQuery('//select[@name=:name]', [':name' => $name]);
$fields = $this->xpath($xpath);
if ($fields) {
$field = $fields[0];
$options = $field->findAll('xpath', 'option');
array_walk($options, function (NodeElement &$option) {
$option = $option->getValue();
});
sort($options);
sort($expected_options);
$this->assertSame($expected_options, $options);
}
else {
$this->fail('Unable to find field ' . $name);
}
$options = $this->assertSession()->selectExists($name)->findAll('xpath', 'option');
array_walk($options, function (NodeElement &$option) {
$option = $option->getValue();
});
$this->assertEqualsCanonicalizing($expected_options, $options);
}
}
......@@ -61,13 +61,11 @@ public function testHandlerUI() {
$this->drupalGet($url);
// Tests the available formatter options.
$result = $this->xpath('//select[@id=:id]/option', [':id' => 'edit-options-type']);
$options = $this->assertSession()->selectExists('edit-options-type')->findAll('css', 'option');
$options = array_map(function ($item) {
return $item->getAttribute('value');
}, $result);
// @todo Replace this sort by assertArray once it's in.
sort($options, SORT_STRING);
$this->assertEqual(['text_default', 'text_trimmed'], $options, 'The text formatters for a simple text field appear as expected.');
return $item->getValue();
}, $options);
$this->assertEqualsCanonicalizing(['text_default', 'text_trimmed'], $options);
$this->submitForm(['options[type]' => 'text_trimmed'], 'Apply');
......@@ -115,13 +113,11 @@ public function testHandlerUIAggregation() {
// Test the click sort column options.
// Tests the available formatter options.
$result = $this->xpath('//select[@id=:id]/option', [':id' => 'edit-options-click-sort-column']);
$options = $this->assertSession()->selectExists('edit-options-click-sort-column')->findAll('css', 'option');
$options = array_map(function ($item) {
return (string) $item->getAttribute('value');
}, $result);
sort($options, SORT_STRING);
$this->assertEqual(['format', 'value'], $options, 'The expected sort field options were found.');
return $item->getValue();
}, $options);
$this->assertEqualsCanonicalizing(['format', 'value'], $options);
}
/**
......
......@@ -350,27 +350,16 @@ public function testFieldAdminHandler() {
* An array of expected options.
*/
protected function assertFieldSelectOptions($name, array $expected_options) {
$xpath = $this->assertSession()->buildXPathQuery('//select[@name=:name]', [':name' => $name]);
$fields = $this->xpath($xpath);
if ($fields) {
$field = $fields[0];
$options = $field->findAll('xpath', 'option');
$optgroups = $field->findAll('xpath', 'optgroup');
foreach ($optgroups as $optgroup) {
$options = array_merge($options, $optgroup->findAll('xpath', 'option'));
}
array_walk($options, function (NodeElement &$option) {
$option = $option->getAttribute('value');
});
sort($options);
sort($expected_options);
$this->assertSame($expected_options, $options);
}
else {
$this->fail('Unable to find field ' . $name);
$field = $this->assertSession()->selectExists($name);
$options = $field->findAll('xpath', 'option');
$optgroups = $field->findAll('xpath', 'optgroup');
foreach ($optgroups as $optgroup) {
$options = array_merge($options, $optgroup->findAll('xpath', 'option'));
}
array_walk($options, function (NodeElement &$option) {
$option = $option->getAttribute('value');
});
$this->assertEqualsCanonicalizing($expected_options, $options);
}
}
......@@ -2,7 +2,6 @@
namespace Drupal\Tests\field_ui\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\field\Entity\FieldConfig;
......@@ -253,9 +252,9 @@ public function addExistingField() {
// Check that fields of other entity types (here, the 'comment_body' field)
// do not show up in the "Re-use existing field" list.
$this->assertEmpty($this->xpath('//select[@id="edit-existing-storage-name"]//option[@value="comment"]'), 'The list of options respects entity type restrictions.');
$this->assertSession()->optionNotExists('edit-existing-storage-name', 'comment');
// Validate the FALSE assertion above by also testing a valid one.
$this->assertNotEmpty($this->xpath('//select[@id="edit-existing-storage-name"]//option[@value=:field_name]', [':field_name' => $this->fieldName]), 'The list of options shows a valid option.');
$this->assertSession()->optionExists('edit-existing-storage-name', $this->fieldName);
// Add a new field based on an existing field.
$this->fieldUIAddExistingField("admin/structure/types/manage/page", $this->fieldName, $this->fieldLabel . '_2');
......@@ -609,8 +608,8 @@ public function testLockedField() {
public function testHiddenFields() {
// Check that the field type is not available in the 'add new field' row.
$this->drupalGet('admin/structure/types/manage/' . $this->contentType . '/fields/add-field');
$this->assertEmpty($this->xpath('//select[@id="edit-new-storage-type"]//option[@value="hidden_test_field"]'), "The 'add new field' select respects field types 'no_ui' property.");
$this->assertNotEmpty($this->xpath('//select[@id="edit-new-storage-type"]//option[@value="shape"]'), "The 'add new field' select shows a valid option.");
$this->assertSession()->optionNotExists('edit-new-storage-type', 'hidden_test_field');
$this->assertSession()->optionExists('edit-new-storage-type', 'shape');
// Create a field storage and a field programmatically.
$field_name = 'hidden_test_field';
......@@ -640,17 +639,17 @@ public function testHiddenFields() {
// Check that the field does not appear in the 're-use existing field' row
// on other bundles.
$this->drupalGet('admin/structure/types/manage/page/fields/add-field');
$this->assertEmpty($this->xpath('//select[@id="edit-existing-storage-name"]//option[@value=:field_name]', [':field_name' => $field_name]), "The 're-use existing field' select respects field types 'no_ui' property.");
$this->assertNotEmpty($this->xpath('//select[@id="edit-existing-storage-name"]//option[@value=:field_name]', [':field_name' => 'field_tags']), "The 're-use existing field' select shows a valid option.");
$this->assertSession()->optionNotExists('edit-existing-storage-name', $field_name);
$this->assertSession()->optionExists('edit-existing-storage-name', 'field_tags');
// Check that non-configurable fields are not available.
$field_types = \Drupal::service('plugin.manager.field.field_type')->getDefinitions();
foreach ($field_types as $field_type => $definition) {
if (empty($definition['no_ui'])) {
$this->assertNotEmpty($this->xpath('//select[@id="edit-new-storage-type"]//option[@value=:field_type]', [':field_type' => $field_type]), new FormattableMarkup('Configurable field type @field_type is available.', ['@field_type' => $field_type]));
$this->assertSession()->optionExists('edit-new-storage-type', $field_type);
}
else {
$this->assertEmpty($this->xpath('//select[@id="edit-new-storage-type"]//option[@value=:field_type]', [':field_type' => $field_type]), new FormattableMarkup('Non-configurable field type @field_type is not available.', ['@field_type' => $field_type]));
$this->assertSession()->optionNotExists('edit-new-storage-type', $field_type);
}
}
}
......
......@@ -178,10 +178,7 @@ protected function doFilterFormTestAsNonAdmin() {
* The HTML ID of the select element.
*/
protected function assertNoSelect($id) {
$select = $this->xpath('//select[@id=:id]', [':id' => $id]);
$this->assertEmpty($select, new FormattableMarkup('Field @id does not exist.', [
'@id' => $id,
]));
$this->assertSession()->elementNotExists('xpath', "//select[@id=$id]");
}
/**
......@@ -198,35 +195,12 @@ protected function assertNoSelect($id) {
* TRUE if the assertion passed; FALSE otherwise.
*/
protected function assertOptions($id, array $expected_options, $selected) {
$select = $this->xpath('//select[@id=:id]', [':id' => $id]);
$this->assertNotEmpty($select, new FormattableMarkup('Field @id exists.', [
'@id' => $id,
]));
$select = reset($select);
$select = $this->assertSession()->selectExists($id);
$found_options = $select->findAll('css', 'option');
foreach ($found_options as $found_key => $found_option) {
$expected_key = array_search($found_option->getValue(), $expected_options);
if ($expected_key !== FALSE) {
unset($found_options[$found_key]);
unset($expected_options[$expected_key]);
}
}
// Make sure that all expected options were found and that there are no
// unexpected options.
foreach ($expected_options as $expected_option) {
$this->fail(new FormattableMarkup('Option @option for field @id exists.', [
'@option' => $expected_option,
'@id' => $id,
]));
}
foreach ($found_options as $found_option) {
$this->fail(new FormattableMarkup('Option @option for field @id does not exist.', [
'@option' => $found_option->getValue(),
'@id' => $id,
]));
}
$found_options = array_map(function ($item) {
return $item->getValue();
}, $found_options);
$this->assertEqualsCanonicalizing($expected_options, $found_options);
$this->assertTrue($this->assertSession()->optionExists($id, $selected)->isSelected());
}
......@@ -243,12 +217,8 @@ protected function assertOptions($id, array $expected_options, $selected) {
* TRUE if the assertion passed; FALSE otherwise.
*/
protected function assertRequiredSelectAndOptions($id, array $options) {
$select = $this->xpath('//select[@id=:id and contains(@required, "required")]', [
':id' => $id,
]);
$this->assertNotEmpty($select, new FormattableMarkup('Required field @id exists.', [
'@id' => $id,
]));
$select = $this->assertSession()->selectExists($id);
$this->assertSame('required', $select->getAttribute('required'));
// A required select element has a "- Select -" option whose key is an empty
// string.
$options[] = '';
......
......@@ -146,20 +146,12 @@ public function testFormatPermissions() {
$this->assertFalse($this->webUser->hasPermission($this->disallowedFormat->getPermissionName()), 'A regular user does not have permission to use the disallowed text format.');
// Make sure that the allowed format appears on the node form and that
// the disallowed format does not.
// the disallowed format and fallback format do not.
$this->drupalLogin($this->webUser);
$this->drupalGet('node/add/page');
$elements = $this->xpath('//select[@name=:name]/option', [
':name' => 'body[0][format]',
':option' => $this->allowedFormat->id(),
]);
$options = [];
foreach ($elements as $element) {
$options[$element->getValue()] = $element;