public function ConsumerImageSylesFunctionalTest::testRead in Consumer Image Styles 4.x
Same name and namespace in other branches
- 8.3 tests/src/Functional/ConsumerImageSylesFunctionalTest.php \Drupal\Tests\consumer_image_styles\Functional\ConsumerImageSylesFunctionalTest::testRead()
- 8 tests/src/Functional/ConsumerImageSylesFunctionalTest.php \Drupal\Tests\consumer_image_styles\Functional\ConsumerImageSylesFunctionalTest::testRead()
- 8.2 tests/src/Functional/ConsumerImageSylesFunctionalTest.php \Drupal\Tests\consumer_image_styles\Functional\ConsumerImageSylesFunctionalTest::testRead()
Test the GET method.
File
- tests/
src/ Functional/ ConsumerImageSylesFunctionalTest.php, line 144
Class
- ConsumerImageSylesFunctionalTest
- @group consumer_image_styles
Namespace
Drupal\Tests\consumer_image_styles\FunctionalCode
public function testRead() {
$this
->createDefaultContent(1);
// 1. Check the request for the image directly.
$url = Url::fromRoute('jsonapi.file--file.individual', [
'entity' => $this->files[0]
->uuid(),
]);
$request_options = [
RequestOptions::HEADERS => [
'X-Consumer-ID' => $this->consumer
->uuid(),
],
];
$response = $this
->request('GET', $url, $request_options);
$output = Json::decode($response
->getBody());
$this
->assertEquals(200, $response
->getStatusCode());
$links = $output['data']['links'];
$derivatives = array_filter($links, function ($link) {
$rels = isset($link['meta']['rel']) ? $link['meta']['rel'] : [];
return !empty($rels) && in_array(ImageStylesProvider::DERIVATIVE_LINK_REL, $rels);
});
$this
->assertNotEmpty($derivatives);
$this
->assertStringContainsString('/files/styles/foo/public/', $derivatives['foo']['href']);
$this
->assertStringContainsString('/files/styles/bar/public/', $derivatives['bar']['href']);
$this
->assertStringContainsString('itok=', $derivatives['foo']['href']);
$this
->assertStringContainsString('itok=', $derivatives['bar']['href']);
// 2. Check the request via the node.
$url = Url::fromRoute(sprintf('jsonapi.node--%s.individual', $this->contentType
->id()), [
'entity' => $this->nodes[0]
->uuid(),
]);
$request_options = [
RequestOptions::QUERY => [
'include' => $this->imageFieldName,
],
RequestOptions::HEADERS => [
'X-Consumer-ID' => $this->consumer
->uuid(),
],
];
$response = $this
->request('GET', $url, $request_options);
$output = Json::decode($response
->getBody());
$this
->assertEquals(200, $response
->getStatusCode());
$links = $output['included'][0]['links'];
$derivatives = array_filter($links, function ($link) {
$rels = isset($link['meta']['rel']) ? $link['meta']['rel'] : [];
return !empty($rels) && in_array(ImageStylesProvider::DERIVATIVE_LINK_REL, $rels);
});
$this
->assertStringContainsString(file_create_url('public://styles/foo/public/'), $derivatives['foo']['href']);
$this
->assertStringContainsString(file_create_url('public://styles/bar/public/'), $derivatives['bar']['href']);
$this
->assertStringContainsString('itok=', $derivatives['foo']['href']);
$this
->assertStringContainsString('itok=', $derivatives['bar']['href']);
// 3. Check the request for the image directly without consumer.
$url = Url::fromRoute('jsonapi.file--file.individual', [
'entity' => $this->files[0]
->uuid(),
]);
$response = $this
->request('GET', $url, []);
$output = Json::decode($response
->getBody());
$this
->assertEquals(200, $response
->getStatusCode());
$links = $output['data']['links'];
$derivatives = array_filter($links, function ($link) {
$rels = isset($link['meta']['rel']) ? $link['meta']['rel'] : [];
return !empty($rels) && in_array(ImageStylesProvider::DERIVATIVE_LINK_REL, $rels);
});
$this
->assertEmpty(empty($derivatives));
// 4. Apply the field enhancer and check the image field.
$url = Url::fromRoute(sprintf('jsonapi.node--%s.individual', $this->contentType
->id()), [
'entity' => $this->nodes[0]
->uuid(),
]);
$request_options = [
RequestOptions::HEADERS => [
'X-Consumer-ID' => $this->consumer
->uuid(),
],
];
$response = $this
->request('GET', $url, $request_options);
$output = Json::decode($response
->getBody());
$this
->assertEquals(200, $response
->getStatusCode());
$links = NestedArray::getValue($output, [
'data',
'relationships',
$this->imageFieldName,
'data',
'meta',
'imageDerivatives',
'links',
]);
$derivatives = array_filter($links, function ($link) {
return ImageStylesProvider::DERIVATIVE_LINK_REL === ($link['meta']['rel'] ?? '');
});
$this
->assertStringContainsString(file_create_url('public://styles/foo/public/'), $derivatives['foo']['href']);
$this
->assertStringContainsString(ImageStylesProvider::DERIVATIVE_LINK_REL, $derivatives['foo']['meta']['rel']);
$this
->assertTrue(empty($derivatives['bar']));
$this
->assertStringContainsString('itok=', $derivatives['foo']['href']);
}