From 26ad8f8529563a946024a58cfb36ccad40a572ae Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Sat, 29 Apr 2017 23:58:48 +0100
Subject: [PATCH] Issue #2863416 by zviryatko, Jo Fitzgerald, michielnugter:
 Convert web tests to browser tests for filter module

---
 .../src/Plugin/Filter/FilterSparkles.php      |  2 +-
 .../src/Functional}/FilterAdminTest.php       | 22 +++++-----
 .../src/Functional}/FilterFormTest.php        | 42 ++++++++-----------
 .../Functional}/FilterFormatAccessTest.php    |  8 ++--
 .../Functional}/FilterHtmlImageSecureTest.php | 22 +++++-----
 5 files changed, 45 insertions(+), 51 deletions(-)
 rename core/modules/filter/{src/Tests => tests/src/Functional}/FilterAdminTest.php (96%)
 rename core/modules/filter/{src/Tests => tests/src/Functional}/FilterFormTest.php (88%)
 rename core/modules/filter/{src/Tests => tests/src/Functional}/FilterFormatAccessTest.php (98%)
 rename core/modules/filter/{src/Tests => tests/src/Functional}/FilterHtmlImageSecureTest.php (88%)

diff --git a/core/modules/filter/tests/filter_test_plugin/src/Plugin/Filter/FilterSparkles.php b/core/modules/filter/tests/filter_test_plugin/src/Plugin/Filter/FilterSparkles.php
index be2055bbcda7..f363fa9eae1c 100644
--- a/core/modules/filter/tests/filter_test_plugin/src/Plugin/Filter/FilterSparkles.php
+++ b/core/modules/filter/tests/filter_test_plugin/src/Plugin/Filter/FilterSparkles.php
@@ -11,7 +11,7 @@
  * This filter does not do anything, but enabling of its module is done in a
  * test.
  *
