Skip to content

Commit

Permalink
Simplify response assertion.
Browse files Browse the repository at this point in the history
  • Loading branch information
donquixote committed Dec 17, 2024
1 parent 238eb2d commit adfeb95
Showing 1 changed file with 60 additions and 35 deletions.
95 changes: 60 additions & 35 deletions tests/src/Kernel/Controller/WopiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use ColinODell\PsrTestLogger\TestLogger;
use Drupal\collabora_online\Jwt\JwtTranscoderInterface;
use Drupal\Component\Serialization\Json;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\media\MediaInterface;
Expand Down Expand Up @@ -100,32 +101,26 @@ protected function setUp(): void {
*/
public function testWopiGetFileInfo(): void {
$file_changed_time = date_create_immutable_from_format('U', (string) $this->file->getChangedTime());
$assert_response = function (Request $request, bool $write) use ($file_changed_time) {
$this->assertResponse(
Response::HTTP_OK,
json_encode([
'BaseFileName' => $this->file->getFilename(),
'Size' => $this->file->getSize(),
'LastModifiedTime' => $file_changed_time->format('c'),
'UserId' => $this->user->id(),
'UserFriendlyName' => $this->user->getDisplayName(),
'UserExtraInfo' => [
'mail' => $this->user->getEmail(),
],
'UserCanWrite' => $write,
'IsAdminUser' => FALSE,
'IsAnonymousUser' => FALSE,
]),
'application/json',
$request,
);
};
$expected_response_data = [
'BaseFileName' => $this->file->getFilename(),
'Size' => $this->file->getSize(),
'LastModifiedTime' => $file_changed_time->format('c'),
'UserId' => $this->user->id(),
'UserFriendlyName' => $this->user->getDisplayName(),
'UserExtraInfo' => [
'mail' => $this->user->getEmail(),
],
'UserCanWrite' => FALSE,
'IsAdminUser' => FALSE,
'IsAnonymousUser' => FALSE,
];

$request = $this->createRequest();
$assert_response($request, FALSE);
$this->assertJsonResponseOk($expected_response_data, $request);

$request = $this->createRequest(write: TRUE);
$assert_response($request, TRUE);
$expected_response_data['UserCanWrite'] = TRUE;
$this->assertJsonResponseOk($expected_response_data, $request);
}

/**
Expand All @@ -150,16 +145,12 @@ public function testWopiGetFile(): void {
*/
public function testWopiPutFile(): void {
$file_changed_time = date_create_immutable_from_format('U', (string) $this->file->getChangedTime());
$assert_response = function (Request $request, string $log_message) use ($file_changed_time): void {
$expected_response_data = [
'LastModifiedTime' => $file_changed_time->format('c'),
];
$assert_response = function (Request $request, string $log_message) use ($expected_response_data): void {
$this->logger->reset();
$this->assertResponse(
Response::HTTP_OK,
json_encode([
'LastModifiedTime' => $file_changed_time->format('c'),
]),
'application/json',
$request
);
$this->assertJsonResponseOk($expected_response_data, $request);
$log_message ??= 'Save reason: Saved by Collabora Online';
$this->assertTrue($this->logger->hasRecord($log_message), 'error');
};
Expand Down Expand Up @@ -199,12 +190,11 @@ public function testWopiPutFileConflict(): void {
$file_changed_time = date_create_immutable_from_format('U', (string) ($this->file->getChangedTime() + 1000));
$request->headers->set('x-cool-wopi-timestamp', $file_changed_time->format(\DateTimeInterface::ATOM));

$this->assertResponse(
$this->assertJsonResponse(
Response::HTTP_CONFLICT,
json_encode([
[
'COOLStatusCode' => 1010,
]),
'application/json',
],
$request
);
}
Expand Down Expand Up @@ -374,6 +364,41 @@ protected function createAccessToken(?int $fid = NULL, ?int $uid = NULL, bool $w
return $transcoder->encode($payload, $expire_timestamp);
}

/**
* Asserts status code and content in a response given a request.
*
* @param array $expected_data
* The expected response JSON data.
* @param \Symfony\Component\HttpFoundation\Request $request
* The request to perform.
* @param string $message
* Message to distinguish this from other assertions.
*/
protected function assertJsonResponseOk(array $expected_data, Request $request, string $message = ''): void {
$this->assertJsonResponse(Response::HTTP_OK, $expected_data, $request, $message);
}

/**
* Asserts a json response given a request.
*
* @param int $expected_code
* The expected response status code.
* @param array $expected_data
* The expected response JSON data.
* @param \Symfony\Component\HttpFoundation\Request $request
* The request to perform.
* @param string $message
* Message to distinguish this from other assertions.
*/
protected function assertJsonResponse(int $expected_code, array $expected_data, Request $request, string $message = ''): void {
$response = $this->handleRequest($request);
$this->assertEquals($expected_code, $response->getStatusCode(), $message);
$this->assertEquals('application/json', $response->headers->get('Content-Type'), $message);
$content = $response->getContent();
$data = Json::decode($content);
$this->assertSame($expected_data, $data, $message);
}

/**
* Asserts an access denied response given a request.
*
Expand Down

0 comments on commit adfeb95

Please sign in to comment.