Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / taxonomy / tests / src / Functional / Hal / TermHalJsonAnonTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional\Hal;
4
5 use Drupal\taxonomy\Entity\Term;
6 use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
7 use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
8 use Drupal\Tests\taxonomy\Functional\Rest\TermResourceTestBase;
9
10 /**
11  * @group hal
12  */
13 class TermHalJsonAnonTest extends TermResourceTestBase {
14
15   use HalEntityNormalizationTrait;
16   use AnonResourceTestTrait;
17
18   /**
19    * {@inheritdoc}
20    */
21   public static $modules = ['hal'];
22
23   /**
24    * {@inheritdoc}
25    */
26   protected static $format = 'hal_json';
27
28   /**
29    * {@inheritdoc}
30    */
31   protected static $mimeType = 'application/hal+json';
32
33   /**
34    * {@inheritdoc}
35    */
36   protected function getExpectedNormalizedEntity() {
37     $default_normalization = parent::getExpectedNormalizedEntity();
38
39     $normalization = $this->applyHalFieldNormalization($default_normalization);
40
41     // We test with multiple parent terms, and combinations thereof.
42     // @see ::createEntity()
43     // @see ::testGet()
44     // @see ::testGetTermWithParent()
45     // @see ::providerTestGetTermWithParent()
46     // @see ::testGetTermWithParent()
47     $parent_term_ids = [];
48     for ($i = 0; $i < $this->entity->get('parent')->count(); $i++) {
49       $parent_term_ids[$i] = (int) $this->entity->get('parent')[$i]->target_id;
50     }
51
52     $expected_parent_normalization_links = FALSE;
53     $expected_parent_normalization_embedded = FALSE;
54     switch ($parent_term_ids) {
55       case [0]:
56         $expected_parent_normalization_links = [
57           NULL,
58         ];
59         $expected_parent_normalization_embedded = [
60           NULL,
61         ];
62         break;
63       case [2]:
64         $expected_parent_normalization_links = [
65           [
66           'href' => $this->baseUrl . '/taxonomy/term/2?_format=hal_json',
67           ],
68         ];
69         $expected_parent_normalization_embedded = [
70           [
71             '_links' => [
72               'self' => [
73                 'href' => $this->baseUrl . '/taxonomy/term/2?_format=hal_json',
74               ],
75               'type' => [
76                 'href' => $this->baseUrl . '/rest/type/taxonomy_term/camelids',
77               ],
78             ],
79             'uuid' => [
80               ['value' => Term::load(2)->uuid()],
81             ],
82           ],
83         ];
84         break;
85       case [0, 2]:
86         $expected_parent_normalization_links = [
87           NULL,
88           [
89             'href' => $this->baseUrl . '/taxonomy/term/2?_format=hal_json',
90           ],
91         ];
92         $expected_parent_normalization_embedded = [
93           NULL,
94           [
95             '_links' => [
96               'self' => [
97                 'href' => $this->baseUrl . '/taxonomy/term/2?_format=hal_json',
98               ],
99               'type' => [
100                 'href' => $this->baseUrl . '/rest/type/taxonomy_term/camelids',
101               ],
102             ],
103             'uuid' => [
104               ['value' => Term::load(2)->uuid()],
105             ],
106           ],
107         ];
108         break;
109       case [3, 2]:
110         $expected_parent_normalization_links = [
111           [
112             'href' => $this->baseUrl . '/taxonomy/term/3?_format=hal_json',
113           ],
114           [
115             'href' => $this->baseUrl . '/taxonomy/term/2?_format=hal_json',
116           ],
117         ];
118         $expected_parent_normalization_embedded = [
119           [
120             '_links' => [
121               'self' => [
122                 'href' => $this->baseUrl . '/taxonomy/term/3?_format=hal_json',
123               ],
124               'type' => [
125                 'href' => $this->baseUrl . '/rest/type/taxonomy_term/camelids',
126               ],
127             ],
128             'uuid' => [
129               ['value' => Term::load(3)->uuid()],
130             ],
131           ],
132           [
133             '_links' => [
134               'self' => [
135                 'href' => $this->baseUrl . '/taxonomy/term/2?_format=hal_json',
136               ],
137               'type' => [
138                 'href' => $this->baseUrl . '/rest/type/taxonomy_term/camelids',
139               ],
140             ],
141             'uuid' => [
142               ['value' => Term::load(2)->uuid()],
143             ],
144           ],
145         ];
146         break;
147     }
148
149     return $normalization + [
150       '_links' => [
151         'self' => [
152           'href' => $this->baseUrl . '/llama?_format=hal_json',
153         ],
154         'type' => [
155           'href' => $this->baseUrl . '/rest/type/taxonomy_term/camelids',
156         ],
157         $this->baseUrl . '/rest/relation/taxonomy_term/camelids/parent' => $expected_parent_normalization_links,
158       ],
159       '_embedded' => [
160         $this->baseUrl . '/rest/relation/taxonomy_term/camelids/parent' => $expected_parent_normalization_embedded,
161       ],
162     ];
163   }
164
165   /**
166    * {@inheritdoc}
167    */
168   protected function getNormalizedPostEntity() {
169     return parent::getNormalizedPostEntity() + [
170       '_links' => [
171         'type' => [
172           'href' => $this->baseUrl . '/rest/type/taxonomy_term/camelids',
173         ],
174       ],
175     ];
176   }
177
178 }