- * @see \Drupal\filter\Tests\FilterFormTest::testFilterForm()
+ * @see \Drupal\Tests\filter\Functional\FilterFormTest::testFilterForm()
  *
  * @Filter(
  *   id = "filter_sparkles",
diff --git a/core/modules/filter/src/Tests/FilterAdminTest.php b/core/modules/filter/tests/src/Functional/FilterAdminTest.php
similarity index 96%
rename from core/modules/filter/src/Tests/FilterAdminTest.php
rename to core/modules/filter/tests/src/Functional/FilterAdminTest.php
index aceeff56cc68..a3729cbbf384 100644
--- a/core/modules/filter/src/Tests/FilterAdminTest.php
+++ b/core/modules/filter/tests/src/Functional/FilterAdminTest.php
@@ -1,13 +1,13 @@
 <?php
 
-namespace Drupal\filter\Tests;
+namespace Drupal\Tests\filter\Functional;
 
 use Drupal\Component\Utility\Html;
 use Drupal\Component\Utility\Unicode;
 use Drupal\filter\Entity\FilterFormat;
 use Drupal\node\Entity\Node;
 use Drupal\node\Entity\NodeType;
-use Drupal\simpletest\WebTestBase;
+use Drupal\Tests\BrowserTestBase;
 use Drupal\user\RoleInterface;
 
 /**
@@ -15,7 +15,7 @@
  *
  * @group filter
  */
-class FilterAdminTest extends WebTestBase {
+class FilterAdminTest extends BrowserTestBase {
 
   /**
    * {@inheritdoc}
@@ -142,7 +142,7 @@ public function testFormatAdmin() {
     $edit_link = $this->xpath('//a[@href=:href]', [
       ':href' => \Drupal::url('entity.filter_format.edit_form', ['filter_format' => $format_id])
     ]);
-    $this->assertTrue($edit_link, format_string('Link href %href found.',
+    $this->assertNotEmpty($edit_link, format_string('Link href %href found.',
       ['%href' => 'admin/config/content/formats/manage/' . $format_id]
     ));
     $this->drupalGet('admin/config/content/formats/manage/' . $format_id);
@@ -218,7 +218,7 @@ public function testFilterAdmin() {
       ':first' => 'filters[' . $first_filter . '][weight]',
       ':second' => 'filters[' . $second_filter . '][weight]',
     ]);
-    $this->assertTrue(!empty($elements), 'Order confirmed in admin interface.');
+    $this->assertNotEmpty($elements, 'Order confirmed in admin interface.');
 
     // Reorder filters.
     $edit = [];
@@ -234,7 +234,7 @@ public function testFilterAdmin() {
       ':first' => 'filters[' . $second_filter . '][weight]',
       ':second' => 'filters[' . $first_filter . '][weight]',
     ]);
-    $this->assertTrue(!empty($elements), 'Reorder confirmed in admin interface.');
+    $this->assertNotEmpty($elements, 'Reorder confirmed in admin interface.');
 
     $filter_format = FilterFormat::load($restricted);
     foreach ($filter_format->filters() as $filter_name => $filter) {
@@ -260,9 +260,9 @@ public function testFilterAdmin() {
     $format = FilterFormat::load($edit['format']);
     $this->assertNotNull($format, 'Format found in database.');
     $this->drupalGet('admin/config/content/formats/manage/' . $format->id());
-    $this->assertFieldByName('roles[' . RoleInterface::AUTHENTICATED_ID . ']', '', 'Role found.');
-    $this->assertFieldByName('filters[' . $second_filter . '][status]', '', 'Line break filter found.');
-    $this->assertFieldByName('filters[' . $first_filter . '][status]', '', 'URL filter found.');
+    $this->assertFieldByName('roles[' . RoleInterface::AUTHENTICATED_ID . ']', RoleInterface::AUTHENTICATED_ID);
+    $this->assertFieldByName('filters[' . $second_filter . '][status]', TRUE);
+    $this->assertFieldByName('filters[' . $first_filter . '][status]', TRUE);
 
     // Disable new filter.
     $this->drupalPostForm('admin/config/content/formats/manage/' . $format->id() . '/disable', [], t('Disable'));
@@ -297,8 +297,8 @@ public function testFilterAdmin() {
     $this->assertText(t('Basic page @title has been created.', ['@title' => $edit['title[0][value]']]), 'Filtered node created.');
 
     // Verify that the creation message contains a link to a node.
-    $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'node/']);
-    $this->assert(isset($view_link), 'The message area contains a link to a node');
+    $view_link = $this->xpath('//div[contains(@class, "messages")]//a[contains(@href, :href)]', [':href' => 'node/']);
+    $this->assertNotEmpty($view_link, 'The message area contains a link to a node');
 
     $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
     $this->assertTrue($node, 'Node found in database.');
diff --git a/core/modules/filter/src/Tests/FilterFormTest.php b/core/modules/filter/tests/src/Functional/FilterFormTest.php
similarity index 88%
rename from core/modules/filter/src/Tests/FilterFormTest.php
rename to core/modules/filter/tests/src/Functional/FilterFormTest.php
index 9d0641e41bd7..fb0046b08a30 100644
--- a/core/modules/filter/src/Tests/FilterFormTest.php
+++ b/core/modules/filter/tests/src/Functional/FilterFormTest.php
@@ -1,17 +1,17 @@
 <?php
 
-namespace Drupal\filter\Tests;
+namespace Drupal\Tests\filter\Functional;
 
 use Drupal\Component\Utility\SafeMarkup;
 use Drupal\filter\Entity\FilterFormat;
-use Drupal\simpletest\WebTestBase;
+use Drupal\Tests\BrowserTestBase;
 
 /**
  * Tests form elements with associated text formats.
  *
  * @group filter
  */
-class FilterFormTest extends WebTestBase {
+class FilterFormTest extends BrowserTestBase {
 
   /**
    * Modules to enable for this test.
@@ -173,13 +173,10 @@ protected function doFilterFormTestAsNonAdmin() {
    *
    * @param string $id
    *   The HTML ID of the select element.
-   *
-   * @return bool
-   *   TRUE if the assertion passed; FALSE otherwise.
    */
   protected function assertNoSelect($id) {
     $select = $this->xpath('//select[@id=:id]', [':id' => $id]);
-    return $this->assertFalse($select, SafeMarkup::format('Field @id does not exist.', [
+    $this->assertEmpty($select, SafeMarkup::format('Field @id does not exist.', [
       '@id' => $id,
     ]));
   }
@@ -199,14 +196,13 @@ protected function assertNoSelect($id) {
    */
   protected function assertOptions($id, array $expected_options, $selected) {
     $select = $this->xpath('//select[@id=:id]', [':id' => $id]);
-    $select = reset($select);
-    $passed = $this->assertTrue($select instanceof \SimpleXMLElement, SafeMarkup::format('Field @id exists.', [
+    $this->assertNotEmpty($select, SafeMarkup::format('Field @id exists.', [
       '@id' => $id,
     ]));
-
-    $found_options = $this->getAllOptions($select);
+    $select = reset($select);
+    $found_options = $select->findAll('css', 'option');
     foreach ($found_options as $found_key => $found_option) {
-      $expected_key = array_search($found_option->attributes()->value, $expected_options);
+      $expected_key = array_search($found_option->getValue(), $expected_options);
       if ($expected_key !== FALSE) {
         $this->pass(SafeMarkup::format('Option @option for field @id exists.', [
           '@option' => $expected_options[$expected_key],
@@ -224,17 +220,15 @@ protected function assertOptions($id, array $expected_options, $selected) {
         '@option' => $expected_option,
         '@id' => $id,
       ]));
-      $passed = FALSE;
     }
     foreach ($found_options as $found_option) {
       $this->fail(SafeMarkup::format('Option @option for field @id does not exist.', [
-        '@option' => $found_option->attributes()->value,
+        '@option' => $found_option->getValue(),
         '@id' => $id,
       ]));
-      $passed = FALSE;
     }
 
-    return $passed && $this->assertOptionSelected($id, $selected);
+    $this->assertOptionSelected($id, $selected);
   }
 
   /**
@@ -253,14 +247,13 @@ protected function assertRequiredSelectAndOptions($id, array $options) {
     $select = $this->xpath('//select[@id=:id and contains(@required, "required")]', [
       ':id' => $id,
     ]);
-    $select = reset($select);
-    $passed = $this->assertTrue($select instanceof \SimpleXMLElement, SafeMarkup::format('Required field @id exists.', [
+    $this->assertNotEmpty($select, SafeMarkup::format('Required field @id exists.', [
       '@id' => $id,
     ]));
     // A required select element has a "- Select -" option whose key is an empty
     // string.
     $options[] = '';
-    return $passed && $this->assertOptions($id, $options, '');
+    $this->assertOptions($id, $options, '');
   }
 
   /**
@@ -276,8 +269,7 @@ protected function assertEnabledTextarea($id) {
     $textarea = $this->xpath('//textarea[@id=:id and not(contains(@disabled, "disabled"))]', [
       ':id' => $id,
     ]);
-    $textarea = reset($textarea);
-    return $this->assertTrue($textarea instanceof \SimpleXMLElement, SafeMarkup::format('Enabled field @id exists.', [
+    $this->assertNotEmpty($textarea, SafeMarkup::format('Enabled field @id exists.', [
       '@id' => $id,
     ]));
   }
@@ -295,17 +287,17 @@ protected function assertDisabledTextarea($id) {
     $textarea = $this->xpath('//textarea[@id=:id and contains(@disabled, "disabled")]', [
       ':id' => $id,
     ]);
-    $textarea = reset($textarea);
-    $passed = $this->assertTrue($textarea instanceof \SimpleXMLElement, SafeMarkup::format('Disabled field @id exists.', [
+    $this->assertNotEmpty($textarea, SafeMarkup::format('Disabled field @id exists.', [
       '@id' => $id,
     ]));
+    $textarea = reset($textarea);
     $expected = 'This field has been disabled because you do not have sufficient permissions to edit it.';
-    $passed = $passed && $this->assertEqual((string) $textarea, $expected, SafeMarkup::format('Disabled textarea @id hides text in an inaccessible text format.', [
+    $this->assertEqual($textarea->getText(), $expected, SafeMarkup::format('Disabled textarea @id hides text in an inaccessible text format.', [
       '@id' => $id,
     ]));
     // Make sure the text format select is not shown.
     $select_id = str_replace('value', 'format--2', $id);
-    return $passed && $this->assertNoSelect($select_id);
+    $this->assertNoSelect($select_id);
   }
 
 }
diff --git a/core/modules/filter/src/Tests/FilterFormatAccessTest.php b/core/modules/filter/tests/src/Functional/FilterFormatAccessTest.php
similarity index 98%
rename from core/modules/filter/src/Tests/FilterFormatAccessTest.php
rename to core/modules/filter/tests/src/Functional/FilterFormatAccessTest.php
index a4c23b2d5531..8c3394bdbf8a 100644
--- a/core/modules/filter/src/Tests/FilterFormatAccessTest.php
+++ b/core/modules/filter/tests/src/Functional/FilterFormatAccessTest.php
@@ -1,11 +1,11 @@
 <?php
 
-namespace Drupal\filter\Tests;
+namespace Drupal\Tests\filter\Functional;
 
 use Drupal\Component\Utility\Unicode;
 use Drupal\Core\Access\AccessResult;
 use Drupal\filter\Entity\FilterFormat;
-use Drupal\simpletest\WebTestBase;
+use Drupal\Tests\BrowserTestBase;
 
 /**
  * Tests access to text formats.
@@ -13,7 +13,7 @@
  * @group Access
  * @group filter
  */
-class FilterFormatAccessTest extends WebTestBase {
+class FilterFormatAccessTest extends BrowserTestBase {
 
   /**
    * Modules to enable.
@@ -151,7 +151,7 @@ public function testFormatPermissions() {
     ]);
     $options = [];
     foreach ($elements as $element) {
-      $options[(string) $element['value']] = $element;
+      $options[$element->getValue()] = $element;
     }
     $this->assertTrue(isset($options[$this->allowedFormat->id()]), 'The allowed text format appears as an option when adding a new node.');
     $this->assertFalse(isset($options[$this->disallowedFormat->id()]), 'The disallowed text format does not appear as an option when adding a new node.');
diff --git a/core/modules/filter/src/Tests/FilterHtmlImageSecureTest.php b/core/modules/filter/tests/src/Functional/FilterHtmlImageSecureTest.php
similarity index 88%
rename from core/modules/filter/src/Tests/FilterHtmlImageSecureTest.php
rename to core/modules/filter/tests/src/Functional/FilterHtmlImageSecureTest.php
index 249021a83263..065d4a6f792d 100644
--- a/core/modules/filter/src/Tests/FilterHtmlImageSecureTest.php
+++ b/core/modules/filter/tests/src/Functional/FilterHtmlImageSecureTest.php
@@ -1,20 +1,22 @@
 <?php
 
-namespace Drupal\filter\Tests;
+namespace Drupal\Tests\filter\Functional;
 
 use Drupal\comment\Tests\CommentTestTrait;
 use Drupal\Core\StreamWrapper\PublicStream;
-use Drupal\simpletest\WebTestBase;
 use Drupal\filter\Entity\FilterFormat;
+use Drupal\Tests\BrowserTestBase;
+use Drupal\Tests\TestFileCreationTrait;
 
 /**
  * Tests restriction of IMG tags in HTML input.
  *
  * @group filter
  */
-class FilterHtmlImageSecureTest extends WebTestBase {
+class FilterHtmlImageSecureTest extends BrowserTestBase {
 
   use CommentTestTrait;
+  use TestFileCreationTrait;
 
   /**
    * Modules to enable.
@@ -90,7 +92,7 @@ public function testImageSource() {
     $title_text = t('This image has been removed. For security reasons, only images from the local domain are allowed.');
 
     // Put a test image in the files directory.
-    $test_images = $this->drupalGetTestFiles('image');
+    $test_images = $this->getTestFiles('image');
     $test_image = $test_images[0]->filename;
 
     // Put a test image in the files directory with special filename.
@@ -141,14 +143,14 @@ public function testImageSource() {
       foreach ($this->xpath('//img[@testattribute="' . hash('sha256', $image) . '"]') as $element) {
         $found = TRUE;
         if ($converted == $red_x_image) {
-          $this->assertEqual((string) $element['src'], $red_x_image);
-          $this->assertEqual((string) $element['alt'], $alt_text);
-          $this->assertEqual((string) $element['title'], $title_text);
-          $this->assertEqual((string) $element['height'], '16');
-          $this->assertEqual((string) $element['width'], '16');
+          $this->assertEqual($element->getAttribute('src'), $red_x_image);
+          $this->assertEqual($element->getAttribute('alt'), $alt_text);
+          $this->assertEqual($element->getAttribute('title'), $title_text);
+          $this->assertEqual($element->getAttribute('height'), '16');
+          $this->assertEqual($element->getAttribute('width'), '16');
         }
         else {
-          $this->assertEqual((string) $element['src'], $converted);
+          $this->assertEqual($element->getAttribute('src'), $converted);
         }
       }
       $this->assertTrue($found, format_string('@image was found.', ['@image' => $image]));
-- 
GitLab