function JsonApiDocumentTopLevelNormalizerTest::testDenormalizeInvalidTypeAndNoType

Same name in other branches
  1. 8.9.x core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testDenormalizeInvalidTypeAndNoType()
  2. 10 core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testDenormalizeInvalidTypeAndNoType()
  3. 11.x core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testDenormalizeInvalidTypeAndNoType()

Tests denormalization for related resources with missing or invalid types.

File

core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php, line 632

Class

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

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);
    $resource_type = $this->container
        ->get('jsonapi.resource_type.repository')
        ->get('node', 'article');
    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);
    $resource_type = $this->container
        ->get('jsonapi.resource_type.repository')
        ->get('node', 'article');
    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());
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.