You are here

public function JsonApiDocumentTopLevelNormalizerTest::testDenormalizeInvalidTypeAndNoType in JSON:API 8.2

Same name and namespace in other branches
  1. 8 tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testDenormalizeInvalidTypeAndNoType()

Tests denormalization for related resources with missing or invalid types.

File

tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php, line 559

Class

JsonApiDocumentTopLevelNormalizerTest
@coversDefaultClass \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer @group jsonapi @group legacy

Namespace

Drupal\Tests\jsonapi\Kernel\Normalizer

Code

public function testDenormalizeInvalidTypeAndNoType() {
  $payload_data = [
    'data' => [
      'type' => 'node--article',
      'attributes' => [
        'title' => 'Testing article',
        'id' => '33095485-70D2-4E51-A309-535CC5BC0115',
      ],
      'relationships' => [
        'uid' => [
          'data' => [
            'type' => 'user--user',
            'id' => $this->user2
              ->uuid(),
          ],
        ],
        'field_tags' => [
          'data' => [
            [
              'type' => 'foobar',
              'id' => $this->term1
                ->uuid(),
            ],
          ],
        ],
      ],
    ],
  ];

  // Test relationship member with invalid type.
  $payload = Json::encode($payload_data);
  list($request, $resource_type) = $this
    ->generateProphecies('node', 'article');
  $this->container
    ->get('request_stack')
    ->push($request);
  try {
    $this
      ->getNormalizer()
      ->denormalize(Json::decode($payload), NULL, 'api_json', [
      'resource_type' => $resource_type,
    ]);
    $this
      ->fail('No assertion thrown for invalid type');
  } catch (BadRequestHttpException $e) {
    $this
      ->assertEquals("Invalid type specified for related resource: 'foobar'", $e
      ->getMessage());
  }

  // Test relationship member with no type.
  unset($payload_data['data']['relationships']['field_tags']['data'][0]['type']);
  $payload = Json::encode($payload_data);
  list($request, $resource_type) = $this
    ->generateProphecies('node', 'article');
  $this->container
    ->get('request_stack')
    ->push($request);
  try {
    $this->container
      ->get('jsonapi_test_normalizers_kernel.jsonapi_document_toplevel')
      ->denormalize(Json::decode($payload), NULL, 'api_json', [
      'resource_type' => $resource_type,
    ]);
    $this
      ->fail('No assertion thrown for missing type');
  } catch (BadRequestHttpException $e) {
    $this
      ->assertEquals("No type specified for related resource", $e
      ->getMessage());
  }
}