diff --git a/browsertest-convert.php b/browsertest-convert.php new file mode 100644 index 0000000..a2c3dfb --- /dev/null +++ b/browsertest-convert.php @@ -0,0 +1,624 @@ +drupalGetMails(') !== FALSE) { + $file_content = str_replace(" extends BrowserTestBase {", " extends BrowserTestBase {\n use AssertMailTrait {\n getMails as drupalGetMails;\n }", $file_content); + $file_content = str_replace("use Drupal\\Tests\\BrowserTestBase;", "use Drupal\\Tests\\BrowserTestBase;\nuse Drupal\Core\Test\AssertMailTrait;", $file_content); + } + + $test_dir = dirname("$browsertest_dir/$file_name"); + if (!file_exists($test_dir)) { + mkdir($test_dir, 0777, TRUE); + } + + file_put_contents("$browsertest_dir/$file_name", $file_content); + + + // Keep existing base classes. + if (strpos($simpletest_file_path, 'Base.php') === FALSE) { + unlink($simpletest_file_path); + } + } + + + print "Tests converted for module $module_name.\n"; +} diff --git a/core/modules/action/src/Tests/ActionListTest.php b/core/modules/action/tests/src/Functional/ActionListTest.php similarity index 87% rename from core/modules/action/src/Tests/ActionListTest.php rename to core/modules/action/tests/src/Functional/ActionListTest.php index f533cdf..2510e86 100644 --- a/core/modules/action/src/Tests/ActionListTest.php +++ b/core/modules/action/tests/src/Functional/ActionListTest.php @@ -1,15 +1,15 @@ 'table', + '#header' => ['Column 1', 'Column 2', 'Column 3'], + 'row_1' => [ + 'col_1' => ['#plain_text' => 'foo'], + 'col_2' => ['#plain_text' => 'bar'], + 'col_3' => ['#plain_text' => 'baz'], + ], + 'row_2' => [ + 'col_1' => ['#plain_text' => 'one'], + 'col_2' => ['#plain_text' => 'two'], + 'col_3' => ['#plain_text' => 'three'], + ], + ]; + + $form['name'] = [ + '#type' => 'textfield', + '#title' => 'Name', + '#default_value' => 'Test name', + ]; + + $form['options'] = [ + '#type' => 'select', + '#title' => 'Options', + '#options' => [ + 1 => 'one', + 2 => 'two', + 3 => 'three', + ], + '#default_value' => 2, + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'test_page_form'; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + // Empty on purpose, we just want to test the rendered form elements. + } + +} diff --git a/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml b/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml index 9d07a34..5d84695 100644 --- a/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml +++ b/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml @@ -74,3 +74,11 @@ test_page_test.pipe: _controller: '\Drupal\test_page_test\Controller\Test::renderPipeInLink' requirements: _access: 'TRUE' + +test_page_test.field_xpath: + path: '/test-field-xpath' + defaults: + _title: 'Table and form elements for field xpath assertion testing' + _form: '\Drupal\test_page_test\Form\TestForm' + requirements: + _access: 'TRUE' diff --git a/core/modules/system/src/Tests/Bootstrap/DrupalSetMessageTest.php b/core/modules/system/tests/src/Functional/Bootstrap/DrupalSetMessageTest.php similarity index 90% rename from core/modules/system/src/Tests/Bootstrap/DrupalSetMessageTest.php rename to core/modules/system/tests/src/Functional/Bootstrap/DrupalSetMessageTest.php index 29ca02f..1b0f31a 100644 --- a/core/modules/system/src/Tests/Bootstrap/DrupalSetMessageTest.php +++ b/core/modules/system/tests/src/Functional/Bootstrap/DrupalSetMessageTest.php @@ -1,15 +1,15 @@ xpath() instead and check the values directly in the test. + */ + protected function assertFieldByXPath($xpath, $value = NULL, $message = '') { + $fields = $this->xpath($xpath); + + $this->assertFieldsByValue($fields, $value, $message); + } + + /** + * Asserts that a field does not exist or its value does not match, by XPath. + * + * @param string $xpath + * XPath used to find the field. + * @param string $value + * (optional) Value of the field, to assert that the field's value on the + * page does not match it. + * @param string $message + * (optional) A message to display with the assertion. Do not translate + * messages with t(). + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $this->assertSession()->fieldNotExists() or + * $this->assertSession()->fieldValueNotEquals() instead. + */ + protected function assertNoFieldByXPath($xpath, $value = NULL, $message = '') { + $fields = $this->xpath($xpath); + + // If value specified then check array for match. + $found = TRUE; + if (isset($value)) { + $found = FALSE; + if ($fields) { + foreach ($fields as $field) { + if ($field->getAttribute('value') == $value) { + $found = TRUE; + } + } + } + } + return $this->assertFalse($fields && $found, $message); + } + + /** + * Asserts that a field exists in the current page with a given Xpath result. + * + * @param \Behat\Mink\Element\NodeElement[] $fields + * Xml elements. + * @param string $value + * (optional) Value of the field to assert. You may pass in NULL (default) to skip + * checking the actual value, while still checking that the field exists. + * @param string $message + * (optional) A message to display with the assertion. Do not translate + * messages with t(). + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Iterate over the fields yourself instead and directly check the values in + * the test. + */ + protected function assertFieldsByValue($fields, $value = NULL, $message = '') { + // If value specified then check array for match. + $found = TRUE; + if (isset($value)) { + $found = FALSE; + if ($fields) { + foreach ($fields as $field) { + if ($field->getAttribute('value') == $value) { + // Input element with correct value. + $found = TRUE; + } + elseif ($field->find('xpath', '//option[@value = ' . (new Escaper())->escapeLiteral($value) . ' and @selected = "selected"]')) { + // Select element with an option. + $found = TRUE; + } + elseif ($field->getText() == $value) { + // Text area with correct text. + $found = TRUE; + } + } + } + } + $this->assertTrue($fields && $found, $message); + } + + /** * Passes if the raw text IS found escaped on the loaded page, fail otherwise. * * Raw text refers to the raw HTML that the page generated. diff --git a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php index eb315af..ef9c6b5 100644 --- a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php +++ b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php @@ -134,9 +134,9 @@ public function testPipeCharInLocator() { } /** - * Tests legacy asserts. + * Tests legacy text asserts. */ - public function testLegacyAsserts() { + public function testLegacyTextAsserts() { $this->drupalGet('test-encoded'); $dangerous = 'Bad html '; $sanitized = Html::escape($dangerous); @@ -144,4 +144,22 @@ public function testLegacyAsserts() { $this->assertText($sanitized); } + /** + * Tests legacy XPath asserts. + */ + public function testLegacyXPathAsserts() { + $this->drupalGet('test-field-xpath'); + $this->assertFieldsByValue($this->xpath("//h1[@class = 'page-title']"), NULL); + $this->assertFieldsByValue($this->xpath('//table/tbody/tr[2]/td[1]'), 'one'); + $this->assertFieldByXPath('//table/tbody/tr[2]/td[1]', 'one'); + + $this->assertFieldsByValue($this->xpath("//input[@id = 'edit-name']"), 'Test name'); + $this->assertFieldByXPath("//input[@id = 'edit-name']", 'Test name'); + $this->assertFieldsByValue($this->xpath("//select[@id = 'edit-options']"), '2'); + $this->assertFieldByXPath("//select[@id = 'edit-options']", '2'); + + $this->assertNoFieldByXPath('//notexisting'); + $this->assertNoFieldByXPath("//input[@id = 'edit-name']", 'wrong value'); + } + }