You are here

public function EntityResourceTest::testCreateIndividual in JSON:API 8.2

Same name and namespace in other branches
  1. 8 tests/src/Kernel/Controller/EntityResourceTest.php \Drupal\Tests\jsonapi\Kernel\Controller\EntityResourceTest::testCreateIndividual()

@covers ::createIndividual

File

tests/src/Kernel/Controller/EntityResourceTest.php, line 405

Class

EntityResourceTest
@coversDefaultClass \Drupal\jsonapi\Controller\EntityResource @group jsonapi @group legacy

Namespace

Drupal\Tests\jsonapi\Kernel\Controller

Code

public function testCreateIndividual() {
  Role::load(Role::ANONYMOUS_ID)
    ->grantPermission('create article content')
    ->save();
  $content = Json::encode([
    'data' => [
      'type' => 'node--article',
      'attributes' => [
        'title' => 'Lorem ipsum',
      ],
    ],
  ]);
  $request = Request::create('/jsonapi/node/article', 'POST', [], [], [], [], $content);
  $resource_type = new ResourceType('node', 'article', Node::class);
  $resource_type
    ->setRelatableResourceTypes([
    'field_relationships' => [
      new ResourceType('node', 'article', NULL),
    ],
  ]);
  $response = $this->entityResource
    ->createIndividual($resource_type, $request);

  // As a side effect, the node will also be saved.
  $this
    ->assertInstanceOf(JsonApiDocumentTopLevel::class, $response
    ->getResponseData());
  $this
    ->assertTrue(entity_load_multiple_by_properties('node', [
    'uuid' => $response
      ->getResponseData()
      ->getData()
      ->getIterator()
      ->offsetGet(0)
      ->getId(),
  ]));
  $this
    ->assertEquals(201, $response
    ->getStatusCode());
}