|
| 1 | +<?php |
| 2 | + |
| 3 | +import('lib.pkp.tests.DatabaseTestCase'); |
| 4 | +import('plugins.generic.dataverse.classes.CrossrefXmlEditor'); |
| 5 | +import('plugins.generic.dataverse.classes.dataverseStudy.DataverseStudy'); |
| 6 | +import('plugins.generic.dataverse.classes.dataverseStudy.DataverseStudyDAO'); |
| 7 | +import('plugins.generic.dataverse.classes.entities.Dataset'); |
| 8 | +import('plugins.generic.dataverse.dataverseAPI.actions.DatasetActions'); |
| 9 | +import('plugins.generic.dataverse.classes.dispatchers.DataStatementDispatcher'); |
| 10 | +import('plugins.generic.dataverse.DataversePlugin'); |
| 11 | + |
| 12 | +use Illuminate\Database\Capsule\Manager as Capsule; |
| 13 | + |
| 14 | +class CrossrefXmlEditorTest extends DatabaseTestCase |
| 15 | +{ |
| 16 | + private $xmlEditor; |
| 17 | + private $doc; |
| 18 | + private $contextId = 1; |
| 19 | + private $submissionId = null; |
| 20 | + private $doiId = null; |
| 21 | + private $doi = '10.1234/PublicKnowledge.17'; |
| 22 | + private $study = null; |
| 23 | + private $dataset = null; |
| 24 | + private $persistentId = 'doi:10.5072/FK2/ABCDEF'; |
| 25 | + |
| 26 | + public function setUp(): void |
| 27 | + { |
| 28 | + parent::setUp(); |
| 29 | + $plugin = new DataversePlugin(); |
| 30 | + $dispatcher = new DataStatementDispatcher($plugin); |
| 31 | + |
| 32 | + $this->doc = $this->createTestXml(); |
| 33 | + $this->submissionId = $this->createTestSubmission(); |
| 34 | + $this->study = $this->createDataverseStudy(); |
| 35 | + $this->dataset = $this->createTestDataset(); |
| 36 | + $this->xmlEditor = $this->createXmlEditor(); |
| 37 | + } |
| 38 | + |
| 39 | + protected function getAffectedTables(): array |
| 40 | + { |
| 41 | + return ['submissions', 'submission_settings', 'publications', 'publication_settings', 'dataverse_studies']; |
| 42 | + } |
| 43 | + |
| 44 | + private function createTestSubmission(): int |
| 45 | + { |
| 46 | + $submissionDao = DAORegistry::getDAO('SubmissionDAO'); |
| 47 | + $submission = $submissionDao->newDataObject(); |
| 48 | + $submission->setData('contextId', $this->contextId); |
| 49 | + $submissionId = $submissionDao->insertObject($submission); |
| 50 | + |
| 51 | + $publicationDao = DAORegistry::getDAO('PublicationDAO'); |
| 52 | + $publication = $publicationDao->newDataObject(); |
| 53 | + $publication->setData('pub-id::doi', $this->doi); |
| 54 | + $publication->setData('submissionId', $submissionId); |
| 55 | + $pubId = $publicationDao->insertObject($publication); |
| 56 | + |
| 57 | + Capsule::table('publication_settings')->insert([ |
| 58 | + 'publication_id' => $pubId, |
| 59 | + 'locale' => '', |
| 60 | + 'setting_name' => 'pub-id::doi', |
| 61 | + 'setting_value' => $this->doi, |
| 62 | + ]); |
| 63 | + |
| 64 | + return $submissionId; |
| 65 | + } |
| 66 | + |
| 67 | + private function createDataverseStudy(): DataverseStudy |
| 68 | + { |
| 69 | + $study = new DataverseStudy(); |
| 70 | + $study->setSubmissionId($this->submissionId); |
| 71 | + $study->setEditUri('https://demo.dataverse.org/dvn/api/data-deposit/v1.1/swordv2/edit/study/' . $this->persistentId); |
| 72 | + $study->setEditMediaUri('https://demo.dataverse.org/dvn/api/data-deposit/v1.1/swordv2/edit-media/study/' . $this->persistentId); |
| 73 | + $study->setStatementUri('https://demo.dataverse.org/dvn/api/data-deposit/v1.1/swordv2/statement/study/' . $this->persistentId); |
| 74 | + $study->setPersistentUri('https://doi.org/10.5072/FK2/ABCDEF'); |
| 75 | + $study->setPersistentId($this->persistentId); |
| 76 | + |
| 77 | + $dataverseStudyDao = new DataverseStudyDAO(); |
| 78 | + $studyId = $dataverseStudyDao->insertStudy($study); |
| 79 | + $study->setId($studyId); |
| 80 | + |
| 81 | + return $study; |
| 82 | + } |
| 83 | + |
| 84 | + private function createTestXml() |
| 85 | + { |
| 86 | + $xml = new DOMDocument('1.0', 'UTF-8'); |
| 87 | + $xml->appendChild($xml->createElement('work')); |
| 88 | + |
| 89 | + return $xml; |
| 90 | + } |
| 91 | + |
| 92 | + private function createTestDataset(): Dataset |
| 93 | + { |
| 94 | + $dataset = new Dataset(); |
| 95 | + $dataset->setPersistentId($this->persistentId); |
| 96 | + $dataset->setVersionState(Dataset::VERSION_STATE_RELEASED); |
| 97 | + |
| 98 | + return $dataset; |
| 99 | + } |
| 100 | + |
| 101 | + private function createXmlEditor(): CrossrefXmlEditor |
| 102 | + { |
| 103 | + $mockDatasetActions = $this->createMock(DatasetActions::class); |
| 104 | + $mockDatasetActions->method('get')->willReturn($this->dataset); |
| 105 | + |
| 106 | + return new CrossrefXmlEditor($mockDatasetActions); |
| 107 | + } |
| 108 | + |
| 109 | + public function testAddsDatasetRelationToWorkNode(): void |
| 110 | + { |
| 111 | + $workNode = $this->doc->documentElement; |
| 112 | + |
| 113 | + $result = $this->xmlEditor->addDatasetRelationToWorkNode($workNode, $this->persistentId); |
| 114 | + |
| 115 | + $programNode = $result->getElementsByTagNameNS('http://www.crossref.org/relations.xsd', 'program')->item(0); |
| 116 | + $resultXml = $result->ownerDocument->saveXML($programNode); |
| 117 | + |
| 118 | + $expectedXml = file_get_contents(__DIR__ . '/fixtures/crossref/expected/dataset_relation.xml'); |
| 119 | + |
| 120 | + $this->assertXmlStringEqualsXmlString($expectedXml, $resultXml); |
| 121 | + } |
| 122 | + |
| 123 | + public function testAddsDatasetRelationToDepositXml(): void |
| 124 | + { |
| 125 | + $this->assertAddingOfRelationToXmlMatchesExpected('preprint_deposit.xml'); |
| 126 | + $this->assertAddingOfRelationToXmlMatchesExpected('article_deposit.xml'); |
| 127 | + } |
| 128 | + |
| 129 | + public function testAddsRelationToXmlAlreadyWithRelation(): void |
| 130 | + { |
| 131 | + $this->assertAddingOfRelationToXmlMatchesExpected('preprint_deposit_with_relation.xml'); |
| 132 | + } |
| 133 | + |
| 134 | + private function assertAddingOfRelationToXmlMatchesExpected(string $fixture): void |
| 135 | + { |
| 136 | + $depositXml = new DOMDocument(); |
| 137 | + $depositXml->load(__DIR__ . '/fixtures/crossref/' . $fixture); |
| 138 | + |
| 139 | + $result = $this->xmlEditor->addDatasetRelationToDepositXml($depositXml); |
| 140 | + |
| 141 | + $expectedXml = file_get_contents(__DIR__ . '/fixtures/crossref/expected/' . $fixture); |
| 142 | + |
| 143 | + $this->assertXmlStringEqualsXmlString($expectedXml, $result->saveXML()); |
| 144 | + } |
| 145 | +} |
0 commit comments