You are here

public function CreateTest::testCreateResourceRestApiNotEnabled in Zircon Profile 8

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

Try to create a resource which is not REST API enabled.

File

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

Class

CreateTest
Tests the creation of resources.

Namespace

Drupal\rest\Tests

Code

public function testCreateResourceRestApiNotEnabled() {
  $entity_type = 'entity_test';

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

  // Get the necessary user permissions to create the current entity type.
  $permissions = $this
    ->entityPermissions($entity_type, 'create');

  // POST method must be allowed for the current entity type.
  $permissions[] = 'restful post entity:' . $entity_type;

  // Create the user.
  $account = $this
    ->drupalCreateUser($permissions);

  // Populate some entity properties before create the entity.
  $entity_values = $this
    ->entityValues($entity_type);
  $entity = EntityTest::create($entity_values);

  // Serialize the entity before the POST request.
  $serialized = $this->serializer
    ->serialize($entity, $this->defaultFormat, [
    'account' => $account,
  ]);

  // Disable all resource types.
  $this
    ->enableService(FALSE);
  $this
    ->drupalLogin($account);

  // POST request to create the current entity. GET request for CSRF token
  // is included into the httpRequest() method.
  $this
    ->httpRequest('entity/entity_test', 'POST', $serialized, $this->defaultMimeType);

  // The resource is not enabled. So, we receive a 'not found' response.
  $this
    ->assertResponse(404);
  $this
    ->assertFalse(EntityTest::loadMultiple(), 'No entity has been created in the database.');
}