Skip to content

Commit

Permalink
Merge pull request #4724 from neos/bugfix/asset-collections-filter
Browse files Browse the repository at this point in the history
BUGFIX: Filter for assets by asset collection without overriding existing WHERE conditions
  • Loading branch information
kitsunet authored Dec 6, 2023
2 parents 5fd80d6 + e4da4f4 commit ecacfbf
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Neos.Media/Classes/Domain/Repository/AssetRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,9 @@ protected function addAssetCollectionToQueryConstraints(QueryInterface $query, A
return;
}

$constraints = $query->getConstraint();
$query->matching($query->logicalAnd([$constraints, $query->contains('assetCollections', $assetCollection)]));
$query->getQueryBuilder()->andWhere(
$query->contains('assetCollections', $assetCollection)
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Doctrine\Common\Collections\ArrayCollection;
use Neos\Flow\Persistence\Doctrine\PersistenceManager;
use Neos\Media\Domain\Model\AssetCollection;
use Neos\Media\Domain\Model\Image;
use Neos\Media\Domain\Model\ImageVariant;
use Neos\Media\Domain\Repository\AssetCollectionRepository;
use Neos\Utility\Files;
use Neos\Media\Domain\Model\Asset;
use Neos\Media\Domain\Model\Tag;
Expand All @@ -34,6 +40,11 @@ class AssetRepositoryTest extends AbstractTest
*/
protected $assetRepository;

/**
* @var AssetCollectionRepository
*/
protected $assetCollectionRepository;

/**
* @var TagRepository
*/
Expand All @@ -52,6 +63,7 @@ public function setUp(): void
$this->prepareResourceManager();

$this->assetRepository = $this->objectManager->get(AssetRepository::class);
$this->assetCollectionRepository = $this->objectManager->get(AssetCollectionRepository::class);
$this->tagRepository = $this->objectManager->get(TagRepository::class);
}

Expand Down Expand Up @@ -147,4 +159,91 @@ public function findBySearchTermAndTagsReturnsFilteredResult()
$asset->getResource()->getSha1();
}
}

/**
* @test
*/
public function testAddAssetVariantFilterClauseWithoutAssetCollection()
{
$resource1 = $this->resourceManager->importResource(__DIR__ . '/../../Fixtures/Resources/417px-Mihaly_Csikszentmihalyi.jpg');
$resource2 = $this->resourceManager->importResource(__DIR__ . '/../../Fixtures/Resources/640px-Goodworkteam.jpg');

$image1 = new Image($resource1);
$image1->setTitle('asset for homepage');
$this->assetRepository->add($image1);

$imageVariant1 = new ImageVariant($image1);
$image1->addVariant($imageVariant1);

$image2 = new Image($resource2);
$image2->setTitle('asset for homepage');
$this->assetRepository->add($image2);

$imageVariant2 = new ImageVariant($image2);
$image2->addVariant($imageVariant2);

$this->persistenceManager->persistAll();
$this->persistenceManager->clearState();

$assets = $this->assetRepository->findAll();

self::assertCount(2, $assets);
foreach ($assets as $asset) {
self::assertInstanceOf(Image::class, $asset);
self::assertNotInstanceOf(ImageVariant::class, $asset);
}

// This is necessary to initialize all resource instances before the tables are deleted
foreach ($this->assetRepository->findAll() as $asset) {
$asset->getResource()->getSha1();
}
}

/**
* @test
*/
public function testAddAssetVariantFilterClauseWithAssetCollection()
{
$resource1 = $this->resourceManager->importResource(__DIR__ . '/../../Fixtures/Resources/417px-Mihaly_Csikszentmihalyi.jpg');
$resource2 = $this->resourceManager->importResource(__DIR__ . '/../../Fixtures/Resources/640px-Goodworkteam.jpg');

$assetCollection1 = new AssetCollection('test-1');
$this->assetCollectionRepository->add($assetCollection1);

$collections1 = new ArrayCollection();
$collections1->add($assetCollection1);

$image1 = new Image($resource1);
$image1->setTitle('asset for homepage');
$image1->setAssetCollections($collections1);
$assetCollection1->addAsset($image1);

$imageVariant1 = new ImageVariant($image1);
$image1->addVariant($imageVariant1);

$assetCollection2 = new AssetCollection('test-2');
$this->assetCollectionRepository->add($assetCollection2);

$collections2 = new ArrayCollection();
$collections2->add($assetCollection2);

$image2 = new Image($resource2);
$image2->setTitle('asset for homepage');
$image2->setAssetCollections($collections2);
$assetCollection2->addAsset($image2);

$this->persistenceManager->persistAll();
$this->persistenceManager->clearState();

$assets = $this->assetRepository->findAll($assetCollection1);
self::assertCount(1, $assets);
self::assertInstanceOf(Image::class, $assets->getFirst());
self::assertNotInstanceOf(ImageVariant::class, $assets->getFirst());
self::assertNotInstanceOf(ImageVariant::class, $assets->getFirst());

// This is necessary to initialize all resource instances before the tables are deleted
foreach ($this->assetRepository->findAll() as $asset) {
$asset->getResource()->getSha1();
}
}
}

0 comments on commit ecacfbf

Please sign in to comment.