Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect error messages in response output #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

Utilities for testing Drupal!

## ErrorMessageDetectionTrait

A trait to detect error messages in `drupalGet` responses and display the error message when the test fails.

### Usage

Once your trait is added to your test base class, you can check detect errors by overriding the `drupalGet` function.
Copy link
Contributor Author

@mstrelan mstrelan Aug 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice if this also covered form submissions and other link clicks. One way to achieve this would be to override the htmlOutput function and detect the error then. The html output contains other messages before the page content so we can't use str_starts_with anymore, and may need to strip some other cruft. Maybe it could check str_contains in the $message and then call $this->getSession()->getPage()->getContent() again to get the error to display.


```php
/**
* {@inheritdoc}
*/
protected function drupalGet($path, array $options = [], array $headers = []): string {
$response = parent::drupalGet($path, $options, $headers);
$this->detectErrorMessageInResponseOutput($response);
return $response;
}
```

## ExpectsCacheableResponseTrait

A trait to add Dynamic Page Cache cacheability testing to every request in your Functional tests.
Expand Down
25 changes: 25 additions & 0 deletions src/Traits/ErrorMessageDetectionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace PNX\DrupalTestUtils\Traits;

use Drupal\Component\Utility\Html;

/**
* Helper functions to detect if the response output contains an error.
*/
trait ErrorMessageDetectionTrait {

/**
* Detects an error in the response output and fails with the error message.
*/
protected function detectErrorMessageInResponseOutput(string $response): void {
$error = 'The website encountered an unexpected error. Please try again later.';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, this won't detect Fatal Error messages, e.g. OOM. Maybe we could expand this a little.

if (str_starts_with($response, $error)) {
$plain = Html::decodeEntities(strip_tags(str_ireplace('<br>', "\n", $response)));
throw new \ErrorException($plain);
}
}

}