|
6 | 6 | */
|
7 | 7 | namespace eZ\Publish\API\Repository\Tests\FieldType;
|
8 | 8 |
|
| 9 | +use Doctrine\DBAL\ParameterType; |
| 10 | +use DOMDocument; |
9 | 11 | use eZ\Publish\API\Repository\Values\Content\Content;
|
10 | 12 | use eZ\Publish\API\Repository\Values\Content\Field;
|
11 | 13 | use eZ\Publish\Core\FieldType\Image\Value as ImageValue;
|
| 14 | +use eZ\Publish\Core\Persistence\Legacy\Content\Gateway; |
12 | 15 |
|
13 | 16 | /**
|
14 | 17 | * Integration test for use field type.
|
@@ -760,6 +763,108 @@ public function testRemovingDraftRemovesOldImage(): void
|
760 | 763 | );
|
761 | 764 | }
|
762 | 765 |
|
| 766 | + public function testDeleteImageWithCorruptedName(): void |
| 767 | + { |
| 768 | + $content = $this->publishNewImage( |
| 769 | + __METHOD__, |
| 770 | + new ImageValue( |
| 771 | + [ |
| 772 | + 'inputUri' => __DIR__ . '/_fixtures/image.jpg', |
| 773 | + 'fileName' => 'image.jpg', |
| 774 | + 'fileSize' => filesize(__DIR__ . '/_fixtures/image.jpg'), |
| 775 | + 'alternativeText' => 'Alternative', |
| 776 | + ] |
| 777 | + ), |
| 778 | + [2] |
| 779 | + ); |
| 780 | + |
| 781 | + $imageFieldDefinition = $content->getContentType()->getFieldDefinition('image'); |
| 782 | + |
| 783 | + $record = $this->fetchXML( |
| 784 | + $content->id, |
| 785 | + $content->getVersionInfo()->versionNo, |
| 786 | + $imageFieldDefinition->id |
| 787 | + ); |
| 788 | + |
| 789 | + $document = $this->corruptImageFieldXML($record); |
| 790 | + |
| 791 | + $this->updateXML( |
| 792 | + $content->id, |
| 793 | + $content->getVersionInfo()->versionNo, |
| 794 | + $imageFieldDefinition->id, |
| 795 | + $document |
| 796 | + ); |
| 797 | + |
| 798 | + $repository = $this->getRepository(false); |
| 799 | + $contentService = $repository->getContentService(); |
| 800 | + |
| 801 | + $contentService->deleteContent($content->getVersionInfo()->getContentInfo()); |
| 802 | + |
| 803 | + // Expect no League\Flysystem\CorruptedPathDetected thrown |
| 804 | + } |
| 805 | + |
| 806 | + /** |
| 807 | + * @return array<string,mixed> |
| 808 | + */ |
| 809 | + private function fetchXML(int $contentId, int $versionNo, int $fieldDefinitionId): array |
| 810 | + { |
| 811 | + $connection = $this->getRawDatabaseConnection(); |
| 812 | + |
| 813 | + $query = $connection->createQueryBuilder(); |
| 814 | + $query |
| 815 | + ->select('data_text') |
| 816 | + ->from(Gateway::CONTENT_FIELD_TABLE) |
| 817 | + ->andWhere('contentclassattribute_id = :contentclassattribute_id') |
| 818 | + ->andWhere('version = :version') |
| 819 | + ->andWhere('contentobject_id = :contentobject_id') |
| 820 | + ->setParameter('contentclassattribute_id', $fieldDefinitionId, ParameterType::INTEGER) |
| 821 | + ->setParameter('version', $versionNo, ParameterType::INTEGER) |
| 822 | + ->setParameter('contentobject_id', $contentId, ParameterType::INTEGER); |
| 823 | + $result = $query->execute(); |
| 824 | + |
| 825 | + return $result->fetchAssociative(); |
| 826 | + } |
| 827 | + |
| 828 | + /** |
| 829 | + * @param array<string,mixed> $row |
| 830 | + */ |
| 831 | + private function corruptImageFieldXML(array $row): DOMDocument |
| 832 | + { |
| 833 | + $corruptedChar = ''; |
| 834 | + |
| 835 | + $document = new DOMDocument('1.0', 'utf-8'); |
| 836 | + $document->loadXML($row['data_text']); |
| 837 | + $elements = $document->getElementsByTagName('ezimage'); |
| 838 | + $element = $elements->item(0); |
| 839 | + $element->setAttribute('filename', $element->getAttribute('filename') . $corruptedChar); |
| 840 | + $element->setAttribute('url', $element->getAttribute('url') . $corruptedChar); |
| 841 | + |
| 842 | + return $document; |
| 843 | + } |
| 844 | + |
| 845 | + private function updateXML( |
| 846 | + int $contentId, |
| 847 | + int $versionNo, |
| 848 | + int $fieldDefinitionId, |
| 849 | + DOMDocument $document |
| 850 | + ): void { |
| 851 | + $connection = $this->getRawDatabaseConnection(); |
| 852 | + |
| 853 | + $query = $connection->createQueryBuilder(); |
| 854 | + $query |
| 855 | + ->update(Gateway::CONTENT_FIELD_TABLE) |
| 856 | + ->set('data_text', ':data_text') |
| 857 | + ->setParameter('data_text', $document->saveXML(), ParameterType::STRING) |
| 858 | + ->andWhere('contentclassattribute_id = :contentclassattribute_id') |
| 859 | + ->andWhere('version = :version') |
| 860 | + ->andWhere('contentobject_id = :contentobject_id') |
| 861 | + ->setParameter('contentclassattribute_id', $fieldDefinitionId, ParameterType::INTEGER) |
| 862 | + ->setParameter('version', $versionNo, ParameterType::INTEGER) |
| 863 | + ->setParameter('contentobject_id', $contentId, ParameterType::INTEGER); |
| 864 | + |
| 865 | + $query->execute(); |
| 866 | + } |
| 867 | + |
763 | 868 | /**
|
764 | 869 | * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException
|
765 | 870 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
|
|
0 commit comments