3 namespace Drupal\Tests\taxonomy\Functional\Rest;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\taxonomy\Entity\Term;
7 use Drupal\taxonomy\Entity\Vocabulary;
8 use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
9 use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
10 use GuzzleHttp\RequestOptions;
12 abstract class TermResourceTestBase extends EntityResourceTestBase {
14 use BcTimestampNormalizerUnixTestTrait;
19 public static $modules = ['taxonomy', 'path'];
24 protected static $entityTypeId = 'taxonomy_term';
29 protected static $patchProtectedFieldNames = [
34 * @var \Drupal\taxonomy\TermInterface
41 protected function setUpAuthorization($method) {
44 $this->grantPermissionsToTestedRole(['access content']);
48 $this->grantPermissionsToTestedRole(['create terms in camelids']);
52 // Grant the 'create url aliases' permission to test the case when
53 // the path field is accessible, see
54 // \Drupal\Tests\rest\Functional\EntityResource\Node\NodeResourceTestBase
55 // for a negative test.
56 $this->grantPermissionsToTestedRole(['edit terms in camelids', 'create url aliases']);
60 $this->grantPermissionsToTestedRole(['delete terms in camelids']);
69 protected function createEntity() {
70 $vocabulary = Vocabulary::load('camelids');
72 // Create a "Camelids" vocabulary.
73 $vocabulary = Vocabulary::create([
80 // Create a "Llama" taxonomy term.
81 $term = Term::create(['vid' => $vocabulary->id()])
83 ->setDescription("It is a little known fact that llamas cannot count higher than seven.")
84 ->setChangedTime(123456789)
85 ->set('path', '/llama');
94 protected function getExpectedNormalizedEntity() {
95 // We test with multiple parent terms, and combinations thereof.
96 // @see ::createEntity()
98 // @see ::testGetTermWithParent()
99 // @see ::providerTestGetTermWithParent()
100 $parent_term_ids = [];
101 for ($i = 0; $i < $this->entity->get('parent')->count(); $i++) {
102 $parent_term_ids[$i] = (int) $this->entity->get('parent')[$i]->target_id;
105 $expected_parent_normalization = FALSE;
106 switch ($parent_term_ids) {
108 $expected_parent_normalization = [
115 $expected_parent_normalization = [
118 'target_type' => 'taxonomy_term',
119 'target_uuid' => Term::load(2)->uuid(),
120 'url' => base_path() . 'taxonomy/term/2',
125 $expected_parent_normalization = [
131 'target_type' => 'taxonomy_term',
132 'target_uuid' => Term::load(2)->uuid(),
133 'url' => base_path() . 'taxonomy/term/2',
138 $expected_parent_normalization = [
141 'target_type' => 'taxonomy_term',
142 'target_uuid' => Term::load(3)->uuid(),
143 'url' => base_path() . 'taxonomy/term/3',
147 'target_type' => 'taxonomy_term',
148 'target_uuid' => Term::load(2)->uuid(),
149 'url' => base_path() . 'taxonomy/term/2',
160 ['value' => $this->entity->uuid()],
164 'target_id' => 'camelids',
165 'target_type' => 'taxonomy_vocabulary',
166 'target_uuid' => Vocabulary::load('camelids')->uuid(),
170 ['value' => 'Llama'],
174 'value' => 'It is a little known fact that llamas cannot count higher than seven.',
176 'processed' => "<p>It is a little known fact that llamas cannot count higher than seven.</p>\n",
179 'parent' => $expected_parent_normalization,
189 $this->formatExpectedTimestampItemValues($this->entity->getChangedTime()),
191 'default_langcode' => [
214 protected function getNormalizedPostEntity() {
218 'target_id' => 'camelids',
223 'value' => 'Dramallama',
228 'value' => 'Dramallamas are the coolest camelids.',
238 protected function getExpectedUnauthorizedAccessMessage($method) {
239 if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
240 return parent::getExpectedUnauthorizedAccessMessage($method);
245 return "The 'access content' permission is required and the taxonomy term must be published.";
247 return "The following permissions are required: 'create terms in camelids' OR 'administer taxonomy'.";
249 return "The following permissions are required: 'edit terms in camelids' OR 'administer taxonomy'.";
251 return "The following permissions are required: 'delete terms in camelids' OR 'administer taxonomy'.";
253 return parent::getExpectedUnauthorizedAccessMessage($method);
258 * Tests PATCHing a term's path.
260 * For a negative test, see the similar test coverage for Node.
262 * @see \Drupal\Tests\rest\Functional\EntityResource\Node\NodeResourceTestBase::testPatchPath()
264 public function testPatchPath() {
265 $this->initAuthentication();
266 $this->provisionEntityResource();
267 $this->setUpAuthorization('GET');
268 $this->setUpAuthorization('PATCH');
270 $url = $this->getEntityResourceUrl()->setOption('query', ['_format' => static::$format]);
272 // GET term's current normalization.
273 $response = $this->request('GET', $url, $this->getAuthenticationRequestOptions('GET'));
274 $normalization = $this->serializer->decode((string) $response->getBody(), static::$format);
276 // Change term's path alias.
277 $normalization['path'][0]['alias'] .= 's-rule-the-world';
279 // Create term PATCH request.
280 $request_options = [];
281 $request_options[RequestOptions::HEADERS]['Content-Type'] = static::$mimeType;
282 $request_options = array_merge_recursive($request_options, $this->getAuthenticationRequestOptions('PATCH'));
283 $request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
285 // PATCH request: 200.
286 $response = $this->request('PATCH', $url, $request_options);
287 $this->assertResourceResponse(200, FALSE, $response);
288 $updated_normalization = $this->serializer->decode((string) $response->getBody(), static::$format);
289 $this->assertSame($normalization['path'], $updated_normalization['path']);
295 protected function getExpectedCacheTags() {
296 return Cache::mergeTags(parent::getExpectedCacheTags(), ['config:filter.format.plain_text', 'config:filter.settings']);
302 protected function getExpectedCacheContexts() {
303 return Cache::mergeContexts(['url.site'], $this->container->getParameter('renderer.config')['required_cache_contexts']);
307 * Tests GETting a term with a parent term other than the default <root> (0).
309 * @see ::getExpectedNormalizedEntity()
311 * @dataProvider providerTestGetTermWithParent
313 public function testGetTermWithParent(array $parent_term_ids) {
314 // Create all possible parent terms.
315 Term::create(['vid' => Vocabulary::load('camelids')->id()])
318 Term::create(['vid' => Vocabulary::load('camelids')->id()])
322 // Modify the entity under test to use the provided parent terms.
323 $this->entity->set('parent', $parent_term_ids)->save();
325 $this->initAuthentication();
326 $url = $this->getEntityResourceUrl();
327 $url->setOption('query', ['_format' => static::$format]);
328 $request_options = $this->getAuthenticationRequestOptions('GET');
329 $this->provisionEntityResource();
330 $this->setUpAuthorization('GET');
331 $response = $this->request('GET', $url, $request_options);
332 $expected = $this->getExpectedNormalizedEntity();
333 static::recursiveKSort($expected);
334 $actual = $this->serializer->decode((string) $response->getBody(), static::$format);
335 static::recursiveKSort($actual);
336 $this->assertSame($expected, $actual);
339 public function providerTestGetTermWithParent() {
341 'root parent: [0] (= no parent)' => [
344 'non-root parent: [2]' => [
347 'multiple parents: [0,2] (root + non-root parent)' => [
350 'multiple parents: [3,2] (both non-root parents)' => [
359 protected function getExpectedUnauthorizedAccessCacheability() {
360 // @see \Drupal\taxonomy\TermAccessControlHandler::checkAccess()
361 return parent::getExpectedUnauthorizedAccessCacheability()
362 ->addCacheTags(['taxonomy_term:1']);