diff --git a/core/modules/rest/src/Tests/RESTFileValidationTest.php b/core/modules/rest/src/Tests/RESTFileValidationTest.php index d5507db..2a3f013 100644 --- a/core/modules/rest/src/Tests/RESTFileValidationTest.php +++ b/core/modules/rest/src/Tests/RESTFileValidationTest.php @@ -11,6 +11,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; +use Drupal\file\Entity\File; use Drupal\node\Entity\Node; use Drupal\user\RoleInterface; @@ -59,22 +60,29 @@ public function testCreateNodeWithFile() { user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, $permissions); // Create a private file and attach it to a node. - $this->createFileField('field_private_file', 'node', 'resttest', ['uri_scheme' => 'private'], ['file_extensions' => 'txt']); - $permissions = ['create resttest content']; - $account = $this->drupalCreateUser($permissions); - $this->drupalLogin($account); - + $this->createFileField('field_private_file', 'node', 'page', ['uri_scheme' => 'private'], ['file_extensions' => 'txt']); $private_files = $this->drupalGetTestFiles('text', NULL, FALSE); - $edit = [ - 'title[0][value]' => 'Test private title', - 'files[field_private_file_0]' => drupal_realpath($private_files[0]->uri), - ]; - $this->drupalPostForm('node/add/resttest', $edit, t('Save')); - preg_match('@node/(\d+)@', $this->getUrl(), $matches); - $nid = $matches[1]; - $node = Node::load($nid); - $this->drupalLogout(); + $private_file_author = $this->drupalCreateUser([]); + $file = File::create([ + 'uri' => $private_files[0]->uri, + ]); + $file->save(); + $node = Node::create([ + 'title' => 'Test private title', + 'type' => 'page', + 'uid' => $private_file_author->id(), + 'field_private_file' => [ + 0 => [ + 'target_id' => $file->id(), + ], + ], + ]); + $node->save(); + $this->drupalLogin($private_file_author); + $this->drupalGet(file_create_url($private_files[0]->uri)); + $this->assertResponse(200); + $this->drupalLogout(); $this->drupalGet(file_create_url($private_files[0]->uri)); $this->assertResponse(403); @@ -88,6 +96,7 @@ public function testCreateNodeWithFile() { // Verify that user cannot create content when trying to write to fields // where it is not possible. + $account = \Drupal::currentUser(); $serialized = $this->serializer->serialize($entity, $this->defaultFormat, ['account' => $account]); $this->httpRequest('entity/node', 'POST', $serialized, $this->defaultMimeType); $this->assertResponse(403); @@ -126,7 +135,7 @@ public function testCreateNodeWithFile() { * * @return \Drupal\Core\Entity\EntityInterface */ - function createFileField($name, $entity_type, $bundle, $storage_settings = [], $field_settings = [], $widget_settings = []) { + protected function createFileField($name, $entity_type, $bundle, $storage_settings = [], $field_settings = [], $widget_settings = []) { $field_storage = FieldStorageConfig::create([ 'entity_type' => $entity_type, 'field_name' => $name, @@ -154,7 +163,7 @@ function createFileField($name, $entity_type, $bundle, $storage_settings = [], $ * @param array $widget_settings * A list of widget settings that will be added to the widget defaults. */ - function attachFileField($name, $entity_type, $bundle, $field_settings = [], $widget_settings = []) { + protected function attachFileField($name, $entity_type, $bundle, $field_settings = [], $widget_settings = []) { FieldConfig::create([ 'field_name' => $name, 'label' => $name, @@ -179,115 +188,4 @@ function attachFileField($name, $entity_type, $bundle, $field_settings = [], $wi ->save(); } - /** - * Creates the entity over the REST API. - * - * @param string $entity_type - * The type of the entity that should be created. - * @param string $serialized - * The body for the POST request. - */ - public function assertCreateEntityOverRestApi($entity_type, $serialized = NULL) { - // Note: this will fail with PHP 5.6 when always_populate_raw_post_data is - // set to something other than -1. See https://www.drupal.org/node/2456025. - $this->httpRequest('entity/' . $entity_type, 'POST', $serialized, $this->defaultMimeType); - $this->assertResponse(201); - } - - /** - * Gets the new entity ID from the location header and tries to read it from - * the database. - * - * @param string $entity_type - * Entity type we need to load the entity from DB. - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity we want to check that was inserted correctly. - * @param array $entity_values - * The values of $entity. - */ - public function assertReadEntityIdFromHeaderAndDb($entity_type, EntityInterface $entity, array $entity_values = array()) { - // Get the location from the HTTP response header. - $location_url = $this->drupalGetHeader('location'); - $url_parts = explode('/', $location_url); - $id = end($url_parts); - - // Get the entity using the ID found. - $loaded_entity = \Drupal::entityManager()->getStorage($entity_type)->load($id); - $this->assertNotIdentical(FALSE, $loaded_entity, 'The new ' . $entity_type . ' was found in the database.'); - $this->assertEqual($entity->uuid(), $loaded_entity->uuid(), 'UUID of created entity is correct.'); - - // Verify that the field values sent and received from DB are the same. - foreach ($entity_values as $property => $value) { - $actual_value = $loaded_entity->get($property)->value; - $send_value = $entity->get($property)->value; - $this->assertEqual($send_value, $actual_value, 'Created property ' . $property . ' expected: ' . $send_value . ', actual: ' . $actual_value); - } - - // Delete the entity loaded from DB. - $loaded_entity->delete(); - } - - /** - * Try to send invalid data that cannot be correctly deserialized. - * - * @param string $entity_type - * The type of the entity that should be created. - */ - public function assertCreateEntityInvalidData($entity_type) { - $this->httpRequest('entity/' . $entity_type, 'POST', 'kaboom!', $this->defaultMimeType); - $this->assertResponse(400); - } - - /** - * Try to send no data at all, which does not make sense on POST requests. - * - * @param string $entity_type - * The type of the entity that should be created. - */ - public function assertCreateEntityNoData($entity_type) { - $this->httpRequest('entity/' . $entity_type, 'POST', NULL, $this->defaultMimeType); - $this->assertResponse(400); - } - - /** - * Send an invalid UUID to trigger the entity validation. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity we want to check that was inserted correctly. - * @param string $entity_type - * The type of the entity that should be created. - * @param array $context - * Options normalizers/encoders have access to. - */ - public function assertCreateEntityInvalidSerialized(EntityInterface $entity, $entity_type, array $context = array()) { - // Add a UUID that is too long. - $entity->set('uuid', $this->randomMachineName(129)); - $invalid_serialized = $this->serializer->serialize($entity, $this->defaultFormat, $context); - - $response = $this->httpRequest('entity/' . $entity_type, 'POST', $invalid_serialized, $this->defaultMimeType); - - // Unprocessable Entity as response. - $this->assertResponse(422); - - // Verify that the text of the response is correct. - $error = Json::decode($response); - $this->assertEqual($error['error'], "Unprocessable Entity: validation failed.\nuuid.0.value: UUID: may not be longer than 128 characters.\n"); - } - - /** - * Try to create an entity without proper permissions. - * - * @param string $entity_type - * The type of the entity that should be created. - * @param string $serialized - * The body for the POST request. - */ - public function assertCreateEntityWithoutProperPermissions($entity_type, $serialized = NULL) { - $this->drupalLogout(); - $this->httpRequest('entity/' . $entity_type, 'POST', $serialized, $this->defaultMimeType); - // Forbidden Error as response. - $this->assertResponse(403); - $this->assertFalse(\Drupal::entityManager()->getStorage($entity_type)->loadMultiple(), 'No entity has been created in the database.'); - } - }