server/src/tests/Models/DocumentTest.php
author ymh <ymh.work@gmail.com>
Mon, 05 Oct 2015 17:02:10 +0200
changeset 4 f55970e41793
parent 2 00e2916104fe
child 19 eadaf0b8f02e
permissions -rw-r--r--
first skeleton of bo client in ember

<?php

use CorpusParole\Models\Document;

/**
 *
 */
class DocumentTest extends TestCase {

    const TEST_DOC = <<<EOT
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
    @prefix sesame: <http://www.openrdf.org/schema/sesame#> .
    @prefix owl: <http://www.w3.org/2002/07/owl#> .
    @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
    @prefix fn: <http://www.w3.org/2005/xpath-functions#> .

    <http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608> <http://purl.org/dc/elements/1.1/description> "Extrait des enquêtes dialectologiques en vue de constituer un atlas linguistique de l'Alsace."@fr ;
    	<http://purl.org/dc/elements/1.1/format> "audio/x-wav"^^<http://purl.org/dc/terms/IMT> ;
    	<http://purl.org/dc/elements/1.1/type> <http://purl.org/dc/dcmitype/Sound> , "primary_text"^^<http://www.language-archives.org/OLAC/1.1/linguistic-type> , "dialogue"^^<http://www.language-archives.org/OLAC/1.1/discourse-type> ;
    	<http://purl.org/dc/elements/1.1/subject> <http://lexvo.org/id/iso639-3/gsw> , "lexicography"^^<http://www.language-archives.org/OLAC/1.1/linguistic-field> , "phonetics"^^<http://www.language-archives.org/OLAC/1.1/linguistic-field> , "anthropological_linguistics"^^<http://www.language-archives.org/OLAC/1.1/linguistic-field> ;
    	<http://purl.org/dc/elements/1.1/language> <http://lexvo.org/id/iso639-3/gsw> ;
    	<http://www.language-archives.org/OLAC/1.1/depositor> <http://viaf.org/viaf/9122216> ;
    	<http://purl.org/dc/elements/1.1/publisher> "Atlas linguistiques, cultures et parlers régionaux de France" ;
    	<http://purl.org/dc/elements/1.1/rights> "Copyright (c) Département de dialectologie alsacienne et mosellane de l'Université de Strasbourg" ;
    	<http://purl.org/dc/elements/1.1/title> "Atlas Linguistique et ethnographique de l'Alsace - Enquetes sur la conscience linguistique - ALA_608"@fr ;
    	<http://www.language-archives.org/OLAC/1.1/interviewer> "Bothorel-Witz, Arlette" , "Huck, Dominique" ;
    	<http://purl.org/dc/terms/isFormatOf> <http://cocoon.huma-num.fr/data/ala/ALA_608_22km.wav> , <http://cocoon.huma-num.fr/data/ala/ALA_608.mp3> ;
    	<http://purl.org/dc/terms/accessRights> "Freely available for non-commercial use" ;
    	<http://purl.org/dc/terms/license> <http://creativecommons.org/licenses/by-nc-nd/2.5/> ;
    	<http://purl.org/dc/terms/isPartOf> <http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-COLLECTION_ALA_CL> , <http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-COLLECTION_LANGUESDEFRANCE> ;
    	<http://purl.org/dc/terms/extent> "PT02H04M16S" ;
    	<http://purl.org/dc/terms/created> "start=1980; end=1989; name=années 80"^^<http://purl.org/dc/terms/Period> ;
    	<http://purl.org/dc/terms/spatial> "France, Alsace"@fr , "FR"^^<http://purl.org/dc/terms/ISO3166> ;
    	<http://crdo.risc.cnrs.fr/schemas/master> <http://cocoon.huma-num.fr/data/ala/masters/ALA_608.wav> ;
    	<http://purl.org/dc/terms/available> "2004-07-03"^^<http://purl.org/dc/terms/W3CDTF> ;
    	<http://purl.org/dc/terms/issued> "2014-11-04T16:25:54+01:00"^^<http://purl.org/dc/terms/W3CDTF> .

    <http://lexvo.org/id/iso639-3/gsw> rdfs:label "Alsacien"@fr .

    <http://viaf.org/viaf/9122216> rdfs:label "Huck, Dominique" ;
    	<http://xmlns.com/foaf/0.1/status> "depositor"^^<http://www.language-archives.org/OLAC/1.1/role> .
EOT;


    public function setUp() {

        parent::setup();
        $this->graph = new EasyRdf\Graph("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", DocumentTest::TEST_DOC);

    }

    public function testConstructor() {
        $this->assertNotNull($this->graph, 'Graph shoud not be null');

        $doc = new Document("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", $this->graph);

        $this->assertEquals('crdo-ALA_608',$doc->getId(),'Must have the correct id');
    }

    public function testTitle() {
        $this->assertNotNull($this->graph, 'Graph shoud not be null');

        $doc = new Document("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", $this->graph);

        $this->assertEquals("Atlas Linguistique et ethnographique de l'Alsace - Enquetes sur la conscience linguistique - ALA_608",$doc->getTitle(),'Must have correct title');
        $this->assertInstanceOf(EasyRdf\Literal::class, $doc->getTitle(), "Title must be a literal");
        $this->assertEquals('fr', $doc->getTitle()->getLang(), "Language title must be fr");
    }

    public function testPublisher() {

        $doc = new Document("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", $this->graph);

        $this->assertCount(1, $doc->getPublishers(), 'Publisher is an array of size 1');
        $this->assertEquals(new EasyRdf\Literal("Atlas linguistiques, cultures et parlers régionaux de France",null,null), $doc->getPublishers()[0], "Must contains correct publisher");

    }

