You are here

public function EntityResourceTest::testPatchIndividualConfig in JSON:API Extras 8.2

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

@covers ::patchIndividual @dataProvider patchIndividualConfigProvider

1 call to EntityResourceTest::testPatchIndividualConfig()
EntityResourceTest::testPatchIndividualFailedConfig in tests/src/Kernel/Controller/EntityResourceTest.php
@covers ::patchIndividual @dataProvider patchIndividualConfigFailedProvider

File

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

Class

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

Namespace

Drupal\Tests\jsonapi_extras\Kernel\Controller

Code

public function testPatchIndividualConfig($values) {

  // List of fields to be ignored.
  $ignored_fields = [
    'uuid',
    'entityTypeId',
    'type',
  ];
  $node_type = NodeType::create([
    'type' => 'test',
    'name' => 'Test Type',
    'description' => '',
  ]);
  $node_type
    ->save();
  $parsed_node_type = NodeType::create($values);
  Role::load(Role::ANONYMOUS_ID)
    ->grantPermission('administer content types')
    ->save();
  Role::load(Role::ANONYMOUS_ID)
    ->grantPermission('edit any article content')
    ->save();
  $payload = Json::encode([
    'data' => [
      'type' => 'node_type',
      'id' => $node_type
        ->uuid(),
      'attributes' => $values,
    ],
  ]);
  $request = new Request([], [], [], [], [], [], $payload);
  $resource_type = new ResourceType('node', 'article', NULL);
  $entity_resource = new EntityResource($resource_type, $this->container
    ->get('entity_type.manager'), $this->container
    ->get('entity_field.manager'), $this->container
    ->get('plugin.manager.field.field_type'), $this->container
    ->get('jsonapi.link_manager'), $this->container
    ->get('jsonapi.resource_type.repository'), $this->container
    ->get('renderer'));
  $response = $entity_resource
    ->patchIndividual($node_type, $parsed_node_type, $request);

  // As a side effect, the node will also be saved.
  $this
    ->assertInstanceOf(JsonApiDocumentTopLevel::class, $response
    ->getResponseData());
  $updated_node_type = $response
    ->getResponseData()
    ->getData();
  $this
    ->assertInstanceOf(NodeType::class, $updated_node_type);

  // If the field is ignored then we should not see a difference.
  foreach ($values as $field_name => $value) {
    in_array($field_name, $ignored_fields) ? $this
      ->assertNotSame($value, $node_type
      ->get($field_name)) : $this
      ->assertSame($value, $node_type
      ->get($field_name));
  }
  $this
    ->assertEquals(200, $response
    ->getStatusCode());
}