BrowserWithJavascriptTest.php in Drupal 8
File
core/tests/Drupal/FunctionalJavascriptTests/BrowserWithJavascriptTest.php
View source
<?php
namespace Drupal\FunctionalJavascriptTests;
use Behat\Mink\Driver\GoutteDriver;
use PHPUnit\Framework\AssertionFailedError;
class BrowserWithJavascriptTest extends WebDriverTestBase {
public static $modules = [
'test_page_test',
];
protected $defaultTheme = 'stark';
public function testJavascript() {
$this
->drupalGet('<front>');
$session = $this
->getSession();
$session
->resizeWindow(400, 300);
$javascript = <<<JS
(function(){
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
x = w.innerWidth || e.clientWidth || g.clientWidth,
y = w.innerHeight || e.clientHeight|| g.clientHeight;
return x == 400 && y == 300;
}());
JS;
$this
->assertJsCondition($javascript);
}
public function testAssertJsCondition() {
$this
->drupalGet('<front>');
$session = $this
->getSession();
$session
->resizeWindow(500, 300);
$javascript = <<<JS
(function(){
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
x = w.innerWidth || e.clientWidth || g.clientWidth,
y = w.innerHeight || e.clientHeight|| g.clientHeight;
return x == 400 && y == 300;
}());
JS;
$this
->expectException(AssertionFailedError::class);
$this
->assertJsCondition($javascript, 100);
}
public function testCreateScreenshot() {
$this
->drupalGet('<front>');
$this
->createScreenshot('public://screenshot.jpg');
$this
->assertFileExists('public://screenshot.jpg');
}
public function testEscapingAssertions() {
$assert = $this
->assertSession();
$this
->drupalGet('test-escaped-characters');
$assert
->assertNoEscaped('<div class="escaped">');
$assert
->responseContains('<div class="escaped">');
$assert
->assertEscaped('Escaped: <"\'&>');
$this
->drupalGet('test-escaped-script');
$assert
->assertNoEscaped('<div class="escaped">');
$assert
->responseContains('<div class="escaped">');
$assert
->assertEscaped("<script>alert('XSS');alert(\"XSS\");</script>");
$this
->drupalGetWithAlert('test-unescaped-script');
$assert
->assertNoEscaped('<div class="unescaped">');
$assert
->responseContains('<div class="unescaped">');
$assert
->responseContains("<script>alert('Marked safe');alert(\"Marked safe\");</script>");
$assert
->assertNoEscaped("<script>alert('Marked safe');alert(\"Marked safe\");</script>");
}
protected function drupalGetWithAlert($path, array $options = [], array $headers = []) {
$options['absolute'] = TRUE;
$url = $this
->buildUrl($path, $options);
$session = $this
->getSession();
$this
->prepareRequest();
foreach ($headers as $header_name => $header_value) {
$session
->setRequestHeader($header_name, $header_value);
}
$session
->visit($url);
$session
->getDriver()
->getWebdriverSession()
->accept_alert();
$session
->getDriver()
->getWebdriverSession()
->accept_alert();
$out = $session
->getPage()
->getContent();
$this
->refreshVariables();
if ($new = $this
->checkForMetaRefresh()) {
$out = $new;
$this->metaRefreshCount = 0;
}
if ($this->htmlOutputEnabled && !$this
->getSession()
->getDriver() instanceof GoutteDriver) {
$html_output = 'GET request to: ' . $url . '<hr />Ending URL: ' . $this
->getSession()
->getCurrentUrl();
$html_output .= '<hr />' . $out;
$html_output .= $this
->getHtmlOutputHeaders();
$this
->htmlOutput($html_output);
}
return $out;
}
}