diff --git a/library/Opus/DocumentFinder.php b/library/Opus/DocumentFinder.php index 0bd08933..291de568 100644 --- a/library/Opus/DocumentFinder.php +++ b/library/Opus/DocumentFinder.php @@ -815,4 +815,17 @@ public function setPublicationState($state) } return $this; } + + /** + * Returns count of documents in used publication states. + * + * @return array + */ + public function getPublicationStateCount() + { + $this->_select->reset('columns'); + $this->_select->columns(['publication_state', 'count(DISTINCT id)']); + $this->_select->group('publication_state'); + return $this->_db->fetchPairs($this->_select); + } } diff --git a/library/Opus/DocumentFinder/DefaultDocumentFinder.php b/library/Opus/DocumentFinder/DefaultDocumentFinder.php index dc1f6504..017000f8 100644 --- a/library/Opus/DocumentFinder/DefaultDocumentFinder.php +++ b/library/Opus/DocumentFinder/DefaultDocumentFinder.php @@ -345,4 +345,12 @@ public function setPublicationState($state) $this->finder->setPublicationState($state); return $this; } + + /** + * @return array + */ + public function getPublicationStateCount() + { + return $this->finder->getPublicationStateCount(); + } } diff --git a/tests/Opus/DocumentFinderTest.php b/tests/Opus/DocumentFinderTest.php index 7348570f..cc6453a3 100644 --- a/tests/Opus/DocumentFinderTest.php +++ b/tests/Opus/DocumentFinderTest.php @@ -983,4 +983,66 @@ public function testSetPublicationStateMultipleValues() $this->assertContains($docId1, $result); $this->assertContains($docId2, $result); } + + public function testGetPublicationStateCount() + { + $doc = Document::new(); + $doc->setPublicationState(PublicationState::SUBMITTED); + $doc->store(); + + $doc = Document::new(); + $doc->setPublicationState(PublicationState::SUBMITTED); + $doc->store(); + + $doc = Document::new(); + $doc->setPublicationState(PublicationState::DRAFT); + $doc->store(); + + $finder = new DefaultDocumentFinder(); + + $result = $finder->getPublicationStateCount(); + + $this->assertIsArray($result); + $this->assertCount(2, $result); + $this->assertArrayHasKey(PublicationState::SUBMITTED, $result); + $this->assertEquals(2, $result[PublicationState::SUBMITTED]); + $this->assertArrayHasKey(PublicationState::DRAFT, $result); + $this->assertEquals(1, $result[PublicationState::DRAFT]); + } + + public function testGetPublicationStateCountNoDocuments() + { + $finder = new DefaultDocumentFinder(); + + $result = $finder->getPublicationStateCount(); + + $this->assertIsArray($result); + $this->assertCount(0, $result); + } + + public function testGetPublicationStateCountForPublishedDocuments() + { + $doc = Document::new(); + $doc->setPublicationState(PublicationState::SUBMITTED); + $doc->setServerState(Document::STATE_PUBLISHED); + $doc->store(); + + $doc = Document::new(); + $doc->setPublicationState(PublicationState::SUBMITTED); + $doc->store(); + + $doc = Document::new(); + $doc->setPublicationState(PublicationState::DRAFT); + $doc->store(); + + $finder = new DefaultDocumentFinder(); + + $finder->setServerState(Document::STATE_PUBLISHED); + $result = $finder->getPublicationStateCount(); + + $this->assertIsArray($result); + $this->assertCount(1, $result); + $this->assertArrayHasKey(PublicationState::SUBMITTED, $result); + $this->assertEquals(1, $result[PublicationState::SUBMITTED]); + } }