/** * @test */ public function addTag() { $asset = $this->buildAssetObject(); $tag = new Tag('test'); $this->tagRepository->add($tag); $asset->addTag($tag); $this->persistenceManager->persistAll(); $this->persistenceManager->clearState(); $asset = $this->assetRepository->findAll()->getFirst(); $this->assertAssetHasTags($asset, ['test']); }
/** * @test */ public function findBySearchTermAndTagsReturnsFilteredResult() { $tag = new Tag('home'); $this->tagRepository->add($tag); $resource1 = $this->resourceManager->importResource(__DIR__ . '/../../Fixtures/Resources/license.txt'); $resource2 = $this->resourceManager->importResource(__DIR__ . '/../../Fixtures/Resources/417px-Mihaly_Csikszentmihalyi.jpg'); $asset1 = new Asset($resource1); $asset1->setTitle('asset for homepage'); $asset2 = new Asset($resource2); $asset2->setTitle('just another asset'); $asset2->addTag($tag); $this->assetRepository->add($asset1); $this->assetRepository->add($asset2); $this->persistenceManager->persistAll(); $this->persistenceManager->clearState(); $this->assertCount(2, $this->assetRepository->findBySearchTermOrTags('home', array($tag))); $this->assertCount(2, $this->assetRepository->findBySearchTermOrTags('homepage', array($tag))); $this->assertCount(1, $this->assetRepository->findBySearchTermOrTags('baz', array($tag))); // This is necessary to initialize all resource instances before the tables are deleted foreach ($this->assetRepository->findAll() as $asset) { $asset->getResource()->getSha1(); } }
/** * List existing assets * * @param string $view * @param string $sortBy * @param string $sortDirection * @param string $filter * @param integer $tagMode * @param Tag $tag * @param string $searchTerm * @param integer $collectionMode * @param AssetCollection $assetCollection * @return void */ public function indexAction($view = null, $sortBy = null, $sortDirection = null, $filter = null, $tagMode = self::TAG_GIVEN, Tag $tag = null, $searchTerm = null, $collectionMode = self::COLLECTION_GIVEN, AssetCollection $assetCollection = null) { if ($view !== null) { $this->browserState->set('view', $view); $this->view->assign('view', $view); } if ($sortBy !== null) { $this->browserState->set('sortBy', $sortBy); $this->view->assign('sortBy', $sortBy); } if ($sortDirection !== null) { $this->browserState->set('sortDirection', $sortDirection); $this->view->assign('sortDirection', $sortDirection); } if ($filter !== null) { $this->browserState->set('filter', $filter); $this->view->assign('filter', $filter); } if ($tagMode === self::TAG_GIVEN && $tag !== null) { $this->browserState->set('activeTag', $tag); $this->view->assign('activeTag', $tag); } elseif ($tagMode === self::TAG_NONE || $tagMode === self::TAG_ALL) { $this->browserState->set('activeTag', null); $this->view->assign('activeTag', null); } $this->browserState->set('tagMode', $tagMode); if ($collectionMode === self::COLLECTION_GIVEN && $assetCollection !== null) { $this->browserState->set('activeAssetCollection', $assetCollection); $this->view->assign('activeAssetCollection', $assetCollection); } elseif ($collectionMode === self::COLLECTION_ALL) { $this->browserState->set('activeAssetCollection', null); $this->view->assign('activeAssetCollection', null); } $this->browserState->set('collectionMode', $collectionMode); try { /** @var AssetCollection $activeAssetCollection */ $activeAssetCollection = $this->browserState->get('activeAssetCollection'); if ($activeAssetCollection instanceof DoctrineProxy) { // To trigger a possible EntityNotFound have to load the entity $activeAssetCollection->__load(); } } catch (EntityNotFoundException $exception) { // If a removed tasset collection is still in the browser state it can not be fetched $this->browserState->set('activeAssetCollection', null); $activeAssetCollection = null; } // Unset active tag if it isn't available in the active asset collection if ($this->browserState->get('activeTag') && $activeAssetCollection !== null && !$activeAssetCollection->getTags()->contains($this->browserState->get('activeTag'))) { $this->browserState->set('activeTag', null); $this->view->assign('activeTag', null); } if ($this->browserState->get('filter') !== 'All') { switch ($this->browserState->get('filter')) { case 'Image': $this->assetRepository = new ImageRepository(); break; case 'Document': $this->assetRepository = new DocumentRepository(); break; case 'Video': $this->assetRepository = new VideoRepository(); break; case 'Audio': $this->assetRepository = new AudioRepository(); break; } } switch ($this->browserState->get('sortBy')) { case 'Name': $this->assetRepository->setDefaultOrderings(array('resource.filename' => $this->browserState->get('sortDirection'))); break; case 'Modified': default: $this->assetRepository->setDefaultOrderings(array('lastModified' => $this->browserState->get('sortDirection'))); break; } if (!$this->browserState->get('activeTag') && $this->browserState->get('tagMode') === self::TAG_GIVEN) { $this->browserState->set('tagMode', self::TAG_ALL); } $assetCollections = array(); foreach ($this->assetCollectionRepository->findAll() as $assetCollection) { $assetCollections[] = array('object' => $assetCollection, 'count' => $this->assetRepository->countByAssetCollection($assetCollection)); } $tags = array(); foreach ($activeAssetCollection !== null ? $activeAssetCollection->getTags() : $this->tagRepository->findAll() as $tag) { $tags[] = array('object' => $tag, 'count' => $this->assetRepository->countByTag($tag, $activeAssetCollection)); } if ($searchTerm !== null) { $assets = $this->assetRepository->findBySearchTermOrTags($searchTerm, array(), $activeAssetCollection); $this->view->assign('searchTerm', $searchTerm); } elseif ($this->browserState->get('tagMode') === self::TAG_NONE) { $assets = $this->assetRepository->findUntagged($activeAssetCollection); } elseif ($this->browserState->get('activeTag') !== null) { $assets = $this->assetRepository->findByTag($this->browserState->get('activeTag'), $activeAssetCollection); } else { $assets = $activeAssetCollection === null ? $this->assetRepository->findAll() : $this->assetRepository->findByAssetCollection($activeAssetCollection); } $allCollectionsCount = $this->assetRepository->countAll(); $maximumFileUploadSize = $this->maximumFileUploadSize(); $this->view->assignMultiple(array('assets' => $assets, 'tags' => $tags, 'allCollectionsCount' => $allCollectionsCount, 'allCount' => $activeAssetCollection ? $this->assetRepository->countByAssetCollection($activeAssetCollection) : $allCollectionsCount, 'untaggedCount' => $this->assetRepository->countUntagged($activeAssetCollection), 'tagMode' => $this->browserState->get('tagMode'), 'assetCollections' => $assetCollections, 'argumentNamespace' => $this->request->getArgumentNamespace(), 'maximumFileUploadSize' => $maximumFileUploadSize, 'humanReadableMaximumFileUploadSize' => Files::bytesToSizeString($maximumFileUploadSize))); }