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