|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) eZ Systems AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace EzSystems\EzPlatformRestBundle\Tests\Functional; |
| 10 | + |
| 11 | +use EzSystems\EzPlatformRestBundle\Tests\Functional\TestCase as RESTFunctionalTestCase; |
| 12 | +use Symfony\Component\HttpFoundation\Response; |
| 13 | + |
| 14 | +final class ImageVariationTest extends RESTFunctionalTestCase |
| 15 | +{ |
| 16 | + public function testCreateContent(): string |
| 17 | + { |
| 18 | + $string = $this->addTestSuffix(__FUNCTION__); |
| 19 | + $fileName = '1px.png'; |
| 20 | + $fileSize = 4718; |
| 21 | + $fileData = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=='; |
| 22 | + $restPrefixPath = '/api/ezp/v2'; |
| 23 | + $body = <<< XML |
| 24 | +<?xml version="1.0" encoding="UTF-8"?> |
| 25 | +<ContentCreate> |
| 26 | + <ContentType href="{$restPrefixPath}/content/types/5" /> |
| 27 | + <mainLanguageCode>eng-GB</mainLanguageCode> |
| 28 | + <LocationCreate> |
| 29 | + <ParentLocation href="{$restPrefixPath}/content/locations/1/2" /> |
| 30 | + <priority>0</priority> |
| 31 | + <hidden>false</hidden> |
| 32 | + <sortField>PATH</sortField> |
| 33 | + <sortOrder>ASC</sortOrder> |
| 34 | + </LocationCreate> |
| 35 | + <Section href="{$restPrefixPath}/content/sections/3" /> |
| 36 | + <alwaysAvailable>true</alwaysAvailable> |
| 37 | + <remoteId>{$string}</remoteId> |
| 38 | + <User href="{$restPrefixPath}/user/users/14" /> |
| 39 | + <modificationDate>2012-09-30T12:30:00</modificationDate> |
| 40 | + <fields> |
| 41 | + <field> |
| 42 | + <fieldDefinitionIdentifier>name</fieldDefinitionIdentifier> |
| 43 | + <languageCode>eng-GB</languageCode> |
| 44 | + <fieldValue>{$string}</fieldValue> |
| 45 | + </field> |
| 46 | + <field> |
| 47 | + <fieldDefinitionIdentifier>image</fieldDefinitionIdentifier> |
| 48 | + <languageCode>eng-GB</languageCode> |
| 49 | + <fieldValue> |
| 50 | + <value key="fileName">{$fileName}</value> |
| 51 | + <value key="fileSize">{$fileSize}</value> |
| 52 | + <value key="data">{$fileData}</value> |
| 53 | + </fieldValue> |
| 54 | + </field> |
| 55 | + </fields> |
| 56 | +</ContentCreate> |
| 57 | +XML; |
| 58 | + $request = $this->createHttpRequest( |
| 59 | + 'POST', |
| 60 | + $restPrefixPath . '/content/objects', |
| 61 | + 'ContentCreate+xml', |
| 62 | + 'ContentInfo+json', |
| 63 | + $body |
| 64 | + ); |
| 65 | + |
| 66 | + $response = $this->sendHttpRequest($request); |
| 67 | + |
| 68 | + $this->assertHttpResponseCodeEquals($response, Response::HTTP_CREATED); |
| 69 | + $this->assertHttpResponseHasHeader($response, 'Location'); |
| 70 | + $this->assertHttpResponseHasHeader($response, 'content-type', 'application/vnd.ez.api.ContentInfo+json'); |
| 71 | + |
| 72 | + $href = $response->getHeader('Location')[0]; |
| 73 | + $this->addCreatedElement($href); |
| 74 | + |
| 75 | + $contentInfo = json_decode((string)$response->getBody(), true, 512, JSON_THROW_ON_ERROR); |
| 76 | + |
| 77 | + return sprintf( |
| 78 | + '%s/%d', $contentInfo['Content']['Versions']['_href'], $contentInfo['Content']['currentVersionNo'] |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @depends testCreateContent |
| 84 | + */ |
| 85 | + public function testPublishContent(string $restContentHref): string |
| 86 | + { |
| 87 | + $response = $this->sendHttpRequest( |
| 88 | + $this->createHttpRequest('PUBLISH', $restContentHref) |
| 89 | + ); |
| 90 | + $this->assertHttpResponseCodeEquals($response, Response::HTTP_NO_CONTENT); |
| 91 | + |
| 92 | + return $restContentHref; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @depends testPublishContent |
| 97 | + */ |
| 98 | + public function testGetImageVariation(string $restVersionHref): void |
| 99 | + { |
| 100 | + $imageResponse = $this->sendHttpRequest( |
| 101 | + $this->createHttpRequest( |
| 102 | + 'GET', |
| 103 | + $restVersionHref, |
| 104 | + '', |
| 105 | + 'Version+json' |
| 106 | + ) |
| 107 | + ); |
| 108 | + |
| 109 | + $jsonResponse = json_decode((string)$imageResponse->getBody(), true, 512, JSON_THROW_ON_ERROR); |
| 110 | + $imageField = $jsonResponse['Version']['Fields']['field'][2]; |
| 111 | + |
| 112 | + self::assertArrayHasKey('variations', $imageField['fieldValue']); |
| 113 | + |
| 114 | + $variationUrl = $imageField['fieldValue']['variations']['medium']['href']; |
| 115 | + |
| 116 | + $variationResponse = $this->sendHttpRequest( |
| 117 | + $this->createHttpRequest( |
| 118 | + 'GET', |
| 119 | + $variationUrl, |
| 120 | + '', |
| 121 | + 'Version+json' |
| 122 | + ) |
| 123 | + ); |
| 124 | + self::assertHttpResponseCodeEquals($variationResponse, Response::HTTP_OK); |
| 125 | + self::assertHttpResponseHasHeader( |
| 126 | + $variationResponse, |
| 127 | + 'content-type', |
| 128 | + 'application/vnd.ez.api.ContentImageVariation+json' |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
0 commit comments