You are here

protected function JsonApiTest::setUp in Entity Reference Integrity 8

Overrides BrowserTestBase::setUp

File

modules/entity_reference_integrity_enforce/tests/src/Functional/JsonApiTest.php, line 79

Class

JsonApiTest
Tests referential integrity via JSONAPI.

Namespace

Drupal\Tests\entity_reference_integrity_enforce\Functional

Code

protected function setUp() : void {
  parent::setUp();
  $entity_type_manager = $this->container
    ->get('entity_type.manager');
  $node_storage = $entity_type_manager
    ->getStorage('node');

  /** @var \Drupal\entity_reference_integrity\EntityReferenceIntegrityEntityHandler $entity_reference_integrity_handler */
  $this->referenceIntegrityHandler = $entity_type_manager
    ->getHandler('node', 'entity_reference_integrity');

  // Create a new node-type.
  $this->nodeType = NodeType::create([
    'type' => $node_type = mb_strtolower($this
      ->randomMachineName()),
    'name' => $this
      ->randomString(),
  ]);
  $this->nodeType
    ->save();

  // Create an entity reference field to test with.
  $this
    ->createEntityReferenceField('node', $node_type, 'test_reference_field', 'Test reference field', 'node');

  // Create testing nodes.
  $this->referencedNode = $node_storage
    ->create([
    'title' => 'Node to delete',
    'type' => $node_type,
  ]);
  $this->referencedNode
    ->save();
  $this->testNode = $node_storage
    ->create([
    'title' => 'Referenced node',
    'type' => $node_type,
    'test_reference_field' => [
      'entity' => $this->referencedNode,
    ],
  ]);
  $this->testNode
    ->save();

  // Create a user with permission to delete the test nodes.
  $this->testUser = $this
    ->createUser([
    'delete any ' . $this->nodeType
      ->id() . ' content',
  ]);

  // Allow entities to be updated via JSONAPI.
  \Drupal::configFactory()
    ->getEditable('jsonapi.settings')
    ->set('read_only', FALSE)
    ->save();

  // Enable reference integrity for nodes.
  \Drupal::configFactory()
    ->getEditable('entity_reference_integrity_enforce.settings')
    ->set('enabled_entity_type_ids', [
    'node' => 'node',
  ])
    ->save();

  // Rebuild routes to include the new node type.
  \Drupal::service('router.builder')
    ->rebuildIfNeeded();
}