Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent error when the system is configured to use dc:modified, but neither concept nor scheme have dates #840

Merged
merged 1 commit into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion controller/WebController.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ public function invokeVocabularyConcept(Request $request)
}

/**
* Return the modified date. First try to get the modified date from the concept. If found, return this
* date.
*
* Then try to get the modified date from the vocabulary's main concept scheme. If found, then return this
* date.
*
* Finally, if no date found, return null.
*
* @param Concept $concept
* @param Vocabulary $vocab
* @return DateTime|null
Expand All @@ -260,7 +268,9 @@ protected function getModifiedDate(Concept $concept, Vocabulary $vocab)
$conceptSchemeGraph = $vocab->getConceptScheme($conceptSchemeURI);
if (!$conceptSchemeGraph->isEmpty()) {
$literal = $conceptSchemeGraph->getLiteral($conceptSchemeURI, "dc:modified");
$modifiedDate = $literal->getValue();
if ($literal) {
$modifiedDate = $literal->getValue();
}
}
}
}
Expand Down
70 changes: 58 additions & 12 deletions tests/WebControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,50 @@ class WebControllerTest extends TestCase
public function modifiedDateDataProvider()
{
return [
[null, null, false, null], # set #0
[$this->datetime('15-Feb-2009'), null, false, $this->datetime('15-Feb-2009')], # set #1
[null, $this->datetime('01-Feb-2009'), false, $this->datetime('01-Feb-2009')], # set #2
[null, $this->datetime('01-Feb-2009'), true, null] # set #3
# when there is no modified date for a concept, and there is no modified date for the main concept scheme,
# then it returns null.
[
null, # modified date from the concept
null, # modified date from the main concept scheme
false, # is the scheme empty?
false, # is the literal (dc:modified) null?
null # expected returned modified date
], # set #0
# when there is a modified date for a concept, it is returned immediately. Other values are unimportant.
[
$this->datetime('15-Feb-2009'), # modified date from the concept
null, # modified date from the main concept scheme
false, # is the scheme empty?
false, # is the literal (dc:modified) null?
$this->datetime('15-Feb-2009') # expected returned modified date
], # set #1
# when there is no modified date for a concept, but there is a modified date for the main concept scheme,
# this last value is then returned.
[
null, # modified date from the concept
$this->datetime('01-Feb-2009'), # modified date from the main concept scheme
false, # is the scheme empty?
false, # is the literal (dc:modified) null?
$this->datetime('01-Feb-2009') # expected returned modified date
], # set #2
# when there is no modified date for a concept, but the concept scheme is returned as empty by the model,
# then it returns null.
[
null, # modified date from the concept
$this->datetime('01-Feb-2009'), # modified date from the main concept scheme
true, # is the scheme empty?
false, # is the literal (dc:modified) null?
null # expected returned modified date
], # set #3
# when there is no modified date for a concept, there is one non-empty concept scheme, but this one
# does not have a dc:modified literal, then it returns null
[
null, # modified date from the concept
$this->datetime('01-Feb-2009'), # modified date from the main concept scheme
false, # is the scheme empty?
true, # is the literal (dc:modified) null?
null # expected returned modified date
]
];
}

Expand All @@ -36,7 +76,7 @@ private function datetime(string $string)
* Finally, if neither of the previous scenarios occur, then it returns null.
* @dataProvider modifiedDateDataProvider
*/
public function testGetModifiedDate($conceptDate, $schemeDate, $isSchemeEmpty, $expected)
public function testGetModifiedDate($conceptDate, $schemeDate, $isSchemeEmpty, $isLiteralNull, $expected)
{
$concept = Mockery::mock("Concept");
$concept
Expand All @@ -56,13 +96,19 @@ public function testGetModifiedDate($conceptDate, $schemeDate, $isSchemeEmpty, $
$scheme
->shouldReceive("isEmpty")
->andReturn($isSchemeEmpty);
$literal = Mockery::mock();
$scheme
->shouldReceive("getLiteral")
->andReturn($literal);
$literal
->shouldReceive("getValue")
->andReturn($schemeDate);
if ($isLiteralNull) {
$scheme
->shouldReceive("getLiteral")
->andReturn(null);
} else {
$literal = Mockery::mock();
$scheme
->shouldReceive("getLiteral")
->andReturn($literal);
$literal
->shouldReceive("getValue")
->andReturn($schemeDate);
}
}
$controller = Mockery::mock('WebController')
->shouldAllowMockingProtectedMethods()
Expand Down