function JsonApiTopLevelResourceNormalizerTest::testDenormalizeUuid

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

@covers ::denormalize

File

core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiTopLevelResourceNormalizerTest.php, line 589

Class

JsonApiTopLevelResourceNormalizerTest
@coversDefaultClass \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer[[api-linebreak]] @group jsonapi

Namespace

Drupal\Tests\jsonapi\Kernel\Normalizer

Code

public function testDenormalizeUuid() : void {
  $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.