diff --git a/core/modules/contact/src/Tests/ContactPersonalTest.php b/core/modules/contact/tests/src/Functional/ContactPersonalTest.php similarity index 97% rename from core/modules/contact/src/Tests/ContactPersonalTest.php rename to core/modules/contact/tests/src/Functional/ContactPersonalTest.php index 936aed5..40be162 100644 --- a/core/modules/contact/src/Tests/ContactPersonalTest.php +++ b/core/modules/contact/tests/src/Functional/ContactPersonalTest.php @@ -1,11 +1,13 @@ drupalGet('user/' . $this->contactUser->id() . '/contact'); $this->assertEscaped($mail); $message = $this->submitPersonalContact($this->contactUser); - $mails = $this->drupalGetMails(); + $mails = $this->getMails(); $this->assertEqual(1, count($mails)); $mail = $mails[0]; $this->assertEqual($mail['to'], $this->contactUser->getEmail()); diff --git a/core/modules/contact/src/Tests/ContactSitewideTest.php b/core/modules/contact/tests/src/Functional/ContactSitewideTest.php similarity index 97% rename from core/modules/contact/src/Tests/ContactSitewideTest.php rename to core/modules/contact/tests/src/Functional/ContactSitewideTest.php index ffc2e90..f3db002 100644 --- a/core/modules/contact/src/Tests/ContactSitewideTest.php +++ b/core/modules/contact/tests/src/Functional/ContactSitewideTest.php @@ -1,13 +1,14 @@ xpath('//table/tbody/tr') as $row) { - if (((string) $row->td[0]->a) == $label) { + foreach ($this->xpath('//table/tbody/tr/td[1]/a') as $row_link) { + if ($row_link->getText() == $label) { break; } $i++; @@ -299,7 +301,7 @@ function testSiteWideContact() { $field_name . '[0][value]' => $this->randomMachineName(), ); $this->drupalPostForm(NULL, $edit, t('Send message')); - $mails = $this->drupalGetMails(); + $mails = $this->getMails(); $mail = array_pop($mails); $this->assertEqual($mail['subject'], t('[@label] @subject', array('@label' => $label, '@subject' => $edit['subject[0][value]']))); $this->assertTrue(strpos($mail['body'], $field_label)); @@ -377,7 +379,7 @@ function testAutoReply() { $this->submitContact($this->randomMachineName(16), $email, $subject, 'foo', $this->randomString(128)); // We are testing the auto-reply, so there should be one email going to the sender. - $captured_emails = $this->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email)); + $captured_emails = $this->getMails(array('id' => 'contact_page_autoreply', 'to' => $email)); $this->assertEqual(count($captured_emails), 1); $this->assertEqual(trim($captured_emails[0]['body']), trim(MailFormatHelper::htmlToText($foo_autoreply))); @@ -386,14 +388,14 @@ function testAutoReply() { $this->submitContact($this->randomMachineName(16), $email, $this->randomString(64), 'bar', $this->randomString(128)); // Auto-reply for form 'bar' should result in one auto-reply email to the sender. - $captured_emails = $this->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email)); + $captured_emails = $this->getMails(array('id' => 'contact_page_autoreply', 'to' => $email)); $this->assertEqual(count($captured_emails), 1); $this->assertEqual(trim($captured_emails[0]['body']), trim(MailFormatHelper::htmlToText($bar_autoreply))); // Verify that no auto-reply is sent when the auto-reply field is left blank. $email = $this->randomMachineName(32) . '@example.com'; $this->submitContact($this->randomMachineName(16), $email, $this->randomString(64), 'no_autoreply', $this->randomString(128)); - $captured_emails = $this->drupalGetMails(array('id' => 'contact_page_autoreply', 'to' => $email)); + $captured_emails = $this->getMails(array('id' => 'contact_page_autoreply', 'to' => $email)); $this->assertEqual(count($captured_emails), 0); // Verify that the current error message doesn't show, that the auto-reply diff --git a/core/modules/contact/src/Tests/ContactStorageTest.php b/core/modules/contact/tests/src/Functional/ContactStorageTest.php similarity index 98% rename from core/modules/contact/src/Tests/ContactStorageTest.php rename to core/modules/contact/tests/src/Functional/ContactStorageTest.php index ad0e8d4..f32233a 100644 --- a/core/modules/contact/src/Tests/ContactStorageTest.php +++ b/core/modules/contact/tests/src/Functional/ContactStorageTest.php @@ -1,6 +1,6 @@ alert(123);'; + $sanitized = Html::escape($dangerous); + + $this->session + ->getResponseHeader('Content-type') + ->willReturn('text/html'); + $this->webAssert + ->pageTextContains($sanitized) + ->shouldNotBeCalled(); + $this->webAssert + ->pageTextContains($dangerous) + ->shouldBeCalled(); + + // The legacy ::assertText() returns nothing and calls no PHPUnit assertion, + // thus we need a real assertion. If ::assertText() fails, then it will + // return the exception and this test will fail. + $this->assertNull($this->assertText($sanitized)); + } + + /** + * @covers ::assertNoText + */ + public function testAssertNoText() { + $dangerous = 'Bad html '; + $sanitized = Html::escape($dangerous); + + $this->session + ->getResponseHeader('Content-type') + ->willReturn('text/html'); + $this->webAssert + ->pageTextNotContains($dangerous) + ->shouldBeCalled(); + $this->webAssert + ->pageTextNotContains($sanitized) + ->shouldNotBeCalled(); + + // The legacy ::assertNoText() returns nothing and calls no PHPUnit + // assertion, thus we need a real assertion. If ::assertText() fails, then + // it will return the exception and this test will fail. + $this->assertNull($this->assertNoText($sanitized)); + } + + /** * Returns a mocked behat session object. * * @return \Behat\Mink\Session diff --git a/core/tests/Drupal/Tests/DocumentElement.php b/core/tests/Drupal/Tests/DocumentElement.php new file mode 100644 index 0000000..323bde2 --- /dev/null +++ b/core/tests/Drupal/Tests/DocumentElement.php @@ -0,0 +1,46 @@ +getContent(); + $dom = new DOMDocument(); + // Suppress libxml warnings when loading HTML 5 content. + @$dom->loadHTML($raw_content); + // Collect DOM elements to be removed because DOMNodeList does not allow us + // to remove them directly when iterating over it. + $remove = []; + + // Remove all JS script tags, they must never be part of the plain text + // content. + $scripts = $dom->getElementsByTagName('script'); + foreach ($scripts as $script) { + $remove[] = $script; + } + + // Remove all CSS style tags, they must never be part of the plain text + // content. + $styles = $dom->getElementsByTagName('style'); + foreach ($styles as $style) { + $remove[] = $style; + } + + foreach ($remove as $item) { + $item->parentNode->removeChild($item); + } + + return $dom->textContent; + } + +} diff --git a/core/tests/Drupal/Tests/Session.php b/core/tests/Drupal/Tests/Session.php new file mode 100644 index 0000000..331fa68 --- /dev/null +++ b/core/tests/Drupal/Tests/Session.php @@ -0,0 +1,43 @@ +page = new DocumentElement($this); + } + + /** + * {@inheirtdoc} + */ + public function getPage() { + // The variable on the parent class is private, so we need to override this + // method. + return $this->page; + } + +} diff --git a/core/tests/Drupal/Tests/WebAssert.php b/core/tests/Drupal/Tests/WebAssert.php index a6cb5b5..179ae4c 100644 --- a/core/tests/Drupal/Tests/WebAssert.php +++ b/core/tests/Drupal/Tests/WebAssert.php @@ -6,7 +6,6 @@ use Behat\Mink\WebAssert as MinkWebAssert; use Behat\Mink\Element\TraversableElement; use Behat\Mink\Exception\ElementNotFoundException; -use Behat\Mink\Session; use Drupal\Component\Utility\Html; use Drupal\Core\Url;