function JsonApiDocumentTopLevelNormalizerTest::testDenormalizeInvalidTypeAndNoType

Same name and namespace in other branches
  1. 9 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 558

Class

JsonApiDocumentTopLevelNormalizerTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21jsonapi%21src%21Normalizer%21JsonApiDocumentTopLevelNormalizer.php/class/JsonApiDocumentTopLevelNormalizer/8.9.x" title="Normalizes the top-level document according to the JSON:API specification." class="local">\Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer</a> @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);
    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());
    }
}

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