Chris@17: fileStorage = $this->container->get('entity_type.manager') Chris@17: ->getStorage('file'); Chris@17: Chris@17: // Add a file field. Chris@17: $this->fieldStorage = FieldStorageConfig::create([ Chris@17: 'entity_type' => 'entity_test', Chris@17: 'field_name' => 'field_rest_file_test', Chris@17: 'type' => 'file', Chris@17: 'settings' => [ Chris@17: 'uri_scheme' => 'public', Chris@17: ], Chris@17: ]) Chris@17: ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); Chris@17: $this->fieldStorage->save(); Chris@17: Chris@17: $this->field = FieldConfig::create([ Chris@17: 'entity_type' => 'entity_test', Chris@17: 'field_name' => 'field_rest_file_test', Chris@17: 'bundle' => 'entity_test', Chris@17: 'settings' => [ Chris@17: 'file_directory' => 'foobar', Chris@17: 'file_extensions' => 'txt', Chris@17: 'max_filesize' => '', Chris@17: ], Chris@17: ]) Chris@17: ->setLabel('Test file field') Chris@17: ->setTranslatable(FALSE); Chris@17: $this->field->save(); Chris@17: Chris@17: // Create an entity that a file can be attached to. Chris@17: $this->entity = EntityTest::create([ Chris@17: 'name' => 'Llama', Chris@17: 'type' => 'entity_test', Chris@17: ]); Chris@17: $this->entity->setOwnerId(isset($this->account) ? $this->account->id() : 0); Chris@17: $this->entity->save(); Chris@17: Chris@17: // Provision entity_test resource. Chris@17: $this->resourceConfigStorage->create([ Chris@17: 'id' => 'entity.entity_test', Chris@17: 'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY, Chris@17: 'configuration' => [ Chris@17: 'methods' => ['POST'], Chris@17: 'formats' => [static::$format], Chris@17: 'authentication' => [static::$auth], Chris@17: ], Chris@17: 'status' => TRUE, Chris@17: ])->save(); Chris@17: Chris@17: $this->refreshTestStateAfterRestConfigChange(); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Tests using the file upload POST route. Chris@17: */ Chris@17: public function testPostFileUpload() { Chris@17: $this->initAuthentication(); Chris@17: Chris@17: $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']); Chris@17: Chris@17: $uri = Url::fromUri('base:' . static::$postUri); Chris@17: Chris@17: // DX: 403 when unauthorized. Chris@17: $response = $this->fileRequest($uri, $this->testFileData); Chris@17: $this->assertResourceErrorResponse(403, $this->getExpectedUnauthorizedAccessMessage('POST'), $response); Chris@17: Chris@17: $this->setUpAuthorization('POST'); Chris@17: Chris@17: // 404 when the field name is invalid. Chris@17: $invalid_uri = Url::fromUri('base:file/upload/entity_test/entity_test/field_rest_file_test_invalid'); Chris@17: $response = $this->fileRequest($invalid_uri, $this->testFileData); Chris@17: $this->assertResourceErrorResponse(404, 'Field "field_rest_file_test_invalid" does not exist', $response); Chris@17: Chris@17: // This request will have the default 'application/octet-stream' content Chris@17: // type header. Chris@17: $response = $this->fileRequest($uri, $this->testFileData); Chris@17: $this->assertSame(201, $response->getStatusCode()); Chris@17: $expected = $this->getExpectedNormalizedEntity(); Chris@17: $this->assertResponseData($expected, $response); Chris@17: Chris@17: // Check the actual file data. Chris@17: $this->assertSame($this->testFileData, file_get_contents('public://foobar/example.txt')); Chris@17: Chris@17: // Test the file again but using 'filename' in the Content-Disposition Chris@17: // header with no 'file' prefix. Chris@17: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'filename="example.txt"']); Chris@17: $this->assertSame(201, $response->getStatusCode()); Chris@17: $expected = $this->getExpectedNormalizedEntity(2, 'example_0.txt'); Chris@17: $this->assertResponseData($expected, $response); Chris@17: Chris@17: // Check the actual file data. Chris@17: $this->assertSame($this->testFileData, file_get_contents('public://foobar/example_0.txt')); Chris@17: $this->assertTrue($this->fileStorage->loadUnchanged(1)->isTemporary()); Chris@17: Chris@17: // Verify that we can create an entity that references the uploaded file. Chris@17: $entity_test_post_url = Url::fromRoute('rest.entity.entity_test.POST') Chris@17: ->setOption('query', ['_format' => static::$format]); Chris@17: $request_options = []; Chris@17: $request_options[RequestOptions::HEADERS]['Content-Type'] = static::$mimeType; Chris@17: $request_options = NestedArray::mergeDeep($request_options, $this->getAuthenticationRequestOptions('POST')); Chris@17: Chris@17: $request_options[RequestOptions::BODY] = $this->serializer->encode($this->getNormalizedPostEntity(), static::$format); Chris@17: $response = $this->request('POST', $entity_test_post_url, $request_options); Chris@17: $this->assertResourceResponse(201, FALSE, $response); Chris@17: $this->assertTrue($this->fileStorage->loadUnchanged(1)->isPermanent()); Chris@17: $this->assertSame([ Chris@17: [ Chris@17: 'target_id' => '1', Chris@17: 'display' => NULL, Chris@17: 'description' => "The most fascinating file ever!", Chris@17: ], Chris@17: ], EntityTest::load(2)->get('field_rest_file_test')->getValue()); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Returns the normalized POST entity referencing the uploaded file. Chris@17: * Chris@17: * @return array Chris@17: * Chris@17: * @see ::testPostFileUpload() Chris@17: * @see \Drupal\Tests\rest\Functional\EntityResource\EntityTest\EntityTestResourceTestBase::getNormalizedPostEntity() Chris@17: */ Chris@17: protected function getNormalizedPostEntity() { Chris@17: return [ Chris@17: 'type' => [ Chris@17: [ Chris@17: 'value' => 'entity_test', Chris@17: ], Chris@17: ], Chris@17: 'name' => [ Chris@17: [ Chris@17: 'value' => 'Dramallama', Chris@17: ], Chris@17: ], Chris@17: 'field_rest_file_test' => [ Chris@17: [ Chris@17: 'target_id' => 1, Chris@17: 'description' => 'The most fascinating file ever!', Chris@17: ], Chris@17: ], Chris@17: ]; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Tests using the file upload POST route with invalid headers. Chris@17: */ Chris@17: public function testPostFileUploadInvalidHeaders() { Chris@17: $this->initAuthentication(); Chris@17: Chris@17: $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']); Chris@17: Chris@17: $this->setUpAuthorization('POST'); Chris@17: Chris@17: $uri = Url::fromUri('base:' . static::$postUri); Chris@17: Chris@17: // The wrong content type header should return a 415 code. Chris@17: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Type' => static::$mimeType]); Chris@17: $this->assertResourceErrorResponse(415, sprintf('No route found that matches "Content-Type: %s"', static::$mimeType), $response); Chris@17: Chris@17: // An empty Content-Disposition header should return a 400. Chris@18: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => FALSE]); Chris@17: $this->assertResourceErrorResponse(400, '"Content-Disposition" header is required. A file name in the format "filename=FILENAME" must be provided', $response); Chris@17: Chris@17: // An empty filename with a context in the Content-Disposition header should Chris@17: // return a 400. Chris@17: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'file; filename=""']); Chris@17: $this->assertResourceErrorResponse(400, 'No filename found in "Content-Disposition" header. A file name in the format "filename=FILENAME" must be provided', $response); Chris@17: Chris@17: // An empty filename without a context in the Content-Disposition header Chris@17: // should return a 400. Chris@17: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'filename=""']); Chris@17: $this->assertResourceErrorResponse(400, 'No filename found in "Content-Disposition" header. A file name in the format "filename=FILENAME" must be provided', $response); Chris@17: Chris@17: // An invalid key-value pair in the Content-Disposition header should return Chris@17: // a 400. Chris@17: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'not_a_filename="example.txt"']); Chris@17: $this->assertResourceErrorResponse(400, 'No filename found in "Content-Disposition" header. A file name in the format "filename=FILENAME" must be provided', $response); Chris@17: Chris@17: // Using filename* extended format is not currently supported. Chris@17: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'filename*="UTF-8 \' \' example.txt"']); Chris@17: $this->assertResourceErrorResponse(400, 'The extended "filename*" format is currently not supported in the "Content-Disposition" header', $response); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Tests using the file upload POST route with a duplicate file name. Chris@17: * Chris@17: * A new file should be created with a suffixed name. Chris@17: */ Chris@17: public function testPostFileUploadDuplicateFile() { Chris@17: $this->initAuthentication(); Chris@17: Chris@17: $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']); Chris@17: Chris@17: $this->setUpAuthorization('POST'); Chris@17: Chris@17: $uri = Url::fromUri('base:' . static::$postUri); Chris@17: Chris@17: // This request will have the default 'application/octet-stream' content Chris@17: // type header. Chris@17: $response = $this->fileRequest($uri, $this->testFileData); Chris@17: Chris@17: $this->assertSame(201, $response->getStatusCode()); Chris@17: Chris@17: // Make the same request again. The file should be saved as a new file Chris@17: // entity that has the same file name but a suffixed file URI. Chris@17: $response = $this->fileRequest($uri, $this->testFileData); Chris@17: $this->assertSame(201, $response->getStatusCode()); Chris@17: Chris@17: // Loading expected normalized data for file 2, the duplicate file. Chris@17: $expected = $this->getExpectedNormalizedEntity(2, 'example_0.txt'); Chris@17: $this->assertResponseData($expected, $response); Chris@17: Chris@17: // Check the actual file data. Chris@17: $this->assertSame($this->testFileData, file_get_contents('public://foobar/example_0.txt')); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Tests using the file upload route with any path prefixes being stripped. Chris@17: * Chris@17: * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Directives Chris@17: */ Chris@17: public function testFileUploadStrippedFilePath() { Chris@17: $this->initAuthentication(); Chris@17: Chris@17: $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']); Chris@17: Chris@17: $this->setUpAuthorization('POST'); Chris@17: Chris@17: $uri = Url::fromUri('base:' . static::$postUri); Chris@17: Chris@17: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'file; filename="directory/example.txt"']); Chris@17: $this->assertSame(201, $response->getStatusCode()); Chris@17: $expected = $this->getExpectedNormalizedEntity(); Chris@17: $this->assertResponseData($expected, $response); Chris@17: Chris@17: // Check the actual file data. It should have been written to the configured Chris@17: // directory, not /foobar/directory/example.txt. Chris@17: $this->assertSame($this->testFileData, file_get_contents('public://foobar/example.txt')); Chris@17: Chris@17: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'file; filename="../../example_2.txt"']); Chris@17: $this->assertSame(201, $response->getStatusCode()); Chris@17: $expected = $this->getExpectedNormalizedEntity(2, 'example_2.txt', TRUE); Chris@17: $this->assertResponseData($expected, $response); Chris@17: Chris@17: // Check the actual file data. It should have been written to the configured Chris@17: // directory, not /foobar/directory/example.txt. Chris@17: $this->assertSame($this->testFileData, file_get_contents('public://foobar/example_2.txt')); Chris@17: $this->assertFalse(file_exists('../../example_2.txt')); Chris@17: Chris@17: // Check a path from the root. Extensions have to be empty to allow a file Chris@17: // with no extension to pass validation. Chris@17: $this->field->setSetting('file_extensions', '') Chris@17: ->save(); Chris@17: $this->refreshTestStateAfterRestConfigChange(); Chris@17: Chris@17: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'file; filename="/etc/passwd"']); Chris@17: $this->assertSame(201, $response->getStatusCode()); Chris@17: $expected = $this->getExpectedNormalizedEntity(3, 'passwd', TRUE); Chris@17: // This mime will be guessed as there is no extension. Chris@17: $expected['filemime'][0]['value'] = 'application/octet-stream'; Chris@17: $this->assertResponseData($expected, $response); Chris@17: Chris@17: // Check the actual file data. It should have been written to the configured Chris@17: // directory, not /foobar/directory/example.txt. Chris@17: $this->assertSame($this->testFileData, file_get_contents('public://foobar/passwd')); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Tests using the file upload route with a unicode file name. Chris@17: */ Chris@17: public function testFileUploadUnicodeFilename() { Chris@17: $this->initAuthentication(); Chris@17: Chris@17: $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']); Chris@17: Chris@17: $this->setUpAuthorization('POST'); Chris@17: Chris@17: $uri = Url::fromUri('base:' . static::$postUri); Chris@17: Chris@18: // It is important that the filename starts with a unicode character. See Chris@18: // https://bugs.php.net/bug.php?id=77239. Chris@18: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'file; filename="Èxample-✓.txt"']); Chris@17: $this->assertSame(201, $response->getStatusCode()); Chris@18: $expected = $this->getExpectedNormalizedEntity(1, 'Èxample-✓.txt', TRUE); Chris@17: $this->assertResponseData($expected, $response); Chris@18: $this->assertSame($this->testFileData, file_get_contents('public://foobar/Èxample-✓.txt')); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Tests using the file upload route with a zero byte file. Chris@17: */ Chris@17: public function testFileUploadZeroByteFile() { Chris@17: $this->initAuthentication(); Chris@17: Chris@17: $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']); Chris@17: Chris@17: $this->setUpAuthorization('POST'); Chris@17: Chris@17: $uri = Url::fromUri('base:' . static::$postUri); Chris@17: Chris@17: // Test with a zero byte file. Chris@17: $response = $this->fileRequest($uri, NULL); Chris@17: $this->assertSame(201, $response->getStatusCode()); Chris@17: $expected = $this->getExpectedNormalizedEntity(); Chris@17: // Modify the default expected data to account for the 0 byte file. Chris@17: $expected['filesize'][0]['value'] = 0; Chris@17: $this->assertResponseData($expected, $response); Chris@17: Chris@17: // Check the actual file data. Chris@17: $this->assertSame('', file_get_contents('public://foobar/example.txt')); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Tests using the file upload route with an invalid file type. Chris@17: */ Chris@17: public function testFileUploadInvalidFileType() { Chris@17: $this->initAuthentication(); Chris@17: Chris@17: $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']); Chris@17: Chris@17: $this->setUpAuthorization('POST'); Chris@17: Chris@17: $uri = Url::fromUri('base:' . static::$postUri); Chris@17: Chris@17: // Test with a JSON file. Chris@17: $response = $this->fileRequest($uri, '{"test":123}', ['Content-Disposition' => 'filename="example.json"']); Chris@17: $this->assertResourceErrorResponse(422, PlainTextOutput::renderFromHtml("Unprocessable Entity: file validation failed.\nOnly files with the following extensions are allowed: txt."), $response); Chris@17: Chris@17: // Make sure that no file was saved. Chris@17: $this->assertEmpty(File::load(1)); Chris@17: $this->assertFalse(file_exists('public://foobar/example.txt')); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Tests using the file upload route with a file size larger than allowed. Chris@17: */ Chris@17: public function testFileUploadLargerFileSize() { Chris@17: // Set a limit of 50 bytes. Chris@17: $this->field->setSetting('max_filesize', 50) Chris@17: ->save(); Chris@17: $this->refreshTestStateAfterRestConfigChange(); Chris@17: Chris@17: $this->initAuthentication(); Chris@17: Chris@17: $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']); Chris@17: Chris@17: $this->setUpAuthorization('POST'); Chris@17: Chris@17: $uri = Url::fromUri('base:' . static::$postUri); Chris@17: Chris@17: // Generate a string larger than the 50 byte limit set. Chris@17: $response = $this->fileRequest($uri, $this->randomString(100)); Chris@17: $this->assertResourceErrorResponse(422, PlainTextOutput::renderFromHtml("Unprocessable Entity: file validation failed.\nThe file is 100 bytes exceeding the maximum file size of 50 bytes."), $response); Chris@17: Chris@17: // Make sure that no file was saved. Chris@17: $this->assertEmpty(File::load(1)); Chris@17: $this->assertFalse(file_exists('public://foobar/example.txt')); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Tests using the file upload POST route with malicious extensions. Chris@17: */ Chris@17: public function testFileUploadMaliciousExtension() { Chris@17: $this->initAuthentication(); Chris@17: Chris@17: $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']); Chris@17: // Allow all file uploads but system.file::allow_insecure_uploads is set to Chris@17: // FALSE. Chris@17: $this->field->setSetting('file_extensions', '')->save(); Chris@17: $this->refreshTestStateAfterRestConfigChange(); Chris@17: Chris@17: $this->setUpAuthorization('POST'); Chris@17: Chris@17: $uri = Url::fromUri('base:' . static::$postUri); Chris@17: Chris@17: $php_string = ''; Chris@17: Chris@17: // Test using a masked exploit file. Chris@17: $response = $this->fileRequest($uri, $php_string, ['Content-Disposition' => 'filename="example.php"']); Chris@17: // The filename is not munged because .txt is added and it is a known Chris@17: // extension to apache. Chris@17: $expected = $this->getExpectedNormalizedEntity(1, 'example.php.txt', TRUE); Chris@17: // Override the expected filesize. Chris@17: $expected['filesize'][0]['value'] = strlen($php_string); Chris@17: $this->assertResponseData($expected, $response); Chris@17: $this->assertTrue(file_exists('public://foobar/example.php.txt')); Chris@17: Chris@17: // Add php as an allowed format. Allow insecure uploads still being FALSE Chris@17: // should still not allow this. So it should still have a .txt extension Chris@17: // appended even though it is not in the list of allowed extensions. Chris@17: $this->field->setSetting('file_extensions', 'php') Chris@17: ->save(); Chris@17: $this->refreshTestStateAfterRestConfigChange(); Chris@17: Chris@17: $response = $this->fileRequest($uri, $php_string, ['Content-Disposition' => 'filename="example_2.php"']); Chris@17: $expected = $this->getExpectedNormalizedEntity(2, 'example_2.php.txt', TRUE); Chris@17: // Override the expected filesize. Chris@17: $expected['filesize'][0]['value'] = strlen($php_string); Chris@17: $this->assertResponseData($expected, $response); Chris@17: $this->assertTrue(file_exists('public://foobar/example_2.php.txt')); Chris@17: $this->assertFalse(file_exists('public://foobar/example_2.php')); Chris@17: Chris@17: // Allow .doc file uploads and ensure even a mis-configured apache will not Chris@17: // fallback to php because the filename will be munged. Chris@17: $this->field->setSetting('file_extensions', 'doc')->save(); Chris@17: $this->refreshTestStateAfterRestConfigChange(); Chris@17: Chris@17: // Test using a masked exploit file. Chris@17: $response = $this->fileRequest($uri, $php_string, ['Content-Disposition' => 'filename="example_3.php.doc"']); Chris@17: // The filename is munged. Chris@17: $expected = $this->getExpectedNormalizedEntity(3, 'example_3.php_.doc', TRUE); Chris@17: // Override the expected filesize. Chris@17: $expected['filesize'][0]['value'] = strlen($php_string); Chris@17: // The file mime should be 'application/msword'. Chris@17: $expected['filemime'][0]['value'] = 'application/msword'; Chris@17: $this->assertResponseData($expected, $response); Chris@17: $this->assertTrue(file_exists('public://foobar/example_3.php_.doc')); Chris@17: $this->assertFalse(file_exists('public://foobar/example_3.php.doc')); Chris@17: Chris@17: // Now allow insecure uploads. Chris@17: \Drupal::configFactory() Chris@17: ->getEditable('system.file') Chris@17: ->set('allow_insecure_uploads', TRUE) Chris@17: ->save(); Chris@17: // Allow all file uploads. This is very insecure. Chris@17: $this->field->setSetting('file_extensions', '')->save(); Chris@17: $this->refreshTestStateAfterRestConfigChange(); Chris@17: Chris@17: $response = $this->fileRequest($uri, $php_string, ['Content-Disposition' => 'filename="example_4.php"']); Chris@17: $expected = $this->getExpectedNormalizedEntity(4, 'example_4.php', TRUE); Chris@17: // Override the expected filesize. Chris@17: $expected['filesize'][0]['value'] = strlen($php_string); Chris@17: // The file mime should also now be PHP. Chris@17: $expected['filemime'][0]['value'] = 'application/x-httpd-php'; Chris@17: $this->assertResponseData($expected, $response); Chris@17: $this->assertTrue(file_exists('public://foobar/example_4.php')); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Tests using the file upload POST route no extension configured. Chris@17: */ Chris@17: public function testFileUploadNoExtensionSetting() { Chris@17: $this->initAuthentication(); Chris@17: Chris@17: $this->provisionResource([static::$format], static::$auth ? [static::$auth] : [], ['POST']); Chris@17: Chris@17: $this->setUpAuthorization('POST'); Chris@17: Chris@17: $uri = Url::fromUri('base:' . static::$postUri); Chris@17: Chris@17: $this->field->setSetting('file_extensions', '') Chris@17: ->save(); Chris@17: $this->refreshTestStateAfterRestConfigChange(); Chris@17: Chris@17: $response = $this->fileRequest($uri, $this->testFileData, ['Content-Disposition' => 'filename="example.txt"']); Chris@17: $expected = $this->getExpectedNormalizedEntity(1, 'example.txt', TRUE); Chris@17: Chris@17: $this->assertResponseData($expected, $response); Chris@17: $this->assertTrue(file_exists('public://foobar/example.txt')); Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@17: protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) { Chris@17: // The file upload resource only accepts binary data, so there are no Chris@17: // normalization edge cases to test, as there are no normalized entity Chris@17: // representations incoming. Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@17: protected function getExpectedUnauthorizedAccessMessage($method) { Chris@17: return "The following permissions are required: 'administer entity_test content' OR 'administer entity_test_with_bundle content' OR 'create entity_test entity_test_with_bundle entities'."; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Gets the expected file entity. Chris@17: * Chris@17: * @param int $fid Chris@17: * The file ID to load and create normalized data for. Chris@17: * @param string $expected_filename Chris@17: * The expected filename for the stored file. Chris@17: * @param bool $expected_as_filename Chris@17: * Whether the expected filename should be the filename property too. Chris@17: * Chris@17: * @return array Chris@17: * The expected normalized data array. Chris@17: */ Chris@17: protected function getExpectedNormalizedEntity($fid = 1, $expected_filename = 'example.txt', $expected_as_filename = FALSE) { Chris@17: $author = User::load(static::$auth ? $this->account->id() : 0); Chris@17: $file = File::load($fid); Chris@17: Chris@17: $expected_normalization = [ Chris@17: 'fid' => [ Chris@17: [ Chris@17: 'value' => (int) $file->id(), Chris@17: ], Chris@17: ], Chris@17: 'uuid' => [ Chris@17: [ Chris@17: 'value' => $file->uuid(), Chris@17: ], Chris@17: ], Chris@17: 'langcode' => [ Chris@17: [ Chris@17: 'value' => 'en', Chris@17: ], Chris@17: ], Chris@17: 'uid' => [ Chris@17: [ Chris@17: 'target_id' => (int) $author->id(), Chris@17: 'target_type' => 'user', Chris@17: 'target_uuid' => $author->uuid(), Chris@17: 'url' => base_path() . 'user/' . $author->id(), Chris@17: ], Chris@17: ], Chris@17: 'filename' => [ Chris@17: [ Chris@17: 'value' => $expected_as_filename ? $expected_filename : 'example.txt', Chris@17: ], Chris@17: ], Chris@17: 'uri' => [ Chris@17: [ Chris@17: 'value' => 'public://foobar/' . $expected_filename, Chris@17: 'url' => base_path() . $this->siteDirectory . '/files/foobar/' . rawurlencode($expected_filename), Chris@17: ], Chris@17: ], Chris@17: 'filemime' => [ Chris@17: [ Chris@17: 'value' => 'text/plain', Chris@17: ], Chris@17: ], Chris@17: 'filesize' => [ Chris@17: [ Chris@17: 'value' => strlen($this->testFileData), Chris@17: ], Chris@17: ], Chris@17: 'status' => [ Chris@17: [ Chris@17: 'value' => FALSE, Chris@17: ], Chris@17: ], Chris@17: 'created' => [ Chris@17: $this->formatExpectedTimestampItemValues($file->getCreatedTime()), Chris@17: ], Chris@17: 'changed' => [ Chris@17: $this->formatExpectedTimestampItemValues($file->getChangedTime()), Chris@17: ], Chris@17: ]; Chris@17: Chris@17: return $expected_normalization; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Performs a file upload request. Wraps the Guzzle HTTP client. Chris@17: * Chris@17: * @see \GuzzleHttp\ClientInterface::request() Chris@17: * Chris@17: * @param \Drupal\Core\Url $url Chris@17: * URL to request. Chris@17: * @param string $file_contents Chris@17: * The file contents to send as the request body. Chris@17: * @param array $headers Chris@17: * Additional headers to send with the request. Defaults will be added for Chris@18: * Content-Type and Content-Disposition. In order to remove the defaults set Chris@18: * the header value to FALSE. Chris@17: * Chris@17: * @return \Psr\Http\Message\ResponseInterface Chris@17: */ Chris@17: protected function fileRequest(Url $url, $file_contents, array $headers = []) { Chris@17: // Set the format for the response. Chris@17: $url->setOption('query', ['_format' => static::$format]); Chris@17: Chris@17: $request_options = []; Chris@18: $headers = $headers + [ Chris@17: // Set the required (and only accepted) content type for the request. Chris@17: 'Content-Type' => 'application/octet-stream', Chris@17: // Set the required Content-Disposition header for the file name. Chris@17: 'Content-Disposition' => 'file; filename="example.txt"', Chris@17: ]; Chris@18: $request_options[RequestOptions::HEADERS] = array_filter($headers, function ($value) { Chris@18: return $value !== FALSE; Chris@18: }); Chris@17: $request_options[RequestOptions::BODY] = $file_contents; Chris@17: $request_options = NestedArray::mergeDeep($request_options, $this->getAuthenticationRequestOptions('POST')); Chris@17: Chris@17: return $this->request('POST', $url, $request_options); Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@17: protected function setUpAuthorization($method) { Chris@17: switch ($method) { Chris@17: case 'GET': Chris@17: $this->grantPermissionsToTestedRole(['view test entity']); Chris@17: break; Chris@17: case 'POST': Chris@17: $this->grantPermissionsToTestedRole(['create entity_test entity_test_with_bundle entities', 'access content']); Chris@17: break; Chris@17: } Chris@17: } Chris@17: Chris@17: /** Chris@17: * Asserts expected normalized data matches response data. Chris@17: * Chris@17: * @param array $expected Chris@17: * The expected data. Chris@17: * @param \Psr\Http\Message\ResponseInterface $response Chris@17: * The file upload response. Chris@17: */ Chris@17: protected function assertResponseData(array $expected, ResponseInterface $response) { Chris@17: static::recursiveKSort($expected); Chris@17: $actual = $this->serializer->decode((string) $response->getBody(), static::$format); Chris@17: static::recursiveKSort($actual); Chris@17: Chris@17: $this->assertSame($expected, $actual); Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@17: protected function getExpectedUnauthorizedAccessCacheability() { Chris@17: // There is cacheability metadata to check as file uploads only allows POST Chris@17: // requests, which will not return cacheable responses. Chris@17: } Chris@17: Chris@17: }