You are here

public function JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid in Drupal 8

Same name in this branch
  1. 8 core/modules/jsonapi/tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Unit\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid()
  2. 8 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 463

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) {
    list($payload_data, $expected) = $this
      ->denormalizeUuidProviderBuilder($configuration);
    $payload = Json::encode($payload_data);
    list($request, $resource_type) = $this
      ->generateProphecies('node', 'article');
    $this->container
      ->get('request_stack')
      ->push($request);
    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);
    }
  }
}