From 63a020844bad84b3181ea2e2932ff8a47a9a3598 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 8 Nov 2023 15:37:03 -0800 Subject: [PATCH] HttpTestTrait - Add more assertion helpers --- Civi/Test/HttpTestTrait.php | 53 ++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/Civi/Test/HttpTestTrait.php b/Civi/Test/HttpTestTrait.php index e74ccd4241..47e5345170 100644 --- a/Civi/Test/HttpTestTrait.php +++ b/Civi/Test/HttpTestTrait.php @@ -130,6 +130,37 @@ trait HttpTestTrait { return $this; } + /** + * Assert that the response did NOT produce a normal page-view. + * + * This is basically `assertStatusCode(404)`, except that the local configuration + * (CMS/setings/exts/yaddayadda) may change how the error manifests. + * + * @param $response + * @return void + */ + protected function assertPageNotShown($response = NULL): void { + $response = $this->resolveResponse($response); + $actualCode = $response->getStatusCode(); + switch ($actualCode) { + case 404: /* Good! Right! */ + case 403: /* Maybe request falls through to `/civicrm/dashboard` */ + case 500: /* Maybe request falls through to `/civicrm/dashboard`, and it's weird */ + // OK, close enough. You convinced that the page was not shown to the user. + // Bump the assertion-counter and carry on. + $this->assertTrue(TRUE); + return; + + case 200: + // Hypothetically, you might do extra checks on the body to detected misreported errors. + // But for now, let's pretend that HTTP 200 means "OK, Page Found!"... since that is exactly what it means. + $this->fail("Expected HTTP response to indicate a failure (e.g. 404). Received HTTP response $actualCode.\n" . $this->formatFailure($response)); + + default: + $this->fail("Expected HTTP response, but the status code makes no sense. Received HTTP response $actualCode.\n" . $this->formatFailure($response)); + } + } + /** * @param $expectType * @param \Psr\Http\Message\ResponseInterface|null $response @@ -146,6 +177,8 @@ trait HttpTestTrait { } /** + * Assert that the response body matches a regular-expression. + * * @param string $regexp * @param \Psr\Http\Message\ResponseInterface $response * @param string $message @@ -156,11 +189,29 @@ trait HttpTestTrait { } $response = $this->resolveResponse($response); - $this->assertRegexp($regexp, (string) $response->getBody(), + $this->assertMatchesRegularExpression($regexp, (string) $response->getBody(), $message . 'Response body does not match pattern' . $this->formatFailure($response)); return $this; } + /** + * Assert that the response body DOES NOT match a regular-expression. + * + * @param string $regexp + * @param \Psr\Http\Message\ResponseInterface $response + * @param string $message + */ + protected function assertNotBodyRegexp($regexp, $response = NULL, $message = NULL) { + if ($message) { + $message .= "\n"; + } + + $response = $this->resolveResponse($response); + $this->assertDoesNotMatchRegularExpression($regexp, (string) $response->getBody(), + $message . 'Response body should not match pattern' . $this->formatFailure($response)); + return $this; + } + /** * @param \Psr\Http\Message\ResponseInterface|null $response * @return \Psr\Http\Message\ResponseInterface -- 2.25.1