Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions src/GraphQL/AssetType/AssetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ public function build(&$config)
'height' => Type::int(),
],
]);
$focalPointsType = new ObjectType([
'name' => 'focalPoints',
'fields' => [
'x' => Type::float(),
'y' => Type::float(),
],
]);

// see https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images#Resolution_switching_Same_size_different_resolutions
$resolutionsType = Type::listOf(new ObjectType([
Expand Down Expand Up @@ -126,6 +133,10 @@ public function build(&$config)
'type' => Type::float(),
'resolve' => [$resolver, 'resolveDuration'],
],
'focalPoints' => [
'type' => $focalPointsType,
'resolve' => [$resolver, 'resolveFocalPoints'],
],
'srcset' => [
'type' => Type::listOf(new ObjectType([
'name' => 'srcset',
Expand Down
28 changes: 28 additions & 0 deletions src/GraphQL/Resolver/AssetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,34 @@ public function resolveVersion($value = null, $args = [], $context = [], ?Resolv
return null;
}

/**
* @return array{x: float, y: float}|null
*
* @throws Exception
*/
public function resolveFocalPoints(?ElementDescriptor $value = null, array $context = []): ?array
{
$asset = $this->getAssetFromValue($value, $context);

if (!$asset instanceof Asset\Image) {
return null;
}

$x = $asset->getCustomSetting('focalPointX');
$y = $asset->getCustomSetting('focalPointY');

// returning an array of nulls would make the default field resolver resolve every
// subfield on its own, so the response would contain {x: null, y: null} instead of null
if ($x === null || $y === null) {
return null;
}

return [
'x' => (float) $x,
'y' => (float) $y,
];
}

/**
* @throws Exception
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"entrypoints": {
"main": {
"js": [
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/109.62a4c31b.js",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/main.9c3ad8a2.js"
],
"css": []
},
"pimcore_datahub_bundle": {
"js": [
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/async/499.5c3b9b56.js",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/async/346.60211bf9.js",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/async/840.b584be96.js",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/async/__federation_expose_default_export.0f3356b6.js",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/async/__federation_expose_plugins.61959a04.js",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/remoteEntry.js"
],
"css": []
},
"exposeRemote": {
"js": [
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/exposeRemote.js"
],
"css": []
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
window.alternativePluginExportPaths = {}
}

window.pluginRemotes.pimcore_datahub_bundle = "/bundles/pimcoredatahub/studio/build/e6891a04-71b8-4c95-aa14-55f166a8139b/static/js/remoteEntry.js"
window.pluginRemotes.pimcore_datahub_bundle = "/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/remoteEntry.js"

window.alternativePluginExportPaths.pimcore_datahub_bundle = "/plugins"

Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html><head><title>Rsbuild App</title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><script defer src="/bundles/pimcoredatahub/studio/build/e6891a04-71b8-4c95-aa14-55f166a8139b/static/js/109.62a4c31b.js"></script><script defer src="/bundles/pimcoredatahub/studio/build/e6891a04-71b8-4c95-aa14-55f166a8139b/static/js/main.9c3ad8a2.js"></script></head><body><div id="root"></div></body></html>
<!DOCTYPE html><html><head><title>Rsbuild App</title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><script defer src="/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/109.62a4c31b.js"></script><script defer src="/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/main.9c3ad8a2.js"></script></head><body><div id="root"></div></body></html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"allFiles": [
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/main.9c3ad8a2.js",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/remoteEntry.js",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/async/__federation_expose_plugins.61959a04.js",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/async/__federation_expose_default_export.0f3356b6.js",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/async/840.b584be96.js",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/async/346.60211bf9.js",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/109.62a4c31b.js",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/async/499.5c3b9b56.js",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/mf-stats.json",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/mf-manifest.json",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/main.html"
],
"entries": {
"main": {
"html": [
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/main.html"
],
"initial": {
"js": [
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/109.62a4c31b.js",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/main.9c3ad8a2.js"
]
}
},
"pimcore_datahub_bundle": {
"initial": {
"js": [
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/remoteEntry.js"
]
},
"async": {
"js": [
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/async/499.5c3b9b56.js",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/async/346.60211bf9.js",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/async/840.b584be96.js",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/async/__federation_expose_default_export.0f3356b6.js",
"/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/static/js/async/__federation_expose_plugins.61959a04.js"
]
}
}
},
"integrity": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"globalName": "pimcore_datahub_bundle",
"pluginVersion": "2.2.3",
"prefetchInterface": false,
"publicPath": "/bundles/pimcoredatahub/studio/build/e6891a04-71b8-4c95-aa14-55f166a8139b/"
"publicPath": "/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/"
},
"shared": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"globalName": "pimcore_datahub_bundle",
"pluginVersion": "2.2.3",
"prefetchInterface": false,
"publicPath": "/bundles/pimcoredatahub/studio/build/e6891a04-71b8-4c95-aa14-55f166a8139b/"
"publicPath": "/bundles/pimcoredatahub/studio/build/479c1d93-27f5-45cc-bd90-1a42d415aaf1/"
},
"shared": [
{
Expand Down

Large diffs are not rendered by default.

This file was deleted.

This file was deleted.

121 changes: 121 additions & 0 deletions tests/GraphQL/Resolver/AssetTypeFocalPointsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

/**
* This source file is available under the terms of the
* Pimcore Open Core License (POCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
* @license Pimcore Open Core License (POCL)
*/

namespace Pimcore\Bundle\DataHubBundle\Tests\GraphQL\Resolver;

use Codeception\Test\Unit;
use Pimcore\Bundle\DataHubBundle\Configuration;
use Pimcore\Bundle\DataHubBundle\GraphQL\ElementDescriptor;
use Pimcore\Bundle\DataHubBundle\GraphQL\Resolver\AssetType as AssetTypeResolver;
use Pimcore\Bundle\DataHubBundle\PimcoreDataHubBundle;
use Pimcore\Cache\RuntimeCache;
use Pimcore\Model\Asset;
use Pimcore\Tests\Support\Util\TestHelper;

class AssetTypeFocalPointsTest extends Unit
{
private AssetTypeResolver $resolver;

private Asset\Image $image;

private ?Asset\Video $video = null;

private mixed $backupContext = null;

protected function setUp(): void
{
$this->resolver = new AssetTypeResolver();

if (RuntimeCache::isRegistered(PimcoreDataHubBundle::RUNTIME_CONTEXT_KEY)) {
$this->backupContext = RuntimeCache::get(PimcoreDataHubBundle::RUNTIME_CONTEXT_KEY);
}
$this->useConfigurationWithoutPermissionCheck();

$this->image = TestHelper::createImageAsset('datahub-focalpoint-');
}

protected function tearDown(): void
{
if ($this->backupContext !== null) {
RuntimeCache::set(PimcoreDataHubBundle::RUNTIME_CONTEXT_KEY, $this->backupContext);
}

$this->image->delete();
$this->video?->delete();
}

public function testResolveFocalPointsReturnsCoordinatesWhenFocalPointIsSet(): void
{
$this->setFocalPoint(50.5, 25.0);

$this->assertSame(
['x' => 50.5, 'y' => 25.0],
$this->resolver->resolveFocalPoints(new ElementDescriptor($this->image))
);
}

/**
* Custom settings survive a round trip through the serialized asset data, so the
* coordinates can come back as strings - the GraphQL field is a Float.
*/
public function testResolveFocalPointsReturnsFloatsForStringCustomSettings(): void
{
$this->setFocalPoint('50.5', '25');

$this->assertSame(
['x' => 50.5, 'y' => 25.0],
$this->resolver->resolveFocalPoints(new ElementDescriptor($this->image))
);
}

/**
* Returning an array of nulls made graphql-php's default field resolver resolve every
* requested subfield individually, so the response contained `focalPoints: {x: null, y: null}`
* instead of `focalPoints: null`.
*/
public function testResolveFocalPointsReturnsNullWhenNoFocalPointIsSet(): void
{
$this->assertNull($this->resolver->resolveFocalPoints(new ElementDescriptor($this->image)));
}

public function testResolveFocalPointsReturnsNullForNonImageAssets(): void
{
$this->video = TestHelper::createVideoAsset('datahub-focalpoint-');

$this->assertNull($this->resolver->resolveFocalPoints(new ElementDescriptor($this->video)));
}

public function testResolveFocalPointsReturnsNullWithoutElement(): void
{
$this->assertNull($this->resolver->resolveFocalPoints());
}

private function setFocalPoint(mixed $x, mixed $y): void
{
$this->image->setCustomSetting('focalPointX', $x);
$this->image->setCustomSetting('focalPointY', $y);
$this->image->save();
}

private function useConfigurationWithoutPermissionCheck(): void
{
$configuration = new Configuration('graphql', '/1', 'datahub-focalpoint-test');
$configuration->setConfiguration([
'security' => ['skipPermissionCheck' => true],
]);

RuntimeCache::set(
PimcoreDataHubBundle::RUNTIME_CONTEXT_KEY,
['clientname' => 'datahub-focalpoint-test', 'configuration' => $configuration]
);
}
}
Loading