Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-6239: Added Content Type for Image Variation #105

Merged
merged 3 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ImageVariation extends ValueObjectVisitor
*/
public function visit(Visitor $visitor, Generator $generator, $data)
{
$visitor->setHeader('Content-Type', $generator->getMediaType('ContentImageVariation'));
$generator->startObjectElement('ContentImageVariation');
$this->visitImageVariationAttributes($visitor, $generator, $data);
$generator->endObjectElement('ContentImageVariation');
Expand Down
131 changes: 131 additions & 0 deletions tests/bundle/Functional/ImageVariationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformRestBundle\Tests\Functional;

use EzSystems\EzPlatformRestBundle\Tests\Functional\TestCase as RESTFunctionalTestCase;
use Symfony\Component\HttpFoundation\Response;

final class ImageVariationTest extends RESTFunctionalTestCase
{
public function testCreateContent(): string
{
$string = $this->addTestSuffix(__FUNCTION__);
$fileName = '1px.png';
$fileSize = 4718;
$fileData = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==';
$restPrefixPath = '/api/ezp/v2';
$body = <<< XML
<?xml version="1.0" encoding="UTF-8"?>
<ContentCreate>
<ContentType href="{$restPrefixPath}/content/types/5" />
<mainLanguageCode>eng-GB</mainLanguageCode>
<LocationCreate>
<ParentLocation href="{$restPrefixPath}/content/locations/1/2" />
<priority>0</priority>
<hidden>false</hidden>
<sortField>PATH</sortField>
<sortOrder>ASC</sortOrder>
</LocationCreate>
<Section href="{$restPrefixPath}/content/sections/3" />
<alwaysAvailable>true</alwaysAvailable>
<remoteId>{$string}</remoteId>
<User href="{$restPrefixPath}/user/users/14" />
<modificationDate>2012-09-30T12:30:00</modificationDate>
<fields>
<field>
<fieldDefinitionIdentifier>name</fieldDefinitionIdentifier>
<languageCode>eng-GB</languageCode>
<fieldValue>{$string}</fieldValue>
</field>
<field>
<fieldDefinitionIdentifier>image</fieldDefinitionIdentifier>
<languageCode>eng-GB</languageCode>
<fieldValue>
<value key="fileName">{$fileName}</value>
<value key="fileSize">{$fileSize}</value>
<value key="data">{$fileData}</value>
</fieldValue>
</field>
</fields>
</ContentCreate>
XML;
$request = $this->createHttpRequest(
'POST',
$restPrefixPath . '/content/objects',
'ContentCreate+xml',
'ContentInfo+json',
$body
);

$response = $this->sendHttpRequest($request);

$this->assertHttpResponseCodeEquals($response, Response::HTTP_CREATED);
$this->assertHttpResponseHasHeader($response, 'Location');
$this->assertHttpResponseHasHeader($response, 'content-type', 'application/vnd.ez.api.ContentInfo+json');

$href = $response->getHeader('Location')[0];
$this->addCreatedElement($href);

$contentInfo = json_decode((string)$response->getBody(), true, 512, JSON_THROW_ON_ERROR);

return sprintf(
'%s/%d', $contentInfo['Content']['Versions']['_href'], $contentInfo['Content']['currentVersionNo']
);
}

/**
* @depends testCreateContent
*/
public function testPublishContent(string $restContentHref): string
{
$response = $this->sendHttpRequest(
$this->createHttpRequest('PUBLISH', $restContentHref)
);
$this->assertHttpResponseCodeEquals($response, Response::HTTP_NO_CONTENT);

return $restContentHref;
}

/**
* @depends testPublishContent
*/
public function testGetImageVariation(string $restVersionHref): void
{
$imageResponse = $this->sendHttpRequest(
$this->createHttpRequest(
'GET',
$restVersionHref,
'',
'Version+json'
)
);

$jsonResponse = json_decode((string)$imageResponse->getBody(), true, 512, JSON_THROW_ON_ERROR);
$imageField = $jsonResponse['Version']['Fields']['field'][2];

self::assertArrayHasKey('variations', $imageField['fieldValue']);

$variationUrl = $imageField['fieldValue']['variations']['medium']['href'];

$variationResponse = $this->sendHttpRequest(
$this->createHttpRequest(
'GET',
$variationUrl,
'',
'Version+json'
)
);
self::assertHttpResponseCodeEquals($variationResponse, Response::HTTP_OK);
self::assertHttpResponseHasHeader(
$variationResponse,
'content-type',
'application/vnd.ez.api.ContentImageVariation+json'
);
}
}
2 changes: 1 addition & 1 deletion tests/bundle/Functional/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected function setUp(): void
{
parent::setUp();

$this->httpHost = getenv('EZP_TEST_REST_HOST') ?: 'localhost';
$this->httpHost = getenv('EZP_TEST_REST_HOST') ?: '127.0.0.1:8000';
kisztof marked this conversation as resolved.
Show resolved Hide resolved
$this->httpScheme = getenv('EZP_TEST_REST_SCHEME') ?: 'http';
$this->httpAuth = getenv('EZP_TEST_REST_AUTH') ?: 'admin:publish';
[$this->loginUsername, $this->loginPassword] = explode(':', $this->httpAuth);
Expand Down
Loading