You are here

public function JsonApiRegressionTest::testThatRoutesAreRebuiltAfterDataModelChangesFromIssue2984886 in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testThatRoutesAreRebuiltAfterDataModelChangesFromIssue2984886()

Ensures that JSON:API routes are caches are dynamically rebuilt.

Adding a new relationship field should cause new routes to be immediately regenerated. The site builder should not need to manually rebuild caches.

See also

https://www.drupal.org/project/drupal/issues/2984886

File

core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php, line 480

Class

JsonApiRegressionTest
JSON:API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testThatRoutesAreRebuiltAfterDataModelChangesFromIssue2984886() {
  $user = $this
    ->drupalCreateUser([
    'access content',
  ]);
  $request_options = [
    RequestOptions::AUTH => [
      $user
        ->getAccountName(),
      $user->pass_raw,
    ],
  ];
  $response = $this
    ->request('GET', Url::fromUri('internal:/jsonapi/node/dog'), $request_options);
  $this
    ->assertSame(404, $response
    ->getStatusCode());
  $node_type_dog = NodeType::create([
    'type' => 'dog',
  ]);
  $node_type_dog
    ->save();
  NodeType::create([
    'type' => 'cat',
  ])
    ->save();
  \Drupal::service('router.builder')
    ->rebuildIfNeeded();
  $response = $this
    ->request('GET', Url::fromUri('internal:/jsonapi/node/dog'), $request_options);
  $this
    ->assertSame(200, $response
    ->getStatusCode());
  $this
    ->createEntityReferenceField('node', 'dog', 'field_test', NULL, 'node');
  \Drupal::service('router.builder')
    ->rebuildIfNeeded();
  $dog = Node::create([
    'type' => 'dog',
    'title' => 'Rosie P. Mosie',
  ]);
  $dog
    ->save();
  $response = $this
    ->request('GET', Url::fromUri('internal:/jsonapi/node/dog/' . $dog
    ->uuid() . '/field_test'), $request_options);
  $this
    ->assertSame(200, $response
    ->getStatusCode());
  $this
    ->createEntityReferenceField('node', 'cat', 'field_test', NULL, 'node');
  \Drupal::service('router.builder')
    ->rebuildIfNeeded();
  $cat = Node::create([
    'type' => 'cat',
    'title' => 'E. Napoleon',
  ]);
  $cat
    ->save();
  $response = $this
    ->request('GET', Url::fromUri('internal:/jsonapi/node/cat/' . $cat
    ->uuid() . '/field_test'), $request_options);
  $this
    ->assertSame(200, $response
    ->getStatusCode());
  FieldConfig::loadByName('node', 'cat', 'field_test')
    ->delete();
  \Drupal::service('router.builder')
    ->rebuildIfNeeded();
  $response = $this
    ->request('GET', Url::fromUri('internal:/jsonapi/node/cat/' . $cat
    ->uuid() . '/field_test'), $request_options);
  $this
    ->assertSame(404, $response
    ->getStatusCode());
  $node_type_dog
    ->delete();
  \Drupal::service('router.builder')
    ->rebuildIfNeeded();
  $response = $this
    ->request('GET', Url::fromUri('internal:/jsonapi/node/dog'), $request_options);
  $this
    ->assertSame(404, $response
    ->getStatusCode());
}