Issue #3468280 by catch, bbrala: Speed up JSON:API ResourceTestBase
(cherry picked from commit ae9641c117
)
merge-requests/9437/head
parent
63e9bca7a2
commit
9048455c90
|
@ -383,7 +383,7 @@ class CommentTest extends ResourceTestBase {
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function testPatchIndividual(): void {
|
||||
protected function doTestPatchIndividual(): void {
|
||||
// Ensure ::getModifiedEntityForPatchTesting() can pick an alternative value
|
||||
// for the 'entity_id' field.
|
||||
EntityTest::create([
|
||||
|
@ -391,7 +391,7 @@ class CommentTest extends ResourceTestBase {
|
|||
'type' => 'bar',
|
||||
])->save();
|
||||
|
||||
parent::testPatchIndividual();
|
||||
parent::doTestPatchIndividual();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -201,11 +201,16 @@ class FileTest extends ResourceTestBase {
|
|||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* Tests POST/PATCH/DELETE for an individual resource.
|
||||
*/
|
||||
public function testPostIndividual(): void {
|
||||
public function testIndividual(): void {
|
||||
// @todo https://www.drupal.org/node/1927648
|
||||
$this->markTestSkipped();
|
||||
// Add doTestPostIndividual().
|
||||
$this->doTestPatchIndividual();
|
||||
$this->entity = $this->resaveEntity($this->entity, $this->account);
|
||||
$this->revokePermissions();
|
||||
$this->config('jsonapi.settings')->set('read_only', TRUE)->save(TRUE);
|
||||
$this->doTestDeleteIndividual();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -113,7 +113,7 @@ class FileUploadTest extends ResourceTestBase {
|
|||
*
|
||||
* @var array
|
||||
*/
|
||||
const SKIP_METHODS = ['testGetIndividual', 'testPostIndividual', 'testPatchIndividual', 'testDeleteIndividual', 'testCollection', 'testRelationships'];
|
||||
const SKIP_METHODS = ['testGetIndividual', 'testIndividual', 'testCollection', 'testRelationships'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
|
|
@ -360,10 +360,10 @@ class MediaTest extends ResourceTestBase {
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function testPostIndividual(): void {
|
||||
protected function doTestPostIndividual(): void {
|
||||
// @todo Mimic \Drupal\Tests\rest\Functional\EntityResource\Media\MediaResourceTestBase::testPost()
|
||||
// @todo Later, use https://www.drupal.org/project/drupal/issues/2958554 to upload files rather than the REST module.
|
||||
parent::testPostIndividual();
|
||||
parent::doTestPostIndividual();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -132,7 +132,7 @@ class MessageTest extends ResourceTestBase {
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function testPatchIndividual(): void {
|
||||
protected function doTestPatchIndividual(): void {
|
||||
// Contact Message entities are not stored, so they cannot be modified.
|
||||
$this->expectException(RouteNotFoundException::class);
|
||||
$this->expectExceptionMessage('Route "jsonapi.contact_message--camelids.individual" does not exist.');
|
||||
|
@ -143,7 +143,7 @@ class MessageTest extends ResourceTestBase {
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function testDeleteIndividual(): void {
|
||||
protected function doTestDeleteIndividual(): void {
|
||||
// Contact Message entities are not stored, so they cannot be deleted.
|
||||
$this->expectException(RouteNotFoundException::class);
|
||||
$this->expectExceptionMessage('Route "jsonapi.contact_message--camelids.individual" does not exist.');
|
||||
|
|
|
@ -229,21 +229,7 @@ abstract class ResourceTestBase extends BrowserTestBase {
|
|||
|
||||
$this->config('system.logging')->set('error_level', ERROR_REPORTING_HIDE)->save();
|
||||
|
||||
// Ensure the anonymous user role has no permissions at all.
|
||||
$user_role = Role::load(RoleInterface::ANONYMOUS_ID);
|
||||
foreach ($user_role->getPermissions() as $permission) {
|
||||
$user_role->revokePermission($permission);
|
||||
}
|
||||
$user_role->save();
|
||||
assert([] === $user_role->getPermissions(), 'The anonymous user role has no permissions at all.');
|
||||
|
||||
// Ensure the authenticated user role has no permissions at all.
|
||||
$user_role = Role::load(RoleInterface::AUTHENTICATED_ID);
|
||||
foreach ($user_role->getPermissions() as $permission) {
|
||||
$user_role->revokePermission($permission);
|
||||
}
|
||||
$user_role->save();
|
||||
assert([] === $user_role->getPermissions(), 'The authenticated user role has no permissions at all.');
|
||||
$this->revokePermissions();
|
||||
|
||||
// Create an account, which tests will use. Also ensure the @current_user
|
||||
// service this account, to ensure certain access check logic in tests works
|
||||
|
@ -338,12 +324,15 @@ abstract class ResourceTestBase extends BrowserTestBase {
|
|||
|
||||
\Drupal::service('router.builder')->rebuildIfNeeded();
|
||||
|
||||
return $this->resaveEntity($entity, $account);
|
||||
}
|
||||
|
||||
protected function resaveEntity(EntityInterface $entity, AccountInterface $account): EntityInterface {
|
||||
// Reload entity so that it has the new field.
|
||||
$reloaded_entity = $this->entityLoadUnchanged($entity->id());
|
||||
// Some entity types are not stored, hence they cannot be reloaded.
|
||||
if ($reloaded_entity !== NULL) {
|
||||
$entity = $reloaded_entity;
|
||||
|
||||
// Set a default value on the fields.
|
||||
$entity->set('field_rest_test', ['value' => 'All the faith he had had had had no effect on the outcome of his life.']);
|
||||
$entity->set('field_jsonapi_test_entity_ref', ['user' => $account->id()]);
|
||||
|
@ -1973,10 +1962,25 @@ abstract class ResourceTestBase extends BrowserTestBase {
|
|||
return $related_response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests POST/PATCH/DELETE for an individual resource.
|
||||
*/
|
||||
public function testIndividual(): void {
|
||||
$this->doTestPostIndividual();
|
||||
$this->entity = $this->resaveEntity($this->entity, $this->account);
|
||||
$this->revokePermissions();
|
||||
$this->config('jsonapi.settings')->set('read_only', TRUE)->save(TRUE);
|
||||
$this->doTestPatchIndividual();
|
||||
$this->entity = $this->resaveEntity($this->entity, $this->account);
|
||||
$this->revokePermissions();
|
||||
$this->config('jsonapi.settings')->set('read_only', TRUE)->save(TRUE);
|
||||
$this->doTestDeleteIndividual();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests POSTing an individual resource, plus edge cases to ensure good DX.
|
||||
*/
|
||||
public function testPostIndividual(): void {
|
||||
protected function doTestPostIndividual(): void {
|
||||
// @todo Remove this in https://www.drupal.org/node/2300677.
|
||||
if ($this->entity instanceof ConfigEntityInterface) {
|
||||
$this->markTestSkipped('POSTing config entities is not yet supported.');
|
||||
|
@ -2191,7 +2195,7 @@ abstract class ResourceTestBase extends BrowserTestBase {
|
|||
/**
|
||||
* Tests PATCHing an individual resource, plus edge cases to ensure good DX.
|
||||
*/
|
||||
public function testPatchIndividual(): void {
|
||||
protected function doTestPatchIndividual(): void {
|
||||
// @todo Remove this in https://www.drupal.org/node/2300677.
|
||||
if ($this->entity instanceof ConfigEntityInterface) {
|
||||
$this->markTestSkipped('PATCHing config entities is not yet supported.');
|
||||
|
@ -2519,7 +2523,7 @@ abstract class ResourceTestBase extends BrowserTestBase {
|
|||
/**
|
||||
* Tests DELETEing an individual resource, plus edge cases to ensure good DX.
|
||||
*/
|
||||
public function testDeleteIndividual(): void {
|
||||
protected function doTestDeleteIndividual(): void {
|
||||
// @todo Remove this in https://www.drupal.org/node/2300677.
|
||||
if ($this->entity instanceof ConfigEntityInterface) {
|
||||
$this->markTestSkipped('DELETEing config entities is not yet supported.');
|
||||
|
@ -3574,4 +3578,23 @@ abstract class ResourceTestBase extends BrowserTestBase {
|
|||
return $expected_document;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the anonymous and authenticated roles have no permissions at all.
|
||||
*/
|
||||
protected function revokePermissions(): void {
|
||||
$user_role = Role::load(RoleInterface::ANONYMOUS_ID);
|
||||
foreach ($user_role->getPermissions() as $permission) {
|
||||
$user_role->revokePermission($permission);
|
||||
}
|
||||
$user_role->save();
|
||||
assert([] === $user_role->getPermissions(), 'The anonymous user role has no permissions at all.');
|
||||
|
||||
$user_role = Role::load(RoleInterface::AUTHENTICATED_ID);
|
||||
foreach ($user_role->getPermissions() as $permission) {
|
||||
$user_role->revokePermission($permission);
|
||||
}
|
||||
$user_role->save();
|
||||
assert([] === $user_role->getPermissions(), 'The authenticated user role has no permissions at all.');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -128,10 +128,10 @@ class UserTest extends ResourceTestBase {
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function testDeleteIndividual(): void {
|
||||
protected function doTestDeleteIndividual(): void {
|
||||
$this->config('user.settings')->set('cancel_method', 'user_cancel_delete')->save(TRUE);
|
||||
|
||||
parent::testDeleteIndividual();
|
||||
parent::doTestDeleteIndividual();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -195,6 +195,21 @@ class UserTest extends ResourceTestBase {
|
|||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getPatchDocument() {
|
||||
return [
|
||||
'data' => [
|
||||
'id' => $this->entity->uuid(),
|
||||
'type' => 'user--user',
|
||||
'attributes' => [
|
||||
'name' => 'Drama llama 2',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue