server/src/tests/libraries/Mappers/CocoonTextRdfMapperTest.php
changeset 152 dd6b3adde73b
parent 151 95dfb40ff3d2
child 153 338bcc78d431
equal deleted inserted replaced
151:95dfb40ff3d2 152:dd6b3adde73b
     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_CHO_URI = "http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-UVE_MOCIKA_SOUND";
       
    54 
       
    55 
       
    56     private $inputGraphes = [];
       
    57     private $resGraphes = [];
       
    58     private $mappers = [];
       
    59 
       
    60     function __construct(string $name = null) {
       
    61         parent::__construct($name);
       
    62     }
       
    63 
       
    64     public function setUp() {
       
    65         parent::setUp();
       
    66 
       
    67         $this->testGraphUri = config('corpusparole.corpus_doc_id_base_uri').CocoonTextRdfMapperTest::TEST_INPUT_ID;
       
    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     /**
       
    92      * Test that the returned graph has the same uri that the original.
       
    93      *
       
    94      * @return void
       
    95      */
       
    96     public function testUri() {
       
    97 
       
    98         //echo $this->resGraphes['BASE']->serialise('turtle');
       
    99         //echo var_export($this->resGraphes['BASE']->toRdfPhp());
       
   100 
       
   101         $this->assertNotNull($this->resGraphes['BASE']);
       
   102         $this->assertEquals($this->testGraphUri, $this->resGraphes['BASE']->getUri(), "URIS must be translated");
       
   103     }
       
   104 
       
   105     /**
       
   106      * Test that the return graph has one ore:Aggregation resource
       
   107      *
       
   108      * @return void
       
   109      */
       
   110     public function testAggregationResource() {
       
   111 
       
   112         $resources = $this->resGraphes['BASE']->allOfType('ore:Aggregation');
       
   113 
       
   114         $this->assertCount(1, $resources, "Must found only one resources of type ore:Aggregation");
       
   115         $this->assertEquals($this->testGraphUri,$resources[0]->getUri());
       
   116     }
       
   117 
       
   118     /**
       
   119      * Test providedCHO identifier.
       
   120      *
       
   121      * @return void
       
   122      */
       
   123     public function testProvidedChoId() {
       
   124         $resources = $this->resGraphes['BASE']->allOfType('edm:ProvidedCHO');
       
   125 
       
   126         $this->assertCount(1, $resources, "Must found only one resources of type edm:ProvidedCHO");
       
   127         $providedCHO = $resources[0];
       
   128 
       
   129         $identifier = $providedCHO->getLiteral('<http://purl.org/dc/elements/1.1/identifier>');
       
   130 
       
   131         $this->assertInstanceOf('EasyRdf\Literal', $identifier, "identifier value must be a literal");
       
   132         $this->assertEquals(config('corpusparole.corpus_id_scheme') . CocoonTextRdfMapperTest::TEST_INPUT_ID, $identifier->getValue(), "Value must be equals to ".config('corpusparole.corpus_id_scheme').CocoonTextRdfMapperTest::TEST_INPUT_ID);
       
   133 
       
   134     }
       
   135 
       
   136     public function testRightsWebResourceIdentity() {
       
   137         $properties = [
       
   138             "http://purl.org/dc/elements/1.1/rights",
       
   139             'http://purl.org/dc/terms/license',
       
   140         ];
       
   141 
       
   142         $webResourceList = $this->resGraphes['BASE']->all('edm:WebResource', '^rdf:type');
       
   143         $sourceNode = $this->inputGraphes['BASE']->get('http://crdo.risc.cnrs.fr/schemas/Resource', '^rdf:type');
       
   144 
       
   145         foreach ($webResourceList as $webResource) {
       
   146             foreach ($properties as $prop) {
       
   147                 $outputValuesStr = [];
       
   148                 foreach($webResource->all($this->resGraphes['BASE']->resource($prop)) as $outputValue) {
       
   149                     array_push($outputValuesStr, strval($outputValue));
       
   150                 }
       
   151                 $this->assertNotEmpty($outputValuesStr, "we must found some values to test $prop");
       
   152                 foreach ($sourceNode->all($this->inputGraphes['BASE']->resource($prop)) as $value) {
       
   153                     $this->assertContains(strval($value), $outputValuesStr, "$prop not found in output graph");
       
   154                 }
       
   155             }
       
   156         }
       
   157     }
       
   158 
       
   159     public function testRightsWebResourceTrim() {
       
   160         $properties = [
       
   161             'http://purl.org/dc/terms/accessRights',
       
   162         ];
       
   163 
       
   164         $webResourceList = $this->resGraphes['BASE']->all('edm:WebResource', '^rdf:type');
       
   165         $sourceNode = $this->inputGraphes['BASE']->get('http://crdo.risc.cnrs.fr/schemas/Resource', '^rdf:type');
       
   166 
       
   167         foreach ($webResourceList as $webResource) {
       
   168             foreach ($properties as $prop) {
       
   169                 $outputValuesStr = [];
       
   170                 foreach($webResource->all($this->resGraphes['BASE']->resource($prop)) as $outputValue) {
       
   171                     array_push($outputValuesStr, strval($outputValue));
       
   172                 }
       
   173                 $this->assertNotEmpty($outputValuesStr, "we must found some values to test $prop");
       
   174                 foreach ($sourceNode->all($this->inputGraphes['BASE']->resource($prop)) as $value) {
       
   175                     $this->assertContains(trim(strval($value)), $outputValuesStr, "$prop not found in output graph");
       
   176                 }
       
   177             }
       
   178         }
       
   179     }
       
   180 
       
   181 
       
   182     /**
       
   183      * Test providedCHO uri.
       
   184      *
       
   185      * @return void
       
   186      */
       
   187     public function testProvidedChoURI() {
       
   188         $resources = $this->resGraphes['BASE']->allOfType('edm:ProvidedCHO');
       
   189 
       
   190         $this->assertCount(1, $resources, "Must found only one resources of type edm:ProvidedCHO");
       
   191         $this->assertEquals(CocoonTextRdfMapperTest::TEST_CHO_URI,$resources[0]->getUri());
       
   192     }
       
   193 
       
   194 
       
   195 
       
   196     /**
       
   197      * Test that the returned graph does not have a http://purl.org/dc/dcmitype/Sound type
       
   198      *
       
   199      * @return void
       
   200      */
       
   201     public function testType() {
       
   202         //"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> ;
       
   203         $providedCHO = $this->resGraphes['BASE']->get('edm:ProvidedCHO', '^rdf:type');
       
   204 
       
   205         $this->assertNotNull($providedCHO);
       
   206 
       
   207         $this->assertcount(3, $providedCHO->all($this->resGraphes['BASE']->resource('http://purl.org/dc/elements/1.1/type')), 'but should find 2 literals');
       
   208 
       
   209         $dcTypeList = $providedCHO->all($this->resGraphes['BASE']->resource('http://purl.org/dc/elements/1.1/type'));
       
   210         foreach ($dcTypeList as $dcType) {
       
   211             $this->assertThat(
       
   212                 $dcType,
       
   213                 $this->logicalXor(
       
   214                     $this->isInstanceOf(EasyRdf\Literal::class),
       
   215                     $this->isInstanceOf(EasyRdf\Resource::class)
       
   216                 )
       
   217             );
       
   218         }
       
   219     }
       
   220 
       
   221      /**
       
   222       * Test that the web resources
       
   223       *
       
   224       * @return void
       
   225       */
       
   226     public function testWebResources() {
       
   227 
       
   228         $resources = $this->resGraphes['BASE']->allOfType('edm:WebResource');
       
   229 
       
   230         $this->assertCount(2, $resources, "Must found three webresource");
       
   231 
       
   232         $aggregation = $this->resGraphes['BASE']->resource($this->testGraphUri);
       
   233 
       
   234         foreach ($resources as $wres) {
       
   235             $mimetypes = $wres->all($this->resGraphes['BASE']->resource('http://purl.org/dc/elements/1.1/format'));
       
   236             $this->assertCount(1, $mimetypes, "Must find one mimetype.");
       
   237             $mimetype = $mimetypes[0];
       
   238             $this->assertInstanceOf("EasyRdf\Literal", $mimetype, "mimetype must be literal");
       
   239             $this->assertEquals("dc:IMT",$mimetype->getDatatype());
       
   240         }
       
   241     }
       
   242 
       
   243      /**
       
   244       * Test that the web resources has good date
       
   245       *
       
   246       * @return void
       
   247       */
       
   248     public function testWebResourcesDate() {
       
   249 
       
   250         $resources = $this->resGraphes['BASE']->allOfType('edm:WebResource');
       
   251 
       
   252         foreach ($resources as $wres) {
       
   253             $this->assertFalse($wres->hasProperty("http://purl.org/dc/terms/available"),"web resource must not have http://purl.org/dc/terms/available");
       
   254             $this->assertFalse($wres->hasProperty("http://purl.org/dc/terms/modified"),"web resource must not have http://purl.org/dc/terms/modified");
       
   255             $this->assertTrue($wres->hasProperty("http://purl.org/dc/terms/issued"), "Must have http://purl.org/dc/terms/issued");
       
   256 
       
   257             $issued = $wres->getLiteral('dc:issued');
       
   258             //<http://purl.org/dc/terms/issued> "2014-12-05T15:00:19+01:00"^^<http://purl.org/dc/terms/W3CDTF> ;
       
   259             $this->assertInstanceOf('EasyRdf\Literal', $issued, "issued value must be a literal");
       
   260             $this->assertEquals('http://purl.org/dc/terms/W3CDTF', $issued->getDatatypeUri(), "issued datatype uri must be a http://purl.org/dc/terms/W3CDTF");
       
   261             $this->assertEquals('2011-02-05T23:22:23+01:00', $issued->getValue(), "Value must be 2014-12-05T15:00:19+01:00");
       
   262 
       
   263         }
       
   264 
       
   265     }
       
   266 
       
   267 
       
   268     /**
       
   269      * Test that the web resources as conformsTo property
       
   270      *
       
   271      * @return void
       
   272      */
       
   273    public function testWebResourcesConformsTo() {
       
   274 
       
   275        $resources = $this->resGraphes['BASE']->allOfType('edm:WebResource');
       
   276        foreach ($resources as $wres) {
       
   277 
       
   278            $mimetypes = $wres->all($this->resGraphes['BASE']->resource('http://purl.org/dc/elements/1.1/format'));
       
   279            $mimetype = $mimetypes[0]->getValue();
       
   280            if($mimetype !== 'application/xml') {
       
   281                continue;
       
   282            }
       
   283            $conformsTo = $wres->get('<http://purl.org/dc/terms/conformsTo>');
       
   284            $this->assertNotNull($conformsTo, "Must have a conformsTo node");
       
   285            $this->assertInstanceOf('EasyRdf\Resource', $conformsTo, "Must be a resource");
       
   286            $this->assertEquals("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-dtd_archive", $conformsTo->getUri(), "Must be http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-dtd_archive");
       
   287        }
       
   288    }
       
   289 
       
   290 
       
   291 }