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-7579:Richtext: Rows are added to ezurl_object_link on every save #402

Open
wants to merge 6 commits into
base: 1.3
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\Core\FieldType\Tests\Integration\Url\UrlStorage;
vidarl marked this conversation as resolved.
Show resolved Hide resolved

use eZ\Publish\Core\FieldType\Tests\Integration\BaseCoreFieldTypeIntegrationTest;
use eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway;

/**
* Url Field Type external storage gateway tests.
*/
abstract class UrlStorageGatewayTest extends BaseCoreFieldTypeIntegrationTest
{
abstract protected function getGateway(): Gateway;

/**
* @covers \eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway\DoctrineStorage::getUrlsFromUrlLink
*/
vidarl marked this conversation as resolved.
Show resolved Hide resolved
public function testGetUrlsFromUrlLink()
vidarl marked this conversation as resolved.
Show resolved Hide resolved
{
$gateway = $this->getGateway();

$urlIds = [];
$urlIds[] = $gateway->insertUrl('https://ibexa.co/example1');
$urlIds[] = $gateway->insertUrl('https://ibexa.co/example2');
$urlIds[] = $gateway->insertUrl('https://ibexa.co/example3');

$gateway->linkUrl($urlIds[0], 10, 1);
$gateway->linkUrl($urlIds[1], 10, 1);
$gateway->linkUrl($urlIds[1], 12, 2);
$gateway->linkUrl($urlIds[2], 14, 1);

self::assertEquals(['https://ibexa.co/example1' => true, 'https://ibexa.co/example2' => true], $gateway->getUrlsFromUrlLink(10, 1), 'Did not get expected urlS for field 10');
self::assertEquals(['https://ibexa.co/example2' => true], $gateway->getUrlsFromUrlLink(12, 2), 'Did not get expected url for field 12');
self::assertEquals(['https://ibexa.co/example3' => true], $gateway->getUrlsFromUrlLink(14, 1), 'Did not get expected url for field 14');
self::assertEquals([], $gateway->getUrlsFromUrlLink(15, 1), 'Expected no urls for field 15');
}
}
7 changes: 7 additions & 0 deletions eZ/Publish/Core/FieldType/Url/UrlStorage/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ abstract public function getUrlIdMap(array $urls);
*/
abstract public function insertUrl($url);

/**
* Return a list of URLs used by the given field and version.
*
* @return bool[] An array of URLs, with urls as keys
*/
abstract public function getUrlsFromUrlLink(int $fieldId, int $versionNo): array;

/**
* Creates link to URL with $urlId for field with $fieldId in $versionNo.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,48 @@ public function insertUrl($url)
);
}

/**
* Return a list of URLs used by the given field and version.
*
* @return bool[] An array of URLs, with urls as keys
vidarl marked this conversation as resolved.
Show resolved Hide resolved
*/
public function getUrlsFromUrlLink(int $fieldId, int $versionNo): array
{
$selectQuery = $this->connection->createQueryBuilder();
$selectQuery
->select($this->connection->quoteIdentifier('url.url'))
->from($this->connection->quoteIdentifier(self::URL_TABLE), 'url')
->leftJoin(
vidarl marked this conversation as resolved.
Show resolved Hide resolved
'url',
$this->connection->quoteIdentifier(self::URL_LINK_TABLE),
'link',
'url.id = link.url_id'
)
->where(
$selectQuery->expr()->eq(
'link.contentobject_attribute_id',
':contentobject_attribute_id'
)
)
->andWhere(
$selectQuery->expr()->eq(
'link.contentobject_attribute_version',
':contentobject_attribute_version'
)
)
->setParameter(':contentobject_attribute_id', $fieldId, ParameterType::INTEGER)
->setParameter(':contentobject_attribute_version', $versionNo, ParameterType::INTEGER);

$statement = $selectQuery->execute();
$rows = $statement->fetchAllAssociativeIndexed();
$result = [];
foreach ($rows as $url => $item) {
vidarl marked this conversation as resolved.
Show resolved Hide resolved
$result[$url] = true;
}

return $result;
}

/**
* Create link to URL with $urlId for field with $fieldId in $versionNo.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

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

namespace Ibexa\Tests\integration\Core\FieldType\Url\UrlStorage\Gateway;
vidarl marked this conversation as resolved.
Show resolved Hide resolved

use eZ\Publish\Core\FieldType\Tests\Integration\Url\UrlStorage\UrlStorageGatewayTest;
use eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway as UrlStorageGateway;
use eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway\DoctrineStorage;

final class UrlDoctrineStorageGatewayTest extends UrlStorageGatewayTest
{
protected function getGateway(): UrlStorageGateway
{
return new DoctrineStorage($this->getDatabaseConnection());
}
}
Loading