3 namespace Drupal\Tests\entity_test\Functional\Rest;
5 use Drupal\entity_test\Entity\EntityTest;
6 use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
7 use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
8 use Drupal\Tests\Traits\ExpectDeprecationTrait;
9 use Drupal\user\Entity\User;
11 abstract class EntityTestResourceTestBase extends EntityResourceTestBase {
13 use BcTimestampNormalizerUnixTestTrait;
14 use ExpectDeprecationTrait;
19 public static $modules = ['entity_test'];
24 protected static $entityTypeId = 'entity_test';
29 protected static $patchProtectedFieldNames = [];
32 * @var \Drupal\entity_test\Entity\EntityTest
39 protected function setUpAuthorization($method) {
42 $this->grantPermissionsToTestedRole(['view test entity']);
45 $this->grantPermissionsToTestedRole(['create entity_test entity_test_with_bundle entities']);
49 $this->grantPermissionsToTestedRole(['administer entity_test content']);
57 protected function createEntity() {
58 // Set flag so that internal field 'internal_string_field' is created.
59 // @see entity_test_entity_base_field_info()
60 $this->container->get('state')->set('entity_test.internal_field', TRUE);
61 \Drupal::entityDefinitionUpdateManager()->applyUpdates();
63 $entity_test = EntityTest::create([
65 'type' => 'entity_test',
66 // Set a value for the internal field to confirm that it will not be
67 // returned in normalization.
68 // @see entity_test_entity_base_field_info().
69 'internal_string_field' => [
70 'value' => 'This value shall not be internal!',
73 $entity_test->setOwnerId(0);
82 protected function getExpectedNormalizedEntity() {
83 $author = User::load(0);
87 'value' => $this->entity->uuid(),
102 'value' => 'entity_test',
111 $this->formatExpectedTimestampItemValues((int) $this->entity->get('created')->value),
115 'target_id' => (int) $author->id(),
116 'target_type' => 'user',
117 'target_uuid' => $author->uuid(),
118 'url' => $author->toUrl()->toString(),
121 'field_test_text' => [],
124 return $normalization;
130 protected function getNormalizedPostEntity() {
134 'value' => 'entity_test',
139 'value' => 'Dramallama',
148 protected function getExpectedUnauthorizedAccessMessage($method) {
149 if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
150 return parent::getExpectedUnauthorizedAccessMessage($method);
155 return "The 'view test entity' permission is required.";
157 return "The following permissions are required: 'administer entity_test content' OR 'administer entity_test_with_bundle content' OR 'create entity_test entity_test_with_bundle entities'.";
159 return parent::getExpectedUnauthorizedAccessMessage($method);