    public function testMediaArray() {
        $doc = new Document("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", $this->graph);

        $this->assertCount(3, $doc->getMediaArray(), "Media array must be of size 3");
        foreach($doc->getMediaArray() as $media) {
            $this->assertCount(2, $media, "media is a 2 element array");
            $this->assertArrayHasKey('format', $media, "media has 'format key'");
            $this->assertArrayHasKey('url', $media, "media has url");
        }
    }

    public function testGetTypes() {
        $doc = new Document("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", $this->graph);

        $this->assertCount(3, $doc->getTypes(), "types array must be of size 3");

        foreach($doc->getTypes() as $type) {
            $this->assertThat(
                $type,
                $this->logicalXor(
                    $this->isInstanceOf(EasyRdf\Literal::class),
                    $this->isInstanceOf(EasyRdf\Resource::class)
                )
            );
        }
    }

    public function testGetOtherTypes() {
        $doc = new Document("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", $this->graph);

        $this->assertCount(2, $doc->getOtherTypes(), "types array must be of size 3");

        foreach($doc->getTypes() as $type) {
            $this->assertThat(
                $type,
                $this->logicalXor(
                    $this->isInstanceOf(EasyRdf\Literal::class),
                    $this->isInstanceOf(EasyRdf\Resource::class)
                )
            );
        }
    }

    public function testGetDiscourseTypes() {
        $doc = new Document("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", $this->graph);

        $this->assertCount(1, $doc->getDiscourseTypes(), "types array must be of size 1");

        $this->assertContainsOnlyInstancesOf("EasyRdf\Literal", $doc->getDiscourseTypes(), "Result contains only literals");
        $type = $doc->getDiscourseTypes()[0];
        $this->assertEquals("dialogue", $type, "discourse type is dialogue");
        $this->assertEquals("http://www.language-archives.org/OLAC/1.1/discourse-type", $type->getDatatypeUri(), "discourse type url");
    }

    public function testCloneDocument() {
        $doc = new Document("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", $this->graph);

        $doc2 = clone $doc;

        $this->assertNotSame($doc, $doc2, "documents must not be the same");
        $this->assertNotSame($doc->getGraph(), $doc2->getGraph(), "documents must not be the same");

        $this->assertTrue(EasyRdf\Isomorphic::isomorphic($doc->getGraph(), $doc2->getGraph()),"graph must be isomorphic");
    }

    public function testIsIsomorphic() {
        $doc1 = new Document("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", $this->graph);
        $doc2 = new Document("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", new EasyRdf\Graph("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", DocumentTest::TEST_DOC));

        $this->assertTrue($doc1->isIsomorphic($doc2),"document must be isomorphic");

        $doc2->addLiteral('dc11:type', new EasyRdf\Literal("oratory", null, Config::get('OLAC_DISCOURSE_TYPE')['uri']));

        $this->assertFalse($doc1->isIsomorphic($doc2),"document must not be isomorphic");
    }

    public function testUpdateDiscourseTypes() {

        $newDiscourseTypes = ['oratory','dialogue','narrative'];

        $doc = new Document("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", $this->graph);
        $this->assertCount(1, $doc->getDiscourseTypes(), "types array must be of size 1");

        $doc->updateDiscourseTypes($newDiscourseTypes);

        $this->assertCount(3, $doc->getDiscourseTypes(), "types array must be of size 3");

        $discourseTypes = $doc->getDiscourseTypes();
        foreach($newDiscourseTypes as $dt) {
            $this->assertContains($dt, $discourseTypes, "all discourse types must be in result list");
        }

    }

    public function testUpdateDiscourseTypesDelta() {

        $newDiscourseTypes = ['oratory','dialogue','narrative'];

        $doc = new Document("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", $this->graph);

        $doc->updateDiscourseTypes($newDiscourseTypes);

        $this->assertTrue($doc->isDirty());

        $this->assertEquals(1, $doc->deltaCount(), "There is one delta");

        $delta = $doc->getDeltaList()[0];

        $this->assertEquals("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", $delta->getDeletedGraph()->getUri(), "uri of deleted graph must be ok");
        $this->assertEquals("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", $delta->getAddedGraph()->getUri(), "uri of added graph must be ok");

        $this->assertEquals(1, $delta->getDeletedGraph()->countTriples(), "deleted graph must have only one triple");
        $this->assertEquals(3, $delta->getAddedGraph()->countTriples(), "deleted graph must have only one triple");

        $resQueryDiscourseType = $delta->getAddedGraph()->allLiterals('http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608', 'dc11:type');
        foreach($resQueryDiscourseType as $dt) {
            $this->assertInstanceOf(EasyRdf\Literal::class, $dt, "This must be a litteral");
            $this->assertEquals('http://www.language-archives.org/OLAC/1.1/discourse-type', $dt->getDatatypeUri(), "The type of the Litteral must be correct");
        }
        foreach($newDiscourseTypes as $dt) {
            $this->assertContains($dt, $resQueryDiscourseType, "all discourse types must be in result list");
        }

    }

    public function testUpdateDiscourseTypesIsomorphic() {

        $newDiscourseTypes = ['oratory','dialogue','narrative'];

        $doc = new Document("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", $this->graph);
        $doc->updateDiscourseTypes($newDiscourseTypes);

        $doc2 = new Document("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", new EasyRdf\Graph("http://purl.org/poi/crdo.vjf.cnrs.fr/crdo-ALA_608", DocumentTest::TEST_DOC));

        $this->assertFalse($doc->isIsomorphic($doc2),"document must not be isomorphic after adding discourse type");
    }
}