Skip to content
Snippets Groups Projects
Commit 7cdd0e5a authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2664150 by dawehner: Expand BrowserTestBase with error handling support

parent ec2819c2
Branches
Tags
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -7,7 +7,9 @@
namespace Drupal\Core\Test\HttpClientMiddleware;
use Drupal\Core\Utility\Error;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Overrides the User-Agent HTTP header for outbound HTTP requests.
......@@ -31,7 +33,29 @@ public function __invoke() {
if ($test_prefix = drupal_valid_test_ua()) {
$request = $request->withHeader('User-Agent', drupal_generate_test_ua($test_prefix));
}
return $handler($request, $options);
return $handler($request, $options)
->then(function (ResponseInterface $response) use ($request) {
if (!drupal_valid_test_ua()) {
return $response;
}
$headers = $response->getHeaders();
foreach ($headers as $header_name => $header_values) {
if (preg_match('/^X-Drupal-Assertion-[0-9]+$/', $header_name, $matches)) {
foreach ($header_values as $header_value) {
// Call \Drupal\simpletest\WebTestBase::error() with the parameters from
// the header.
$parameters = unserialize(urldecode($header_value));
if (count($parameters) === 3) {
throw new \Exception($parameters[1] . ': ' . $parameters[0] . "\n" . Error::formatBacktrace([$parameters[2]]));
}
else {
throw new \Exception('Error thrown with the wrong amount of parameters.');
}
}
}
}
return $response;
});
};
};
}
......
......@@ -231,11 +231,17 @@ abstract class BrowserTestBase extends \PHPUnit_Framework_TestCase {
*/
protected function initMink() {
$driver = $this->getDefaultDriverInstance();
if ($driver instanceof GoutteDriver) {
$driver->getClient()->setClient(\Drupal::httpClient());
}
$session = new Session($driver);
$this->mink = new Mink();
$this->mink->registerSession('default', $session);
$this->mink->setDefaultSessionName('default');
$this->registerSessions();
return $session;
}
......
......@@ -58,4 +58,9 @@ public function testForm() {
$this->assertSame('green', $value);
}
public function testError() {
$this->setExpectedException('\Exception', 'User notice: foo');
$this->drupalGet('test-error');
}
}
......@@ -86,4 +86,11 @@ public function httpResponseException($code) {
throw new HttpException($code);
}
public function error() {
trigger_error('foo', E_USER_NOTICE);
return [
'#markup' => 'Content',
];
}
}
......@@ -50,3 +50,11 @@ test_page_test.http_response_exception:
code: 200
requirements:
_access: 'TRUE'
test_page_test.error:
path: '/test-error'
defaults:
_controller: '\Drupal\test_page_test\Controller\Test::error'
code: 200
requirements:
_access: 'TRUE'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment