You are here

public function EntityResourceTest::testCreateIndividualConfig in JSON:API Extras 8.3

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/Controller/EntityResourceTest.php \Drupal\Tests\jsonapi_extras\Kernel\Controller\EntityResourceTest::testCreateIndividualConfig()

@covers ::createIndividual

File

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

Class

EntityResourceTest
@coversDefaultClass \Drupal\jsonapi\Controller\EntityResource @covers \Drupal\jsonapi_extras\Normalizer\ConfigEntityDenormalizer @group jsonapi_extras @group legacy

Namespace

Drupal\Tests\jsonapi_extras\Kernel\Controller

Code

public function testCreateIndividualConfig() {
  Role::load(Role::ANONYMOUS_ID)
    ->grantPermission('administer content types')
    ->save();
  $resource_type = new ResourceType('node_type', 'node_type', NodeType::class);
  $resource_type
    ->setRelatableResourceTypes([]);
  $payload = Json::encode([
    'data' => [
      'type' => 'node--test',
      'attributes' => [
        'type' => 'test',
        'name' => 'Test Type',
        'description' => 'Lorem ipsum',
      ],
    ],
  ]);
  $entity_resource = new EntityResource($this->container
    ->get('entity_type.manager'), $this->container
    ->get('entity_field.manager'), $this->container
    ->get('jsonapi.resource_type.repository'), $this->container
    ->get('renderer'), $this->container
    ->get('entity.repository'), $this->container
    ->get('jsonapi.include_resolver'), new EntityAccessChecker($this->container
    ->get('jsonapi.resource_type.repository'), $this->container
    ->get('router.no_access_checks'), $this->container
    ->get('current_user'), $this->container
    ->get('entity.repository')), $this->container
    ->get('jsonapi.field_resolver'), $this->container
    ->get('jsonapi.serializer'), $this->container
    ->get('datetime.time'), $this->container
    ->get('current_user'));
  $response = $entity_resource
    ->createIndividual($resource_type, Request::create('/jsonapi/node_type/node_type', 'POST', [], [], [], [], $payload));

  // As a side effect, the node type will also be saved.
  $node_type = NodeType::load('test');
  $this
    ->assertInstanceOf(JsonApiDocumentTopLevel2::class, $response
    ->getResponseData());
  $data = $response
    ->getResponseData()
    ->getData()
    ->getIterator()
    ->offsetGet(0);
  $this
    ->assertInstanceOf(ResourceObject::class, $data);
  $this
    ->assertEquals($node_type
    ->uuid(), $data
    ->getId());
  $this
    ->assertEquals(201, $response
    ->getStatusCode());
}