server/src/tests/Libraries/Transcript/TranscriptConverterBaseTest.php
changeset 162 a6cf5a06f02d
child 163 59c68fc4848e
equal deleted inserted replaced
161:5f011170de74 162:a6cf5a06f02d
       
     1 <?php
       
     2 
       
     3 use Mockery as m;
       
     4 
       
     5 use CorpusParole\Models\Document;
       
     6 
       
     7 
       
     8 /**
       
     9  *
       
    10  */
       
    11 class TranscriptConverterBaseTest extends TestCase {
       
    12     const TEST_DOC_BASE = "crdo-UVE_MOCIKA";
       
    13 
       
    14     public function setUp() {
       
    15         parent::setup();
       
    16         $graphContent = sprintf(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . self::TEST_DOC_BASE.".ttl"), config('corpusparole.corpus_doc_id_base_uri'), config('corpusparole.corpus_id_scheme'));
       
    17         $this->graph = new EasyRdf\Graph(config('corpusparole.corpus_doc_id_base_uri')."crdo-UVE_MOCIKA_SOUND", $graphContent);
       
    18         $this->doc = new Document(config('corpusparole.corpus_doc_id_base_uri')."crdo-UVE_MOCIKA_SOUND", $this->graph);
       
    19 
       
    20         $this->transcriptSource = simplexml_load_file(__DIR__ . DIRECTORY_SEPARATOR . self::TEST_DOC_BASE.".xml");
       
    21     }
       
    22 
       
    23     public function getMockConverter(...$contructorArgs) {
       
    24         return  m::mock("CorpusParole\Libraries\Transcript\TranscriptConverterBase", $contructorArgs)
       
    25             ->shouldReceive('parseSource')
       
    26             ->andReturn(null)
       
    27             ->shouldReceive('buildAnnotations')
       
    28             ->andReturn([]);
       
    29     }
       
    30 
       
    31 
       
    32     public function testConstructor() {
       
    33         $converter = $this->getMockConverter($this->doc, $this->transcriptSource)->getMock()->makePartial();
       
    34         $json = $converter->convertToJson();
       
    35         $this->assertNotnull($json);
       
    36     }
       
    37 
       
    38     public function testHeaderMeta() {
       
    39         $datetime =  (new DateTime())->format(DateTime::ATOM);
       
    40         $converter =  $this->getMockConverter($this->doc, $this->transcriptSource, $datetime)->getMock()->makePartial();
       
    41         $resJson = $converter->convertTojson();
       
    42         $this->assertArrayHasKey('format', $resJson, 'Must have format key');
       
    43         $this->assertEquals('http://advene.org/ns/cinelab/', $resJson['format'], 'Format key must be http://advene.org/ns/cinelab/');
       
    44         $this->assertArrayHasKey('@context', $resJson, 'Must have @context key');
       
    45         $this->assertEquals(
       
    46             [
       
    47                 "dc" =>  "http://purl.org/dc/elements/1.1/",
       
    48                 "corpus" => "http://corpusdelaparole.huma-num.fr/ns/corpus#"
       
    49             ],
       
    50             $resJson['@context'],
       
    51             "array must contains dc and corpus declaration"
       
    52         );
       
    53         $this->assertArrayHasKey('meta', $resJson, 'Must have meta declaration');
       
    54         $meta = $resJson['meta'];
       
    55         $this->assertTrue(is_array($meta), 'meta is an array');
       
    56 
       
    57         $this->assertArrayHasKey('dc:creator', $meta, "meta must have dc:creator key");
       
    58         $this->assertEquals(config('corpusparole.transcript_default_creator'), $meta['dc:creator'], "dc:creator must be ".config('corpusparole.transcript_default_creator'));
       
    59         $this->assertArrayHasKey('dc:contributor', $meta, "meta must have dc:contributor key");
       
    60         $this->assertEquals(config('corpusparole.transcript_default_creator'), $meta['dc:contributor'], "dc:contributor must be ".config('corpusparole.transcript_default_creator'));
       
    61         $this->assertArrayHasKey('dc:created', $meta, "meta must have dc:created key");
       
    62         $this->assertEquals($datetime, $meta["dc:created"], "meta created time mus be $datetime");
       
    63         $this->assertArrayHasKey('dc:modified', $meta, "meta must have dc:modified key");
       
    64         $this->assertEquals($datetime, $meta["dc:modified"], "meta modified time mus be $datetime");
       
    65 
       
    66         $this->assertArrayHasKey('dc:title', $meta, "meta must have dc:title key");
       
    67         $this->assertTrue(is_array($meta['dc:title']), 'meta title is an array');
       
    68         $this->assertCount(2, $meta['dc:title'], 'meta title array has 2 elements');
       
    69         $this->assertArrayHasKey('@language', $meta['dc:title'], "meta title must have @language key");
       
    70         $this->assertArrayHasKey('@value', $meta['dc:title'], "meta title must have @value key");
       
    71         $this->assertEquals('en',  $meta['dc:title']['@language'], "meta title language must be en");
       
    72         $this->assertEquals('The two hermit crabs and the coconut crab',  $meta['dc:title']['@value'], "meta title value must be the Two hermit crabs and the coconut crab");
       
    73     }
       
    74 
       
    75     public function testHeaderMetaTitleOverrideString() {
       
    76         $converter = $this->getMockConverter($this->doc, $this->transcriptSource)->shouldReceive("getSourceTitle")->andReturn("The two hermit crabs and the coconut crab")->getMock()->makePartial();
       
    77         $resJson = $converter->convertTojson();
       
    78 
       
    79         $meta = $resJson['meta'];
       
    80         $this->assertArrayHasKey('dc:title', $meta, "meta must have dc:title key");
       
    81         $this->assertTrue(is_string($meta['dc:title']), 'meta title is a string');
       
    82         $this->assertEquals('The two hermit crabs and the coconut crab',  $meta['dc:title'], "meta title value must be the Two hermit crabs and the coconut crab");
       
    83 
       
    84     }
       
    85 
       
    86     public function testHeaderMetaTitleOverrideLanguageMap() {
       
    87         $converter = $this->getMockConverter($this->doc, $this->transcriptSource)->shouldReceive("getSourceTitle")->andReturn(['en' => "The two hermit crabs and the coconut crab", "fr" => 'Les deux bernard-l\'hermite et le crabe de cocotier'])->getMock()->makePartial();
       
    88         $resJson = $converter->convertTojson();
       
    89 
       
    90         $meta = $resJson['meta'];
       
    91         $this->assertArrayHasKey('dc:title', $meta, "meta must have dc:title key");
       
    92         $this->assertTrue(is_array($meta['dc:title']), 'meta title is an array');
       
    93         $this->assertCount(2, $meta['dc:title'], "title must be an array of size 2");
       
    94         $this->assertEquals(["@language" => 'en', '@value' => 'The two hermit crabs and the coconut crab'],  $meta['dc:title'][0], "meta title value must be the Two hermit crabs and the coconut crab");
       
    95         $this->assertEquals(["@language" => 'fr', '@value' => 'Les deux bernard-l\'hermite et le crabe de cocotier'],  $meta['dc:title'][1], "meta title value must be the Les deux bernard-l\'hermite et le crabe de cocotier");
       
    96     }
       
    97 
       
    98     public function testMedias() {
       
    99         $converter =  $this->getMockConverter($this->doc, $this->transcriptSource)->getMock()->makePartial();
       
   100         $resJson = $converter->convertTojson();
       
   101 
       
   102         $this->assertArrayHasKey('medias', $resJson, "Must have a medias field");
       
   103         $this->assertTrue(is_array($resJson['medias']), 'media is an array');
       
   104     }
       
   105 
       
   106     public function testMediasContent() {
       
   107         $converter =  $this->getMockConverter($this->doc, $this->transcriptSource)->getMock()->makePartial();
       
   108         $resJson = $converter->convertTojson();
       
   109 
       
   110         $medias = $resJson['medias'];
       
   111 
       
   112         $this->assertCount(3, $medias, "Should have 3 media");
       
   113         $this->assertCount(3, array_filter($medias, function($m) {
       
   114             return array_key_exists('meta', $m) &&
       
   115                 array_key_exists('url', $m) &&
       
   116                 array_key_exists('origin', $m) &&
       
   117                 array_key_exists('unit', $m) &&
       
   118                 array_key_exists('id', $m) ; }), "all media must have url, id, origin, unit, meta key");
       
   119 
       
   120         $this->assertCount(1, array_filter($medias, function($m) { return array_key_exists('corpus:master', $m['meta'])?$m['meta']['corpus:master']:false; }), "should have at least 1 master");
       
   121 
       
   122     }
       
   123 
       
   124     public function testMediasContentDetail() {
       
   125         $converter =  $this->getMockConverter($this->doc, $this->transcriptSource)->getMock()->makePartial();
       
   126         $resJson = $converter->convertTojson();
       
   127 
       
   128         $medias = $resJson['medias'];
       
   129         foreach($medias as $m) {
       
   130             $this->assertEquals(0, $m['origin'], "Origin must be 0");
       
   131             $this->assertEquals('ms', $m['unit'], "unit must be ms");
       
   132             $this->assertStringStartsWith('http', $m['url'], "urls must be string and start with http");
       
   133             $this->assertRegExp('/^11280\\.100\\/crdo-UVE_MOCIKA_SOUND_m\d+$/', $m['id'], "id must start with 11280.100\/crdo-UVE_MOCIKA_SOUND_m");
       
   134             $this->assertArrayHasKey('meta', $m, "Media def must have a meta");
       
   135             $mediaMeta = $m['meta'];
       
   136             $this->assertArrayHasKey('dc:duration', $mediaMeta, "media meta must have duration");
       
   137             $this->assertEquals(155000, $mediaMeta['dc:duration'], "media meta duration is 1555000 ms");
       
   138             $this->assertArrayHasKey('dc:title', $mediaMeta, "Media meta mus have title");
       
   139             $this->assertEquals(["@language" => 'en', "@value" => "The two hermit crabs and the coconut crab"], $mediaMeta['dc:title'], "media meta title is ...");
       
   140             $this->assertArrayHasKey('dc:format', $mediaMeta, 'media meta has dc:format');
       
   141             $this->assertStringStartsWith('audio/', $mediaMeta['dc:format'], "media meta dc:format starts with audio");
       
   142         }
       
   143     }
       
   144 
       
   145     public function testOtherNodes() {
       
   146         $converter =  $this->getMockConverter($this->doc, $this->transcriptSource)->getMock()->makePartial();
       
   147         $resJson = $converter->convertTojson();
       
   148 
       
   149         $this->assertArrayHasKey('resources', $resJson, 'res must have resources');
       
   150         $this->assertEquals([], $resJson['resources'], 'Resources must be empty array');
       
   151         $this->assertArrayHasKey('lists', $resJson, 'res must have lists');
       
   152         $this->assertEquals([], $resJson['lists'], 'Lists must be empty array');
       
   153         $this->assertArrayHasKey('annotation-types', $resJson, 'res must have annotation-types');
       
   154         $this->assertEquals([], $resJson['annotation-types'], 'Annotation types must be empty array');
       
   155         $this->assertArrayHasKey('annotations', $resJson, 'res must have annotations');
       
   156         $this->assertEquals([], $resJson['annotations'], 'Annotations must be empty array');
       
   157     }
       
   158 
       
   159     public function tearDown() {
       
   160         m::close();
       
   161     }
       
   162 
       
   163 }