Skip to content

Commit

Permalink
Merged branch '1.3' of ezsystems/ezplatform-kernel into 4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
alongosz committed Jan 31, 2024
2 parents 9150cc6 + 6e5fa6f commit f4b774c
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ parameters:
message: "#^Cannot call method (fetchOne|fetchColumn|fetchAllAssociative|fetchAssociative|fetchAllKeyValue)\\(\\) on Doctrine\\\\DBAL\\\\ForwardCompatibility\\\\Result\\|int\\|string\\.$#"
paths:
- src/*
- tests/*
paths:
- src
- tests
Expand Down
124 changes: 124 additions & 0 deletions tests/integration/Core/Repository/FieldType/ImageIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
*/
namespace Ibexa\Tests\Integration\Core\Repository\FieldType;

use Doctrine\DBAL\ParameterType;
use DOMDocument;
use DOMElement;
use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException;
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\Contracts\Core\Repository\Values\Content\Field;
use Ibexa\Core\FieldType\Image\Value as ImageValue;
use Ibexa\Core\Persistence\Legacy\Content\Gateway;

/**
* Integration test for use field type.
Expand Down Expand Up @@ -719,6 +723,126 @@ public function testRemovingDraftRemovesOldImage(): void
);
}

/**
* @throws \Doctrine\DBAL\Driver\Exception
* @throws \Doctrine\DBAL\Exception
* @throws \ErrorException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\Exception
*/
public function testDeleteImageWithCorruptedName(): void
{
$content = $this->publishNewImage(
__METHOD__,
new ImageValue(
[
'inputUri' => __DIR__ . '/_fixtures/image.jpg',
'fileName' => 'image.jpg',
'fileSize' => filesize(__DIR__ . '/_fixtures/image.jpg'),
'alternativeText' => 'Alternative',
]
),
[2]
);

$imageFieldDefinition = $content->getContentType()->getFieldDefinition('image');
self::assertNotNull($imageFieldDefinition);

$record = $this->fetchXML(
$content->id,
$content->getVersionInfo()->versionNo,
$imageFieldDefinition->id
);

$document = $this->corruptImageFieldXML($record);

$this->updateXML(
$content->id,
$content->getVersionInfo()->versionNo,
$imageFieldDefinition->id,
$document
);

$repository = $this->getRepository(false);
$contentService = $repository->getContentService();

$contentService->deleteContent($content->getVersionInfo()->getContentInfo());

// Expect no League\Flysystem\CorruptedPathDetected thrown
}

/**
* @return array<string,mixed>
*
* @throws \Doctrine\DBAL\Exception
* @throws \ErrorException
* @throws \Doctrine\DBAL\Driver\Exception
*/
private function fetchXML(int $contentId, int $versionNo, int $fieldDefinitionId): array
{
$connection = $this->getRawDatabaseConnection();

$query = $connection->createQueryBuilder();
$query
->select('data_text')
->from(Gateway::CONTENT_FIELD_TABLE)
->andWhere('contentclassattribute_id = :contentclassattribute_id')
->andWhere('version = :version')
->andWhere('contentobject_id = :contentobject_id')
->setParameter('contentclassattribute_id', $fieldDefinitionId, ParameterType::INTEGER)
->setParameter('version', $versionNo, ParameterType::INTEGER)
->setParameter('contentobject_id', $contentId, ParameterType::INTEGER);

$result = $query->execute()->fetchAssociative();
self::assertNotFalse($result);

return $result;
}

/**
* @param array<string,mixed> $row
*/
private function corruptImageFieldXML(array $row): DOMDocument
{
$corruptedChar = '­';

$document = new DOMDocument('1.0', 'utf-8');
$document->loadXML($row['data_text']);
$elements = $document->getElementsByTagName('ezimage');
$element = $elements->item(0);
self::assertInstanceOf(DOMElement::class, $element);
$element->setAttribute('filename', $element->getAttribute('filename') . $corruptedChar);
$element->setAttribute('url', $element->getAttribute('url') . $corruptedChar);

return $document;
}

/**
* @throws \Doctrine\DBAL\Exception
* @throws \ErrorException
*/
private function updateXML(
int $contentId,
int $versionNo,
int $fieldDefinitionId,
DOMDocument $document
): void {
$connection = $this->getRawDatabaseConnection();

$query = $connection->createQueryBuilder();
$query
->update(Gateway::CONTENT_FIELD_TABLE)
->set('data_text', ':data_text')
->setParameter('data_text', $document->saveXML(), ParameterType::STRING)
->andWhere('contentclassattribute_id = :contentclassattribute_id')
->andWhere('version = :version')
->andWhere('contentobject_id = :contentobject_id')
->setParameter('contentclassattribute_id', $fieldDefinitionId, ParameterType::INTEGER)
->setParameter('version', $versionNo, ParameterType::INTEGER)
->setParameter('contentobject_id', $contentId, ParameterType::INTEGER);

$query->execute();
}

/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\ForbiddenException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
Expand Down

0 comments on commit f4b774c

Please sign in to comment.