Skip to content

Commit

Permalink
Merge branch 'validate_work_exists_3_3_0' into 'stable-3_3_0'
Browse files Browse the repository at this point in the history
Validate Work exists

See merge request softwares-pkp/plugins_ojs/thoth-omp-plugin!48
  • Loading branch information
thiagolepidus committed Jan 28, 2025
2 parents b5ba5f6 + 348dac2 commit 0839b29
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 9 deletions.
2 changes: 2 additions & 0 deletions ThothPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* @brief Plugin for integration with Thoth for communication and synchronization of book data between the two platforms
*/

require_once(__DIR__ . '/vendor/autoload.php');

import('lib.pkp.classes.plugins.GenericPlugin');
import('plugins.generic.thoth.classes.ThothBadgeRender');
import('plugins.generic.thoth.classes.ThothNotification');
Expand Down
73 changes: 70 additions & 3 deletions classes/ThothValidator.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
* @brief Validate submission metadata to Thoth submit
*/

require_once(__DIR__ . '/../vendor/autoload.php');

use Biblys\Isbn\Isbn;
use ThothApi\GraphQL\Models\Work as ThothWork;

import('plugins.generic.thoth.classes.facades.ThothService');

Expand All @@ -25,6 +24,29 @@ public static function validate($submission)
{
$errors = [];

$publication = $submission->getCurrentPublication();
$doi = $publication->getStoredPubId('doi');
$doiUrl = ThothService::work()->getDoiResolvingUrl($doi);

if ($doiUrl !== null) {
$errors = array_merge($errors, self::validateDoiExists($doiUrl));
}

$request = Application::get()->getRequest();
$context = $request->getContext();
$dispatcher = $request->getDispatcher();

$landingPage = $dispatcher->url(
$request,
ROUTE_PAGE,
$context->getPath(),
'catalog',
'book',
$submission->getBestId()
);

$errors = array_merge($errors, self::validateLandingPageExists($landingPage));

$publicationFormats = Application::getRepresentationDao()
->getApprovedByPublicationId($submission->getData('currentPublicationId'))
->toArray();
Expand All @@ -44,7 +66,10 @@ public static function validateIsbn($publicationFormats)
foreach ($publicationFormats as $publicationFormat) {
try {
$isbn = ThothService::publication()->getIsbnByPublicationFormat($publicationFormat);
Isbn::validateAsIsbn13($isbn);
if ($isbn !== null) {
Isbn::validateAsIsbn13($isbn);
}
$errors = array_merge($errors, self::validateIsbnExists($isbn));
} catch (Exception $e) {
$errors[] = __('plugins.generic.thoth.validation.isbn', [
'isbn' => $isbn,
Expand All @@ -55,4 +80,46 @@ public static function validateIsbn($publicationFormats)

return $errors;
}

public static function validateDoiExists($doi)
{
$errors = [];

try {
$work = ThothService::work()->getByDoi($doi);
if ($work instanceof ThothWork) {
$errors[] = __('plugins.generic.thoth.validation.doiExists', ['doi' => $doi]);
}
} catch (Exception $e) {
return $errors;
}

return $errors;
}

public static function validateLandingPageExists($landingPage)
{
$errors = [];

$works = ThothService::work()->search($landingPage);

if (!empty($works)) {
$errors[] = __('plugins.generic.thoth.validation.landingPageExists', ['landingPage' => $landingPage]);
}

return $errors;
}

public static function validateIsbnExists($isbn)
{
$errors = [];

$publications = ThothService::publication()->search($isbn);

if (!empty($publications)) {
$errors[] = __('plugins.generic.thoth.validation.isbnExists', ['isbn' => $isbn]);
}

return $errors;
}
}
2 changes: 2 additions & 0 deletions classes/container/ThothContainerProvider.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* @brief Utility class to package all plugin container bindings
*/

require_once(__DIR__ . '/../../vendor/autoload.php');

use ThothApi\GraphQL\Client;

import('plugins.generic.thoth.classes.container.ContainerProvider');
Expand Down
6 changes: 6 additions & 0 deletions classes/services/ThothPublicationService.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public function new($params)
return $publication;
}

public function search($filter)
{
$thothClient = ThothContainer::getInstance()->get('client');
return $thothClient->publications(['filter' => $filter]);
}

public function register($publicationFormat, $workId, $chapterId = null)
{
$thothPublication = $this->newByPublicationFormat($publicationFormat);
Expand Down
14 changes: 13 additions & 1 deletion classes/services/ThothWorkService.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

class ThothWorkService
{
private function getDoiResolvingUrl($doi)
public function getDoiResolvingUrl($doi)
{
if (empty($doi)) {
return $doi;
Expand Down Expand Up @@ -119,6 +119,18 @@ public function get($thothWorkId)
return $thothClient->work($thothWorkId);
}

public function search($filter)
{
$thothClient = ThothContainer::getInstance()->get('client');
return $thothClient->works(['filter' => $filter]);
}

public function getByDoi($doi)
{
$thothClient = ThothContainer::getInstance()->get('client');
return $thothClient->workByDoi($doi);
}

public function registerBook($submission, $thothImprintId)
{
$thothBook = $this->newBySubmission($submission);
Expand Down
9 changes: 9 additions & 0 deletions locale/en_US/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ msgstr "The submission metadata can not be submitted to Thoth because of the fol
msgid "plugins.generic.thoth.validation.isbn"
msgstr "\"{$isbn}\" of \"{$formatName}\" publication format is not a valid ISBN-13. A valid ISBN-13 must be exactly 17 characters (numbers and hyphens)."

msgid "plugins.generic.thoth.validation.doiExists"
msgstr "A work with DOI \"{$doi}\" already exists."

msgid "plugins.generic.thoth.validation.landingPageExists"
msgstr "A work with the landing page \"{$landingPage}\" already exists."

msgid "plugins.generic.thoth.validation.isbnExists"
msgstr "A publication with the ISBN \"{$isbn}\" already exists."

msgid "plugins.generic.thoth.register.confirmation"
msgstr "Do you want to register in Thoth the metadata related to this submission?"

Expand Down
9 changes: 9 additions & 0 deletions locale/es_ES/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ msgstr "Los metadatos de envio no pueden ser enviados a Thoth debido a los sigui
msgid "plugins.generic.thoth.validation.isbn"
msgstr "\"{$isbn}\" del formato de publicación \"{$formatName}\" no es un ISBN-13 válido. Un ISBN-13 válido debe tener exactamente 17 caracteres (números y guiones)."

msgid "plugins.generic.thoth.validation.doiExists"
msgstr "Un trabajo con el DOI \"{$doi}\" ya existe."

msgid "plugins.generic.thoth.validation.landingPageExists"
msgstr "Un trabajo con la página de destino \"{$landingPage}\" ya existe."

msgid "plugins.generic.thoth.validation.isbnExists"
msgstr "Una publicación con el ISBN \"{$isbn}\" ya existe."

msgid "plugins.generic.thoth.register.confirmation"
msgstr "¿Desea registrar en Thoth los metadatos relacionados con este envío?"

Expand Down
9 changes: 9 additions & 0 deletions locale/pt_BR/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ msgstr "Os metadados da submissão não podem ser enviados para Thoth devido aos
msgid "plugins.generic.thoth.validation.isbn"
msgstr "\"{$isbn}\" do formato de publicação \"{$formatName}\" não é um ISBN-13 válido. Um ISBN-13 válido deve ter exatamente 17 caracteres (números e hífens)."

msgid "plugins.generic.thoth.validation.doiExists"
msgstr "Um trabalho com o DOI \"{$doi}\" já existe."

msgid "plugins.generic.thoth.validation.landingPageExists"
msgstr "Um trabalho com a página de destino \"{$landingPage}\" já existe."

msgid "plugins.generic.thoth.validation.isbnExists"
msgstr "Uma publicação com o ISBN \"{$isbn}\" já existe."

msgid "plugins.generic.thoth.register.confirmation"
msgstr "Deseja realmente registrar na Thoth os metadados relacionados a essa submissão?"

Expand Down
86 changes: 86 additions & 0 deletions tests/classes/ThothValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
* @brief Test class for the ThothValidator class
*/

use ThothApi\GraphQL\Client as ThothClient;
use ThothApi\GraphQL\Models\Publication as ThothPublication;
use ThothApi\GraphQL\Models\Work as ThothWork;

import('lib.pkp.tests.PKPTestCase');
import('plugins.generic.thoth.classes.ThothValidator');
import('classes.publicationFormat.PublicationFormat');
Expand Down Expand Up @@ -73,4 +77,86 @@ public function testISBNValidationFails()
'##plugins.generic.thoth.validation.isbn##'
], $errors);
}

public function testDOIExistsValidationFails()
{
$doi = 'https://doi.org/10.12345/12345678';

$mockThothClient = $this->getMockBuilder(ThothClient::class)
->setMethods([
'workByDoi',
])
->getMock();
$mockThothClient->expects($this->any())
->method('workByDoi')
->will($this->returnValue(new ThothWork([
'doi' => $doi
])));

ThothContainer::getInstance()->set('client', function () use ($mockThothClient) {
return $mockThothClient;
});

$errors = ThothValidator::validateDoiExists($doi);

$this->assertEquals([
'##plugins.generic.thoth.validation.doiExists##',
], $errors);
}

public function testLandingPageExistsValidationFails()
{
$landingPage = 'http://www.publicknowledge.omp/index.php/publicknowledge/catalog/book/14';

$mockThothClient = $this->getMockBuilder(ThothClient::class)
->setMethods([
'works',
])
->getMock();
$mockThothClient->expects($this->any())
->method('works')
->will($this->returnValue([
new ThothWork([
'landingPage' => $landingPage
])
]));

ThothContainer::getInstance()->set('client', function () use ($mockThothClient) {
return $mockThothClient;
});

$errors = ThothValidator::validateLandingPageExists($landingPage);

$this->assertEquals([
'##plugins.generic.thoth.validation.landingPageExists##',
], $errors);
}

public function testISBNExistsValidationFails()
{
$isbn = '978-65-89999-01-3';

$mockThothClient = $this->getMockBuilder(ThothClient::class)
->setMethods([
'publications',
])
->getMock();
$mockThothClient->expects($this->any())
->method('publications')
->will($this->returnValue([
new ThothPublication([
'isbn' => $isbn
])
]));

ThothContainer::getInstance()->set('client', function () use ($mockThothClient) {
return $mockThothClient;
});

$errors = ThothValidator::validateIsbnExists($isbn);

$this->assertEquals([
'##plugins.generic.thoth.validation.isbnExists##',
], $errors);
}
}
2 changes: 0 additions & 2 deletions tests/classes/services/ThothContributorServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,13 @@ public function testGetManyContributors()
'firstName' => 'Brenna Clarke',
'lastName' => 'Gray',
'fullName' => 'Brenna Clarke Gray',
// 'orcid' => 'https://orcid.org/0000-0002-6079-0484',
'website' => 'http://brennaclarkegray.ca'
]),
new ThothContributor([
'contributorId' => '5b0d32d4-bfd9-4db1-88fb-4cb91bdaf246',
'firstName' => 'Dilton Oliveira de',
'lastName' => 'Araújo',
'fullName' => 'Dilton Oliveira de Araújo',
// 'orcid' => null,
'website' => 'http://buscatextual.cnpq.br/buscatextual/visualizacv.do?id=B00408'
])
]));
Expand Down
32 changes: 31 additions & 1 deletion tests/classes/services/ThothPublicationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function testCreateNewPublicationByPublicationFormat()
$this->assertEquals($expectedPublication, $publication);
}

public function testCreateNewContributor()
public function testCreateNewPublication()
{
$expectedPublication = new ThothPublication();
$expectedPublication->setPublicationType(ThothPublication::PUBLICATION_TYPE_PDF);
Expand All @@ -179,6 +179,36 @@ public function testCreateNewContributor()
$this->assertEquals($expectedPublication, $publication);
}

public function testSearchPublication()
{
$isbn = '978-65-89999-01-3';

$expectedThothPublications = [
new ThothPublication([
'isbn' => $isbn
])
];

$mockThothClient = $this->getMockBuilder(ThothClient::class)
->setMethods(['publications'])
->getMock();
$mockThothClient->expects($this->any())
->method('publications')
->will($this->returnValue([
new ThothPublication([
'isbn' => $isbn
])
]));

ThothContainer::getInstance()->set('client', function () use ($mockThothClient) {
return $mockThothClient;
});

$thothPublications = $this->publicationService->search($isbn);

$this->assertEquals($expectedThothPublications, $thothPublications);
}

public function testRegisterPublication()
{
$workId = '2a065323-76cd-4f54-b83b-19f2a925f426';
Expand Down
Loading

0 comments on commit 0839b29

Please sign in to comment.