Skip to content

Commit 68baf87

Browse files
authored
Merge pull request #104 from CollaboraOnline/timing-error-in-tests
Timing error in tests
2 parents 85d3266 + c95f8ec commit 68baf87

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

Diff for: tests/src/Kernel/Controller/WopiControllerTest.php

+15-12
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ public function testWopiGetFileInfo(): void {
7171
];
7272

7373
$request = $this->createRequest();
74-
$this->assertJsonResponseOk($expected_response_data, $request);
74+
$this->assertSame($expected_response_data, $this->assertJsonResponseOk($request));
7575

7676
$request = $this->createRequest(write: TRUE);
7777
$expected_response_data['UserCanWrite'] = TRUE;
78-
$this->assertJsonResponseOk($expected_response_data, $request);
78+
$this->assertSame($expected_response_data, $this->assertJsonResponseOk($request));
7979

8080
// Create a user picture, and attach it to the user.
8181
$user_picture_file = $this->createUserPicture($this->user);
@@ -89,7 +89,7 @@ public function testWopiGetFileInfo(): void {
8989
$expected_response_data['UserExtraInfo']['avatar'] = 'http://localhost/' . $user_picture_realpath;
9090

9191
$request = $this->createRequest();
92-
$this->assertJsonResponseOk($expected_response_data, $request);
92+
$this->assertSame($expected_response_data, $this->assertJsonResponseOk($request));
9393
}
9494

9595
/**
@@ -216,17 +216,21 @@ protected function doTestWopiPutFile(
216216
$request->headers->add($request_headers);
217217

218218
$request_time = \Drupal::time()->getRequestTime();
219-
$this->assertJsonResponseOk(
220-
[
221-
'LastModifiedTime' => DateTimeHelper::format($request_time),
222-
],
223-
$request,
224-
);
219+
$response_data = $this->assertJsonResponseOk($request);
225220

226221
$media = Media::load($this->media->id());
227222
$file = $this->loadCurrentMediaFile();
228223

224+
$this->assertSame([
225+
'LastModifiedTime' => DateTimeHelper::format($file->getChangedTime()),
226+
], $response_data);
227+
229228
// File entity and content are updated.
229+
// The changed timestamp in $file is filled from TestTime->getRequestTime(),
230+
// which should always produce the same result during one request/process,
231+
// but does not, due to a bug in datetime_testing.
232+
// See https://www.drupal.org/project/datetime_testing/issues/3513073
233+
$this->assertLessThanOrEqual(1, abs($request_time - $file->getChangedTime()));
230234
$this->assertSame($request_time, $file->getChangedTime());
231235
$actual_file_content = file_get_contents($file->getFileUri());
232236
$this->assertSame($new_file_content, $actual_file_content);
@@ -326,12 +330,11 @@ public function testWopiPutFileConflict(): void {
326330
$wopi_changed_time = \DateTimeImmutable::createFromFormat('U', (string) ($this->file->getChangedTime() + 1000));
327331
$request->headers->set('x-cool-wopi-timestamp', $wopi_changed_time->format(\DateTimeInterface::ATOM));
328332

329-
$this->assertJsonResponse(
330-
Response::HTTP_CONFLICT,
333+
$this->assertSame(
331334
[
332335
'COOLStatusCode' => 1010,
333336
],
334-
$request,
337+
$this->assertJsonResponse(Response::HTTP_CONFLICT, $request),
335338
);
336339
$this->assertLogMessage(
337340
RfcLogLevel::ERROR,

Diff for: tests/src/Kernel/Controller/WopiControllerTestBase.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -208,30 +208,32 @@ protected function createAccessToken(?int $fid = NULL, ?int $uid = NULL, bool $w
208208
/**
209209
* Asserts a successful json response given a request.
210210
*
211-
* @param array $expected_data
212-
* The expected response JSON data.
213211
* @param \Symfony\Component\HttpFoundation\Request $request
214212
* The request to perform.
215213
* @param string $message
216214
* Message to distinguish this from other assertions.
215+
*
216+
* @return array
217+
* Response data, parsed from json.
217218
*/
218-
protected function assertJsonResponseOk(array $expected_data, Request $request, string $message = ''): void {
219-
$this->assertJsonResponse(Response::HTTP_OK, $expected_data, $request, $message);
219+
protected function assertJsonResponseOk(Request $request, string $message = ''): array {
220+
return $this->assertJsonResponse(Response::HTTP_OK, $request, $message);
220221
}
221222

222223
/**
223224
* Asserts a json response given a request.
224225
*
225226
* @param int $expected_code
226227
* The expected response status code.
227-
* @param array $expected_data
228-
* The expected response JSON data.
229228
* @param \Symfony\Component\HttpFoundation\Request $request
230229
* The request to perform.
231230
* @param string $message
232231
* Message to distinguish this from other assertions.
232+
*
233+
* @return array
234+
* The response data, parsed from json.
233235
*/
234-
protected function assertJsonResponse(int $expected_code, array $expected_data, Request $request, string $message = ''): void {
236+
protected function assertJsonResponse(int $expected_code, Request $request, string $message = ''): array {
235237
$response = $this->handleRequest($request);
236238
$content = $response->getContent();
237239
$this->assertIsString($content);
@@ -240,7 +242,7 @@ protected function assertJsonResponse(int $expected_code, array $expected_data,
240242
$this->assertEquals('application/json', $response->headers->get('Content-Type'), $extended_message);
241243
$data = Json::decode($content);
242244
$this->assertNotNull($data, $extended_message);
243-
$this->assertSame($expected_data, $data, $message);
245+
return $data;
244246
}
245247

246248
/**

0 commit comments

Comments
 (0)