You are here

public function ApiTest::testEntities in Lightning API 8.4

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/ApiTest.php \Drupal\Tests\lightning_api\Functional\ApiTest::testEntities()

Tests create, read, and update of content entities via the API.

File

tests/src/Functional/ApiTest.php, line 141

Class

ApiTest
Tests that OAuth and JSON:API authenticate and authorize entity operations.

Namespace

Drupal\Tests\lightning_api\Functional

Code

public function testEntities() {
  $access_token = $this
    ->createApiUser([], NULL, TRUE);

  // Create a taxonomy vocabulary. This cannot currently be done over the API
  // because jsonapi doesn't really support it, and will not be able to
  // properly support it until config entities can be internally validated
  // and access controlled outside of the UI.
  $vocabulary = Vocabulary::create([
    'name' => "I'm a vocab",
    'vid' => 'im_a_vocab',
    'status' => TRUE,
  ]);
  $vocabulary
    ->save();
  $endpoint = '/jsonapi/taxonomy_vocabulary/taxonomy_vocabulary/' . $vocabulary
    ->uuid();

  // Read the newly created vocabulary.
  $response = $this
    ->request($endpoint, 'get', $access_token);
  $body = $this
    ->decodeResponse($response);
  $this
    ->assertSame($vocabulary
    ->label(), $body['data']['attributes']['name']);
  $vocabulary
    ->set('name', 'Still a vocab, just a different title');
  $vocabulary
    ->save();

  // The router needs to be rebuilt in order for the new vocabulary to be
  // available to JSON:API.
  $this->container
    ->get('router.builder')
    ->rebuild();

  // Read the updated vocabulary.
  $response = $this
    ->request($endpoint, 'get', $access_token);
  $body = $this
    ->decodeResponse($response);
  $this
    ->assertSame($vocabulary
    ->label(), $body['data']['attributes']['name']);

  // Assert that the newly created vocabulary's endpoint is reachable.
  $response = $this
    ->request('/jsonapi/taxonomy_term/im_a_vocab');
  $this
    ->assertSame(200, $response
    ->getStatusCode());
  $name = 'zebra';
  $term_uuid = $this->container
    ->get('uuid')
    ->generate();
  $endpoint = '/jsonapi/taxonomy_term/im_a_vocab/' . $term_uuid;

  // Create a taxonomy term (content entity).
  $this
    ->request('/jsonapi/taxonomy_term/im_a_vocab', 'post', $access_token, [
    'data' => [
      'type' => 'taxonomy_term--im_a_vocab',
      'id' => $term_uuid,
      'attributes' => [
        'name' => $name,
        'uuid' => $term_uuid,
      ],
      'relationships' => [
        'vid' => [
          'data' => [
            'type' => 'taxonomy_vocabulary--taxonomy_vocabulary',
            'id' => $vocabulary
              ->uuid(),
          ],
        ],
      ],
    ],
  ]);

  // Read the taxonomy term.
  $response = $this
    ->request($endpoint, 'get', $access_token);
  $body = $this
    ->decodeResponse($response);
  $this
    ->assertSame($name, $body['data']['attributes']['name']);
  $new_name = 'squid';

  // Update the taxonomy term.
  $this
    ->request($endpoint, 'patch', $access_token, [
    'data' => [
      'type' => 'taxonomy_term--im_a_vocab',
      'id' => $term_uuid,
      'attributes' => [
        'name' => $new_name,
      ],
    ],
  ]);

  // Read the updated taxonomy term.
  $response = $this
    ->request($endpoint, 'get', $access_token);
  $body = $this
    ->decodeResponse($response);
  $this
    ->assertSame($new_name, $body['data']['attributes']['name']);
}