Open Journal Systems  3.3.0
FunctionalDataciteExportTestCase.php
1 <?php
2 
23 import('tests.functional.plugins.importexport.FunctionalDoiExportTest');
24 import('plugins.importexport.datacite.DataciteExportPlugin');
25 
26 class FunctionalDataciteExportTest extends FunctionalDoiExportTest {
27  const TEST_ACCOUNT = 'TIB.OJSTEST';
28 
30  private $fileToRegister, $dcPassword;
31 
35  protected function setUp() : void {
36  $this->pluginId = 'datacite';
37 
38  // Retrieve and check configuration. (We're in a chicken
39  // and egg situation: This means that we cannot call
40  // parent::setUp() at this point so we have to retrieve
41  // the base URL here although it will be retrieved again
42  // in the parent class.)
43  $baseUrl = Config::getVar('debug', 'webtest_base_url');
44  $this->dcPassword = Config::getVar('debug', 'webtest_datacite_pw');
45  if (empty($baseUrl) || empty($this->dcPassword)) {
46  $this->markTestSkipped(
47  'Please set webtest_base_url and webtest_datacite_pw in your ' .
48  'config.php\'s [debug] section to the base url of your test server ' .
49  'and the password of your DataCite test account.'
50  );
51  }
52 
53  $indexPage = $baseUrl . '/index.php/test/manager/importexport/plugin/DataciteExportPlugin';
54  $this->pages = array(
55  'index' => $indexPage,
56  'settings' => $baseUrl . '/index.php/test/manager/plugin/importexport/DataciteExportPlugin/settings'
57  );
58 
59  $this->defaultPluginSettings = array(
60  'username' => self::TEST_ACCOUNT,
61  'password' => $this->dcPassword
62  );
63 
64  parent::setUp('10.5072');
65 
66  $this->configurePlugin();
67  }
68 
69 
78  public function testExportIssue() {
79  $this->doTestExpectJournalNameAsPublisher();
80  $this->doExportObjectTest('issue', 1, 'DataciteExportPlugin', 'datacite-issue.xml');
81  }
82 
83 
87  protected function checkThatPublisherIsJournalName($xml) {
88  // Test that the publisher is set to the journal title.
89  self::assertContains('<publisher>test</publisher>', $xml);
90  self::assertContains('<creatorName>test</creatorName>', $xml);
91 
92  // Change publisher to the default.
93  $xml = str_replace(
94  '<publisher>test</publisher>',
95  '<publisher>Test Publisher</publisher>',
96  $xml
97  );
98 
99  // Change (issue) creator to the default.
100  return str_replace(
101  '<creatorName>test</creatorName>',
102  '<creatorName>Test Publisher</creatorName>',
103  $xml
104  );
105  }
106 
107 
116  public function testExportArticle() {
117  $this->doExportObjectTest('article', 1, 'DataciteExportPlugin', 'datacite-article.xml');
118  }
119 
120 
129  public function testExportGalley() {
130  $sampleFiles = array(
131  'datacite-galley-1.xml',
132  'datacite-galley-2.xml',
133  'datacite-galley-3.xml',
134  );
135  $this->doExportObjectTest('galley', array(1,2,3), 'DataciteExportPlugin', $sampleFiles);
136  }
137 
138 
147  public function testExportUnregisteredDois() {
148  $objects = array(
149  'issue' => 1,
150  'article' => 1,
151  'galley' => array(1,2,3),
152  );
153  $xmlFiles = array(
154  'datacite-article.xml',
155  'datacite-galley-1.xml',
156  'datacite-galley-2.xml',
157  'datacite-galley-3.xml',
158  'datacite-issue.xml',
159  );
160  parent::testExportUnregisteredDois('DataciteExportPlugin', $objects, $xmlFiles);
161  }
162 
163 
177  public function testRegisterOrExportSpecificObjects() {
178  parent::testRegisterOrExportSpecificObjects('DataciteExportPlugin', array('issue', 'article', 'galley'), self::TEST_ACCOUNT);
179  }
180 
181 
185  public function testRegisterUnregisteredDois() {
186  parent::testRegisterUnregisteredDois('DataciteExportPlugin', array('Issue', 'Article', 'Galley'), self::TEST_ACCOUNT);
187 
188  // Check whether the DOIs and meta-data have actually been registered.
189  $registrationTests = array(
190  array('datacite-issue.xml', 'issue/view/1', '10.5072/t.v1i1'),
191  array('datacite-article.xml', 'article/view/1', '10.5072/t.v1i1.1'),
192  array('datacite-galley-1.xml', 'article/view/1/1', '10.5072/t.v1i1.1.g1'),
193  array('datacite-galley-2.xml', 'article/view/1/2', '10.5072/t.v1i1.1.g2'),
194  array('datacite-galley-3.xml', 'article/view/1/3', '10.5072/t.v1i1.1.g3'),
195  );
196  foreach($registrationTests as $registrationTest) {
197  list($sampleFile, $targetUrl, $doi) = $registrationTest;
198  $targetUrl = 'http://example.com/index.php/test/' . $targetUrl;
199  $this->checkDoiRegistration($doi, $sampleFile, $targetUrl);
200  }
201  }
202 
203 
216  parent::testObjectsWithoutDOICannotBeSelectedForExport(array('issues', 'articles', 'galleys', 'all'));
217  }
218 
219 
223  public function testPluginSettings() {
224  $tests = array(
225  array(
226  'username' => 'some-test-symbol',
227  'password' => 'some-password'
228  ),
229  array(
230  'username' => 'some-other-symbol',
231  'password' => 'another-password'
232  )
233  );
234  $inputTypes = array(
235  'username' => TEST_INPUTTYPE_TEXT,
236  'password' => TEST_INPUTTYPE_TEXT
237  );
238  parent::testPluginSettings($tests, $inputTypes);
239  }
240 
241 
250  public function testConfigurationError() {
251  parent::testConfigurationError(array('issues', 'articles', 'galleys', 'all'));
252  }
253 
254 
278  public function testExportAndRegisterObjectsViaCli() {
279  $examples = array(
280  array('issues', '1', 'datacite-issue.xml'),
281  array('articles', '1', 'datacite-article.xml'),
282  array('galleys', '1', 'datacite-galley-1.xml'),
283  array('galleys', '1 2 3', array('datacite-galley-1.xml', 'datacite-galley-2.xml', 'datacite-galley-3.xml')),
284  );
285 
286  foreach($examples as $example) {
287  list($exportObjectType, $objectIds, $xmlFiles) = $example;
288  parent::testExportAndRegisterObjectsViaCli('DataciteExportPlugin', 'export', $exportObjectType, $objectIds, $xmlFiles);
289  parent::testExportAndRegisterObjectsViaCli('DataciteExportPlugin', 'register', $exportObjectType, $objectIds);
290  }
291  }
292 
293 
297  public function testUnsupportedObjectTypeCliError() {
298  parent::testUnsupportedObjectTypeCliError('DataciteExportPlugin');
299  }
300 
301 
305  public function testNonExistentJournalPathCliError() {
306  parent::testNonExistentJournalPathCliError('DataciteExportPlugin');
307  }
308 
309 
313  public function testOutputFileNotWritableCliError() {
314  parent::testOutputFileNotWritableCliError('DataciteExportPlugin');
315  }
316 
317 
321  public function testNonExistentObjectIdCliError() {
322  parent::testNonExistentObjectIdCliError('DataciteExportPlugin');
323  }
324 
325 
326  //
327  // Implement template methods from FunctionalDoiExportTest
328  //
332  protected function checkDoiRegistration($doi, $sampleFile, $expectedTargetUrl) {
333  // Prepare HTTP session.
334  import('lib.pkp.classes.helpers.PKPCurlHelper');
336 
337  // Set up basic authentication.
338  $login = 'TIB.OJSTEST:' . $this->dcPassword;
339  curl_setopt($curlCh, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
340  curl_setopt($curlCh, CURLOPT_USERPWD, $login);
341 
342  // Request the DOI's URL over SSL.
343  $apiUrl = "https://mds.datacite.org/%action/$doi?testMode=true";
344 
345  curl_setopt($curlCh, CURLOPT_RETURNTRANSFER, true);
346  try {
347  $tests = array(
348  'doi' => $doi,
349  'metadata' => $sampleFile
350  );
351  foreach($tests as $action => $expectedResponse) {
352  // Set the URL for the API request.
353  curl_setopt($curlCh, CURLOPT_URL, str_replace('%action', $action, $apiUrl));
354 
355  // Wait for Handle to propagate our information
356  // but not longer than 1 minute.
357  $lastStatus = null;
358  $tryAgain = true;
359  for ($secs=0; $secs <= 60 && $tryAgain; $secs+=10) {
360  // Status 204 means that the DOI has been registered
361  // but is not yet available due to Handle's latency.
362  if ($lastStatus == 204) {
363  sleep(5);
364  }
365  $response = curl_exec($curlCh);
366  $lastStatus = curl_getinfo($curlCh, CURLINFO_HTTP_CODE);
367  $tryAgain = ($lastStatus == 204);
368  }
369 
370  if ($lastStatus == 204) {
371  self::fail("The DOI $doi has been correctly registered but is not yet available " .
372  "due to Handle's latency. Please re-execute this test in a few minutes.");
373  }
374 
375  // The return status should be 200 - OK.
376  self::assertEquals(200, $lastStatus);
377  if ($action == 'doi') {
378  // Check the registered target URL.
379  self::assertEquals($expectedTargetUrl, $response);
380  } else {
381  // Check the registered meta-data.
382  $this->assertXml($sampleFile, $response);
383  }
384  }
385  // Check the registered meta-data.
386  } catch(Exception $e) {
387  curl_close($curlCh);
388  throw $e;
389  }
390 
391 
392  // Destroy HTTP session.
393  curl_close($curlCh);
394  }
395 
399  protected function cleanXml($xml) {
400  // Fix missing translations.
401  $xml = str_replace('##editor.issues.pages##', 'Pages', $xml);
402 
403  // Fix modified date.
404  if (strpos($xml, '<date dateType="Updated">') !== false) {
405  // We have to fix the modified date as it changes
406  // too often (e.g. by using the test server manually)
407  // to be controlled reliably.
408  // Make sure we got the actual modified date but
409  // replace it with the modified date in the sample data
410  // so that our tests do not bail.
411  $submissionDao = DAORegistry::getDAO('SubmissionDAO'); /* @var $submissionDao SubmissionDAO */
412  $article = $submissionDao->getById(1);
413  $modifiedDate = date('Y-m-d', strtotime($article->getLastModified()));
414  $xml = str_replace('<date dateType="Updated">' . $modifiedDate, '<date dateType="Updated">2011-12-09', $xml);
415  }
416 
417  return parent::cleanXml($xml);
418  }
419 }
420 
FunctionalDataciteExportTest\testExportIssue
testExportIssue()
Definition: FunctionalDataciteExportTestCase.php:81
FunctionalDataciteExportTest\testPluginSettings
testPluginSettings()
Definition: FunctionalDataciteExportTestCase.php:226
FunctionalDataciteExportTest\$dcPassword
$dcPassword
Definition: FunctionalDataciteExportTestCase.php:33
FunctionalDataciteExportTest\checkDoiRegistration
checkDoiRegistration($doi, $sampleFile, $expectedTargetUrl)
Definition: FunctionalDataciteExportTestCase.php:335
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
FunctionalDataciteExportTest\testExportGalley
testExportGalley()
Definition: FunctionalDataciteExportTestCase.php:132
FunctionalDataciteExportTest\testOutputFileNotWritableCliError
testOutputFileNotWritableCliError()
Definition: FunctionalDataciteExportTestCase.php:316
FunctionalDataciteExportTest\testExportAndRegisterObjectsViaCli
testExportAndRegisterObjectsViaCli()
Definition: FunctionalDataciteExportTestCase.php:281
PKPCurlHelper\getCurlObject
static getCurlObject($url=null, $useProxySettings=true)
Definition: PKPCurlHelper.inc.php:23
FunctionalDataciteExportTest\testRegisterOrExportSpecificObjects
testRegisterOrExportSpecificObjects()
Definition: FunctionalDataciteExportTestCase.php:180
FunctionalDataciteExportTest\setUp
setUp()
Definition: FunctionalDataciteExportTestCase.php:38
FunctionalDataciteExportTest\testRegisterUnregisteredDois
testRegisterUnregisteredDois()
Definition: FunctionalDataciteExportTestCase.php:188
FunctionalDataciteExportTest\cleanXml
cleanXml($xml)
Definition: FunctionalDataciteExportTestCase.php:402
Config\getVar
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
FunctionalDataciteExportTest\testNonExistentJournalPathCliError
testNonExistentJournalPathCliError()
Definition: FunctionalDataciteExportTestCase.php:308
FunctionalDataciteExportTest\testConfigurationError
testConfigurationError()
Definition: FunctionalDataciteExportTestCase.php:253
FunctionalDataciteExportTest
Test the DataCite plug-in.
Definition: FunctionalDataciteExportTestCase.php:26
FunctionalDataciteExportTest\testExportArticle
testExportArticle()
Definition: FunctionalDataciteExportTestCase.php:119
FunctionalDataciteExportTest\checkThatPublisherIsJournalName
checkThatPublisherIsJournalName($xml)
Definition: FunctionalDataciteExportTestCase.php:90
FunctionalDataciteExportTest\testObjectsWithoutDOICannotBeSelectedForExport
testObjectsWithoutDOICannotBeSelectedForExport()
Definition: FunctionalDataciteExportTestCase.php:218
FunctionalDataciteExportTest\TEST_ACCOUNT
const TEST_ACCOUNT
Definition: FunctionalDataciteExportTestCase.php:27
FunctionalDataciteExportTest\testUnsupportedObjectTypeCliError
testUnsupportedObjectTypeCliError()
Definition: FunctionalDataciteExportTestCase.php:300
FunctionalDataciteExportTest\testNonExistentObjectIdCliError
testNonExistentObjectIdCliError()
Definition: FunctionalDataciteExportTestCase.php:324
FunctionalDataciteExportTest\testExportUnregisteredDois
testExportUnregisteredDois()
Definition: FunctionalDataciteExportTestCase.php:150