server/src/tests/Libraries/Transcript/LacitoTranscriptConverterTest.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 use CorpusParole\Libraries\Transcript\LacitoTranscriptConverter;
       
     7 
       
     8 
       
     9 /**
       
    10  *
       
    11  */
       
    12 class LacitoTranscriptConverterTest extends TestCase {
       
    13     const TEST_DOC_BASE = "crdo-UVE_MOCIKA";
       
    14 
       
    15     public function setUp() {
       
    16         parent::setup();
       
    17         $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'));
       
    18         $this->graph = new EasyRdf\Graph(config('corpusparole.corpus_doc_id_base_uri')."crdo-UVE_MOCIKA_SOUND", $graphContent);
       
    19         $this->doc = new Document(config('corpusparole.corpus_doc_id_base_uri')."crdo-UVE_MOCIKA_SOUND", $this->graph);
       
    20 
       
    21         $this->transcriptSource = new DOMDocument();
       
    22         $this->transcriptSource->load(__DIR__ . DIRECTORY_SEPARATOR . self::TEST_DOC_BASE.".xml", LIBXML_NOCDATA);
       
    23 
       
    24         $this->transcriptSourceSpeaker = new DOMDocument();
       
    25         $this->transcriptSourceSpeaker->load(__DIR__ . DIRECTORY_SEPARATOR ."crdo-FRA_PK_IV_10.xml", LIBXML_NOCDATA);
       
    26 
       
    27         $this->transcriptSourceNoContent = new DOMDocument();
       
    28         $this->transcriptSourceNoContent->load(__DIR__ . DIRECTORY_SEPARATOR ."crdo-FSL-CUC023.xml", LIBXML_NOCDATA);
       
    29 
       
    30     }
       
    31 
       
    32     public function getMockConverter(...$contructorArgs) {
       
    33         return new LacitoTranscriptConverter(...$contructorArgs);
       
    34     }
       
    35 
       
    36 
       
    37     public function testConstructor() {
       
    38         $converter = $this->getMockConverter($this->doc, $this->transcriptSource);
       
    39         $json = $converter->convertToJson();
       
    40         $this->assertNotnull($json);
       
    41     }
       
    42 
       
    43     public function testGetSourceTitle() {
       
    44         $converter = $this->getMockConverter($this->doc, $this->transcriptSource);
       
    45         $titles = $converter->getSourceTitle();
       
    46 
       
    47         $this->assertEquals(['en' => 'The two hermit crabs and the coconut crab', 'fr' => "Les deux bernard-l'hermite et le crabe de cocotier"], $titles, "Titles must come from the xml file");
       
    48     }
       
    49 
       
    50     public function testBuildAnnotations() {
       
    51         $converter = $this->getMockConverter($this->doc, $this->transcriptSource);
       
    52         $annotations = $converter->buildAnnotations();
       
    53 
       
    54         $this->assertCount(32, $annotations, "Must have 32 annotation");
       
    55         foreach($annotations as $i => $a) {
       
    56             $this->assertArrayHasKey('begin', $a, "Must have begin key");
       
    57             $this->assertArrayHasKey('end', $a, "Must have begin key");
       
    58             $this->assertTrue($a['begin']<$a['end'], "Begin is < to endP");
       
    59             $this->assertEquals($this->doc->getId()."_a".sprintf("%03s",($i+1)), $a['id']);
       
    60             $this->assertEquals($converter->getMediaRefId(), $a['media']);
       
    61             $this->assertArrayHasKey('content', $a, "must have content");
       
    62             $this->assertTrue(is_array($a['content']));
       
    63             $this->assertArrayHasKey('mimetype', $a['content']);
       
    64             $this->assertEquals('application/json', $a['content']['mimetype']);
       
    65             $this->assertArrayHasKey('data', $a['content']);
       
    66             $this->assertTrue(is_array($a['content']['data']));
       
    67         }
       
    68     }
       
    69 
       
    70 
       
    71     public function testBuildAnnotationsContent() {
       
    72         $converter = $this->getMockConverter($this->doc, $this->transcriptSource);
       
    73         $annotations = $converter->buildAnnotations();
       
    74 
       
    75         foreach($annotations as $i => $a) {
       
    76             $data = $a['content']['data'];
       
    77             $this->assertArrayNotHasKey('speaker', $data, "No spreaker in data for this source");
       
    78             $this->assertArrayHasKey('content', $data, "data has content");
       
    79             $this->assertNotEmpty($data['content'], "content not empty");
       
    80             $this->arrayHasKey('transl', $data, 'data has translation');
       
    81             $this->assertTrue(is_array($data['transl']), 'data transl is array');
       
    82             $this->assertArrayHasKey('@value', $data['transl'], 'dats transl has @value key');
       
    83             $this->assertArrayHasKey('@language', $data['transl'], 'dats transl has @language key');
       
    84 
       
    85             $this->assertArrayHasKey('words', $data, 'data has words');
       
    86             $this->assertNotEmpty($data['words'], "words are not empty");
       
    87             foreach($data['words'] as $w) {
       
    88                 $this->assertTrue(is_array($w), 'words are array');
       
    89                 $this->assertArrayHasKey('content', $w, "words have content");
       
    90                 $this->assertNotEmpty($w['content'], "words have non empty content");
       
    91                 $this->assertArrayHasKey('transl', $w, "words have transl");
       
    92                 $this->assertArrayHasKey('@value', $w['transl'], 'words transl has @value key');
       
    93                 $this->assertArrayHasKey('@language', $w['transl'], 'words transl has @language key');
       
    94             }
       
    95         }
       
    96     }
       
    97 
       
    98 
       
    99     public function testBuildAnnotationsSpeaker() {
       
   100         $converter = $this->getMockConverter($this->doc, $this->transcriptSourceSpeaker);
       
   101         $annotations = $converter->buildAnnotations();
       
   102         foreach($annotations as $i => $a) {
       
   103             $data = $a['content']['data'];
       
   104             $this->assertArrayHasKey('speaker', $data, 'annotation must have speaker');
       
   105             $this->assertArrayNotHasKey('words', $data, 'No words in data here');
       
   106         }
       
   107     }
       
   108 
       
   109     public function testBuildAnnotationsSpeakerTitle() {
       
   110         $converter = $this->getMockConverter($this->doc, $this->transcriptSourceSpeaker);
       
   111         $this->assertNull($converter->getSourceTitle(), "Title is not on source");
       
   112     }
       
   113 
       
   114     public function testBuildAnnotationsNoContent() {
       
   115         $converter = $this->getMockConverter($this->doc, $this->transcriptSourceNoContent);
       
   116         $annotations = $converter->buildAnnotations();
       
   117         foreach($annotations as $i => $a) {
       
   118             $data = $a['content']['data'];
       
   119             $this->assertArrayNotHasKey('words', $data, 'No words in data here');
       
   120             $this->assertArrayNotHasKey('content', $data, 'No content in data here');
       
   121             $this->arrayHasKey('transl', $data, 'data has translation');
       
   122             $this->assertTrue(is_array($data['transl']), 'data transl is array');
       
   123             $this->assertArrayHasKey('@value', $data['transl'], 'dats transl has @value key');
       
   124             $this->assertArrayHasKey('@language', $data['transl'], 'dats transl has @language key');
       
   125         }
       
   126     }
       
   127 
       
   128 
       
   129     public function tearDown() {
       
   130         m::close();
       
   131     }
       
   132 
       
   133 }