function JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid

Same name in this branch
  1. 9 core/modules/jsonapi/tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Unit\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid()
Same name in other branches
  1. 8.9.x core/modules/jsonapi/tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Unit\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid()
  2. 8.9.x core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid()
  3. 10 core/modules/jsonapi/tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Unit\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid()
  4. 10 core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid()
  5. 11.x core/modules/jsonapi/tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Unit\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid()
  6. 11.x core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid()

Try to POST a node and check if it exists afterwards.

@covers ::denormalize

File

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

Class

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

Namespace

Drupal\Tests\jsonapi\Kernel\Normalizer

Code

public function testDenormalizeUuid() {
    $configurations = [
        // Good data.
[
            [
                [
                    $this->term2
                        ->uuid(),
                    $this->term1
                        ->uuid(),
                ],
                $this->user2
                    ->uuid(),
            ],
            [
                [
                    $this->term2
                        ->id(),
                    $this->term1
                        ->id(),
                ],
                $this->user2
                    ->id(),
            ],
        ],
        // Good data, without any tags.
[
            [
                [],
                $this->user2
                    ->uuid(),
            ],
            [
                [],
                $this->user2
                    ->id(),
            ],
        ],
        // Bad data in first tag.
[
            [
                [
                    'invalid-uuid',
                    $this->term1
                        ->uuid(),
                ],
                $this->user2
                    ->uuid(),
            ],
            [
                [
                    $this->term1
                        ->id(),
                ],
                $this->user2
                    ->id(),
            ],
            'taxonomy_term--tags:invalid-uuid',
        ],
        // Bad data in user and first tag.
[
            [
                [
                    'invalid-uuid',
                    $this->term1
                        ->uuid(),
                ],
                'also-invalid-uuid',
            ],
            [
                [
                    $this->term1
                        ->id(),
                ],
                NULL,
            ],
            'user--user:also-invalid-uuid',
        ],
    ];
    foreach ($configurations as $configuration) {
        [
            $payload_data,
            $expected,
        ] = $this->denormalizeUuidProviderBuilder($configuration);
        $payload = Json::encode($payload_data);
        $resource_type = $this->container
            ->get('jsonapi.resource_type.repository')
            ->get('node', 'article');
        try {
            $node = $this->getNormalizer()
                ->denormalize(Json::decode($payload), NULL, 'api_json', [
                'resource_type' => $resource_type,
            ]);
        } catch (NotFoundHttpException $e) {
            $non_existing_resource_identifier = $configuration[2];
            $this->assertEquals("The resource identified by `{$non_existing_resource_identifier}` (given as a relationship item) could not be found.", $e->getMessage());
            continue;
        }
        
        /** @var \Drupal\node\Entity\Node $node */
        $this->assertInstanceOf(Node::class, $node);
        $this->assertSame('Testing article', $node->getTitle());
        if (!empty($expected['user_id'])) {
            $owner = $node->getOwner();
            $this->assertEquals($expected['user_id'], $owner->id());
        }
        $tags = $node->get('field_tags')
            ->getValue();
        if (!empty($expected['tag_ids'][0])) {
            $this->assertEquals($expected['tag_ids'][0], $tags[0]['target_id']);
        }
        else {
            $this->assertArrayNotHasKey(0, $tags);
        }
        if (!empty($expected['tag_ids'][1])) {
            $this->assertEquals($expected['tag_ids'][1], $tags[1]['target_id']);
        }
        else {
            $this->assertArrayNotHasKey(1, $tags);
        }
    }
}

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