View source
<?php
namespace Drupal\Tests\entity_reference_integrity_enforce\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\entity_reference_integrity\EntityReferenceIntegrityEntityHandler;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\jsonapi\Functional\JsonApiRequestTestTrait;
use GuzzleHttp\RequestOptions;
class JsonApiTest extends BrowserTestBase {
use EntityReferenceTestTrait;
use JsonApiRequestTestTrait;
use StringTranslationTrait;
protected static $modules = [
'basic_auth',
'jsonapi',
'node',
'entity_reference_integrity_enforce',
];
protected $defaultTheme = 'stark';
protected $referenceIntegrityHandler;
protected $nodeType;
protected $testUser;
protected $testNode;
protected $referencedNode;
protected function setUp() : void {
parent::setUp();
$entity_type_manager = $this->container
->get('entity_type.manager');
$node_storage = $entity_type_manager
->getStorage('node');
$this->referenceIntegrityHandler = $entity_type_manager
->getHandler('node', 'entity_reference_integrity');
$this->nodeType = NodeType::create([
'type' => $node_type = mb_strtolower($this
->randomMachineName()),
'name' => $this
->randomString(),
]);
$this->nodeType
->save();
$this
->createEntityReferenceField('node', $node_type, 'test_reference_field', 'Test reference field', 'node');
$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();
$this->testUser = $this
->createUser([
'delete any ' . $this->nodeType
->id() . ' content',
]);
\Drupal::configFactory()
->getEditable('jsonapi.settings')
->set('read_only', FALSE)
->save();
\Drupal::configFactory()
->getEditable('entity_reference_integrity_enforce.settings')
->set('enabled_entity_type_ids', [
'node' => 'node',
])
->save();
\Drupal::service('router.builder')
->rebuildIfNeeded();
}
public function testApiResourceDelete() {
$request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
$request_options[RequestOptions::HEADERS]['Content-Type'] = 'application/vnd.api+json';
$request_options[RequestOptions::HEADERS]['Authorization'] = 'Basic ' . base64_encode($this->testUser->name->value . ':' . $this->testUser->passRaw);
$resource_type_name = $this->referencedNode
->getEntityTypeId() . '--' . $this->referencedNode
->bundle();
$node_url = Url::fromRoute(sprintf('jsonapi.%s.individual', $resource_type_name), [
'entity' => $this->referencedNode
->uuid(),
])
->setAbsolute();
$this
->assertTrue($this->referenceIntegrityHandler
->hasDependents($this->referencedNode));
$response = $this
->request('DELETE', $node_url, $request_options);
$this
->assertEquals(403, $response
->getStatusCode());
$response_data = Json::decode((string) $response
->getBody());
$this
->assertArrayHasKey('errors', $response_data);
$this
->assertEquals(1, sizeof($response_data['errors']));
$this
->assertArrayHasKey('status', $response_data['errors'][0]);
$this
->assertEquals('403', $response_data['errors'][0]['status']);
$reason = EntityReferenceIntegrityEntityHandler::getAccessDeniedReason($this->referencedNode, FALSE);
$this
->assertArrayHasKey('detail', $response_data['errors'][0]);
$this
->assertEquals($reason, $response_data['errors'][0]['detail']);
$this->testNode->test_reference_field = [];
$this->testNode
->save();
$this
->assertFalse($this->referenceIntegrityHandler
->hasDependents($this->referencedNode));
$response = $this
->request('DELETE', $node_url, $request_options);
$this
->assertEquals(204, $response
->getStatusCode());
$node_url = Url::fromRoute(sprintf('jsonapi.%s.individual', $resource_type_name), [
'entity' => $this->testNode
->uuid(),
])
->setAbsolute();
$this
->assertFalse($this->referenceIntegrityHandler
->hasDependents($this->referencedNode));
$response = $this
->request('DELETE', $node_url, $request_options);
$this
->assertEquals(204, $response
->getStatusCode());
}
}