You are here

public function JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid in JSON:API 8

Same name in this branch
  1. 8 tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Unit\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid()
  2. 8 tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid()
Same name and namespace in other branches
  1. 8.2 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

tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php, line 467

Class

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

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(),
      ],
    ],
    // Bad data in user and first tag.
    [
      [
        [
          'invalid-uuid',
          $this->term1
            ->uuid(),
        ],
        'also-invalid-uuid',
      ],
      [
        [
          $this->term1
            ->id(),
        ],
        NULL,
      ],
    ],
  ];
  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);
    $node = $this
      ->getNormalizer()
      ->denormalize(Json::decode($payload), NULL, 'api_json', [
      'request' => $request,
      'resource_type' => $resource_type,
    ]);

    /* @var \Drupal\node\Entity\Node $node */
    $this
      ->assertInstanceOf('\\Drupal\\node\\Entity\\Node', $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);
    }
  }
}