Skip to content

Commit

Permalink
#345 Finder function for PublicationState statistic
Browse files Browse the repository at this point in the history
  • Loading branch information
j3nsch committed Apr 26, 2024
1 parent 4250f89 commit 612b293
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
13 changes: 13 additions & 0 deletions library/Opus/DocumentFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 8 additions & 0 deletions library/Opus/DocumentFinder/DefaultDocumentFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,12 @@ public function setPublicationState($state)
$this->finder->setPublicationState($state);
return $this;
}

/**
* @return array
*/
public function getPublicationStateCount()
{
return $this->finder->getPublicationStateCount();
}
}
62 changes: 62 additions & 0 deletions tests/Opus/DocumentFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

0 comments on commit 612b293

Please sign in to comment.