You are here

public function EntityCrudTest::testEntities in Lightning API 8

Same name and namespace in other branches
  1. 8.2 modules/api_test/tests/src/Functional/EntityCrudTest.php \Drupal\Tests\api_test\Functional\EntityCrudTest::testEntities()

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

File

modules/api_test/tests/src/Functional/EntityCrudTest.php, line 69

Class

EntityCrudTest
Tests the ability to Create, Read, and Update config and config entities via the API.

Namespace

Drupal\Tests\api_test\Functional

Code

public function testEntities() {

  // 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', $this->token);
  $body = $this
    ->decodeResponse($response);
  $this
    ->assertEquals($vocabulary
    ->label(), $body['data']['attributes']['name']);
  $vocabulary
    ->set('name', 'Still a vocab, just a different title');
  $vocabulary
    ->save();

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

  // Assert that the newly created vocabulary's endpoint is reachable.
  // @todo figure out why we need to rebuild caches for it to be available.
  drupal_flush_all_caches();
  $response = $this
    ->request('/jsonapi/taxonomy_term/im_a_vocab');
  $this
    ->assertEquals(200, $response
    ->getStatusCode());
  $name = 'zebra';
  $term_uuid = $this->container
    ->get('uuid')
    ->generate();
  $endpoint = '/jsonapi/taxonomy_term/im_a_vocab/' . $term_uuid;
  $data = [
    '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(),
          ],
        ],
      ],
    ],
  ];

  // Create a taxonomy term (content entity).
  $this
    ->request('/jsonapi/taxonomy_term/im_a_vocab', 'post', $this->token, $data);

  // Read the taxonomy term.
  $response = $this
    ->request($endpoint, 'get', $this->token);
  $body = $this
    ->decodeResponse($response);
  $this
    ->assertEquals($name, $body['data']['attributes']['name']);
  $new_name = 'squid';
  $data = [
    'data' => [
      'type' => 'taxonomy_term--im_a_vocab',
      'id' => $term_uuid,
      'attributes' => [
        'name' => $new_name,
      ],
    ],
  ];

  // Update the taxonomy term.
  $this
    ->request($endpoint, 'patch', $this->token, $data);

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