From 7760306f000426488d26433f009bc6a3ec50904d Mon Sep 17 00:00:00 2001 From: haogatyp Date: Thu, 10 Feb 2022 16:45:06 +0100 Subject: [PATCH 01/11] #129 Implementation of the DefaultDocumentFinder using Doctrine DBAL. --- .../DocumentFinder/DefaultDocumentFinder.php | 234 +++++- .../DefaultDocumentFinderTest.php | 778 ++++++++++++++++++ 2 files changed, 975 insertions(+), 37 deletions(-) create mode 100644 tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php diff --git a/library/Opus/DocumentFinder/DefaultDocumentFinder.php b/library/Opus/DocumentFinder/DefaultDocumentFinder.php index 73f93d5e..d97955b3 100644 --- a/library/Opus/DocumentFinder/DefaultDocumentFinder.php +++ b/library/Opus/DocumentFinder/DefaultDocumentFinder.php @@ -31,13 +31,16 @@ namespace Opus\DocumentFinder; -use Opus\DocumentFinder; +use Doctrine\DBAL\Connection; +use Opus\Db2\Database; use Opus\DocumentFinderInterface; +use function array_unique; +use function count; use function is_array; /** - * Wrapper implementing DocumentFinderInterface around old DocumentFinder using Zend_Db. + * Implementing DocumentFinderInterface using Doctrine DBAL. * * Mit diesem Zwischenschritt kann die Application von der DocumentFinder Klasse abgekoppelt werden ohne sofort einen * neuen DocumentFinder entwickeln zu müssen. @@ -46,11 +49,17 @@ */ class DefaultDocumentFinder implements DocumentFinderInterface { - private $finder; + /** @var Connection */ + private $connection; + + /** @var Doctrine\DBAL\Query\QueryBuilder */ + private $select; public function __construct() { - $this->finder = new DocumentFinder(); + $this->connection = Database::getConnection(); + $queryBuilder = $this->connection->createQueryBuilder(); + $this->select = $queryBuilder->select('*')->from('documents', 'd'); } /** @@ -58,7 +67,11 @@ public function __construct() */ public function getIds() { - return $this->finder->ids(); + $this->select->select("d.id"); + + $result = $this->select->execute()->fetchFirstColumn(); + + return array_unique($result); } /** @@ -66,7 +79,8 @@ public function getIds() */ public function getCount() { - return $this->finder->count(); + $this->select->select("count(d.id)")->distinct(); + return $this->select->execute()->fetchOne(); } /** @@ -76,21 +90,47 @@ public function getCount() */ public function setOrder($criteria, $ascending = true) { + $order = $ascending ? 'ASC' : 'DESC'; + switch ($criteria) { case self::ORDER_ID: - $this->finder->orderById($ascending); + // Order by id + $this->select->orderBy('d.id', $order); break; case self::ORDER_AUTHOR: - $this->finder->orderByAuthorLastname($ascending); + // Order by author lastname + $this->select + ->leftJoin( + 'd', + 'link_persons_documents', + 'pd', + 'd.id = pd.document_id AND pd.role = "author"' + ) + ->leftJoin('pd', 'persons', 'p', 'pd.person_id = p.id') + ->groupBy('d.id') + ->addGroupBy('p.last_name') + ->orderBy('p.last_name ', $order); break; case self::ORDER_TITLE: - $this->finder->orderByTitleMain($ascending); + // Order by title main + $this->select + ->leftJoin( + 'd', + 'document_title_abstracts', + 't', + 't.document_id = d.id AND t.type = "main"' + ) + ->groupBy('d.id') + ->addGroupBy('t.value') + ->orderBy('t.value', $order); break; case self::ORDER_DOCUMENT_TYPE: - $this->finder->orderByType($ascending); + // Order by type + $this->select->orderBy('d.type ', $order); break; case self::ORDER_SERVER_DATE_PUBLISHED: - $this->finder->orderByServerDatePublished($ascending); + // Order by server date published + $this->select->orderBy('d.server_date_published', $order); break; default: break; @@ -104,7 +144,15 @@ public function setOrder($criteria, $ascending = true) */ public function setDocumentIds($documentIds) { - $this->finder->setIdSubset($documentIds); + // Hotfix: If $subset is empty, return empty set. + if (! is_array($documentIds) || count($documentIds) < 1) { + $this->select->andWhere('1 = 0'); + return $this; + } + + $this->select->andWhere('d.id IN (:setDocumentIds_documentIds)') + ->setParameter('setDocumentIds_documentIds', $documentIds, Connection::PARAM_STR_ARRAY); + return $this; } @@ -116,26 +164,31 @@ public function setDocumentIds($documentIds) public function setDocumentIdRange($start = null, $end = null) { if ($start !== null) { - $this->finder->setIdRangeStart($start); + $this->select->andWhere('d.id >= :setDocumentIdRange_start') + ->setParameter('setDocumentIdRange_start', $start); } if ($end !== null) { - $this->finder->setIdRangeEnd($end); + $this->select->andWhere('d.id <= :setDocumentIdRange_end') + ->setParameter('setDocumentIdRange_end', $end); } return $this; } /** - * @param string $serverState + * @param string|string[] $serverState * @return $this */ public function setServerState($serverState) { if (is_array($serverState)) { - $this->finder->setServerStateInList($serverState); + $this->select->andWhere('server_state IN (:setServerState_serverStates)', $serverState) + ->setParameter('setServerState_serverStates', $serverState, Connection::PARAM_STR_ARRAY); } else { - $this->finder->setServerState($serverState); + $this->select->andWhere('server_state = :setServerState_serverState') + ->setParameter('setServerState_serverState', $serverState); } + return $this; } @@ -145,7 +198,9 @@ public function setServerState($serverState) */ public function setBelongsToBibliography($partOfBibliography = true) { - $this->finder->setBelongsToBibliography($partOfBibliography); + $this->select->andWhere('d.belongs_to_bibliography = :setBelongsToBibliography_partOfBibliography') + ->setParameter('setBelongsToBibliography_partOfBibliography', $partOfBibliography); + return $this; } @@ -155,7 +210,25 @@ public function setBelongsToBibliography($partOfBibliography = true) */ public function setCollectionId($collectionId) { - $this->finder->setCollectionId($collectionId); + $queryBuilder = $this->connection->createQueryBuilder(); + + $subSelect = $queryBuilder->select('l.document_id') + ->from('link_documents_collections', 'l') + ->where('l.document_id = d.id') + ->andWhere('l.collection_id IN (:setCollectionId_collectionId)'); + + $this->select->andWhere("EXISTS (" . $subSelect->getSQL() . ")"); + + if (is_array($collectionId)) { + $this->select->setParameter( + 'setCollectionId_collectionId', + $collectionId, + Connection::PARAM_STR_ARRAY + ); + } else { + $this->select->setParameter('setCollectionId_collectionId', $collectionId); + } + return $this; } @@ -165,7 +238,18 @@ public function setCollectionId($collectionId) */ public function setCollectionRoleId($roleId) { - $this->finder->setCollectionRoleId($roleId); + $queryBuilder = $this->connection->createQueryBuilder(); + + $subSelect = $queryBuilder->select('l.document_id') + ->from('collections', 'c') + ->from('link_documents_collections', 'l') + ->where('l.document_id = d.id') + ->andWhere('l.collection_id = c.id') + ->andWhere('c.role_id = :setCollectionRoleId_roleId'); + + $this->select->andWhere("EXISTS (" . $subSelect->getSQL() . ")") + ->setParameter('setCollectionRoleId_roleId', $roleId); + return $this; } @@ -175,7 +259,16 @@ public function setCollectionRoleId($roleId) */ public function setIdentifierExists($name) { - $this->finder->setIdentifierTypeExists($name); + $queryBuilder = $this->connection->createQueryBuilder(); + + $subSelect = $queryBuilder->select('i.id') + ->from('document_identifiers', 'i') + ->where('i.document_id = d.id') + ->andWhere('type = :setIdentifierExists_name'); + + $this->select->andWhere("EXISTS (" . $subSelect->getSQL() . ")") + ->setParameter('setIdentifierExists_name', $name); + return $this; } @@ -186,7 +279,18 @@ public function setIdentifierExists($name) */ public function setIdentifierValue($name, $value) { - $this->finder->setIdentifierTypeValue($name, $value); + $queryBuilder = $this->connection->createQueryBuilder(); + + $subSelect = $queryBuilder->select('d.id') + ->from('document_identifiers', 'i') + ->where('i.document_id = d.id') + ->andWhere('type = :setIdentifierValue_name') + ->andWhere('value = :setIdentifierValue_value'); + + $this->select->andWhere("EXISTS (" . $subSelect->getSQL() . ")") + ->setParameter('setIdentifierValue_name', $name) + ->setParameter('setIdentifierValue_value', $value); + return $this; } @@ -197,9 +301,11 @@ public function setIdentifierValue($name, $value) public function setDocumentType($type) { if (is_array($type)) { - $this->finder->setTypeInList($type); + $this->select->andWhere('type IN (:setDocumentType_types)') + ->setParameter('setDocumentType_types', $type, Connection::PARAM_STR_ARRAY); } else { - $this->finder->setType($type); + $this->select->andWhere('type = :setDocumentType_type') + ->setParameter('setDocumentType_type', $type); } return $this; } @@ -210,7 +316,16 @@ public function setDocumentType($type) */ public function setEnrichmentExists($name) { - $this->finder->setEnrichmentKeyExists($name); + $queryBuilder = $this->connection->createQueryBuilder(); + + $subSelect = $queryBuilder->select('d.id') + ->from('document_enrichments', e) + ->where('document_id = d.id') + ->andWhere('key_name = :setEnrichmentExists_name'); + + $this->select->andWhere('EXISTS (' . $subSelect->getSQL() . ')') + ->setParameter('setEnrichmentExists_name', $name); + return $this; } @@ -221,7 +336,18 @@ public function setEnrichmentExists($name) */ public function setEnrichmentValue($key, $value) { - $this->finder->setEnrichmentKeyValue($key, $value); + $queryBuilder = $this->connection->createQueryBuilder(); + + $subSelect = $queryBuilder->select('d.id') + ->from('document_enrichments', 'e') + ->where('document_id = d.id') + ->andWhere('key_name = :setEnrichmentValue_key') + ->andWhere('value = :setEnrichmentValue_value'); + + $this->select->andWhere("EXISTS (" . $subSelect->getSQL() . ")") + ->setParameter('setEnrichmentValue_key', $key) + ->setParameter('setEnrichmentValue_value', $value); + return $this; } @@ -232,6 +358,10 @@ public function setEnrichmentValue($key, $value) public function setServerDatePublishedBefore($date) { $this->finder->setServerDatePublishedBefore($date); + + $this->select->andWhere('d.server_date_published < :setServerDatePublishedBefore_date') + ->setParameter('setServerDatePublishedBefore_date', $date); + return $this; } @@ -242,7 +372,11 @@ public function setServerDatePublishedBefore($date) */ public function setServerDatePublishedRange($from, $until) { - $this->finder->setServerDatePublishedRange($from, $until); + $this->select->andWhere('d.server_date_published >= :setServerDatePublishedRange_from') + ->andWhere('d.server_date_published < :setServerDatePublishedRange_until') + ->setParameter('setServerDatePublishedRange_from', $from) + ->setParameter('setServerDatePublishedRange_until', $until); + return $this; } @@ -252,7 +386,8 @@ public function setServerDatePublishedRange($from, $until) */ public function setServerDateModifiedBefore($date) { - $this->finder->setServerDateModifiedBefore($date); + $this->select->andWhere('d.server_date_modified < :setServerDateModifiedBefore_date') + ->setParameter('setServerDateModifiedBefore_date', $date); return $this; } @@ -262,7 +397,9 @@ public function setServerDateModifiedBefore($date) */ public function setServerDateModifiedAfter($date) { - $this->finder->setServerDateModifiedAfter($date); + $this->select->andWhere('d.server_date_modified >= :setServerDateModifiedAfter_date') + ->setParameter('setServerDateModifiedAfter_data', $date); + return $this; } @@ -272,7 +409,9 @@ public function setServerDateModifiedAfter($date) */ public function setEmbargoDateBefore($date) { - $this->finder->setEmbargoDateBefore($date); + $this->select->andWhere('d.embargo_date < :setEmbargoDateBefore_date') + ->setParameter('setEmbargoDateBefore_date', $date); + return $this; } @@ -282,7 +421,9 @@ public function setEmbargoDateBefore($date) */ public function setNotEmbargoedOn($date) { - $this->finder->setNotEmbargoedOn($date); + $this->select->andWhere('d.embargo_date < :setNotEmbargoedOn_date or d.embargo_date IS NULL') + ->setParameter('setNotEmbargoedOn_date', $date); + return $this; } @@ -291,7 +432,7 @@ public function setNotEmbargoedOn($date) */ public function setNotModifiedAfterEmbargoDate() { - $this->finder->setNotModifiedAfterEmbargoDate(); + $this->select->andWhere('d.server_date_modified < d.embargo_date'); return $this; } @@ -300,7 +441,15 @@ public function setNotModifiedAfterEmbargoDate() */ public function setHasFilesVisibleInOai() { - $this->finder->setFilesVisibleInOai(); + $queryBuilder = $this->connection->createQueryBuilder(); + + $subSelect = $queryBuilder->select('f.document_id') + ->from('document_files', 'f') + ->where('f.document_id = d.id') + ->andWhere('f.visible_in_oai=1'); + + $this->select->andWhere('d.id IN (' . $subSelect->getSQL() . ')'); + return $this; } @@ -309,7 +458,14 @@ public function setHasFilesVisibleInOai() */ public function setNotInXmlCache() { - $this->finder->setNotInXmlCache(); + $queryBuilder = $this->connection->createQueryBuilder(); + + // get all IDs in XML cache + $subSelect = $queryBuilder->select('dxc.document_id') + ->from('document_xml_cache', 'dxc'); + + $this->select->andWhere(' NOT d.id IN (' . $subSelect->getSQL() . ')'); + return $this; } @@ -320,9 +476,12 @@ public function setNotInXmlCache() public function getDocumentTypes($includeCount = false) { if ($includeCount) { - return $this->finder->groupedTypesPlusCount(); + $this->select->select('type AS type', 'count(DISTINCT id) AS count') + ->groupBy('type'); + return $this->select->execute()->fetchAllAssociative(); } else { - return $this->finder->groupedTypes(); + $this->select->select("type")->distinct(); + return $this->select->execute()->fetchFirstColumn(); } } @@ -331,6 +490,7 @@ public function getDocumentTypes($includeCount = false) */ public function getYearsPublished() { - return $this->finder->groupedServerYearPublished(); + $this->select->select("substr(server_date_published, 1, 4)")->distinct(); + return $this->select->execute()->fetchFirstColumn(); } } diff --git a/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php new file mode 100644 index 00000000..414679d7 --- /dev/null +++ b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php @@ -0,0 +1,778 @@ +clearTables(false, [ + 'documents', + 'persons', + 'link_persons_documents', + 'document_title_abstracts', + ]); + } + + private function prepareDocuments() + { + $publishedDoc1 = new Document(); + $publishedDoc1->setType("preprint") + ->setServerState('published') + ->store(); + + $title = $publishedDoc1->addTitleMain(); + $title->setValue('Title 1'); + $title->setLanguage('deu'); + + $title = $publishedDoc1->addTitleMain(); + $title->setValue('Title 2'); + $title->setLanguage('eng'); + + $publishedDoc1->store(); + + $publishedDoc2 = new Document(); + $publishedDoc2->setType("article") + ->setServerState('published') + ->store(); + + $title = $publishedDoc2->addTitleMain(); + $title->setValue('A Title 1'); + $title->setLanguage('deu'); + + $publishedDoc2->store(); + + $unpublishedDoc1 = new Document(); + $unpublishedDoc1->setType("doctoral_thesis") + ->setServerState('unpublished') + ->store(); + + $person = new Person(); + $person->setLastName('B'); + $unpublishedDoc1->addPersonAuthor($person); + + $unpublishedDoc1->store(); + + $unpublishedDoc2 = new Document(); + $unpublishedDoc2->setType("preprint") + ->setServerState('unpublished') + ->store(); + + $person = new Person(); + $person->setLastName('C'); + $unpublishedDoc2->addPersonAuthor($person); + + $person = new Person(); + $person->setLastName('A'); + $unpublishedDoc2->addPersonAuthor($person); + + $unpublishedDoc2->store(); + + $deletedDoc1 = new Document(); + $deletedDoc1->setType("article") + ->setServerState('deleted') + ->store(); + + $deletedDoc2 = new Document(); + $deletedDoc2->setType("doctoral_thesis") + ->setServerState('deleted') + ->store(); + } + + /** + * @param int[] $ids + * @param string $state + * @throws ModelException + */ + private function checkServerState($ids, $state) + { + foreach ($ids as $id) { + $doc = new Document($id); + $this->assertEquals($state, $doc->getServerState()); + } + } + + /** + * Basic functionality + */ + public function testCountOnEmptyDb() + { + $finder = new DefaultDocumentFinder(); + $this->assertEquals(0, $finder->getCount()); + } + + /** + * Basic functionality + */ + public function testIdsOnEmptyDb() + { + $finder = new DefaultDocumentFinder(); + $this->assertEquals([], $finder->getIds()); + } + + /** + * Basic functionality + */ + public function testAllEntriesNoConstraints() + { + $this->prepareDocuments(); + + // published + $finder = new DefaultDocumentFinder(); + $this->assertEquals(6, $finder->getCount()); + $this->assertEquals(6, count($finder->getIds())); + } + + /** + * Basic functionality + */ + public function testAllConstraints() + { + $this->markTestSkipped('TODO DOCTRINE DBAL Issue #129: Does this test even make sense?'); + + // published + $finder = new DefaultDocumentFinder(); + $finder->setEnrichmentKeyExists('foobar') + ->setEnrichmentKeyValue('foo', 'bar') + ->setIdRange(1, 2) + ->setIdRangeStart(2) + ->setIdRangeEnd(1) + ->setIdentifierTypeValue('opus-3', 23) + ->setServerState('published') + ->setServerStateInList(['published']) + ->setType('fooprintbar') + ->setTypeInList(['fooprintbar']) + ->setServerDateModifiedRange('2010-01-01', '2000-01-01') + ->setServerDatePublishedRange('1999-12-31', '1900-01-01') + ->setIdSubset(null) + ->setIdSubset([]) + ->setIdSubset([1]) + ->setIdSubset([-1]) + ->setIdSubset([1, 2]) + ->setIdSubset(['foo']); + + $this->assertEquals(0, $finder->getCount()); + $this->assertEquals(0, count($finder->getIds())); + } + + /** + * Basic functionality + */ + public function testIdsByState() + { + // $this->markTestSkipped('TODO DOCTRINE DBAL Issue #129'); + + $this->prepareDocuments(); + + // published + $finder = new DefaultDocumentFinder(); + $finder->setServerState('published'); + $this->assertEquals(2, $finder->getCount()); + + $publishedDocs = $finder->getIds(); + $this->assertEquals(2, count($publishedDocs)); + $this->checkServerState($publishedDocs, 'published'); + + // unpublished + $finder = new DefaultDocumentFinder(); + $finder->setServerState('unpublished'); + $this->assertEquals(2, count($finder->getIds())); + + $unpublishedDocs = $finder->getIds(); + $this->assertEquals(2, count($unpublishedDocs)); + $this->checkServerState($unpublishedDocs, 'unpublished'); + + // deleted + $finder = new DefaultDocumentFinder(); + $finder->setServerState('deleted'); + $this->assertEquals(2, count($finder->getIds())); + + $deletedDocs = $finder->getIds(); + $this->assertEquals(2, count($deletedDocs)); + $this->checkServerState($deletedDocs, 'deleted'); + } + + /** + * Extended functionality: Grouping + */ + public function testGroupedDocumentTypes() + { + $this->markTestSkipped('TODO DOCTRINE DBAL Issue #129: Function groupedTypes() is no part of the DocumentFinderInterface'); + + $this->prepareDocuments(); + + // all + $finder = new DefaultDocumentFinder(); + $types = $finder->groupedTypes(); + $this->assertEquals(3, count($types)); + + // published + $finder = new DefaultDocumentFinder(); + $finder->setServerState('published'); + $types = $finder->groupedTypes(); + $this->assertEquals(2, count($types)); + + // unpublished + $finder = new DefaultDocumentFinder(); + $finder->setServerState('unpublished'); + $types = $finder->groupedTypes(); + $this->assertEquals(2, count($types)); + + // deleted + $finder = new DefaultDocumentFinder(); + $finder->setServerState('deleted'); + $types = $finder->groupedTypes(); + $this->assertEquals(2, count($types)); + } + + /** + * Extended functionality: Sorting + */ + public function testSortByAuthorLastName() + { + //$this->markTestSkipped('TODO DOCTRINE DBAL Issue #129'); + + $this->prepareDocuments(); + + // By Author + $finder = new DefaultDocumentFinder(); + + $finder->setOrder(DocumentFinderInterface::ORDER_AUTHOR); + + $docs = $finder->getIds(); + + $this->assertEquals(6, count($docs)); + + $this->assertEquals(4, $docs[4]); + $this->assertEquals(3, $docs[5]); + } + + public function testSortById() + { + //$this->markTestSkipped('TODO DOCTRINE DBAL Issue #129'); + + $this->prepareDocuments(); + + // By Id + $finder = new DefaultDocumentFinder(); + + $finder->setOrder(DocumentFinderInterface::ORDER_ID); + + $docs = $finder->getIds(); + + $this->assertEquals(6, count($docs)); + + $lastDoc = $docs[0]; + + foreach ($docs as $docId) { + if ($lastDoc > $docId) { + $this->fail('documents are not sorted by id'); + } + } + } + + /** + * @throws ModelException + * @throws SecurityException + * + * TODO the testdata for this text is not meaningfull + */ + public function testSortByServerDatePublished() + { + //$this->markTestSkipped('TODO DOCTRINE DBAL Issue #129'); + + $this->prepareDocuments(); + + // By ServerDatePublished + $finder = new DefaultDocumentFinder(); + + $finder->setOrder(DocumentFinderInterface::ORDER_SERVER_DATE_PUBLISHED); + + $docs = $finder->getIds(); + + $this->assertEquals(6, count($docs)); + + $lastDate = null; + + foreach ($docs as $docId) { + $doc = new Document($docId); + if ($lastDate === null) { + $lastDate = $doc->getServerDatePublished(); + } + + if ($lastDate !== null && $lastDate->compare($doc->getServerDatePublished()) === 1) { + $this->fail('documents are not sorted properly'); + } + } + } + + public function testSortByTitleMain() + { + //$this->markTestSkipped('TODO DOCTRINE DBAL Issue #129'); + + $this->prepareDocuments(); + + // By TitleMain + $finder = new DefaultDocumentFinder(); + + $finder->setOrder(DocumentFinderInterface::ORDER_TITLE); + + $docs = $finder->getIds(); + + $this->assertEquals(6, count($docs)); + + // documents without title come first (0-3) + $this->assertEquals(2, $docs[4]); + $this->assertEquals(1, $docs[5]); + } + + public function testSortByType() + { + //$this->markTestSkipped('TODO DOCTRINE DBAL Issue #129'); + + $this->prepareDocuments(); + + // By DocumentType + $finder = new DefaultDocumentFinder(); + + $finder->setOrder(DocumentFinderInterface::ORDER_DOCUMENT_TYPE); + + $docs = $finder->getIds(); + + $this->assertEquals(6, count($docs)); + + $expectedOrder = [2, 5, 3, 6, 1, 4]; + + foreach ($docs as $index => $docId) { + if ((int) $docId !== $expectedOrder[$index]) { + $this->fail('documents are not in expected order'); + } + } + } + + /** + * test for added functionality setServerDateCreated[Before|After]() + */ + public function testFindByDateCreated() + { + $this->markTestSkipped( + 'TODO DOCTRINE DBAL Issue #129: Function setServerDateCreatedAfter() and setServerDateCreatedBefore()' + . ' are no part of the DocumentFinderInterface' + ); + + $this->prepareDocuments(); + $date = new Date(); + $date->setNow(); + $date->setDay(date('d') - 1); + $date->setHour(date('H') - 1); + + $finder = new DefaultDocumentFinder(); + $this->assertEquals(6, $finder->getCount()); + $finder->setServerDateCreatedAfter(date("Y-m-d", time() + (60 * 60 * 24))); + $this->assertEquals(0, $finder->getCount()); + $finder = new DefaultDocumentFinder(); + $finder->setServerDateCreatedAfter(date("Y-m-d", time() - (60 * 60 * 24))); + $this->assertEquals(6, $finder->getCount()); + $finder = new DefaultDocumentFinder(); + $finder->setServerDateCreatedBefore(date("Y-m-d", time() - (60 * 60 * 24))); + $this->assertEquals(0, $finder->getCount()); + $finder = new DefaultDocumentFinder(); + $finder->setServerDateCreatedBefore(date("Y-m-d", time() + (60 * 60 * 24))); + $this->assertEquals(6, $finder->getCount()); + } + + public function testSetDependentModel() + { + $this->markTestSkipped('TODO DOCTRINE DBAL Issue #129: Function setDependentModel() is no part of the DocumentFinderInterface'); + + $docIds = []; + $doc1 = new Document(); + $docIds[] = $doc1->setType("article") + ->setServerState('published') + ->store(); + + $doc2 = new Document(); + $docIds[] = $doc2->setType("article") + ->setServerState('unpublished') + ->store(); + + $doc3 = new Document(); + $docIds[] = $doc3->setType("preprint") + ->setServerState('unpublished') + ->store(); + + // test dependent model + $title = $doc3->addTitleMain(); + $title->setValue('Ein deutscher Titel'); + $title->setLanguage('deu'); + $titleId = $title->store(); + + $title = new Title($titleId); + $docfinder = new DefaultDocumentFinder(); + $resultDocIds = $docfinder->setDependentModel($title)->getIds(); + $this->assertEquals(1, count($resultDocIds), 'Excpected 1 ID in result'); + $this->assertTrue(in_array($doc3->getId(), $resultDocIds), 'Expected Document-ID in result set'); + $this->assertFalse(in_array($doc1->getId(), $resultDocIds), 'Expected Document-ID not in result set'); + $this->assertFalse(in_array($doc2->getId(), $resultDocIds), 'Expected Document-ID not in result set'); + + // test linked model + //person + $author = new Person(); + $author->setFirstName('Karl'); + $author->setLastName('Tester'); + $author->setDateOfBirth('1857-11-26'); + $author->setPlaceOfBirth('Genf'); + + $doc2->addPersonAuthor($author); + $doc2->store(); + + $docfinder = new DefaultDocumentFinder(); + $resultDocIds = $docfinder->setDependentModel($author)->getIds(); + $this->assertEquals(1, count($resultDocIds), 'Excpected 1 ID in result'); + $this->assertTrue(in_array($doc2->getId(), $resultDocIds), 'Expected Document-ID in result set'); + $this->assertFalse(in_array($doc1->getId(), $resultDocIds), 'Expected Document-ID not in result set'); + $this->assertFalse(in_array($doc3->getId(), $resultDocIds), 'Expected Document-ID not in result set'); + + // licence + $licence = new Licence(); + $licence->setNameLong('LongNameLicence'); + $licence->setLinkLicence('http://licence.link'); + $licenceId = $licence->store(); + $doc1->addLicence($licence); + $doc1->store(); + + $licence = new Licence($licenceId); + $docfinder = new DefaultDocumentFinder(); + $resultDocIds = $docfinder->setDependentModel($licence)->getIds(); + + $this->assertEquals(1, count($resultDocIds), 'Excpected 1 ID in result'); + $this->assertTrue(in_array($doc1->getId(), $resultDocIds), 'Expected Document-ID in result set'); + $this->assertFalse(in_array($doc2->getId(), $resultDocIds), 'Expected Document-ID not in result set'); + $this->assertFalse(in_array($doc3->getId(), $resultDocIds), 'Expected Document-ID not in result set'); + + $doc2->addLicence($licence); + $doc2->store(); + + $resultDocIds = $docfinder->getIds(); + + $this->assertEquals(2, count($resultDocIds), 'Excpected 2 IDs in result'); + $this->assertTrue(in_array($doc1->getId(), $resultDocIds), 'Expected Document-ID in result set'); + $this->assertTrue(in_array($doc2->getId(), $resultDocIds), 'Expected Document-ID in result set'); + $this->assertFalse(in_array($doc3->getId(), $resultDocIds), 'Expected Document-ID not in result set'); + + // collections (are implemented differently) + $collectionRole = new CollectionRole(); + $collectionRole->setName("role-name-" . rand()); + $collectionRole->setOaiName("role-oainame-" . rand()); + $collectionRole->setVisible(1); + $collectionRole->setVisibleBrowsingStart(1); + $collectionRoleId = $collectionRole->store(); + + $collection = $collectionRole->addRootCollection(); + $collection->setTheme('dummy'); + $collectionId = $collection->store(); + + $doc1->addCollection($collection); + $doc1->store(); + $doc3->addCollection($collection); + $doc3->store(); + + $collection = new Collection($collectionId); + $docfinder = new DefaultDocumentFinder(); + $resultDocIds = $docfinder->setDependentModel($collection)->getIds(); + + $this->assertEquals(2, count($resultDocIds), 'Excpected 2 IDs in result'); + $this->assertTrue(in_array($doc1->getId(), $resultDocIds), 'Expected Document-ID in result set'); + $this->assertFalse(in_array($doc2->getId(), $resultDocIds), 'Expected Document-ID not in result set'); + $this->assertTrue(in_array($doc3->getId(), $resultDocIds), 'Expected Document-ID in result set'); + } + + public function testSetFilesVisibleInOai() + { + $this->markTestSkipped('TODO DOCTRINE DBAL Issue #129: Function setFilesVisibleInOai() is no part of the DocumentFinderInterface'); + + $visibleFileDoc = new Document(); + $visibleFile = new File(); + + $visibleFile->setPathName('visible_file.txt'); + $visibleFile->setVisibleInOai(true); + + $visibleFileDoc->addFile($visibleFile); + + $invisibleFileDoc = new Document(); + $invisibleFile = new File(); + + $invisibleFile->setPathName('invisible_file.txt'); + $invisibleFile->setVisibleInOai(false); + + $invisibleFileDoc->addFile($invisibleFile); + + $visibleFileDocId = $visibleFileDoc->store(); + $invisibleFileDocId = $invisibleFileDoc->store(); + + $mixedFileDoc = new Document(); + $visibleFile = new File(); + + $visibleFile->setPathName('another_visible_file.txt'); + $visibleFile->setVisibleInOai(true); + + $invisibleFile = new File(); + + $invisibleFile->setPathName('another_invisible_file.txt'); + $invisibleFile->setVisibleInOai(false); + + $mixedFileDoc->addFile($visibleFile); + $mixedFileDoc->addFile($invisibleFile); + + $mixedFileDocId = $mixedFileDoc->store(); + + $docfinder = new DefaultDocumentFinder(); + $docfinder->setFilesVisibleInOai(); + $foundIds = $docfinder->getIds(); + + $this->assertTrue(in_array($visibleFileDocId, $foundIds), 'Expected id of Document with visible file in OAI'); + $this->assertTrue(in_array($mixedFileDocId, $foundIds), 'Expected id of Document with visible and invisible file in OAI'); + $this->assertFalse(in_array($invisibleFileDocId, $foundIds), 'Expected no id of Document with invisible file in OAI'); + } + + public function testSetEmbargoDateBefore() + { + //$this->markTestSkipped('TODO DOCTRINE DBAL Issue #129'); + + $doc = new Document(); + $doc->setEmbargoDate('2016-10-16'); + $doc1Id = $doc->store(); + + $doc = new Document(); + $doc->setEmbargoDate('2016-10-14'); + $doc2Id = $doc->store(); + + $docfinder = new DefaultDocumentFinder(); + $docfinder->setEmbargoDateBefore('2016-10-15'); + $foundIds = $docfinder->getIds(); + + $this->assertCount(1, $foundIds); + $this->assertContains($doc2Id, $foundIds); + $this->assertNotContains($doc1Id, $foundIds); + } + + public function testSetEmbargoDateAfter() + { + $this->markTestSkipped('TODO DOCTRINE DBAL Issue #129: Function setEmbargoDateAfter() is no part of the DocumentFinderInterface'); + + $doc = new Document(); + $doc->setEmbargoDate('2016-10-16'); + $doc1Id = $doc->store(); + + $doc = new Document(); + $doc->setEmbargoDate('2016-10-14'); + $doc2Id = $doc->store(); + + $doc = new Document(); + $doc->setEmbargoDate('2016-10-15'); + $doc3Id = $doc->store(); + + $docfinder = new DefaultDocumentFinder(); + $docfinder->setEmbargoDateAfter('2016-10-15'); + $foundIds = $docfinder->getIds(); + + $this->assertCount(2, $foundIds); + $this->assertContains($doc1Id, $foundIds); + $this->assertContains($doc3Id, $foundIds); + $this->assertNotContains($doc2Id, $foundIds); + } + + public function testSetEmbargoDateRange() + { + $this->markTestSkipped('TODO DOCTRINE DBAL Issue #129: Function setEmbargoDateRange() is no part of the DocumentFinderInterface'); + + $doc = new Document(); + $doc->setEmbargoDate('2016-10-16'); // not in range + $doc1Id = $doc->store(); + + $doc = new Document(); + $doc->setEmbargoDate('2016-10-13'); // not in range + $doc2Id = $doc->store(); + + $doc = new Document(); + $doc->setEmbargoDate('2016-10-14'); // in range + $doc3Id = $doc->store(); + + $docfinder = new DefaultDocumentFinder(); + $docfinder->setEmbargoDateRange('2016-10-14', '2016-10-16'); + $foundIds = $docfinder->getIds(); + + $this->assertCount(1, $foundIds); + $this->assertContains($doc3Id, $foundIds); + $this->assertNotContains($doc1Id, $foundIds); + $this->assertNotContains($doc2Id, $foundIds); + } + + /** + * Tests from a perspective of two days in the future to avoid the need to manipulate ServerDateModified. + */ + public function testFindDocumentsWithExpiredEmbargoDateForUpdatingServerDateModified() + { + $this->markTestSkipped('TODO DOCTRINE DBAL Issue #129: Function setEmbargoDateBeforeNotModifiedAfter() is no part of the DocumentFinderInterface'); + + $tomorrow = date('Y-m-d', time() + (60 * 60 * 24)); + $dayaftertomorrow = date('Y-m-d', time() + (2 * 60 * 60 * 24)); + $today = date('Y-m-d', time()); + $yesterday = date('Y-m-d', time() - (60 * 60 * 24)); + + $doc = new Document(); + $doc->setEmbargoDate($dayaftertomorrow); + $notExpiredId = $doc->store(); // not in result - not yet expired embargo + + $doc = new Document(); + $doc->setEmbargoDate($yesterday); + $expiredUpdatedId = $doc->store(); // not in result - expired and saved after expiration + + $doc = new Document(); + $doc->setEmbargoDate($tomorrow); + $expiredNotUpdatedId = $doc->store(); // in result - expired and saved before expiration + + $docfinder = new DefaultDocumentFinder(); + $docfinder->setEmbargoDateBeforeNotModifiedAfter($dayaftertomorrow); + $foundIds = $docfinder->getIds(); + + $this->assertContains($expiredNotUpdatedId, $foundIds); + $this->assertNotContains($expiredUpdatedId, $foundIds); + $this->assertNotContains($notExpiredId, $foundIds); + } + + public function testSetEmbargoDateBeforeWithTime() + { + //$this->markTestSkipped('TODO DOCTRINE DBAL Issue #129'); + + $now = new Date(); + $now->setNow(); + + $past = new Date(); + $past->setDateTime(new DateTime(date('Y-m-d H:i:s', strtotime('-1 hour')))); + + $future = new Date(); + $future->setDateTime(new DateTime(date('Y-m-d H:i:s', strtotime('+1 hour')))); + + $doc = new Document(); + $doc->setEmbargoDate($past); + $pastId = $doc->store(); + + $doc = new Document(); + $doc->setEmbargoDate($now); + $nowId = $doc->store(); + + $doc = new Document(); + $doc->setEmbargoDate($future); + $futureId = $doc->store(); + + $docfinder = new DefaultDocumentFinder(); + $docfinder->setEmbargoDateBefore($now); + $foundIds = $docfinder->getIds(); + + $this->assertContains($pastId, $foundIds); + $this->assertNotContains($nowId, $foundIds); + $this->assertNotContains($futureId, $foundIds); + } + + public function testFindDocumentsForXMetaDissPlus() + { + $this->markTestSkipped('TODO DOCTRINE DBAL Issue #129'); + + $today = date('Y-m-d', time()); + + $doc = new Document(); + $doc->setServerState('published'); + $doc->setType('article'); + $publishedId = $doc->store(); + + $doc = new Document(); + $doc->setServerState('published'); + $doc->setType('periodical'); + $periodicalId = $doc->store(); + + $doc = new Document(); + $doc->setServerState('published'); + $doc->setType('article'); + $doc->setEmbargoDate($today); // today still in embargo until tomorrow + $embargoedId = $doc->store(); + + $doc = new Document(); + $doc->setServerState('unpublished'); + $unpublishedId = $doc->store(); + + $docfinder = new DefaultDocumentFinder(); + + $docfinder->setServerStateInList('published'); + $docfinder->setTypeInList('article'); + $docfinder->setNotEmbargoedOn($today); + + $foundIds = $docfinder->getIds(); + + $this->assertCount(1, $foundIds); + $this->assertContains($publishedId, $foundIds); + } +} From a517e2b9d40f9af718c732e3f40d6d630856f1de Mon Sep 17 00:00:00 2001 From: haogatyp Date: Thu, 10 Feb 2022 16:52:00 +0100 Subject: [PATCH 02/11] #129 Removed some code which was commented out. --- .../DocumentFinder/DefaultDocumentFinderTest.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php index 414679d7..56ca8ca2 100644 --- a/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php +++ b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php @@ -293,8 +293,6 @@ public function testGroupedDocumentTypes() */ public function testSortByAuthorLastName() { - //$this->markTestSkipped('TODO DOCTRINE DBAL Issue #129'); - $this->prepareDocuments(); // By Author @@ -312,8 +310,6 @@ public function testSortByAuthorLastName() public function testSortById() { - //$this->markTestSkipped('TODO DOCTRINE DBAL Issue #129'); - $this->prepareDocuments(); // By Id @@ -342,8 +338,6 @@ public function testSortById() */ public function testSortByServerDatePublished() { - //$this->markTestSkipped('TODO DOCTRINE DBAL Issue #129'); - $this->prepareDocuments(); // By ServerDatePublished @@ -371,8 +365,6 @@ public function testSortByServerDatePublished() public function testSortByTitleMain() { - //$this->markTestSkipped('TODO DOCTRINE DBAL Issue #129'); - $this->prepareDocuments(); // By TitleMain @@ -391,8 +383,6 @@ public function testSortByTitleMain() public function testSortByType() { - //$this->markTestSkipped('TODO DOCTRINE DBAL Issue #129'); - $this->prepareDocuments(); // By DocumentType @@ -600,8 +590,6 @@ public function testSetFilesVisibleInOai() public function testSetEmbargoDateBefore() { - //$this->markTestSkipped('TODO DOCTRINE DBAL Issue #129'); - $doc = new Document(); $doc->setEmbargoDate('2016-10-16'); $doc1Id = $doc->store(); @@ -706,8 +694,6 @@ public function testFindDocumentsWithExpiredEmbargoDateForUpdatingServerDateModi public function testSetEmbargoDateBeforeWithTime() { - //$this->markTestSkipped('TODO DOCTRINE DBAL Issue #129'); - $now = new Date(); $now->setNow(); From fdf2a15a3f23c8fc670db56605563a577d31ac94 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Wed, 16 Feb 2022 10:09:19 +0100 Subject: [PATCH 03/11] #129 Using the static function new() to instantiate documents. --- .../DefaultDocumentFinderTest.php | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php index 56ca8ca2..9d9c214f 100644 --- a/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php +++ b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php @@ -79,7 +79,7 @@ public function setUp() private function prepareDocuments() { - $publishedDoc1 = new Document(); + $publishedDoc1 = Document::new(); $publishedDoc1->setType("preprint") ->setServerState('published') ->store(); @@ -94,7 +94,7 @@ private function prepareDocuments() $publishedDoc1->store(); - $publishedDoc2 = new Document(); + $publishedDoc2 = Document::new(); $publishedDoc2->setType("article") ->setServerState('published') ->store(); @@ -105,7 +105,7 @@ private function prepareDocuments() $publishedDoc2->store(); - $unpublishedDoc1 = new Document(); + $unpublishedDoc1 = Document::new(); $unpublishedDoc1->setType("doctoral_thesis") ->setServerState('unpublished') ->store(); @@ -116,7 +116,7 @@ private function prepareDocuments() $unpublishedDoc1->store(); - $unpublishedDoc2 = new Document(); + $unpublishedDoc2 = Document::new(); $unpublishedDoc2->setType("preprint") ->setServerState('unpublished') ->store(); @@ -131,12 +131,12 @@ private function prepareDocuments() $unpublishedDoc2->store(); - $deletedDoc1 = new Document(); + $deletedDoc1 = Document::new(); $deletedDoc1->setType("article") ->setServerState('deleted') ->store(); - $deletedDoc2 = new Document(); + $deletedDoc2 = Document::new(); $deletedDoc2->setType("doctoral_thesis") ->setServerState('deleted') ->store(); @@ -439,17 +439,17 @@ public function testSetDependentModel() $this->markTestSkipped('TODO DOCTRINE DBAL Issue #129: Function setDependentModel() is no part of the DocumentFinderInterface'); $docIds = []; - $doc1 = new Document(); + $doc1 = Document::new(); $docIds[] = $doc1->setType("article") ->setServerState('published') ->store(); - $doc2 = new Document(); + $doc2 = Document::new(); $docIds[] = $doc2->setType("article") ->setServerState('unpublished') ->store(); - $doc3 = new Document(); + $doc3 = Document::new(); $docIds[] = $doc3->setType("preprint") ->setServerState('unpublished') ->store(); @@ -544,7 +544,7 @@ public function testSetFilesVisibleInOai() { $this->markTestSkipped('TODO DOCTRINE DBAL Issue #129: Function setFilesVisibleInOai() is no part of the DocumentFinderInterface'); - $visibleFileDoc = new Document(); + $visibleFileDoc = Document::new(); $visibleFile = new File(); $visibleFile->setPathName('visible_file.txt'); @@ -552,7 +552,7 @@ public function testSetFilesVisibleInOai() $visibleFileDoc->addFile($visibleFile); - $invisibleFileDoc = new Document(); + $invisibleFileDoc = Document::new(); $invisibleFile = new File(); $invisibleFile->setPathName('invisible_file.txt'); @@ -563,7 +563,7 @@ public function testSetFilesVisibleInOai() $visibleFileDocId = $visibleFileDoc->store(); $invisibleFileDocId = $invisibleFileDoc->store(); - $mixedFileDoc = new Document(); + $mixedFileDoc = Document::new(); $visibleFile = new File(); $visibleFile->setPathName('another_visible_file.txt'); @@ -590,11 +590,11 @@ public function testSetFilesVisibleInOai() public function testSetEmbargoDateBefore() { - $doc = new Document(); + $doc = Document::new(); $doc->setEmbargoDate('2016-10-16'); $doc1Id = $doc->store(); - $doc = new Document(); + $doc = Document::new(); $doc->setEmbargoDate('2016-10-14'); $doc2Id = $doc->store(); @@ -611,15 +611,15 @@ public function testSetEmbargoDateAfter() { $this->markTestSkipped('TODO DOCTRINE DBAL Issue #129: Function setEmbargoDateAfter() is no part of the DocumentFinderInterface'); - $doc = new Document(); + $doc = Document::new(); $doc->setEmbargoDate('2016-10-16'); $doc1Id = $doc->store(); - $doc = new Document(); + $doc = Document::new(); $doc->setEmbargoDate('2016-10-14'); $doc2Id = $doc->store(); - $doc = new Document(); + $doc = Document::new(); $doc->setEmbargoDate('2016-10-15'); $doc3Id = $doc->store(); @@ -637,15 +637,15 @@ public function testSetEmbargoDateRange() { $this->markTestSkipped('TODO DOCTRINE DBAL Issue #129: Function setEmbargoDateRange() is no part of the DocumentFinderInterface'); - $doc = new Document(); + $doc = Document::new(); $doc->setEmbargoDate('2016-10-16'); // not in range $doc1Id = $doc->store(); - $doc = new Document(); + $doc = Document::new(); $doc->setEmbargoDate('2016-10-13'); // not in range $doc2Id = $doc->store(); - $doc = new Document(); + $doc = Document::new(); $doc->setEmbargoDate('2016-10-14'); // in range $doc3Id = $doc->store(); @@ -671,15 +671,15 @@ public function testFindDocumentsWithExpiredEmbargoDateForUpdatingServerDateModi $today = date('Y-m-d', time()); $yesterday = date('Y-m-d', time() - (60 * 60 * 24)); - $doc = new Document(); + $doc = Document::new(); $doc->setEmbargoDate($dayaftertomorrow); $notExpiredId = $doc->store(); // not in result - not yet expired embargo - $doc = new Document(); + $doc = Document::new(); $doc->setEmbargoDate($yesterday); $expiredUpdatedId = $doc->store(); // not in result - expired and saved after expiration - $doc = new Document(); + $doc = Document::new(); $doc->setEmbargoDate($tomorrow); $expiredNotUpdatedId = $doc->store(); // in result - expired and saved before expiration @@ -703,15 +703,15 @@ public function testSetEmbargoDateBeforeWithTime() $future = new Date(); $future->setDateTime(new DateTime(date('Y-m-d H:i:s', strtotime('+1 hour')))); - $doc = new Document(); + $doc = Document::new(); $doc->setEmbargoDate($past); $pastId = $doc->store(); - $doc = new Document(); + $doc = Document::new(); $doc->setEmbargoDate($now); $nowId = $doc->store(); - $doc = new Document(); + $doc = Document::new(); $doc->setEmbargoDate($future); $futureId = $doc->store(); @@ -730,23 +730,23 @@ public function testFindDocumentsForXMetaDissPlus() $today = date('Y-m-d', time()); - $doc = new Document(); + $doc = Document::new(); $doc->setServerState('published'); $doc->setType('article'); $publishedId = $doc->store(); - $doc = new Document(); + $doc = Document::new(); $doc->setServerState('published'); $doc->setType('periodical'); $periodicalId = $doc->store(); - $doc = new Document(); + $doc = Document::new(); $doc->setServerState('published'); $doc->setType('article'); $doc->setEmbargoDate($today); // today still in embargo until tomorrow $embargoedId = $doc->store(); - $doc = new Document(); + $doc = Document::new(); $doc->setServerState('unpublished'); $unpublishedId = $doc->store(); From 1130a848c40bf6d94cc9b6833793ec5bbb7f3768 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Wed, 16 Feb 2022 10:24:14 +0100 Subject: [PATCH 04/11] #129 Use of new/renamed DocumentFinder functions in testFindDocumentsForXMetaDissPlus. --- tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php index 9d9c214f..75386097 100644 --- a/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php +++ b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php @@ -223,8 +223,6 @@ public function testAllConstraints() */ public function testIdsByState() { - // $this->markTestSkipped('TODO DOCTRINE DBAL Issue #129'); - $this->prepareDocuments(); // published @@ -726,8 +724,6 @@ public function testSetEmbargoDateBeforeWithTime() public function testFindDocumentsForXMetaDissPlus() { - $this->markTestSkipped('TODO DOCTRINE DBAL Issue #129'); - $today = date('Y-m-d', time()); $doc = Document::new(); @@ -752,8 +748,8 @@ public function testFindDocumentsForXMetaDissPlus() $docfinder = new DefaultDocumentFinder(); - $docfinder->setServerStateInList('published'); - $docfinder->setTypeInList('article'); + $docfinder->setServerState('published'); + $docfinder->setDocumentType('article'); $docfinder->setNotEmbargoedOn($today); $foundIds = $docfinder->getIds(); From f7655c2a295cec7de844414f9bdad2fd4593076a Mon Sep 17 00:00:00 2001 From: haogatyp Date: Wed, 16 Feb 2022 11:08:59 +0100 Subject: [PATCH 05/11] #129 Replaced some functions with their new counterparts. --- .../DefaultDocumentFinderTest.php | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php index 75386097..5c37245c 100644 --- a/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php +++ b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php @@ -258,31 +258,29 @@ public function testIdsByState() */ public function testGroupedDocumentTypes() { - $this->markTestSkipped('TODO DOCTRINE DBAL Issue #129: Function groupedTypes() is no part of the DocumentFinderInterface'); - $this->prepareDocuments(); // all $finder = new DefaultDocumentFinder(); - $types = $finder->groupedTypes(); + $types = $finder->getDocumentTypes(); $this->assertEquals(3, count($types)); // published $finder = new DefaultDocumentFinder(); $finder->setServerState('published'); - $types = $finder->groupedTypes(); + $types = $finder->getDocumentTypes(); $this->assertEquals(2, count($types)); // unpublished $finder = new DefaultDocumentFinder(); $finder->setServerState('unpublished'); - $types = $finder->groupedTypes(); + $types = $finder->getDocumentTypes(); $this->assertEquals(2, count($types)); // deleted $finder = new DefaultDocumentFinder(); $finder->setServerState('deleted'); - $types = $finder->groupedTypes(); + $types = $finder->getDocumentTypes(); $this->assertEquals(2, count($types)); } @@ -538,10 +536,8 @@ public function testSetDependentModel() $this->assertTrue(in_array($doc3->getId(), $resultDocIds), 'Expected Document-ID in result set'); } - public function testSetFilesVisibleInOai() + public function testSetHasFilesVisibleInOai() { - $this->markTestSkipped('TODO DOCTRINE DBAL Issue #129: Function setFilesVisibleInOai() is no part of the DocumentFinderInterface'); - $visibleFileDoc = Document::new(); $visibleFile = new File(); @@ -578,7 +574,7 @@ public function testSetFilesVisibleInOai() $mixedFileDocId = $mixedFileDoc->store(); $docfinder = new DefaultDocumentFinder(); - $docfinder->setFilesVisibleInOai(); + $docfinder->setHasFilesVisibleInOai(); $foundIds = $docfinder->getIds(); $this->assertTrue(in_array($visibleFileDocId, $foundIds), 'Expected id of Document with visible file in OAI'); @@ -662,8 +658,6 @@ public function testSetEmbargoDateRange() */ public function testFindDocumentsWithExpiredEmbargoDateForUpdatingServerDateModified() { - $this->markTestSkipped('TODO DOCTRINE DBAL Issue #129: Function setEmbargoDateBeforeNotModifiedAfter() is no part of the DocumentFinderInterface'); - $tomorrow = date('Y-m-d', time() + (60 * 60 * 24)); $dayaftertomorrow = date('Y-m-d', time() + (2 * 60 * 60 * 24)); $today = date('Y-m-d', time()); @@ -682,7 +676,8 @@ public function testFindDocumentsWithExpiredEmbargoDateForUpdatingServerDateModi $expiredNotUpdatedId = $doc->store(); // in result - expired and saved before expiration $docfinder = new DefaultDocumentFinder(); - $docfinder->setEmbargoDateBeforeNotModifiedAfter($dayaftertomorrow); + $docfinder->setEmbargoDateBefore($dayaftertomorrow); + $docfinder->setNotModifiedAfterEmbargoDate(); $foundIds = $docfinder->getIds(); $this->assertContains($expiredNotUpdatedId, $foundIds); From eaee48c6c913460a8530df1f64b62a46e2e00f0f Mon Sep 17 00:00:00 2001 From: haogatyp Date: Wed, 16 Feb 2022 14:46:19 +0100 Subject: [PATCH 06/11] #129 Ensuring that constraints can be used repeatedly. --- .../DocumentFinder/DefaultDocumentFinder.php | 157 ++++++++++++------ .../DefaultDocumentFinderTest.php | 29 ++++ 2 files changed, 132 insertions(+), 54 deletions(-) diff --git a/library/Opus/DocumentFinder/DefaultDocumentFinder.php b/library/Opus/DocumentFinder/DefaultDocumentFinder.php index d97955b3..f53b2739 100644 --- a/library/Opus/DocumentFinder/DefaultDocumentFinder.php +++ b/library/Opus/DocumentFinder/DefaultDocumentFinder.php @@ -49,6 +49,11 @@ */ class DefaultDocumentFinder implements DocumentFinderInterface { + /** + * @var int + */ + private $namedParameterCounter = 0; + /** @var Connection */ private $connection; @@ -67,8 +72,7 @@ public function __construct() */ public function getIds() { - $this->select->select("d.id"); - + $this->select->select('d.id'); $result = $this->select->execute()->fetchFirstColumn(); return array_unique($result); @@ -144,14 +148,16 @@ public function setOrder($criteria, $ascending = true) */ public function setDocumentIds($documentIds) { + $queryParam = $this->createQueryParameterName('setDocumentIdsParam'); + // Hotfix: If $subset is empty, return empty set. if (! is_array($documentIds) || count($documentIds) < 1) { $this->select->andWhere('1 = 0'); return $this; } - $this->select->andWhere('d.id IN (:setDocumentIds_documentIds)') - ->setParameter('setDocumentIds_documentIds', $documentIds, Connection::PARAM_STR_ARRAY); + $this->select->andWhere('d.id IN (:' . $queryParam . ')') + ->setParameter($queryParam, $documentIds, Connection::PARAM_STR_ARRAY); return $this; } @@ -163,14 +169,17 @@ public function setDocumentIds($documentIds) */ public function setDocumentIdRange($start = null, $end = null) { + $queryParamStart = $this->createQueryParameterName('setDocumentIdRangeStart'); + $queryParamEnd = $this->createQueryParameterName('setDocumentIdRangeEnd'); + if ($start !== null) { - $this->select->andWhere('d.id >= :setDocumentIdRange_start') - ->setParameter('setDocumentIdRange_start', $start); + $this->select->andWhere('d.id >= :' . $queryParamStart) + ->setParameter($queryParamStart, $start); } if ($end !== null) { - $this->select->andWhere('d.id <= :setDocumentIdRange_end') - ->setParameter('setDocumentIdRange_end', $end); + $this->select->andWhere('d.id <= :' . $queryParamEnd) + ->setParameter($queryParamEnd, $end); } return $this; } @@ -181,12 +190,14 @@ public function setDocumentIdRange($start = null, $end = null) */ public function setServerState($serverState) { + $queryParam = $this->createQueryParameterName('setServerStateParam'); + if (is_array($serverState)) { - $this->select->andWhere('server_state IN (:setServerState_serverStates)', $serverState) - ->setParameter('setServerState_serverStates', $serverState, Connection::PARAM_STR_ARRAY); + $this->select->andWhere('server_state IN (:' . $queryParam . ')', $serverState) + ->setParameter($queryParam, $serverState, Connection::PARAM_STR_ARRAY); } else { - $this->select->andWhere('server_state = :setServerState_serverState') - ->setParameter('setServerState_serverState', $serverState); + $this->select->andWhere('server_state = :' . $queryParam) + ->setParameter($queryParam, $serverState); } return $this; @@ -198,8 +209,10 @@ public function setServerState($serverState) */ public function setBelongsToBibliography($partOfBibliography = true) { - $this->select->andWhere('d.belongs_to_bibliography = :setBelongsToBibliography_partOfBibliography') - ->setParameter('setBelongsToBibliography_partOfBibliography', $partOfBibliography); + $queryParam = $this->createQueryParameterName('setBelongsToBibliographyParam'); + + $this->select->andWhere('d.belongs_to_bibliography = :' . $queryParam) + ->setParameter($queryParam, $partOfBibliography); return $this; } @@ -210,23 +223,21 @@ public function setBelongsToBibliography($partOfBibliography = true) */ public function setCollectionId($collectionId) { + $queryParam = $this->createQueryParameterName('setCollectionIdParam'); + $queryBuilder = $this->connection->createQueryBuilder(); $subSelect = $queryBuilder->select('l.document_id') ->from('link_documents_collections', 'l') ->where('l.document_id = d.id') - ->andWhere('l.collection_id IN (:setCollectionId_collectionId)'); + ->andWhere('l.collection_id IN (:' . $queryParam . ')'); $this->select->andWhere("EXISTS (" . $subSelect->getSQL() . ")"); if (is_array($collectionId)) { - $this->select->setParameter( - 'setCollectionId_collectionId', - $collectionId, - Connection::PARAM_STR_ARRAY - ); + $this->select->setParameter($queryParam, $collectionId, Connection::PARAM_STR_ARRAY); } else { - $this->select->setParameter('setCollectionId_collectionId', $collectionId); + $this->select->setParameter($queryParam, $collectionId); } return $this; @@ -238,6 +249,8 @@ public function setCollectionId($collectionId) */ public function setCollectionRoleId($roleId) { + $queryParam = $this->createQueryParameterName('setCollectionRoleIdParam'); + $queryBuilder = $this->connection->createQueryBuilder(); $subSelect = $queryBuilder->select('l.document_id') @@ -245,10 +258,10 @@ public function setCollectionRoleId($roleId) ->from('link_documents_collections', 'l') ->where('l.document_id = d.id') ->andWhere('l.collection_id = c.id') - ->andWhere('c.role_id = :setCollectionRoleId_roleId'); + ->andWhere('c.role_id = :' . $queryParam); $this->select->andWhere("EXISTS (" . $subSelect->getSQL() . ")") - ->setParameter('setCollectionRoleId_roleId', $roleId); + ->setParameter($queryParam, $roleId); return $this; } @@ -259,15 +272,17 @@ public function setCollectionRoleId($roleId) */ public function setIdentifierExists($name) { + $queryParam = $this->createQueryParameterName('setIdentifierExistsParam'); + $queryBuilder = $this->connection->createQueryBuilder(); $subSelect = $queryBuilder->select('i.id') ->from('document_identifiers', 'i') ->where('i.document_id = d.id') - ->andWhere('type = :setIdentifierExists_name'); + ->andWhere('type = :' . $queryParam); $this->select->andWhere("EXISTS (" . $subSelect->getSQL() . ")") - ->setParameter('setIdentifierExists_name', $name); + ->setParameter($queryParam, $name); return $this; } @@ -279,17 +294,20 @@ public function setIdentifierExists($name) */ public function setIdentifierValue($name, $value) { + $queryParamName = $this->createQueryParameterName('setIdentifierValueName'); + $queryParamValue = $this->createQueryParameterName('setIdentifierValue'); + $queryBuilder = $this->connection->createQueryBuilder(); - $subSelect = $queryBuilder->select('d.id') + $subSelect = $queryBuilder->select('id') ->from('document_identifiers', 'i') ->where('i.document_id = d.id') - ->andWhere('type = :setIdentifierValue_name') - ->andWhere('value = :setIdentifierValue_value'); + ->andWhere('type = :' . $queryParamName) + ->andWhere('value = :' . $queryParamValue); $this->select->andWhere("EXISTS (" . $subSelect->getSQL() . ")") - ->setParameter('setIdentifierValue_name', $name) - ->setParameter('setIdentifierValue_value', $value); + ->setParameter($queryParamName, $name) + ->setParameter($queryParamValue, $value); return $this; } @@ -300,12 +318,14 @@ public function setIdentifierValue($name, $value) */ public function setDocumentType($type) { + $queryParam = $this->createQueryParameterName('setDocumentTypeParam'); + if (is_array($type)) { - $this->select->andWhere('type IN (:setDocumentType_types)') - ->setParameter('setDocumentType_types', $type, Connection::PARAM_STR_ARRAY); + $this->select->andWhere('type IN (:' . $queryParam . ')') + ->setParameter($queryParam, $type, Connection::PARAM_STR_ARRAY); } else { - $this->select->andWhere('type = :setDocumentType_type') - ->setParameter('setDocumentType_type', $type); + $this->select->andWhere('type = :' . $queryParam) + ->setParameter($queryParam, $type); } return $this; } @@ -316,15 +336,17 @@ public function setDocumentType($type) */ public function setEnrichmentExists($name) { + $queryParam = $this->createQueryParameterName('setEnrichmentExistsParam'); + $queryBuilder = $this->connection->createQueryBuilder(); $subSelect = $queryBuilder->select('d.id') ->from('document_enrichments', e) ->where('document_id = d.id') - ->andWhere('key_name = :setEnrichmentExists_name'); + ->andWhere('key_name = :' . $queryParam); $this->select->andWhere('EXISTS (' . $subSelect->getSQL() . ')') - ->setParameter('setEnrichmentExists_name', $name); + ->setParameter($queryParam, $name); return $this; } @@ -336,17 +358,20 @@ public function setEnrichmentExists($name) */ public function setEnrichmentValue($key, $value) { + $queryParamKey = $this->createQueryParameterName('setEnrichmentValueKey'); + $queryParamValue = $this->createQueryParameterName('setEnrichmentValue'); + $queryBuilder = $this->connection->createQueryBuilder(); $subSelect = $queryBuilder->select('d.id') ->from('document_enrichments', 'e') ->where('document_id = d.id') - ->andWhere('key_name = :setEnrichmentValue_key') - ->andWhere('value = :setEnrichmentValue_value'); + ->andWhere('key_name = :' . $queryParamKey) + ->andWhere('value = :' . $queryParamValue); $this->select->andWhere("EXISTS (" . $subSelect->getSQL() . ")") - ->setParameter('setEnrichmentValue_key', $key) - ->setParameter('setEnrichmentValue_value', $value); + ->setParameter($queryParamKey, $key) + ->setParameter($queryParamValue, $value); return $this; } @@ -357,10 +382,12 @@ public function setEnrichmentValue($key, $value) */ public function setServerDatePublishedBefore($date) { + $queryParam = $this->createQueryParameterName('setServerDatePublishedBeforeParam'); + $this->finder->setServerDatePublishedBefore($date); - $this->select->andWhere('d.server_date_published < :setServerDatePublishedBefore_date') - ->setParameter('setServerDatePublishedBefore_date', $date); + $this->select->andWhere('d.server_date_published < :' . $queryParam) + ->setParameter($queryParam, $date); return $this; } @@ -372,10 +399,13 @@ public function setServerDatePublishedBefore($date) */ public function setServerDatePublishedRange($from, $until) { - $this->select->andWhere('d.server_date_published >= :setServerDatePublishedRange_from') - ->andWhere('d.server_date_published < :setServerDatePublishedRange_until') - ->setParameter('setServerDatePublishedRange_from', $from) - ->setParameter('setServerDatePublishedRange_until', $until); + $queryParamFrom = $this->createQueryParameterName('setServerDatePublishedRangeFrom'); + $queryParamUntil = $this->createQueryParameterName('setServerDatePublishedRangeUntil'); + + $this->select->andWhere('d.server_date_published >= :' . $queryParamFrom) + ->andWhere('d.server_date_published < :' . $queryParamUntil) + ->setParameter($queryParamFrom, $from) + ->setParameter($queryParamUntil, $until); return $this; } @@ -386,8 +416,10 @@ public function setServerDatePublishedRange($from, $until) */ public function setServerDateModifiedBefore($date) { - $this->select->andWhere('d.server_date_modified < :setServerDateModifiedBefore_date') - ->setParameter('setServerDateModifiedBefore_date', $date); + $queryParam = $this->createQueryParameterName('setServerDateModifiedBeforeDate'); + + $this->select->andWhere('d.server_date_modified < :' . $queryParam) + ->setParameter($queryParam, $date); return $this; } @@ -397,8 +429,10 @@ public function setServerDateModifiedBefore($date) */ public function setServerDateModifiedAfter($date) { - $this->select->andWhere('d.server_date_modified >= :setServerDateModifiedAfter_date') - ->setParameter('setServerDateModifiedAfter_data', $date); + $queryParam = $this->createQueryParameterName('setServerDateModifiedAfterDate'); + + $this->select->andWhere('d.server_date_modified >= :' . $queryParam) + ->setParameter($queryParam, $date); return $this; } @@ -409,8 +443,10 @@ public function setServerDateModifiedAfter($date) */ public function setEmbargoDateBefore($date) { - $this->select->andWhere('d.embargo_date < :setEmbargoDateBefore_date') - ->setParameter('setEmbargoDateBefore_date', $date); + $queryParam = $this->createQueryParameterName('setEmbargoDateBeforeDate'); + + $this->select->andWhere('d.embargo_date < :' . $queryParam) + ->setParameter($queryParam, $date); return $this; } @@ -421,8 +457,10 @@ public function setEmbargoDateBefore($date) */ public function setNotEmbargoedOn($date) { - $this->select->andWhere('d.embargo_date < :setNotEmbargoedOn_date or d.embargo_date IS NULL') - ->setParameter('setNotEmbargoedOn_date', $date); + $queryParam = $this->createQueryParameterName('setNotEmbargoedOnDate'); + + $this->select->andWhere('d.embargo_date < :' . $queryParam . ' or d.embargo_date IS NULL') + ->setParameter($queryParam, $date); return $this; } @@ -493,4 +531,15 @@ public function getYearsPublished() $this->select->select("substr(server_date_published, 1, 4)")->distinct(); return $this->select->execute()->fetchFirstColumn(); } + + /** + * Creates a unique named query parameter. + * + * @param string $prefix + */ + protected function createQueryParameterName($prefix) + { + return $prefix . $this->namedParameterCounter++; + } + } diff --git a/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php index 5c37245c..e7e36aa6 100644 --- a/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php +++ b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php @@ -74,6 +74,7 @@ public function setUp() 'persons', 'link_persons_documents', 'document_title_abstracts', + 'document_identifiers' ]); } @@ -218,6 +219,34 @@ public function testAllConstraints() $this->assertEquals(0, count($finder->getIds())); } + /** + * Basic functionality + */ + public function testDoubleConstraints() + { + $document = Document::new(); + $document->setType("article"); + + $title = $document->addTitleMain(); + $title->setValue('Title'); + $title->setLanguage('de'); + + $issn1 = $document->addIdentifierIssn(); + $issn1->setValue('1000-1000-1000'); + + $issn2 = $document->addIdentifierIssn(); + $issn2->setValue('2000-2000-2000'); + + $id = $document->store(); + + $finder = new DefaultDocumentFinder(); + $finder->setDocumentType('article'); + $finder->setIdentifierValue('issn', '123-123-123'); + $finder->setIdentifierValue('issn', '2000-2000-2000'); + + $this->assertEquals(0, count($finder->getIds())); + } + /** * Basic functionality */ From 8711f2ae9efa1ccb92e6b2967544e8b6fca2db02 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Wed, 16 Feb 2022 15:00:24 +0100 Subject: [PATCH 07/11] #129 Instantiation of the finder via extra create function instead of using new directly. --- .../DefaultDocumentFinderTest.php | 72 ++++++++++--------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php index e7e36aa6..c2ec9b7a 100644 --- a/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php +++ b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php @@ -78,6 +78,14 @@ public function setUp() ]); } + /** + * @return DefaultDocumentFinder + */ + private function createDocumentFinder() + { + return new DefaultDocumentFinder(); + } + private function prepareDocuments() { $publishedDoc1 = Document::new(); @@ -161,7 +169,7 @@ private function checkServerState($ids, $state) */ public function testCountOnEmptyDb() { - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $this->assertEquals(0, $finder->getCount()); } @@ -170,7 +178,7 @@ public function testCountOnEmptyDb() */ public function testIdsOnEmptyDb() { - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $this->assertEquals([], $finder->getIds()); } @@ -182,7 +190,7 @@ public function testAllEntriesNoConstraints() $this->prepareDocuments(); // published - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $this->assertEquals(6, $finder->getCount()); $this->assertEquals(6, count($finder->getIds())); } @@ -195,7 +203,7 @@ public function testAllConstraints() $this->markTestSkipped('TODO DOCTRINE DBAL Issue #129: Does this test even make sense?'); // published - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $finder->setEnrichmentKeyExists('foobar') ->setEnrichmentKeyValue('foo', 'bar') ->setIdRange(1, 2) @@ -239,7 +247,7 @@ public function testDoubleConstraints() $id = $document->store(); - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $finder->setDocumentType('article'); $finder->setIdentifierValue('issn', '123-123-123'); $finder->setIdentifierValue('issn', '2000-2000-2000'); @@ -255,7 +263,7 @@ public function testIdsByState() $this->prepareDocuments(); // published - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $finder->setServerState('published'); $this->assertEquals(2, $finder->getCount()); @@ -264,7 +272,7 @@ public function testIdsByState() $this->checkServerState($publishedDocs, 'published'); // unpublished - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $finder->setServerState('unpublished'); $this->assertEquals(2, count($finder->getIds())); @@ -273,7 +281,7 @@ public function testIdsByState() $this->checkServerState($unpublishedDocs, 'unpublished'); // deleted - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $finder->setServerState('deleted'); $this->assertEquals(2, count($finder->getIds())); @@ -290,24 +298,24 @@ public function testGroupedDocumentTypes() $this->prepareDocuments(); // all - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $types = $finder->getDocumentTypes(); $this->assertEquals(3, count($types)); // published - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $finder->setServerState('published'); $types = $finder->getDocumentTypes(); $this->assertEquals(2, count($types)); // unpublished - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $finder->setServerState('unpublished'); $types = $finder->getDocumentTypes(); $this->assertEquals(2, count($types)); // deleted - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $finder->setServerState('deleted'); $types = $finder->getDocumentTypes(); $this->assertEquals(2, count($types)); @@ -321,7 +329,7 @@ public function testSortByAuthorLastName() $this->prepareDocuments(); // By Author - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $finder->setOrder(DocumentFinderInterface::ORDER_AUTHOR); @@ -338,7 +346,7 @@ public function testSortById() $this->prepareDocuments(); // By Id - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $finder->setOrder(DocumentFinderInterface::ORDER_ID); @@ -366,7 +374,7 @@ public function testSortByServerDatePublished() $this->prepareDocuments(); // By ServerDatePublished - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $finder->setOrder(DocumentFinderInterface::ORDER_SERVER_DATE_PUBLISHED); @@ -393,7 +401,7 @@ public function testSortByTitleMain() $this->prepareDocuments(); // By TitleMain - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $finder->setOrder(DocumentFinderInterface::ORDER_TITLE); @@ -411,7 +419,7 @@ public function testSortByType() $this->prepareDocuments(); // By DocumentType - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $finder->setOrder(DocumentFinderInterface::ORDER_DOCUMENT_TYPE); @@ -444,17 +452,17 @@ public function testFindByDateCreated() $date->setDay(date('d') - 1); $date->setHour(date('H') - 1); - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $this->assertEquals(6, $finder->getCount()); $finder->setServerDateCreatedAfter(date("Y-m-d", time() + (60 * 60 * 24))); $this->assertEquals(0, $finder->getCount()); - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $finder->setServerDateCreatedAfter(date("Y-m-d", time() - (60 * 60 * 24))); $this->assertEquals(6, $finder->getCount()); - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $finder->setServerDateCreatedBefore(date("Y-m-d", time() - (60 * 60 * 24))); $this->assertEquals(0, $finder->getCount()); - $finder = new DefaultDocumentFinder(); + $finder = $this->createDocumentFinder(); $finder->setServerDateCreatedBefore(date("Y-m-d", time() + (60 * 60 * 24))); $this->assertEquals(6, $finder->getCount()); } @@ -486,7 +494,7 @@ public function testSetDependentModel() $titleId = $title->store(); $title = new Title($titleId); - $docfinder = new DefaultDocumentFinder(); + $docfinder = $this->createDocumentFinder(); $resultDocIds = $docfinder->setDependentModel($title)->getIds(); $this->assertEquals(1, count($resultDocIds), 'Excpected 1 ID in result'); $this->assertTrue(in_array($doc3->getId(), $resultDocIds), 'Expected Document-ID in result set'); @@ -504,7 +512,7 @@ public function testSetDependentModel() $doc2->addPersonAuthor($author); $doc2->store(); - $docfinder = new DefaultDocumentFinder(); + $docfinder = $this->createDocumentFinder(); $resultDocIds = $docfinder->setDependentModel($author)->getIds(); $this->assertEquals(1, count($resultDocIds), 'Excpected 1 ID in result'); $this->assertTrue(in_array($doc2->getId(), $resultDocIds), 'Expected Document-ID in result set'); @@ -520,7 +528,7 @@ public function testSetDependentModel() $doc1->store(); $licence = new Licence($licenceId); - $docfinder = new DefaultDocumentFinder(); + $docfinder = $this->createDocumentFinder(); $resultDocIds = $docfinder->setDependentModel($licence)->getIds(); $this->assertEquals(1, count($resultDocIds), 'Excpected 1 ID in result'); @@ -556,7 +564,7 @@ public function testSetDependentModel() $doc3->store(); $collection = new Collection($collectionId); - $docfinder = new DefaultDocumentFinder(); + $docfinder = $this->createDocumentFinder(); $resultDocIds = $docfinder->setDependentModel($collection)->getIds(); $this->assertEquals(2, count($resultDocIds), 'Excpected 2 IDs in result'); @@ -602,7 +610,7 @@ public function testSetHasFilesVisibleInOai() $mixedFileDocId = $mixedFileDoc->store(); - $docfinder = new DefaultDocumentFinder(); + $docfinder = $this->createDocumentFinder(); $docfinder->setHasFilesVisibleInOai(); $foundIds = $docfinder->getIds(); @@ -621,7 +629,7 @@ public function testSetEmbargoDateBefore() $doc->setEmbargoDate('2016-10-14'); $doc2Id = $doc->store(); - $docfinder = new DefaultDocumentFinder(); + $docfinder = $this->createDocumentFinder(); $docfinder->setEmbargoDateBefore('2016-10-15'); $foundIds = $docfinder->getIds(); @@ -646,7 +654,7 @@ public function testSetEmbargoDateAfter() $doc->setEmbargoDate('2016-10-15'); $doc3Id = $doc->store(); - $docfinder = new DefaultDocumentFinder(); + $docfinder = $this->createDocumentFinder(); $docfinder->setEmbargoDateAfter('2016-10-15'); $foundIds = $docfinder->getIds(); @@ -672,7 +680,7 @@ public function testSetEmbargoDateRange() $doc->setEmbargoDate('2016-10-14'); // in range $doc3Id = $doc->store(); - $docfinder = new DefaultDocumentFinder(); + $docfinder = $this->createDocumentFinder(); $docfinder->setEmbargoDateRange('2016-10-14', '2016-10-16'); $foundIds = $docfinder->getIds(); @@ -704,7 +712,7 @@ public function testFindDocumentsWithExpiredEmbargoDateForUpdatingServerDateModi $doc->setEmbargoDate($tomorrow); $expiredNotUpdatedId = $doc->store(); // in result - expired and saved before expiration - $docfinder = new DefaultDocumentFinder(); + $docfinder = $this->createDocumentFinder(); $docfinder->setEmbargoDateBefore($dayaftertomorrow); $docfinder->setNotModifiedAfterEmbargoDate(); $foundIds = $docfinder->getIds(); @@ -737,7 +745,7 @@ public function testSetEmbargoDateBeforeWithTime() $doc->setEmbargoDate($future); $futureId = $doc->store(); - $docfinder = new DefaultDocumentFinder(); + $docfinder = $this->createDocumentFinder(); $docfinder->setEmbargoDateBefore($now); $foundIds = $docfinder->getIds(); @@ -770,7 +778,7 @@ public function testFindDocumentsForXMetaDissPlus() $doc->setServerState('unpublished'); $unpublishedId = $doc->store(); - $docfinder = new DefaultDocumentFinder(); + $docfinder = $this->createDocumentFinder(); $docfinder->setServerState('published'); $docfinder->setDocumentType('article'); From 40e0c53f4bd1ae90bd71f88b4b156f6fc7b11328 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Wed, 16 Feb 2022 15:03:29 +0100 Subject: [PATCH 08/11] #129 Removed not needed hot fix. --- library/Opus/DocumentFinder/DefaultDocumentFinder.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/library/Opus/DocumentFinder/DefaultDocumentFinder.php b/library/Opus/DocumentFinder/DefaultDocumentFinder.php index f53b2739..2602d390 100644 --- a/library/Opus/DocumentFinder/DefaultDocumentFinder.php +++ b/library/Opus/DocumentFinder/DefaultDocumentFinder.php @@ -150,12 +150,6 @@ public function setDocumentIds($documentIds) { $queryParam = $this->createQueryParameterName('setDocumentIdsParam'); - // Hotfix: If $subset is empty, return empty set. - if (! is_array($documentIds) || count($documentIds) < 1) { - $this->select->andWhere('1 = 0'); - return $this; - } - $this->select->andWhere('d.id IN (:' . $queryParam . ')') ->setParameter($queryParam, $documentIds, Connection::PARAM_STR_ARRAY); From 51aff0df20100e2b800fb688c2712ade352aa6d5 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Wed, 16 Feb 2022 15:10:45 +0100 Subject: [PATCH 09/11] #129 Fixed coding style. --- .../Opus/DocumentFinder/DefaultDocumentFinder.php | 15 ++++++--------- .../DocumentFinder/DefaultDocumentFinderTest.php | 2 +- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/library/Opus/DocumentFinder/DefaultDocumentFinder.php b/library/Opus/DocumentFinder/DefaultDocumentFinder.php index 2602d390..3c1b4cd4 100644 --- a/library/Opus/DocumentFinder/DefaultDocumentFinder.php +++ b/library/Opus/DocumentFinder/DefaultDocumentFinder.php @@ -36,7 +36,6 @@ use Opus\DocumentFinderInterface; use function array_unique; -use function count; use function is_array; /** @@ -49,9 +48,7 @@ */ class DefaultDocumentFinder implements DocumentFinderInterface { - /** - * @var int - */ + /** @var int */ private $namedParameterCounter = 0; /** @var Connection */ @@ -164,7 +161,7 @@ public function setDocumentIds($documentIds) public function setDocumentIdRange($start = null, $end = null) { $queryParamStart = $this->createQueryParameterName('setDocumentIdRangeStart'); - $queryParamEnd = $this->createQueryParameterName('setDocumentIdRangeEnd'); + $queryParamEnd = $this->createQueryParameterName('setDocumentIdRangeEnd'); if ($start !== null) { $this->select->andWhere('d.id >= :' . $queryParamStart) @@ -288,7 +285,7 @@ public function setIdentifierExists($name) */ public function setIdentifierValue($name, $value) { - $queryParamName = $this->createQueryParameterName('setIdentifierValueName'); + $queryParamName = $this->createQueryParameterName('setIdentifierValueName'); $queryParamValue = $this->createQueryParameterName('setIdentifierValue'); $queryBuilder = $this->connection->createQueryBuilder(); @@ -352,7 +349,7 @@ public function setEnrichmentExists($name) */ public function setEnrichmentValue($key, $value) { - $queryParamKey = $this->createQueryParameterName('setEnrichmentValueKey'); + $queryParamKey = $this->createQueryParameterName('setEnrichmentValueKey'); $queryParamValue = $this->createQueryParameterName('setEnrichmentValue'); $queryBuilder = $this->connection->createQueryBuilder(); @@ -393,7 +390,7 @@ public function setServerDatePublishedBefore($date) */ public function setServerDatePublishedRange($from, $until) { - $queryParamFrom = $this->createQueryParameterName('setServerDatePublishedRangeFrom'); + $queryParamFrom = $this->createQueryParameterName('setServerDatePublishedRangeFrom'); $queryParamUntil = $this->createQueryParameterName('setServerDatePublishedRangeUntil'); $this->select->andWhere('d.server_date_published >= :' . $queryParamFrom) @@ -530,10 +527,10 @@ public function getYearsPublished() * Creates a unique named query parameter. * * @param string $prefix + * @return string */ protected function createQueryParameterName($prefix) { return $prefix . $this->namedParameterCounter++; } - } diff --git a/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php index c2ec9b7a..63b1933f 100644 --- a/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php +++ b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php @@ -74,7 +74,7 @@ public function setUp() 'persons', 'link_persons_documents', 'document_title_abstracts', - 'document_identifiers' + 'document_identifiers', ]); } From 9692924fd6187007dd80470fbf786375c8f57ac5 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Thu, 17 Feb 2022 15:26:21 +0100 Subject: [PATCH 10/11] #129 Added some basic functionality tests. --- .../DocumentFinder/DefaultDocumentFinder.php | 12 +- .../DefaultDocumentFinderTest.php | 228 +++++++++++++++--- 2 files changed, 205 insertions(+), 35 deletions(-) diff --git a/library/Opus/DocumentFinder/DefaultDocumentFinder.php b/library/Opus/DocumentFinder/DefaultDocumentFinder.php index 3c1b4cd4..8e0ac0a6 100644 --- a/library/Opus/DocumentFinder/DefaultDocumentFinder.php +++ b/library/Opus/DocumentFinder/DefaultDocumentFinder.php @@ -331,9 +331,9 @@ public function setEnrichmentExists($name) $queryBuilder = $this->connection->createQueryBuilder(); - $subSelect = $queryBuilder->select('d.id') - ->from('document_enrichments', e) - ->where('document_id = d.id') + $subSelect = $queryBuilder->select('id') + ->from('document_enrichments', 'e') + ->where('e.document_id = d.id') ->andWhere('key_name = :' . $queryParam); $this->select->andWhere('EXISTS (' . $subSelect->getSQL() . ')') @@ -375,8 +375,6 @@ public function setServerDatePublishedBefore($date) { $queryParam = $this->createQueryParameterName('setServerDatePublishedBeforeParam'); - $this->finder->setServerDatePublishedBefore($date); - $this->select->andWhere('d.server_date_published < :' . $queryParam) ->setParameter($queryParam, $date); @@ -407,7 +405,7 @@ public function setServerDatePublishedRange($from, $until) */ public function setServerDateModifiedBefore($date) { - $queryParam = $this->createQueryParameterName('setServerDateModifiedBeforeDate'); + $queryParam = $this->createQueryParameterName('setServerDateModifiedBeforeParam'); $this->select->andWhere('d.server_date_modified < :' . $queryParam) ->setParameter($queryParam, $date); @@ -420,7 +418,7 @@ public function setServerDateModifiedBefore($date) */ public function setServerDateModifiedAfter($date) { - $queryParam = $this->createQueryParameterName('setServerDateModifiedAfterDate'); + $queryParam = $this->createQueryParameterName('setServerDateModifiedAfterParam'); $this->select->andWhere('d.server_date_modified >= :' . $queryParam) ->setParameter($queryParam, $date); diff --git a/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php index 63b1933f..512eed16 100644 --- a/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php +++ b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php @@ -41,6 +41,7 @@ use Opus\Document; use Opus\DocumentFinder\DefaultDocumentFinder; use Opus\DocumentFinderInterface; +use Opus\Enrichment; use Opus\File; use Opus\Licence; use Opus\Model\ModelException; @@ -75,6 +76,8 @@ public function setUp() 'link_persons_documents', 'document_title_abstracts', 'document_identifiers', + 'document_enrichments', + 'enrichmentkeys', ]); } @@ -91,6 +94,7 @@ private function prepareDocuments() $publishedDoc1 = Document::new(); $publishedDoc1->setType("preprint") ->setServerState('published') + ->setBelongsToBibliography(true) ->store(); $title = $publishedDoc1->addTitleMain(); @@ -227,10 +231,98 @@ public function testAllConstraints() $this->assertEquals(0, count($finder->getIds())); } - /** - * Basic functionality - */ - public function testDoubleConstraints() + public function testIdsByState() + { + $this->prepareDocuments(); + + // published + $finder = $this->createDocumentFinder(); + $finder->setServerState('published'); + $this->assertEquals(2, $finder->getCount()); + + $publishedDocs = $finder->getIds(); + $this->assertEquals(2, count($publishedDocs)); + $this->checkServerState($publishedDocs, 'published'); + + // unpublished + $finder = $this->createDocumentFinder(); + $finder->setServerState('unpublished'); + $this->assertEquals(2, count($finder->getIds())); + + $unpublishedDocs = $finder->getIds(); + $this->assertEquals(2, count($unpublishedDocs)); + $this->checkServerState($unpublishedDocs, 'unpublished'); + + // deleted + $finder = $this->createDocumentFinder(); + $finder->setServerState('deleted'); + $this->assertEquals(2, count($finder->getIds())); + + $deletedDocs = $finder->getIds(); + $this->assertEquals(2, count($deletedDocs)); + $this->checkServerState($deletedDocs, 'deleted'); + } + + public function testSubsetOfDocumentIds() + { + for ($i = 0; $i < 10; $i++) { + $document = Document::new(); + $document->setType('book'); + $title = $document->addTitleMain(); + $title->setValue('Title' . $i); + $title->setLanguage('de'); + $document->store(); + } + + $finder = $this->createDocumentFinder(); + $finder->setDocumentIds([1, 3, 5, 7, 9]); + $this->assertEquals([1, 3, 5, 7, 9], $finder->getIds()); + + $finder = $this->createDocumentFinder(); + $finder->setDocumentIdRange(3, 7); + $this->assertEquals([3, 4, 5, 6, 7], $finder->getIds()); + } + + public function testIdentifierExists() + { + $document = Document::new(); + $isbn = $document->addIdentifierIsbn(); + $isbn->setValue('1234-1234-1234'); + $document->store(); + + $document = Document::new(); + $issn = $document->addIdentifierIssn(); + $issn->setValue('2345-2345-2345'); + $doi = $document->addIdentifierDoi(); + $doi->setValue('3576934857'); + $document->store(); + + $document = Document::new(); + $doi = $document->addIdentifierDoi(); + $doi->setValue('1234567890'); + $document->store(); + + $document = Document::new(); + $issn = $document->addIdentifierIssn(); + $issn->setValue('5678-5678-5678'); + $document->store(); + + $finder = $this->createDocumentFinder(); + $finder->setIdentifierExists('issn'); + $this->assertEquals(2, $finder->getCount()); + + $finder = $this->createDocumentFinder(); + $finder->setIdentifierExists('issn'); + $finder->setIdentifierExists('doi'); + $this->assertEquals(1, $finder->getCount()); + + $finder = $this->createDocumentFinder(); + $finder->setIdentifierExists('isbn'); + $finder->setIdentifierExists('doi'); + $this->assertEquals(0, $finder->getCount()); + } + + public function testIdentifierValue() { $document = Document::new(); $document->setType("article"); @@ -239,55 +331,135 @@ public function testDoubleConstraints() $title->setValue('Title'); $title->setLanguage('de'); + $isbn = $document->addIdentifierIsbn(); + $isbn->setValue('111-111-111'); + $issn1 = $document->addIdentifierIssn(); $issn1->setValue('1000-1000-1000'); $issn2 = $document->addIdentifierIssn(); $issn2->setValue('2000-2000-2000'); - $id = $document->store(); + $document->store(); + + $finder = $this->createDocumentFinder(); + $finder->setIdentifierValue('isbn', '111-111-111'); + $this->assertEquals(1, count($finder->getIds())); $finder = $this->createDocumentFinder(); $finder->setDocumentType('article'); $finder->setIdentifierValue('issn', '123-123-123'); $finder->setIdentifierValue('issn', '2000-2000-2000'); - $this->assertEquals(0, count($finder->getIds())); } - /** - * Basic functionality - */ - public function testIdsByState() + public function testEnrichments() { - $this->prepareDocuments(); + $enrichment1 = new Enrichment(); + $enrichment1->setKeyName('enrichmentKey1'); + $enrichment1->setValue('enrichment-value1'); + + $enrichment2 = new Enrichment(); + $enrichment2->setKeyName('enrichmentKey2'); + $enrichment2->setValue('enrichment-value2'); + + $doc1 = Document::new(); + $doc1->addEnrichment($enrichment1); + $doc1->addEnrichment($enrichment2); + $doc1Id = $doc1->store(); + + $enrichment3 = new Enrichment(); + $enrichment3->setKeyName('enrichmentKey1'); + $enrichment3->setValue('enrichment-value1'); + + $doc2 = Document::new(); + $doc2->addEnrichment($enrichment3); + $doc2->store(); - // published $finder = $this->createDocumentFinder(); - $finder->setServerState('published'); + $finder->setEnrichmentExists('enrichmentKey1'); $this->assertEquals(2, $finder->getCount()); + $finder->setEnrichmentExists('enrichmentKey2'); + $this->assertEquals(1, $finder->getCount()); - $publishedDocs = $finder->getIds(); - $this->assertEquals(2, count($publishedDocs)); - $this->checkServerState($publishedDocs, 'published'); + $finder = $this->createDocumentFinder(); + $finder->setEnrichmentValue('enrichmentKey2', 'enrichment-value2'); + $this->assertEquals([$doc1Id], $finder->getIds()); + } + + public function testServerDatePublished() + { + $doc = Document::new(); + $doc->setServerDatePublished('2022-01-01'); + $doc->store(); + + $doc = Document::new(); + $doc->setServerDatePublished('2021-10-20'); + $doc->store(); + + $doc = Document::new(); + $doc->setServerDatePublished('2021-08-10'); + $doc->store(); + + $doc = Document::new(); + $doc->setServerDatePublished('2021-07-08'); + $doc->store(); + + $doc = Document::new(); + $doc->setServerDatePublished('2021-01-01'); + $doc->store(); - // unpublished $finder = $this->createDocumentFinder(); - $finder->setServerState('unpublished'); - $this->assertEquals(2, count($finder->getIds())); + $finder->setServerDatePublishedBefore('2021-08-30'); + $this->assertEquals(3, $finder->getCount()); - $unpublishedDocs = $finder->getIds(); - $this->assertEquals(2, count($unpublishedDocs)); - $this->checkServerState($unpublishedDocs, 'unpublished'); + $finder = $this->createDocumentFinder(); + $finder->setServerDatePublishedRange('2021-07-01', '2021-10-30'); + $this->assertEquals(3, $finder->getCount()); - // deleted $finder = $this->createDocumentFinder(); - $finder->setServerState('deleted'); - $this->assertEquals(2, count($finder->getIds())); + $this->assertEquals(['2022', '2021'], $finder->getYearsPublished()); + } - $deletedDocs = $finder->getIds(); - $this->assertEquals(2, count($deletedDocs)); - $this->checkServerState($deletedDocs, 'deleted'); + public function testServerDateModified() + { + $doc = Document::new(); + $id = $doc->store(); + Document::setServerDateModifiedByIds(new Date('2022-01-01'), [$id]); + + $doc = Document::new(); + $id = $doc->store(); + Document::setServerDateModifiedByIds(new Date('2021-10-20'), [$id]); + + $doc = Document::new(); + $id = $doc->store(); + Document::setServerDateModifiedByIds(new Date('2021-08-10'), [$id]); + + $doc = Document::new(); + $id = $doc->store(); + Document::setServerDateModifiedByIds(new Date('2021-07-08'), [$id]); + + $doc = Document::new(); + $id = $doc->store(); + Document::setServerDateModifiedByIds(new Date('2021-01-01'), [$id]); + + $finder = $this->createDocumentFinder(); + $finder->setServerDateModifiedBefore('2021-08-30'); + $this->assertEquals(3, $finder->getCount()); + + $finder = $this->createDocumentFinder(); + $finder->setServerDateModifiedAfter('2021-08-01'); + $this->assertEquals(3, $finder->getCount()); + } + + public function testBelongsToBibliography() + { + $this->prepareDocuments(); + + $finder = $this->createDocumentFinder(); + $finder->setServerState('published'); + $finder->setBelongsToBibliography(true); + $this->assertEquals(1, $finder->getCount()); } /** From 875a67c46499a7f2297cc5363472e2e1bf8f5c6f Mon Sep 17 00:00:00 2001 From: haogatyp Date: Wed, 23 Feb 2022 17:18:14 +0100 Subject: [PATCH 11/11] #129 Added simple tests for setCollectionId, setCollectionRoleId und setNotInXmlCache. --- .../DefaultDocumentFinderTest.php | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php index 512eed16..8b91790f 100644 --- a/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php +++ b/tests/Opus/DocumentFinder/DefaultDocumentFinderTest.php @@ -38,6 +38,7 @@ use Opus\Collection; use Opus\CollectionRole; use Opus\Date; +use Opus\Db\DocumentXmlCache; use Opus\Document; use Opus\DocumentFinder\DefaultDocumentFinder; use Opus\DocumentFinderInterface; @@ -78,6 +79,10 @@ public function setUp() 'document_identifiers', 'document_enrichments', 'enrichmentkeys', + 'collections_roles', + 'collections', + 'link_documents_collections', + 'document_xml_cache', ]); } @@ -513,6 +518,88 @@ public function testSortByAuthorLastName() $this->assertEquals(3, $docs[5]); } + public function testCollections() + { + $collectionRoles = []; + $collections = []; + + for ($i = 0; $i < 4; $i++) { + $collectionRole = new CollectionRole(); + $collectionRole->setName("role-name-" . rand()); + $collectionRole->setOaiName("role-oainame-" . rand()); + $collectionRole->setVisible(1); + $collectionRole->setVisibleBrowsingStart(1); + $collectionRole->store(); + + $collectionRoles[$i] = $collectionRole; + + $collection = $collectionRole->addRootCollection(); + $collection->setTheme('dummy'); + $collection->store(); + + $collections[] = $collection; + } + + $doc1 = Document::new(); + $doc1->addCollection($collections[1]); + $doc1->store(); + + $doc2 = Document::new(); + $doc2->addCollection($collections[0]); + $doc2->store(); + + $doc3 = Document::new(); + $doc3->setType('article'); + $doc3->addCollection($collections[2]); + $doc3->addCollection($collections[1]); + $doc3->store(); + + $doc4 = Document::new(); + $doc4->setType('article'); + $doc4->addCollection($collections[2]); + $doc4->store(); + + $finder = $this->createDocumentFinder(); + $finder->setCollectionId($collections[2]->getId()); + $this->assertEquals(2, $finder->getCount()); + $finder->setCollectionId($collections[1]->getId()); + $this->assertEquals(1, $finder->getCount()); + + $finder = $this->createDocumentFinder(); + $finder->setDocumentType('article'); + $finder->setCollectionId($collections[1]->getId()); + $this->assertEquals(1, $finder->getCount()); + + $finder = $this->createDocumentFinder(); + $finder->setCollectionId($collectionRoles[1]->getId()); + $this->assertEquals(2, $finder->getCount()); + $finder->setCollectionId($collectionRoles[2]->getId()); + $this->assertEquals(1, $finder->getCount()); + + $finder = $this->createDocumentFinder(); + $finder->setDocumentType('article'); + $finder->setCollectionId($collectionRoles[1]->getId()); + $this->assertEquals(1, $finder->getCount()); + } + + public function testNotInXmlCache() + { + $documentIds = []; + + for ($i = 0; $i < 4; $i++) { + $doc = Document::new(); + $documentIds[$i] = $doc->store(); + } + + $xmlCache = new DocumentXmlCache(); + $xmlCache->delete('document_id = ' . $documentIds[2]); + + $finder = $this->createDocumentFinder(); + $finder->setNotInXmlCache(); + + $this->assertEquals(1, $finder->getCount()); + } + public function testSortById() { $this->prepareDocuments();