function JsonApiRegressionTest::testThatRoutesAreRebuiltAfterDataModelChangesFromIssue2984886

Same name in other branches
  1. 8.9.x core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testThatRoutesAreRebuiltAfterDataModelChangesFromIssue2984886()
  2. 10 core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testThatRoutesAreRebuiltAfterDataModelChangesFromIssue2984886()
  3. 11.x 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());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.