You are here

public function JsonExtrasApiFunctionalTest::testRead in JSON:API Extras 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/JsonExtrasApiFunctionalTest.php \Drupal\Tests\jsonapi_extras\Functional\JsonExtrasApiFunctionalTest::testRead()

Test the GET method.

File

tests/src/Functional/JsonExtrasApiFunctionalTest.php, line 97

Class

JsonExtrasApiFunctionalTest
The test class for the main functionality.

Namespace

Drupal\Tests\jsonapi_extras\Functional

Code

public function testRead() {
  $num_articles = 61;
  $this
    ->createDefaultContent($num_articles, 5, TRUE, TRUE, static::IS_NOT_MULTILINGUAL);

  // Make the link for node/3 to point to an entity.
  $this->nodes[3]->field_link
    ->setValue([
    'uri' => 'entity:node/' . $this->nodes[2]
      ->id(),
  ]);
  $this->nodes[3]
    ->save();
  $this->nodes[40]->uid
    ->set(0, 1);
  $this->nodes[40]
    ->save();

  // 1. Make sure the api root is under '/api' and not '/jsonapi'.

  /** @var \Symfony\Component\Routing\RouteCollection $route_collection */
  $route_collection = \Drupal::service('router.route_provider')
    ->getRoutesByPattern('/api');
  $this
    ->assertInstanceOf(Route::class, $route_collection
    ->get('jsonapi.resource_list'));
  $this
    ->drupalGet('/jsonapi');
  $this
    ->assertSession()
    ->statusCodeEquals(404);

  // 2. Make sure the count is included in collections. This also tests the
  // overridden paths.
  $output = Json::decode($this
    ->drupalGet('/api/articles'));
  $this
    ->assertSame($num_articles, (int) $output['meta']['count']);
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // 3. Check disabled resources.
  $this
    ->drupalGet('/api/taxonomy_vocabulary/taxonomy_vocabulary');
  $this
    ->assertSession()
    ->statusCodeEquals(404);

  // 4. Check renamed fields.
  $output = Json::decode($this
    ->drupalGet('/api/articles/' . $this->nodes[0]
    ->uuid()));
  $this
    ->assertArrayNotHasKey('type', $output['data']['attributes']);
  $this
    ->assertArrayHasKey('contentType', $output['data']['relationships']);
  $this
    ->assertSame('contentTypes', $output['data']['relationships']['contentType']['data']['type']);
  $output = Json::decode($this
    ->drupalGet('/api/contentTypes/' . $this->nodes[0]->type->entity
    ->uuid()));
  $this
    ->assertArrayNotHasKey('type', $output['data']['attributes']);
  $this
    ->assertSame('article', $output['data']['attributes']['machineName']);

  // 5. Check disabled fields.
  $output = Json::decode($this
    ->drupalGet('/api/articles/' . $this->nodes[1]
    ->uuid()));
  $this
    ->assertArrayNotHasKey('uuid', $output['data']['attributes']);

  // 6. Test the field enhancers: DateTimeEnhancer.
  $output = Json::decode($this
    ->drupalGet('/api/articles/' . $this->nodes[2]
    ->uuid()));
  $timestamp = \DateTime::createFromFormat('Y-m-d\\TH:i:sO', $output['data']['attributes']['createdAt'])
    ->format('U');
  $this
    ->assertSame((int) $timestamp, $this->nodes[2]
    ->getCreatedTime());

  // 7. Test the field enhancers: UuidLinkEnhancer.
  $output = Json::decode($this
    ->drupalGet('/api/articles/' . $this->nodes[3]
    ->uuid()));
  $expected_link = 'entity:node/article/' . $this->nodes[2]
    ->uuid();
  $this
    ->assertSame($expected_link, $output['data']['attributes']['link']['uri']);

  // 8. Test the field enhancers: SingleNestedEnhancer.
  $output = Json::decode($this
    ->drupalGet('/api/articles/' . $this->nodes[3]
    ->uuid()));
  $this
    ->assertInternalType('string', $output['data']['attributes']['body']);

  // 9. Test the related endpoint.
  // This tests the overridden resource name, the overridden field names and
  // the disabled fields.
  $output = Json::decode($this
    ->drupalGet('/api/articles/' . $this->nodes[4]
    ->uuid() . '/contentType'));
  $this
    ->assertArrayNotHasKey('type', $output['data']['attributes']);
  $this
    ->assertSame('article', $output['data']['attributes']['machineName']);
  $this
    ->assertSame('contentTypes', $output['data']['type']);
  $this
    ->assertArrayNotHasKey('uuid', $output['data']['attributes']);

  // 10. Test the relationships endpoint.
  $output = Json::decode($this
    ->drupalGet('/api/articles/' . $this->nodes[4]
    ->uuid() . '/relationships/contentType'));
  $this
    ->assertSame('contentTypes', $output['data']['type']);
  $this
    ->assertArrayHasKey('id', $output['data']);

  // 11. Test the related endpoint on a multiple cardinality relationship.
  $output = Json::decode($this
    ->drupalGet('/api/articles/' . $this->nodes[5]
    ->uuid() . '/tags'));
  $this
    ->assertCount(count($this->nodes[5]
    ->get('field_tags')
    ->getValue()), $output['data']);
  $this
    ->assertSame('taxonomy_term--tags', $output['data'][0]['type']);

  // 12. Test the relationships endpoint.
  $output = Json::decode($this
    ->drupalGet('/api/articles/' . $this->nodes[5]
    ->uuid() . '/relationships/tags'));
  $this
    ->assertCount(count($this->nodes[5]
    ->get('field_tags')
    ->getValue()), $output['data']);
  $this
    ->assertArrayHasKey('id', $output['data'][0]);

  // 13. Test a disabled related resource of single cardinality.
  $this
    ->drupalGet('/api/taxonomy_term/tags/' . $this->tags[0]
    ->uuid() . '/vid');
  $this
    ->assertSession()
    ->statusCodeEquals(404);
  $this
    ->drupalGet('/api/taxonomy_term/tags/' . $this->tags[0]
    ->uuid() . '/relationships/vid');
  $this
    ->assertSession()
    ->statusCodeEquals(404);

  // 14. Test a disabled related resource of multiple cardinality.
  $this->tags[1]->vocabs
    ->set(0, 'tags');
  $this->tags[1]
    ->save();
  $output = Json::decode($this
    ->drupalGet('/api/taxonomy_term/tags/' . $this->tags[0]
    ->uuid() . '/vocabs'));
  $this
    ->assertTrue(empty($output['data']));
  $output = Json::decode($this
    ->drupalGet('/api/taxonomy_term/tags/' . $this->tags[0]
    ->uuid() . '/relationships/vocabs'));
  $this
    ->assertTrue(empty($output['data']));

  // 15. Test included resource.
  $output = Json::decode($this
    ->drupalGet('/api/articles/' . $this->nodes[6]
    ->uuid(), [
    'query' => [
      'include' => 'owner',
    ],
  ]));
  $this
    ->assertSame('user--user', $output['included'][0]['type']);

  // 16. Test disabled included resources.
  $output = Json::decode($this
    ->drupalGet('/api/taxonomy_term/tags/' . $this->tags[0]
    ->uuid(), [
    'query' => [
      'include' => 'vocabs,vid',
    ],
  ]));
  $this
    ->assertArrayNotHasKey('included', $output);

  // 17. Test nested filters with renamed field.
  $output = Json::decode($this
    ->drupalGet('/api/articles', [
    'query' => [
      'filter' => [
        'owner.name' => [
          'value' => User::load(1)
            ->getAccountName(),
        ],
      ],
    ],
  ]));

  // There is only one article for the admin.
  $this
    ->assertSame($this->nodes[40]
    ->uuid(), $output['data'][0]['id']);
}