server/src/tests/libraries/Mappers/CocoonTextRdfMapperTest.php
changeset 18 f2a40bbc27f6
child 19 eadaf0b8f02e
equal deleted inserted replaced
17:ac3dc090e987 18:f2a40bbc27f6
       
     1 <?php
       
     2 
       
     3 use CorpusParole\Libraries\Mappers\CocoonTextRdfMapper;
       
     4 use CorpusParole\Libraries\CocoonUtils;
       
     5 
       
     6 use EasyRdf\Graph;
       
     7 use EasyRdf\Resource;
       
     8 use EasyRdf\Literal;
       
     9 
       
    10 use Illuminate\Foundation\Testing\WithoutMiddleware;
       
    11 use Illuminate\Foundation\Testing\DatabaseMigrations;
       
    12 use Illuminate\Foundation\Testing\DatabaseTransactions;
       
    13 
       
    14 class CocoonTextRdfMapperTest extends TestCase
       
    15 {
       
    16 
       
    17     const TEST_INPUT_DOCS = [
       
    18         'BASE' => <<<EOT
       
    19         @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
       
    20         @prefix owl: <http://www.w3.org/2002/07/owl#> .
       
    21         @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
       
    22         @prefix fn: <http://www.w3.org/2005/xpath-functions#> .
       
    23         @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
       
    24         @prefix sesame: <http://www.openrdf.org/schema/sesame#> .
       
    25         @prefix v: <http://rdf.data-vocabulary.org/#> .
       
    26         <http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-UVE_MOCIKA> a <http://crdo.risc.cnrs.fr/schemas/Resource> ;
       
    27             <http://purl.org/dc/elements/1.1/description> "Voilà pourquoi le bernard-l'hermite, aujourd'hui, se cache dans les coquilles vides qu'il trouve, alors que le crabe de cocotier n'a pas honte de se promener tout nu."@fr ;
       
    28             <http://purl.org/dc/elements/1.1/format> "text/xml"^^<http://purl.org/dc/terms/IMT> ;
       
    29             <http://purl.org/dc/elements/1.1/type> "primary_text"^^<http://www.language-archives.org/OLAC/1.1/linguistic-type> , <http://purl.org/dc/dcmitype/Text> , "narrative"^^<http://www.language-archives.org/OLAC/1.1/discourse-type> ;
       
    30             <http://purl.org/dc/elements/1.1/subject> <http://lexvo.org/id/iso639-3/uve> ;
       
    31             <http://purl.org/dc/elements/1.1/language> <http://lexvo.org/id/iso639-3/uve> ;
       
    32             <http://www.language-archives.org/OLAC/1.1/depositor> <http://viaf.org/viaf/56614135> ;
       
    33             <http://purl.org/dc/elements/1.1/publisher> <http://viaf.org/viaf/154919513> ;
       
    34             <http://purl.org/dc/elements/1.1/rights> "Copyright (c) Moyse-Faurie, Claire" ;
       
    35             <http://purl.org/dc/elements/1.1/title> "The two hermit crabs and the coconut crab"@en ;
       
    36             <http://purl.org/dc/terms/isFormatOf> <http://cocoon.huma-num.fr/exist/crdo/moyse-faurie/uve/crdo-UVE_MOCIKA.xml> , <http://cocoon.huma-num.fr/exist/crdo/moyse-faurie/uve/crdo-UVE_MOCIKA.xhtml> ;
       
    37             <http://purl.org/dc/terms/accessRights> "Freely available for non-commercial use" ;
       
    38             <http://purl.org/dc/terms/license> <http://creativecommons.org/licenses/by-nc-sa/2.5/> ;
       
    39             <http://purl.org/dc/terms/isPartOf> <http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-COLLECTION_LANGUESDEFRANCE> , <http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-COLLECTION_LACITO> ;
       
    40             <http://purl.org/dc/terms/spatial> "NC"^^<http://purl.org/dc/terms/ISO3166> , "New Caledonia, Ohnyat (Ouvéa)" ;
       
    41             <http://purl.org/dc/terms/available> "2011-02-05"^^<http://purl.org/dc/terms/W3CDTF> ;
       
    42             <http://purl.org/dc/terms/issued> "2011-02-05T23:22:23+01:00"^^<http://purl.org/dc/terms/W3CDTF> ;
       
    43             <http://www.language-archives.org/OLAC/1.1/speaker> "Idakote, Félicien" ;
       
    44             <http://purl.org/dc/terms/requires> <http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-UVE_MOCIKA_SOUND> ;
       
    45             <http://purl.org/dc/terms/alternative> "Les deux bernard-l'hermite et le crabe de cocotier"@fr ;
       
    46             <http://www.language-archives.org/OLAC/1.1/researcher> "Moyse-Faurie, Claire" ;
       
    47             <http://purl.org/dc/terms/modified> "2002-02-20"^^<http://purl.org/dc/terms/W3CDTF> ;
       
    48             <http://purl.org/dc/terms/conformsTo> <http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-dtd_archive> .
       
    49 EOT
       
    50     ];
       
    51 
       
    52     const TEST_INPUT_ID = "crdo-UVE_MOCIKA_SOUND";
       
    53     const TEST_GRAPH_URI = "http://purl.org/poi/corpusdelaparole.huma-num.fr/crdo-UVE_MOCIKA_SOUND";
       
    54     const TEST_CHO_URI = "http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-UVE_MOCIKA_SOUND";
       
    55 
       
    56 
       
    57     private $inputGraphes = [];
       
    58     private $resGraphes = [];
       
    59     private $mappers = [];
       
    60 
       
    61     function __construct(string $name = null) {
       
    62         parent::__construct($name);
       
    63     }
       
    64 
       
    65     public function setUp() {
       
    66         parent::setUp();
       
    67 
       
    68         foreach(CocoonTextRdfMapperTest::TEST_INPUT_DOCS as $key => $inputDoc) {
       
    69             $this->inputGraphes[$key] = new Graph("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-UVE_MOCIKA", $inputDoc);
       
    70             $this->mappers[$key] = new CocoonTextRdfMapper($this->inputGraphes[$key]);
       
    71             $this->mappers[$key]->mapGraph();
       
    72             $this->resGraphes[$key] = $this->mappers[$key]->getOutputGraphes()["http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-UVE_MOCIKA_SOUND"];
       
    73         }
       
    74     }
       
    75 
       
    76     public function tearDown() {
       
    77         parent::tearDown();
       
    78     }
       
    79 
       
    80     /**
       
    81      * Just test that the construct and setup are ok
       
    82      *
       
    83      * @return void
       
    84      */
       
    85     public function testInit()
       
    86     {
       
    87         $this->assertTrue(true);
       
    88     }
       
    89 
       
    90     /**
       
    91      * Test that the returned graph has the same uri that the original.
       
    92      *
       
    93      * @return void
       
    94      */
       
    95     public function testUri() {
       
    96 
       
    97         echo $this->resGraphes['BASE']->serialise('turtle');
       
    98         //echo var_export($this->resGraphes['BASE']->toRdfPhp());
       
    99 
       
   100         $this->assertNotNull($this->resGraphes['BASE']);
       
   101         $this->assertEquals(CocoonTextRdfMapperTest::TEST_GRAPH_URI, $this->resGraphes['BASE']->getUri(), "URIS must be translated");
       
   102     }
       
   103 
       
   104     /**
       
   105      * Test that the return graph has one ore:Aggregation resource
       
   106      *
       
   107      * @return void
       
   108      */
       
   109     public function testAggregationResource() {
       
   110 
       
   111         $resources = $this->resGraphes['BASE']->allOfType('ore:Aggregation');
       
   112 
       
   113         $this->assertCount(1, $resources, "Must found only one resources of type ore:Aggregation");
       
   114         $this->assertEquals(CocoonTextRdfMapperTest::TEST_GRAPH_URI,$resources[0]->getUri());
       
   115     }
       
   116 
       
   117     /**
       
   118      * Test providedCHO uri.
       
   119      *
       
   120      * @return void
       
   121      */
       
   122     public function testProvidedChoURI() {
       
   123         $resources = $this->resGraphes['BASE']->allOfType('edm:ProvidedCHO');
       
   124 
       
   125         $this->assertCount(1, $resources, "Must found only one resources of type edm:ProvidedCHO");
       
   126         $this->assertEquals(CocoonTextRdfMapperTest::TEST_CHO_URI,$resources[0]->getUri());
       
   127     }
       
   128 
       
   129     /**
       
   130      * Test taht the returned graph does not have a http://purl.org/dc/dcmitype/Sound type
       
   131      *
       
   132      * @return void
       
   133      */
       
   134     public function testType() {
       
   135         //"primary_text"^^<http://www.language-archives.org/OLAC/1.1/linguistic-type> , <http://purl.org/dc/dcmitype/Text> , "narrative"^^<http://www.language-archives.org/OLAC/1.1/discourse-type> ;
       
   136         $providedCHO = $this->resGraphes['BASE']->get('edm:ProvidedCHO', '^rdf:type');
       
   137 
       
   138         $this->assertNotNull($providedCHO);
       
   139 
       
   140         $this->assertEmpty($providedCHO->all($this->resGraphes['BASE']->resource('http://purl.org/dc/elements/1.1/type'), 'resource'), 'Should not find dc element:type value as resource');
       
   141         $this->assertcount(2, $providedCHO->all($this->resGraphes['BASE']->resource('http://purl.org/dc/elements/1.1/type')), 'but should find 2 literals');
       
   142 
       
   143         $expResMap = [
       
   144             'primary_text' => 'http://www.language-archives.org/OLAC/1.1/linguistic-type',
       
   145             'narrative' => 'http://www.language-archives.org/OLAC/1.1/discourse-type'
       
   146         ];
       
   147 
       
   148         $dcTypeList = $providedCHO->all($this->resGraphes['BASE']->resource('http://purl.org/dc/elements/1.1/type'));
       
   149         foreach ($dcTypeList as $dcType) {
       
   150             $this->assertInstanceOf('EasyRdf\Literal', $dcType, "dc type must be a literal");
       
   151             $this->assertArrayHasKey($dcType->getValue(),$expResMap, 'Value must be in expected result Map');
       
   152             $this->assertEquals($expResMap[$dcType->getValue()], $dcType->getDatatypeUri());
       
   153         }
       
   154     }
       
   155 
       
   156      /**
       
   157       * Test that the web resources
       
   158       *
       
   159       * @return void
       
   160       */
       
   161     public function testWebResources() {
       
   162 
       
   163         $resources = $this->resGraphes['BASE']->allOfType('edm:WebResources');
       
   164 
       
   165         $this->assertCount(2, $resources, "Must found three webresources");
       
   166 
       
   167         $aggregation = $this->resGraphes['BASE']->resource(CocoonTextRdfMapperTest::TEST_GRAPH_URI);
       
   168 
       
   169         foreach ($resources as $wres) {
       
   170             $mimetypes = $wres->all($this->resGraphes['BASE']->resource('http://purl.org/dc/elements/1.1/format'));
       
   171             $this->assertCount(1, $mimetypes, "Must find one mimetype.");
       
   172             $mimetype = $mimetypes[0];
       
   173             $this->assertInstanceOf("EasyRdf\Literal", $mimetype, "mimetype must be literal");
       
   174             $this->assertEquals("dc:IMT",$mimetype->getDatatype());
       
   175         }
       
   176     }
       
   177 
       
   178      /**
       
   179       * Test that the web resources
       
   180       *
       
   181       * @return void
       
   182       */
       
   183     public function testWebResourcesDate() {
       
   184 
       
   185         $resources = $this->resGraphes['BASE']->allOfType('edm:WebResources');
       
   186 
       
   187         foreach ($resources as $wres) {
       
   188             $this->assertFalse($wres->hasProperty("http://purl.org/dc/terms/available"),"web resource must not have http://purl.org/dc/terms/available");
       
   189             $this->assertFalse($wres->hasProperty("http://purl.org/dc/terms/modified"),"web resource must not have http://purl.org/dc/terms/modified");
       
   190             $this->assertTrue($wres->hasProperty("http://purl.org/dc/terms/issued"), "Must have http://purl.org/dc/terms/issued");
       
   191 
       
   192             $issued = $wres->getLiteral('dc:issued');
       
   193             //<http://purl.org/dc/terms/issued> "2014-12-05T15:00:19+01:00"^^<http://purl.org/dc/terms/W3CDTF> ;
       
   194             $this->assertInstanceOf('EasyRdf\Literal', $issued, "issued value must be a literal");
       
   195             $this->assertEquals('http://purl.org/dc/terms/W3CDTF', $issued->getDatatypeUri(), "issued datatype uri must be a http://purl.org/dc/terms/W3CDTF");
       
   196             $this->assertEquals('2011-02-05T23:22:23+01:00', $issued->getValue(), "Value must be 2014-12-05T15:00:19+01:00");
       
   197 
       
   198         }
       
   199 
       
   200     }
       
   201 
       
   202 
       
   203 }