You are here

public function EntityResourceTest::testCreateIndividualDuplicateError 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::testCreateIndividualDuplicateError()

@covers ::createIndividual

File

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

Class

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

Namespace

Drupal\Tests\jsonapi\Kernel\Controller

Code

public function testCreateIndividualDuplicateError() {
  Role::load(Role::ANONYMOUS_ID)
    ->grantPermission('create article content')
    ->save();
  $node = Node::create([
    'type' => 'article',
    'title' => 'Lorem ipsum',
  ]);
  $node
    ->save();
  $node
    ->enforceIsNew();
  $payload = Json::encode([
    'data' => [
      'type' => 'article',
      'id' => $this->node
        ->uuid(),
      'attributes' => [
        'title' => 'foobar',
      ],
    ],
  ]);
  $this
    ->setExpectedException(ConflictHttpException::class, 'Conflict: Entity already exists.');
  $resource_type = new ResourceType('node', 'article', Node::class);
  $resource_type
    ->setRelatableResourceTypes([
    'field_relationships' => [
      new ResourceType('node', 'article', NULL),
    ],
  ]);
  $this->entityResource
    ->createIndividual($resource_type, Request::create('/jsonapi/node/article', 'POST', [], [], [], [], $payload));
}