Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
CitationDAOTest.php
1 <?php
2 
17 import('lib.pkp.tests.DatabaseTestCase');
18 import('lib.pkp.classes.citation.CitationDAO');
19 import('lib.pkp.classes.citation.Citation');
20 import('lib.pkp.classes.metadata.MetadataDescription');
21 
22 if (!defined('ASSOC_TYPE_ARTICLE')) {
23  define('ASSOC_TYPE_ARTICLE', 0x9999);
24 }
25 
27 
28  protected function getAffectedTables() {
29  return array('citations', 'citation_settings');
30  }
31 
35  public function testCitationCrud() {
36  $citationDao = DAORegistry::getDAO('CitationDAO'); /* @var $citationDao CitationDAO */
37 
38  $nameSchemaName = 'lib.pkp.plugins.metadata.nlm30.schema.Nlm30NameSchema';
39  $nameDescription = new MetadataDescription($nameSchemaName, ASSOC_TYPE_AUTHOR);
40  $nameDescription->addStatement('given-names', $value = 'Peter');
41  $nameDescription->addStatement('given-names', $value = 'B');
42  $nameDescription->addStatement('surname', $value = 'Bork');
43  $nameDescription->addStatement('prefix', $value = 'Mr.');
44 
45  $citationSchemaName = 'lib.pkp.plugins.metadata.nlm30.schema.Nlm30CitationSchema';
46  $citationDescription = new MetadataDescription($citationSchemaName, ASSOC_TYPE_CITATION);
47  $citationDescription->addStatement('person-group[@person-group-type="author"]', $nameDescription);
48  $citationDescription->addStatement('article-title', $value = 'PHPUnit in a nutshell', 'en_US');
49  $citationDescription->addStatement('article-title', $value = 'PHPUnit in Kürze', 'de_DE');
50  $citationDescription->addStatement('date', $value = '2009-08-17');
51  $citationDescription->addStatement('size', $value = 320);
52  $citationDescription->addStatement('uri', $value = 'http://phpunit.org/nutshell');
53 
54  // Add a simple source description.
55  $sourceDescription = new MetadataDescription($citationSchemaName, ASSOC_TYPE_CITATION);
56  $sourceDescription->setDisplayName('test');
57  $sourceDescription->addStatement('article-title', $value = 'a simple source description', 'en_US');
58  $sourceDescription->setSeq(0);
59 
60  $citation = new Citation('raw citation');
61  $citation->setAssocType(ASSOC_TYPE_ARTICLE);
62  $citation->setAssocId(999999);
63  $citation->setSeq(50);
64  $citation->addSourceDescription($sourceDescription);
65  $citation->injectMetadata($citationDescription);
66 
67  // Create citation.
68  $citationId = $citationDao->insertObject($citation);
69  self::assertTrue(is_numeric($citationId));
70  self::assertTrue($citationId > 0);
71 
72  // Retrieve citation.
73  $citationById = $citationDao->getObjectById($citationId);
74  // Fix state differences for comparison.
75  $citation->removeSupportedMetadataAdapter($citationSchemaName);
76  $citationById->removeSupportedMetadataAdapter($citationSchemaName);
77  $citationById->_extractionAdaptersLoaded = true;
78  $citationById->_injectionAdaptersLoaded = true;
79  $sourceDescription->setAssocId($citationId);
80  $sourceDescription->removeSupportedMetadataAdapter($citationSchemaName);
81  $sourceDescriptions = $citationById->getSourceDescriptions();
82  $sourceDescriptions['test']->getMetadataSchema(); // this will instantiate the meta-data schema internally.
83  self::assertEquals($citation, $citationById);
84 
85  $citationsByAssocIdDaoFactory = $citationDao->getObjectsByAssocId(ASSOC_TYPE_ARTICLE, 999999);
86  $citationsByAssocId = $citationsByAssocIdDaoFactory->toArray();
87  self::assertEquals(1, count($citationsByAssocId));
88  // Fix state differences for comparison.
89  $citationsByAssocId[0]->_extractionAdaptersLoaded = true;
90  $citationsByAssocId[0]->_injectionAdaptersLoaded = true;
91  $citationsByAssocId[0]->removeSupportedMetadataAdapter($citationSchemaName);
92  $sourceDescriptionsByAssocId = $citationsByAssocId[0]->getSourceDescriptions();
93  $sourceDescriptionsByAssocId['test']->getMetadataSchema(); // this will instantiate the meta-data schema internally.
94  self::assertEquals($citation, $citationsByAssocId[0]);
95 
96  // Update citation.
97  $citationDescription->removeStatement('date');
98  $citationDescription->addStatement('article-title', $value = 'PHPUnit rápido', 'pt_BR');
99 
100  // Update source descriptions.
101  $sourceDescription->addStatement('article-title', $value = 'edited source description', 'en_US', true);
102 
103  $updatedCitation = new Citation('another raw citation');
104  $updatedCitation->setId($citationId);
105  $updatedCitation->setAssocType(ASSOC_TYPE_ARTICLE);
106  $updatedCitation->setAssocId(999998);
107  $updatedCitation->setSeq(50);
108  $updatedCitation->addSourceDescription($sourceDescription);
109  $updatedCitation->injectMetadata($citationDescription);
110 
111  $citationDao->updateObject($updatedCitation);
112  $citationAfterUpdate = $citationDao->getObjectById($citationId);
113  // Fix state differences for comparison.
114  $updatedCitation->removeSupportedMetadataAdapter($citationSchemaName);
115  $citationAfterUpdate->removeSupportedMetadataAdapter($citationSchemaName);
116  $citationAfterUpdate->_extractionAdaptersLoaded = true;
117  $citationAfterUpdate->_injectionAdaptersLoaded = true;
118  $sourceDescriptionsAfterUpdate = $citationAfterUpdate->getSourceDescriptions();
119  $sourceDescriptionsAfterUpdate['test']->getMetadataSchema(); // this will instantiate the meta-data schema internally.
120  $sourceDescription->removeSupportedMetadataAdapter($citationSchemaName);
121  self::assertEquals($updatedCitation, $citationAfterUpdate);
122 
123  // Delete citation
124  $citationDao->deleteObjectsByAssocId(ASSOC_TYPE_ARTICLE, 999998);
125  self::assertNull($citationDao->getObjectById($citationId));
126  }
127 }
128 ?>
static & getDAO($name, $dbconn=null)
Test class for CitationDAO.
Base class for unit tests that require database support. The schema TestName.setUp.xml will be installed before each individual test case (if present). The schema TestName.tearDown.xml may be used to clean up after each test case.
Class representing a citation (bibliographic reference)
Class modeling a description (DCMI abstract model) or subject- predicate-object graph (RDF)...