comparison core/modules/rest/tests/src/Functional/FileUploadResourceTestBase.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
252 // The wrong content type header should return a 415 code. 252 // The wrong content type header should return a 415 code.
253 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Type' => static::$mimeType]); 253 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Type' => static::$mimeType]);
254 $this->assertResourceErrorResponse(415, sprintf('No route found that matches "Content-Type: %s"', static::$mimeType), $response); 254 $this->assertResourceErrorResponse(415, sprintf('No route found that matches "Content-Type: %s"', static::$mimeType), $response);
255 255
256 // An empty Content-Disposition header should return a 400. 256 // An empty Content-Disposition header should return a 400.
257 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => '']); 257 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => FALSE]);
258 $this->assertResourceErrorResponse(400, '"Content-Disposition" header is required. A file name in the format "filename=FILENAME" must be provided', $response); 258 $this->assertResourceErrorResponse(400, '"Content-Disposition" header is required. A file name in the format "filename=FILENAME" must be provided', $response);
259 259
260 // An empty filename with a context in the Content-Disposition header should 260 // An empty filename with a context in the Content-Disposition header should
261 // return a 400. 261 // return a 400.
262 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'file; filename=""']); 262 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'file; filename=""']);
371 371
372 $this->setUpAuthorization('POST'); 372 $this->setUpAuthorization('POST');
373 373
374 $uri = Url::fromUri('base:' . static::$postUri); 374 $uri = Url::fromUri('base:' . static::$postUri);
375 375
376 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'file; filename="example-✓.txt"']); 376 // It is important that the filename starts with a unicode character. See
377 $this->assertSame(201, $response->getStatusCode()); 377 // https://bugs.php.net/bug.php?id=77239.
378 $expected = $this->getExpectedNormalizedEntity(1, 'example-✓.txt', TRUE); 378 $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'file; filename="Èxample-✓.txt"']);
379 $this->assertResponseData($expected, $response); 379 $this->assertSame(201, $response->getStatusCode());
380 $this->assertSame($this->testFileData, file_get_contents('public://foobar/example-✓.txt')); 380 $expected = $this->getExpectedNormalizedEntity(1, 'Èxample-✓.txt', TRUE);
381 $this->assertResponseData($expected, $response);
382 $this->assertSame($this->testFileData, file_get_contents('public://foobar/Èxample-✓.txt'));
381 } 383 }
382 384
383 /** 385 /**
384 * Tests using the file upload route with a zero byte file. 386 * Tests using the file upload route with a zero byte file.
385 */ 387 */
656 * URL to request. 658 * URL to request.
657 * @param string $file_contents 659 * @param string $file_contents
658 * The file contents to send as the request body. 660 * The file contents to send as the request body.
659 * @param array $headers 661 * @param array $headers
660 * Additional headers to send with the request. Defaults will be added for 662 * Additional headers to send with the request. Defaults will be added for
661 * Content-Type and Content-Disposition. 663 * Content-Type and Content-Disposition. In order to remove the defaults set
664 * the header value to FALSE.
662 * 665 *
663 * @return \Psr\Http\Message\ResponseInterface 666 * @return \Psr\Http\Message\ResponseInterface
664 */ 667 */
665 protected function fileRequest(Url $url, $file_contents, array $headers = []) { 668 protected function fileRequest(Url $url, $file_contents, array $headers = []) {
666 // Set the format for the response. 669 // Set the format for the response.
667 $url->setOption('query', ['_format' => static::$format]); 670 $url->setOption('query', ['_format' => static::$format]);
668 671
669 $request_options = []; 672 $request_options = [];
670 $request_options[RequestOptions::HEADERS] = $headers + [ 673 $headers = $headers + [
671 // Set the required (and only accepted) content type for the request. 674 // Set the required (and only accepted) content type for the request.
672 'Content-Type' => 'application/octet-stream', 675 'Content-Type' => 'application/octet-stream',
673 // Set the required Content-Disposition header for the file name. 676 // Set the required Content-Disposition header for the file name.
674 'Content-Disposition' => 'file; filename="example.txt"', 677 'Content-Disposition' => 'file; filename="example.txt"',
675 ]; 678 ];
679 $request_options[RequestOptions::HEADERS] = array_filter($headers, function ($value) {
680 return $value !== FALSE;
681 });
676 $request_options[RequestOptions::BODY] = $file_contents; 682 $request_options[RequestOptions::BODY] = $file_contents;
677 $request_options = NestedArray::mergeDeep($request_options, $this->getAuthenticationRequestOptions('POST')); 683 $request_options = NestedArray::mergeDeep($request_options, $this->getAuthenticationRequestOptions('POST'));
678 684
679 return $this->request('POST', $url, $request_options); 685 return $this->request('POST', $url, $request_options);
680 } 686 }