1010use APP \plugins \generic \dataverse \classes \entities \Dataset ;
1111use APP \plugins \generic \dataverse \classes \facades \Repo ;
1212use APP \plugins \generic \dataverse \dataverseAPI \actions \DatasetActions ;
13+ use APP \plugins \generic \dataverse \classes \services \DataStatementService ;
1314use APP \plugins \generic \dataverse \classes \dispatchers \DataStatementDispatcher ;
1415use APP \plugins \generic \dataverse \DataversePlugin ;
1516
1617class CrossrefXmlEditorTest extends DatabaseTestCase
1718{
1819 private CrossrefXmlEditor $ xmlEditor ;
19- private DOMDocument $ doc ;
2020 private $ contextId = 1 ;
21- private ?int $ submissionId = null ;
21+ private ?Submission $ submission = null ;
22+ private ?Publication $ publication = null ;
2223 private ?int $ doiId = null ;
2324 private string $ doi = '10.1234/PublicKnowledge.17 ' ;
2425 private ?DataverseStudy $ study = null ;
2526 private ?Dataset $ dataset = null ;
2627 private string $ persistentId = 'doi:10.5072/FK2/ABCDEF ' ;
28+ private string $ externalDatasetUrl = 'https://doi.org/10.1234/zenodo.98765 ' ;
2729
2830 public function setUp (): void
2931 {
3032 parent ::setUp ();
3133 $ plugin = new DataversePlugin ();
3234 $ dispatcher = new DataStatementDispatcher ($ plugin );
3335
34- $ this ->doc = $ this ->createTestXml ();
35- $ this ->submissionId = $ this ->createTestSubmission ();
36+ $ this ->createTestSubmission ();
3637 $ this ->study = $ this ->createDataverseStudy ();
3738 $ this ->dataset = $ this ->createTestDataset ();
3839 $ this ->xmlEditor = $ this ->createXmlEditor ();
@@ -41,19 +42,15 @@ public function setUp(): void
4142 public function tearDown (): void
4243 {
4344 parent ::tearDown ();
44-
45- $ submission = Repo::submission ()->get ($ this ->submissionId );
46- if ($ submission ) {
47- Repo::submission ()->delete ($ submission );
48- }
45+ Repo::submission ()->delete ($ this ->submission );
4946
5047 $ doi = Repo::doi ()->get ($ this ->doiId );
5148 if ($ doi ) {
5249 Repo::doi ()->delete ($ doi );
5350 }
5451 }
5552
56- private function createTestSubmission (): int
53+ private function createTestSubmission ()
5754 {
5855 $ context = Application::getContextDAO ()->getById ($ this ->contextId );
5956
@@ -62,7 +59,7 @@ private function createTestSubmission(): int
6259 $ publication = new Publication ();
6360
6461 $ submissionId = Repo::submission ()->add ($ submission , $ publication , $ context );
65- $ submission = Repo::submission ()->get ($ submissionId );
62+ $ this -> submission = Repo::submission ()->get ($ submissionId );
6663
6764 $ doi = Repo::doi ()->newDataObject ([
6865 'contextId ' => $ this ->contextId ,
@@ -71,17 +68,23 @@ private function createTestSubmission(): int
7168 ]);
7269 $ this ->doiId = Repo::doi ()->add ($ doi );
7370
74- $ publication = $ submission ->getCurrentPublication ();
75- $ publication ->setData ('doiId ' , $ this ->doiId );
76- Repo::publication ()->dao ->update ($ publication );
71+ $ this ->publication = $ this ->submission ->getCurrentPublication ();
72+ $ this ->publication ->setData ('doiId ' , $ this ->doiId );
73+ Repo::publication ()->dao ->update ($ this ->publication );
74+ }
7775
78- return $ submissionId ;
76+ private function addExternalDatasetsToPublication ()
77+ {
78+ $ this ->publication ->setData ('dataStatementTypes ' , [DataStatementService::DATA_STATEMENT_TYPE_REPO_AVAILABLE ]);
79+ $ this ->publication ->setData ('dataStatementUrls ' , [$ this ->externalDatasetUrl ]);
80+
81+ Repo::publication ()->dao ->update ($ this ->publication );
7982 }
8083
8184 private function createDataverseStudy (): DataverseStudy
8285 {
8386 $ study = new DataverseStudy ();
84- $ study ->setSubmissionId ($ this ->submissionId );
87+ $ study ->setSubmissionId ($ this ->submission -> getId () );
8588 $ study ->setEditUri ('https://demo.dataverse.org/dvn/api/data-deposit/v1.1/swordv2/edit/study/ ' . $ this ->persistentId );
8689 $ study ->setEditMediaUri ('https://demo.dataverse.org/dvn/api/data-deposit/v1.1/swordv2/edit-media/study/ ' . $ this ->persistentId );
8790 $ study ->setStatementUri ('https://demo.dataverse.org/dvn/api/data-deposit/v1.1/swordv2/statement/study/ ' . $ this ->persistentId );
@@ -94,10 +97,10 @@ private function createDataverseStudy(): DataverseStudy
9497 return $ study ;
9598 }
9699
97- private function createTestXml ()
100+ private function openTestXml ( string $ fixture ): DOMDocument
98101 {
99102 $ xml = new DOMDocument ('1.0 ' , 'UTF-8 ' );
100- $ xml ->appendChild ( $ xml -> createElement ( ' work ' ) );
103+ $ xml ->load ( __DIR__ . ' /fixtures/crossref/ ' . $ fixture );
101104
102105 return $ xml ;
103106 }
@@ -121,37 +124,58 @@ private function createXmlEditor(): CrossrefXmlEditor
121124
122125 public function testAddsDatasetRelationToWorkNode (): void
123126 {
124- $ workNode = $ this ->doc ->documentElement ;
127+ $ worksXml = $ this ->openTestXml ('work_node.xml ' );
128+ $ doi = preg_replace ('/^doi:/i ' , '' , $ this ->persistentId );
129+
130+ $ noPreviousRelWorkNode = $ worksXml ->getElementsByTagName ('work ' )->item (0 );
131+ $ this ->xmlEditor ->addDatasetRelationToWorkNode ($ noPreviousRelWorkNode , $ doi );
132+
133+ $ withPreviousRelWorkNode = $ worksXml ->getElementsByTagName ('work ' )->item (1 );
134+ $ this ->xmlEditor ->addDatasetRelationToWorkNode ($ withPreviousRelWorkNode , $ doi );
135+
136+ $ resultXml = $ worksXml ->saveXML ();
137+ $ expectedXml = file_get_contents (__DIR__ . '/fixtures/crossref/expected/work_node.xml ' );
138+
139+ $ this ->assertXmlStringEqualsXmlString ($ expectedXml , $ resultXml );
140+ }
141+
142+ public function testAddsExternalDatasetRelationToWorkNode (): void
143+ {
144+ $ worksXml = $ this ->openTestXml ('work_node.xml ' );
125145
126- $ result = $ this ->xmlEditor ->addDatasetRelationToWorkNode ($ workNode , $ this ->persistentId );
146+ $ noPreviousRelWorkNode = $ worksXml ->getElementsByTagName ('work ' )->item (0 );
147+ $ this ->xmlEditor ->addDatasetRelationToWorkNode ($ noPreviousRelWorkNode , $ this ->externalDatasetUrl , true );
127148
128- $ programNode = $ result -> getElementsByTagNameNS ( ' http://www.crossref.org/relations.xsd ' , ' program ' )->item (0 );
129- $ resultXml = $ result -> ownerDocument -> saveXML ( $ programNode );
149+ $ withPreviousRelWorkNode = $ worksXml -> getElementsByTagName ( ' work ' )->item (1 );
150+ $ this -> xmlEditor -> addDatasetRelationToWorkNode ( $ withPreviousRelWorkNode , $ this -> externalDatasetUrl , true );
130151
131- $ expectedXml = file_get_contents (__DIR__ . '/fixtures/crossref/expected/dataset_relation.xml ' );
152+ $ resultXml = $ worksXml ->saveXML ();
153+ $ expectedXml = file_get_contents (__DIR__ . '/fixtures/crossref/expected/work_node_external.xml ' );
132154
133155 $ this ->assertXmlStringEqualsXmlString ($ expectedXml , $ resultXml );
134156 }
135157
136158 public function testAddsDatasetRelationToDepositXml (): void
137159 {
138- $ this ->assertAddingOfRelationToXmlMatchesExpected ('preprint_deposit.xml ' );
139- $ this ->assertAddingOfRelationToXmlMatchesExpected ('article_deposit.xml ' );
160+ $ this ->assertAddingOfRelationToXmlMatchesExpected ('preprint_deposit.xml ' , ' preprint_deposit.xml ' );
161+ $ this ->assertAddingOfRelationToXmlMatchesExpected ('article_deposit.xml ' , ' article_deposit.xml ' );
140162 }
141163
142- public function testAddsRelationToXmlAlreadyWithRelation (): void
164+ public function testAddsExternalDatasetRelationToDepositXml (): void
143165 {
144- $ this ->assertAddingOfRelationToXmlMatchesExpected ('preprint_deposit_with_relation.xml ' );
166+ $ this ->addExternalDatasetsToPublication ();
167+
168+ $ this ->assertAddingOfRelationToXmlMatchesExpected ('preprint_deposit.xml ' , 'preprint_deposit_external.xml ' );
145169 }
146170
147- private function assertAddingOfRelationToXmlMatchesExpected (string $ fixture ): void
171+ private function assertAddingOfRelationToXmlMatchesExpected (string $ fixture, string $ expectedFixture ): void
148172 {
149173 $ depositXml = new DOMDocument ();
150174 $ depositXml ->load (__DIR__ . '/fixtures/crossref/ ' . $ fixture );
151175
152176 $ result = $ this ->xmlEditor ->addDatasetRelationToDepositXml ($ depositXml );
153177
154- $ expectedXml = file_get_contents (__DIR__ . '/fixtures/crossref/expected/ ' . $ fixture );
178+ $ expectedXml = file_get_contents (__DIR__ . '/fixtures/crossref/expected/ ' . $ expectedFixture );
155179
156180 $ this ->assertXmlStringEqualsXmlString ($ expectedXml , $ result ->saveXML ());
157181 }
0 commit comments