forked from alxp/islandora
-
Notifications
You must be signed in to change notification settings - Fork 118
/
LinkHeaderTest.php
206 lines (183 loc) · 7.9 KB
/
LinkHeaderTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
namespace Drupal\Tests\islandora\Functional;
use Drupal\Core\Url;
/**
* Tests link headers get added to GET requests.
*
* @group islandora
*/
class LinkHeaderTest extends IslandoraFunctionalTestBase {
/**
* Node that has node and term entity reference fields.
*
* @var \Drupal\node\NodeInterface
*/
protected $referencer;
/**
* Another similar node, to be referenced by referencer.
*
* @var \Drupal\node\NodeInterface
*/
protected $referenced;
/**
* Media to belong to the referencer.
*
* @var \Drupal\media\MediaInterface
*/
protected $media;
/**
* File to belong to the media.
*
* @var \Drupal\file\FileInterface
*/
protected $file;
/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();
$account = $this->createUserAndLogin();
$this->createImageTag();
$this->createPreservationMasterTag();
// Node to be referenced via member of.
$this->referenced = $this->container->get('entity_type.manager')->getStorage('node')->create([
'type' => $this->testType->id(),
'title' => 'Referenced',
]);
$this->referenced->save();
// Node that is member of something, with an Image tag.
$this->referencer = $this->container->get('entity_type.manager')->getStorage('node')->create([
'type' => $this->testType->id(),
'title' => 'Referencer',
'field_member_of' => [$this->referenced->id()],
'field_model' => [$this->imageTerm->id()],
]);
$this->referencer->save();
list($this->file, $this->media) = $this->makeMediaAndFile($account);
$this->media->set('field_media_of', $this->referencer);
$this->media->set('field_media_use', $this->preservationMasterTerm);
$this->media->save();
}
/**
* @covers \Drupal\islandora\EventSubscriber\NodeLinkHeaderSubscriber::onResponse
*/
public function testNodeLinkHeaders() {
// Visit the referenced node, there should not be a related header since
// its entity reference field is empty.
$this->drupalGet('node/' . $this->referenced->id());
$this->assertTrue(
$this->doesNotHaveLinkHeader('related'),
"Node that has empty entity reference field must not return link header."
);
// Visit the referencer. It should return a rel="related" link header
// for referenced node and the referencing media, plus one for the tag.
$this->drupalGet('node/' . $this->referencer->id());
$this->assertTrue(
$this->validateLinkHeaderWithEntity('related', $this->referenced, 'Member Of') == 1,
"Malformed related node link header"
);
$this->assertTrue(
$this->validateLinkHeaderWithEntity('related', $this->media, $this->preservationMasterTerm->label()) == 1,
"Malformed related media link header"
);
$this->assertTrue(
$this->validateLinkHeaderWithUrl('tag', $this->imageTerm->get('field_external_uri')->first()->getValue()['uri'], $this->imageTerm->label()) == 1,
"Malformed tag link header"
);
// Check for links to REST endpoints for metadata.
$entity_url = $this->referencer->toUrl('canonical', ['absolute' => TRUE])
->toString();
$this->assertTrue(
$this->validateLinkHeaderWithUrl('alternate', "$entity_url?_format=json", NULL, 'application/json') == 1,
"Node must have link header pointing to json REST endpoint."
);
$this->assertTrue(
$this->validateLinkHeaderWithUrl('alternate', "$entity_url?_format=jsonld", NULL, 'application/ld+json') == 1,
"Node must have link header pointing to jsonld REST endpoint."
);
$this->assertTrue(
$this->validateLinkHeaderWithUrl('alternate', "$entity_url?_format=xml", NULL, 'application/xml') == 0,
"Node must not have link header pointing to disabled xml REST endpoint."
);
// Check that the current representation is not advertised when visitng
// a REST endpoint (e.g. the json link header doesn't appear when you're
// visiting the ?_format=json endpoint).
$this->drupalGet('node/' . $this->referencer->id(), ['query' => ['_format' => 'json']]);
$this->assertTrue(
$this->validateLinkHeaderWithUrl('alternate', "$entity_url?_format=json", NULL, 'application/json') == 0,
"Node must not have link header pointing to json REST endpoint when vising the json REST endpoint."
);
$this->assertTrue(
$this->validateLinkHeaderWithUrl('alternate', "$entity_url?_format=jsonld", NULL, 'application/ld+json') == 1,
"Node must have link header pointing to jsonld REST endpoint when visiting the json REST endpoint."
);
}
/**
* @covers \Drupal\islandora\EventSubscriber\MediaLinkHeaderSubscriber
*/
public function testMediaLinkHeaders() {
// Get the file to check its url in the response headers.
$file_url = $this->file->createFileUrl(FALSE);
$rest_url = Url::fromRoute('islandora.media_source_update', ['media' => $this->media->id()])
->setAbsolute()
->toString();
$media_url = $this->media->toUrl('canonical', ['absolute' => TRUE])->toString();
// Perform a GET request as anonymous.
$this->drupalGet($media_url, [], ['Cache-Control' => 'no-cache']);
// Check link headers.
$this->assertTrue(
$this->validateLinkHeaderWithUrl('describes', $file_url, '', 'text/plain') == 1,
"Malformed 'describes' link header, expecting $file_url"
);
$this->assertTrue(
$this->validateLinkHeaderWithUrl('edit-media', $rest_url, '', '') == 0,
"Anonymous should not be able to see the edit-media link"
);
$this->assertTrue(
$this->validateLinkHeaderWithUrl('tag', $this->preservationMasterTerm->get('field_external_uri')->first()->getValue()['uri'], $this->preservationMasterTerm->label()) == 1,
"Malformed tag link header"
);
$this->assertTrue(
$this->validateLinkHeaderWithUrl('alternate', $media_url . "?_format=json", NULL, 'application/json') == 1,
"Media must have link header pointing to json REST endpoint."
);
$this->assertTrue(
$this->validateLinkHeaderWithUrl('alternate', $media_url . "?_format=jsonld", NULL, 'application/ld+json') == 1,
"Media must have link header pointing to jsonld REST endpoint."
);
$this->assertTrue(
$this->validateLinkHeaderWithUrl('alternate', $media_url . "?_format=xml", NULL, 'application/xml') == 0,
"Media must not have link header pointing to disabled xml REST endpoint."
);
// Create a test user with edit media permissions.
$account = $this->drupalCreateUser(['update media']);
$this->drupalLogin($account);
// Perform a GET request with update media permissions.
$this->drupalGet($media_url, [], ['Cache-Control' => 'no-cache']);
// Check link headers again, the edit-media link header should be present.
$this->assertTrue(
$this->validateLinkHeaderWithUrl('describes', $file_url, '', 'text/plain') == 1,
"Malformed 'describes' link header, expecting $file_url"
);
$this->assertTrue(
$this->validateLinkHeaderWithUrl('edit-media', $rest_url, '', '') == 1,
"Malformed 'edit-media' link, expecting $rest_url"
);
$this->assertTrue(
$this->validateLinkHeaderWithUrl('tag', $this->preservationMasterTerm->get('field_external_uri')->first()->getValue()['uri'], $this->preservationMasterTerm->label()) == 1,
"Malformed tag link header"
);
$this->assertTrue(
$this->validateLinkHeaderWithUrl('alternate', $media_url . "?_format=json", NULL, 'application/json') == 1,
"Media must have link header pointing to json REST endpoint."
);
$this->assertTrue(
$this->validateLinkHeaderWithUrl('alternate', $media_url . "?_format=jsonld", NULL, 'application/ld+json') == 1,
"Media must have link header pointing to jsonld REST endpoint."
);
$this->assertTrue(
$this->validateLinkHeaderWithUrl('alternate', $media_url . "?_format=xml", NULL, 'application/xml') == 0,
"Media must not have link header pointing to disabled xml REST endpoint."
);
}
}