66import ('plugins.generic.dataverse.classes.dataverseStudy.DataverseStudyDAO ' );
77import ('plugins.generic.dataverse.classes.entities.Dataset ' );
88import ('plugins.generic.dataverse.dataverseAPI.actions.DatasetActions ' );
9+ import ('plugins.generic.dataverse.classes.services.DataStatementService ' );
910import ('plugins.generic.dataverse.classes.dispatchers.DataStatementDispatcher ' );
1011import ('plugins.generic.dataverse.DataversePlugin ' );
1112
1415class CrossrefXmlEditorTest extends DatabaseTestCase
1516{
1617 private $ xmlEditor ;
17- private $ doc ;
1818 private $ contextId = 1 ;
19- private $ submissionId = null ;
20- private $ doiId = null ;
19+ private $ submission ;
20+ private $ publication ;
2121 private $ doi = '10.1234/PublicKnowledge.17 ' ;
2222 private $ study = null ;
2323 private $ dataset = null ;
2424 private $ persistentId = 'doi:10.5072/FK2/ABCDEF ' ;
25+ private $ externalDatasetUrl = 'https://doi.org/10.1234/zenodo.98765 ' ;
2526
2627 public function setUp (): void
2728 {
2829 parent ::setUp ();
2930 $ plugin = new DataversePlugin ();
3031 $ dispatcher = new DataStatementDispatcher ($ plugin );
3132
32- $ this ->doc = $ this ->createTestXml ();
33- $ this ->submissionId = $ this ->createTestSubmission ();
33+ $ this ->createTestSubmission ();
3434 $ this ->study = $ this ->createDataverseStudy ();
3535 $ this ->dataset = $ this ->createTestDataset ();
3636 $ this ->xmlEditor = $ this ->createXmlEditor ();
@@ -41,7 +41,7 @@ protected function getAffectedTables(): array
4141 return ['submissions ' , 'submission_settings ' , 'publications ' , 'publication_settings ' , 'dataverse_studies ' ];
4242 }
4343
44- private function createTestSubmission (): int
44+ private function createTestSubmission ()
4545 {
4646 $ submissionDao = DAORegistry::getDAO ('SubmissionDAO ' );
4747 $ submission = $ submissionDao ->newDataObject ();
@@ -50,24 +50,36 @@ private function createTestSubmission(): int
5050
5151 $ publicationDao = DAORegistry::getDAO ('PublicationDAO ' );
5252 $ publication = $ publicationDao ->newDataObject ();
53- $ publication ->setData ('pub-id::doi ' , $ this ->doi );
5453 $ publication ->setData ('submissionId ' , $ submissionId );
55- $ pubId = $ publicationDao ->insertObject ($ publication );
54+ $ publicationId = $ publicationDao ->insertObject ($ publication );
55+
56+ $ submission ->setData ('currentPublicationId ' , $ publicationId );
57+ $ submissionDao ->updateObject ($ submission );
5658
5759 Capsule::table ('publication_settings ' )->insert ([
58- 'publication_id ' => $ pubId ,
60+ 'publication_id ' => $ publicationId ,
5961 'locale ' => '' ,
6062 'setting_name ' => 'pub-id::doi ' ,
6163 'setting_value ' => $ this ->doi ,
6264 ]);
6365
64- return $ submissionId ;
66+ $ this ->submission = $ submissionDao ->getById ($ submissionId );
67+ $ this ->publication = $ this ->submission ->getCurrentPublication ();
68+ }
69+
70+ private function addExternalDatasetsToPublication ()
71+ {
72+ $ this ->publication ->setData ('dataStatementTypes ' , [DATA_STATEMENT_TYPE_REPO_AVAILABLE ]);
73+ $ this ->publication ->setData ('dataStatementUrls ' , [$ this ->externalDatasetUrl ]);
74+
75+ $ publicationDao = DAORegistry::getDAO ('PublicationDAO ' );
76+ $ publicationDao ->updateObject ($ this ->publication );
6577 }
6678
6779 private function createDataverseStudy (): DataverseStudy
6880 {
6981 $ study = new DataverseStudy ();
70- $ study ->setSubmissionId ($ this ->submissionId );
82+ $ study ->setSubmissionId ($ this ->submission -> getId () );
7183 $ study ->setEditUri ('https://demo.dataverse.org/dvn/api/data-deposit/v1.1/swordv2/edit/study/ ' . $ this ->persistentId );
7284 $ study ->setEditMediaUri ('https://demo.dataverse.org/dvn/api/data-deposit/v1.1/swordv2/edit-media/study/ ' . $ this ->persistentId );
7385 $ study ->setStatementUri ('https://demo.dataverse.org/dvn/api/data-deposit/v1.1/swordv2/statement/study/ ' . $ this ->persistentId );
@@ -81,10 +93,10 @@ private function createDataverseStudy(): DataverseStudy
8193 return $ study ;
8294 }
8395
84- private function createTestXml ()
96+ private function openTestXml ( string $ fixture ): DomDocument
8597 {
8698 $ xml = new DOMDocument ('1.0 ' , 'UTF-8 ' );
87- $ xml ->appendChild ( $ xml -> createElement ( ' work ' ) );
99+ $ xml ->load ( __DIR__ . ' /fixtures/crossref/ ' . $ fixture );
88100
89101 return $ xml ;
90102 }
@@ -108,37 +120,58 @@ private function createXmlEditor(): CrossrefXmlEditor
108120
109121 public function testAddsDatasetRelationToWorkNode (): void
110122 {
111- $ workNode = $ this ->doc ->documentElement ;
123+ $ worksXml = $ this ->openTestXml ('work_node.xml ' );
124+ $ doi = preg_replace ('/^doi:/i ' , '' , $ this ->persistentId );
112125
113- $ result = $ this ->xmlEditor ->addDatasetRelationToWorkNode ($ workNode , $ this ->persistentId );
126+ $ noPreviousRelWorkNode = $ worksXml ->getElementsByTagName ('work ' )->item (0 );
127+ $ this ->xmlEditor ->addDatasetRelationToWorkNode ($ noPreviousRelWorkNode , $ doi );
114128
115- $ programNode = $ result -> getElementsByTagNameNS ( ' http://www.crossref.org/relations.xsd ' , ' program ' )->item (0 );
116- $ resultXml = $ result -> ownerDocument -> saveXML ( $ programNode );
129+ $ withPreviousRelWorkNode = $ worksXml -> getElementsByTagName ( ' work ' )->item (1 );
130+ $ this -> xmlEditor -> addDatasetRelationToWorkNode ( $ withPreviousRelWorkNode , $ doi );
117131
118- $ expectedXml = file_get_contents (__DIR__ . '/fixtures/crossref/expected/dataset_relation.xml ' );
132+ $ resultXml = $ worksXml ->saveXML ();
133+ $ expectedXml = file_get_contents (__DIR__ . '/fixtures/crossref/expected/work_node.xml ' );
134+
135+ $ this ->assertXmlStringEqualsXmlString ($ expectedXml , $ resultXml );
136+ }
137+
138+ public function testAddsExternalDatasetRelationToWorkNode (): void
139+ {
140+ $ worksXml = $ this ->openTestXml ('work_node.xml ' );
141+
142+ $ noPreviousRelWorkNode = $ worksXml ->getElementsByTagName ('work ' )->item (0 );
143+ $ this ->xmlEditor ->addDatasetRelationToWorkNode ($ noPreviousRelWorkNode , $ this ->externalDatasetUrl , true );
144+
145+ $ withPreviousRelWorkNode = $ worksXml ->getElementsByTagName ('work ' )->item (1 );
146+ $ this ->xmlEditor ->addDatasetRelationToWorkNode ($ withPreviousRelWorkNode , $ this ->externalDatasetUrl , true );
147+
148+ $ resultXml = $ worksXml ->saveXML ();
149+ $ expectedXml = file_get_contents (__DIR__ . '/fixtures/crossref/expected/work_node_external.xml ' );
119150
120151 $ this ->assertXmlStringEqualsXmlString ($ expectedXml , $ resultXml );
121152 }
122153
123154 public function testAddsDatasetRelationToDepositXml (): void
124155 {
125- $ this ->assertAddingOfRelationToXmlMatchesExpected ('preprint_deposit.xml ' );
126- $ this ->assertAddingOfRelationToXmlMatchesExpected ('article_deposit.xml ' );
156+ $ this ->assertAddingOfRelationToXmlMatchesExpected ('preprint_deposit.xml ' , ' preprint_deposit.xml ' );
157+ $ this ->assertAddingOfRelationToXmlMatchesExpected ('article_deposit.xml ' , ' article_deposit.xml ' );
127158 }
128159
129- public function testAddsRelationToXmlAlreadyWithRelation (): void
160+ public function testAddsExternalDatasetRelationToDepositXml (): void
130161 {
131- $ this ->assertAddingOfRelationToXmlMatchesExpected ('preprint_deposit_with_relation.xml ' );
162+ $ this ->addExternalDatasetsToPublication ();
163+
164+ $ this ->assertAddingOfRelationToXmlMatchesExpected ('preprint_deposit.xml ' , 'preprint_deposit_external.xml ' );
132165 }
133166
134- private function assertAddingOfRelationToXmlMatchesExpected (string $ fixture ): void
167+ private function assertAddingOfRelationToXmlMatchesExpected (string $ fixture, string $ expectedFixture ): void
135168 {
136169 $ depositXml = new DOMDocument ();
137170 $ depositXml ->load (__DIR__ . '/fixtures/crossref/ ' . $ fixture );
138171
139172 $ result = $ this ->xmlEditor ->addDatasetRelationToDepositXml ($ depositXml );
140173
141- $ expectedXml = file_get_contents (__DIR__ . '/fixtures/crossref/expected/ ' . $ fixture );
174+ $ expectedXml = file_get_contents (__DIR__ . '/fixtures/crossref/expected/ ' . $ expectedFixture );
142175
143176 $ this ->assertXmlStringEqualsXmlString ($ expectedXml , $ result ->saveXML ());
144177 }
0 commit comments