3 namespace Drupal\Tests\rest\Functional\EntityResource\Node;
5 use Drupal\node\Entity\Node;
6 use Drupal\node\Entity\NodeType;
7 use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
8 use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
9 use Drupal\user\Entity\User;
10 use GuzzleHttp\RequestOptions;
12 abstract class NodeResourceTestBase extends EntityResourceTestBase {
14 use BcTimestampNormalizerUnixTestTrait;
19 public static $modules = ['node', 'path'];
24 protected static $entityTypeId = 'node';
29 protected static $patchProtectedFieldNames = [
40 * @var \Drupal\node\NodeInterface
47 protected function setUpAuthorization($method) {
50 $this->grantPermissionsToTestedRole(['access content']);
53 $this->grantPermissionsToTestedRole(['access content', 'create camelids content']);
56 // Do not grant the 'create url aliases' permission to test the case
57 // when the path field is protected/not accessible, see
58 // \Drupal\Tests\rest\Functional\EntityResource\Term\TermResourceTestBase
59 // for a positive test.
60 $this->grantPermissionsToTestedRole(['access content', 'edit any camelids content']);
63 $this->grantPermissionsToTestedRole(['access content', 'delete any camelids content']);
71 protected function createEntity() {
72 if (!NodeType::load('camelids')) {
73 // Create a "Camelids" node type.
80 // Create a "Llama" node.
81 $node = Node::create(['type' => 'camelids']);
82 $node->setTitle('Llama')
83 ->setOwnerId(static::$auth ? $this->account->id() : 0)
85 ->setCreatedTime(123456789)
86 ->setChangedTime(123456789)
87 ->setRevisionCreationTime(123456789)
88 ->set('path', '/llama')
97 protected function getExpectedNormalizedEntity() {
98 $author = User::load($this->entity->getOwnerId());
104 ['value' => $this->entity->uuid()],
116 'target_id' => 'camelids',
117 'target_type' => 'node_type',
118 'target_uuid' => NodeType::load('camelids')->uuid(),
132 $this->formatExpectedTimestampItemValues(123456789),
135 $this->formatExpectedTimestampItemValues($this->entity->getChangedTime()),
147 'revision_timestamp' => [
148 $this->formatExpectedTimestampItemValues(123456789),
150 'revision_translation_affected' => [
155 'default_langcode' => [
162 'target_id' => (int) $author->id(),
163 'target_type' => 'user',
164 'target_uuid' => $author->uuid(),
165 'url' => base_path() . 'user/' . $author->id(),
170 'target_id' => (int) $author->id(),
171 'target_type' => 'user',
172 'target_uuid' => $author->uuid(),
173 'url' => base_path() . 'user/' . $author->id(),
176 'revision_log' => [],
190 protected function getNormalizedPostEntity() {
194 'target_id' => 'camelids',
199 'value' => 'Dramallama',
208 protected function getExpectedUnauthorizedAccessMessage($method) {
209 if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
210 return parent::getExpectedUnauthorizedAccessMessage($method);
213 if ($method === 'GET' || $method == 'PATCH' || $method == 'DELETE') {
214 return "The 'access content' permission is required.";
216 return parent::getExpectedUnauthorizedAccessMessage($method);
220 * Tests PATCHing a node's path with and without 'create url aliases'.
222 * For a positive test, see the similar test coverage for Term.
224 * @see \Drupal\Tests\rest\Functional\EntityResource\Term\TermResourceTestBase::testPatchPath()
226 public function testPatchPath() {
227 $this->initAuthentication();
228 $this->provisionEntityResource();
229 $this->setUpAuthorization('GET');
230 $this->setUpAuthorization('PATCH');
232 $url = $this->getEntityResourceUrl()->setOption('query', ['_format' => static::$format]);
234 // GET node's current normalization.
235 $response = $this->request('GET', $url, $this->getAuthenticationRequestOptions('GET'));
236 $normalization = $this->serializer->decode((string) $response->getBody(), static::$format);
238 // Change node's path alias.
239 $normalization['path'][0]['alias'] .= 's-rule-the-world';
241 // Create node PATCH request.
242 $request_options = [];
243 $request_options[RequestOptions::HEADERS]['Content-Type'] = static::$mimeType;
244 $request_options = array_merge_recursive($request_options, $this->getAuthenticationRequestOptions('PATCH'));
245 $request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
247 // PATCH request: 403 when creating URL aliases unauthorized. Before
248 // asserting the 403 response, assert that the stored path alias remains
250 $response = $this->request('PATCH', $url, $request_options);
251 $this->assertSame('/llama', $this->entityStorage->loadUnchanged($this->entity->id())->get('path')->alias);
252 $this->assertResourceErrorResponse(403, "Access denied on updating field 'path'.", $response);
254 // Grant permission to create URL aliases.
255 $this->grantPermissionsToTestedRole(['create url aliases']);
257 // Repeat PATCH request: 200.
258 $response = $this->request('PATCH', $url, $request_options);
259 $this->assertResourceResponse(200, FALSE, $response);
260 $updated_normalization = $this->serializer->decode((string) $response->getBody(), static::$format);
261 $this->assertSame($normalization['path'], $updated_normalization['path']);