You are here

protected function CreateTest::testCreateComment in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/rest/src/Tests/CreateTest.php \Drupal\rest\Tests\CreateTest::testCreateComment()

Test comment creation.

File

core/modules/rest/src/Tests/CreateTest.php, line 234
Contains \Drupal\rest\Tests\CreateTest.

Class

CreateTest
Tests the creation of resources.

Namespace

Drupal\rest\Tests

Code

protected function testCreateComment() {
  $node = Node::create([
    'type' => 'resttest',
    'title' => 'some node',
  ]);
  $node
    ->save();
  $entity_type = 'comment';

  // Enable the REST service for 'comment' entity type.
  $this
    ->enableService('entity:' . $entity_type, 'POST');

  // Create two accounts that have the required permissions to create
  // resources, The second one has administrative permissions.
  $accounts = $this
    ->createAccountPerEntity($entity_type);
  $account = end($accounts);
  $this
    ->drupalLogin($account);
  $entity_values = $this
    ->entityValues($entity_type);
  $entity_values['entity_id'] = $node
    ->id();
  $entity = Comment::create($entity_values);

  // Changed field can never be added.
  unset($entity->changed);
  $serialized = $this->serializer
    ->serialize($entity, $this->defaultFormat, [
    'account' => $account,
  ]);

  // Create the entity over the REST API.
  $this
    ->assertCreateEntityOverRestApi($entity_type, $serialized);

  // Get the new entity ID from the location header and try to read it from
  // the database.
  $this
    ->assertReadEntityIdFromHeaderAndDb($entity_type, $entity, $entity_values);

  // Try to send invalid data that cannot be correctly deserialized.
  $this
    ->assertCreateEntityInvalidData($entity_type);

  // Try to send no data at all, which does not make sense on POST requests.
  $this
    ->assertCreateEntityNoData($entity_type);

  // Try to send invalid data to trigger the entity validation constraints.
  // Send a UUID that is too long.
  $this
    ->assertCreateEntityInvalidSerialized($entity, $entity_type);
}