xref: /dokuwiki/_test/tests/inc/parser/parser_media.test.php (revision 058c6bae05adf8905c416ad1b29716c3f3a70c88)
1912be8f6SMichael Große<?php
2912be8f6SMichael Großerequire_once 'parser.inc.php';
3912be8f6SMichael Große
4912be8f6SMichael Große/**
5912be8f6SMichael Große * Tests for the implementation of audio and video files
6912be8f6SMichael Große *
779fdbafcSLarsDW223 * @group parser_media
8912be8f6SMichael Große * @author  Michael Große <grosse@cosmocode.de>
9912be8f6SMichael Große*/
10912be8f6SMichael Großeclass TestOfDoku_Parser_Media extends TestOfDoku_Parser {
11912be8f6SMichael Große
12912be8f6SMichael Große    function testVideoOGVExternal() {
13912be8f6SMichael Große        $file = 'http://some.where.far/away.ogv';
14912be8f6SMichael Große        $parser_response = p_get_instructions('{{' . $file . '}}');
15912be8f6SMichael Große
16912be8f6SMichael Große        $calls = array (
17912be8f6SMichael Große            array('document_start',array()),
18912be8f6SMichael Große            array('p_open',array()),
19d8ab8746SAndreas Gohr            array('externalmedia',array($file,null,null,null,null,'cache','details')),
20912be8f6SMichael Große            array('cdata',array(null)),
21912be8f6SMichael Große            array('p_close',array()),
22d8ab8746SAndreas Gohr            array('document_end',array()),
23912be8f6SMichael Große        );
24912be8f6SMichael Große        $this->assertEquals(array_map('stripbyteindex',$parser_response),$calls);
25912be8f6SMichael Große
26912be8f6SMichael Große        $Renderer = new Doku_Renderer_xhtml();
27d8ab8746SAndreas Gohr        $url = $Renderer->externalmedia($file,null,null,null,null,'cache','details',true);
28912be8f6SMichael Große        //print_r("url: " . $url);
29d8ab8746SAndreas Gohr        $video = '<video class="media" width="320" height="240" controls="controls">';
30d8ab8746SAndreas Gohr        $this->assertEquals(substr($url,0,66),$video);
31912be8f6SMichael Große        $source = '<source src="http://some.where.far/away.ogv" type="video/ogg" />';
32d8ab8746SAndreas Gohr        $this->assertEquals(substr($url,67,64),$source);
33912be8f6SMichael Große        // work around random token
34*058c6baeSAndreas Gohr        $a_first_part = '<a href="' . DOKU_BASE . 'lib/exe/fetch.php?tok=';
35912be8f6SMichael Große        $a_second_part = '&amp;media=http%3A%2F%2Fsome.where.far%2Faway.ogv" class="media mediafile mf_ogv" title="http://some.where.far/away.ogv">';
36d09fc643SPhil Hopper
37d8ab8746SAndreas Gohr        $substr_start = 132;
38d09fc643SPhil Hopper        $substr_len = strlen($a_first_part);
39d09fc643SPhil Hopper        $this->assertEquals($a_first_part, substr($url, $substr_start, $substr_len));
40d09fc643SPhil Hopper
41d09fc643SPhil Hopper        $substr_start = strpos($url, '&amp;media', $substr_start + $substr_len);
42d09fc643SPhil Hopper        $this->assertNotSame(false, $substr_start, 'Substring not found.');
43d09fc643SPhil Hopper        $substr_len = strlen($a_second_part);
44d09fc643SPhil Hopper        $this->assertEquals($a_second_part, substr($url, $substr_start, $substr_len));
45d09fc643SPhil Hopper
46912be8f6SMichael Große        $rest = 'away.ogv</a></video>'."\n";
47d09fc643SPhil Hopper        $substr_start = strlen($url) - strlen($rest);
48d09fc643SPhil Hopper        $this->assertEquals($rest, substr($url, $substr_start));
49912be8f6SMichael Große    }
50912be8f6SMichael Große
51912be8f6SMichael Große    /**
52912be8f6SMichael Große     * unknown extension of external media file
53912be8f6SMichael Große     */
54912be8f6SMichael Große    function testVideoVIDExternal() {
55912be8f6SMichael Große        $file = 'http://some.where.far/away.vid';
56912be8f6SMichael Große        $parser_response = p_get_instructions('{{' . $file . '}}');
57912be8f6SMichael Große
58912be8f6SMichael Große        $calls = array(
59912be8f6SMichael Große            array('document_start', array()),
60912be8f6SMichael Große            array('p_open', array()),
61d8ab8746SAndreas Gohr            array('externalmedia', array($file, null, null, null, null, 'cache', 'details')),
62912be8f6SMichael Große            array('cdata', array(null)),
63912be8f6SMichael Große            array('p_close', array()),
64912be8f6SMichael Große            array('document_end', array()),
65912be8f6SMichael Große        );
66912be8f6SMichael Große        $this->assertEquals(array_map('stripbyteindex', $parser_response), $calls);
67912be8f6SMichael Große
68912be8f6SMichael Große        $Renderer = new Doku_Renderer_xhtml();
69d8ab8746SAndreas Gohr        $url = $Renderer->externalmedia($file, null, null, null, null, 'cache', 'details', true);
70912be8f6SMichael Große        // work around random token
71d09fc643SPhil Hopper        $a_first_part = '<a href="' . DOKU_BASE . 'lib/exe/fetch.php?tok=';
72912be8f6SMichael Große        $a_second_part = '&amp;media=http%3A%2F%2Fsome.where.far%2Faway.vid" class="media mediafile mf_vid" title="http://some.where.far/away.vid">';
73d09fc643SPhil Hopper
74d09fc643SPhil Hopper        $substr_start = 0;
75d09fc643SPhil Hopper        $substr_len = strlen($a_first_part);
76d09fc643SPhil Hopper        $this->assertEquals($a_first_part, substr($url, $substr_start, $substr_len));
77d09fc643SPhil Hopper
78d09fc643SPhil Hopper        $substr_start = strpos($url, '&amp;media', $substr_start + $substr_len);
79d09fc643SPhil Hopper        $this->assertNotSame(false, $substr_start, 'Substring not found.');
80d09fc643SPhil Hopper        $substr_len = strlen($a_second_part);
81d09fc643SPhil Hopper        $this->assertEquals($a_second_part, substr($url, $substr_start, $substr_len));
82d09fc643SPhil Hopper
83912be8f6SMichael Große        $rest = 'away.vid</a>';
84d09fc643SPhil Hopper        $substr_start = strlen($url) - strlen($rest);
85d09fc643SPhil Hopper        $this->assertEquals($rest, substr($url, $substr_start));
86912be8f6SMichael Große    }
87db42b6f8SMichael Große
88db42b6f8SMichael Große
89db42b6f8SMichael Große    function testVideoOGVInternal() {
90db42b6f8SMichael Große        $file = 'wiki:kind_zu_katze.ogv';
91db42b6f8SMichael Große        $parser_response = p_get_instructions('{{' . $file . '}}');
92db42b6f8SMichael Große
93db42b6f8SMichael Große        $calls = array (
94db42b6f8SMichael Große            array('document_start',array()),
95db42b6f8SMichael Große            array('p_open',array()),
96d8ab8746SAndreas Gohr            array('internalmedia',array($file,null,null,null,null,'cache','details')),
97db42b6f8SMichael Große            array('cdata',array(null)),
98db42b6f8SMichael Große            array('p_close',array()),
99db42b6f8SMichael Große            array('document_end',array()),
100db42b6f8SMichael Große        );
101db42b6f8SMichael Große        $this->assertEquals(array_map('stripbyteindex',$parser_response),$calls);
102db42b6f8SMichael Große
103db42b6f8SMichael Große        $Renderer = new Doku_Renderer_xhtml();
104d8ab8746SAndreas Gohr        $url = $Renderer->externalmedia($file,null,null,null,null,'cache','details',true);
105db42b6f8SMichael Große
106d8ab8746SAndreas Gohr        $video = '<video class="media" width="320" height="240" controls="controls" poster="' . DOKU_BASE . 'lib/exe/fetch.php?media=wiki:kind_zu_katze.png">';
107d09fc643SPhil Hopper        $substr_start = 0;
108d09fc643SPhil Hopper        $substr_len = strlen($video);
109d09fc643SPhil Hopper        $this->assertEquals($video, substr($url, $substr_start, $substr_len));
110db42b6f8SMichael Große
111d09fc643SPhil Hopper        // find $source_webm in $url
112d09fc643SPhil Hopper        $source_webm = '<source src="' . DOKU_BASE . 'lib/exe/fetch.php?media=wiki:kind_zu_katze.webm" type="video/webm" />';
113d09fc643SPhil Hopper        $substr_start = strpos($url, $source_webm, $substr_start + $substr_len);
114d09fc643SPhil Hopper        $this->assertNotSame(false, $substr_start, 'Substring not found.');
115db42b6f8SMichael Große
116d09fc643SPhil Hopper        // find $source_ogv in $url
117d09fc643SPhil Hopper        $source_ogv = '<source src="' . DOKU_BASE . 'lib/exe/fetch.php?media=wiki:kind_zu_katze.ogv" type="video/ogg" />';
118d09fc643SPhil Hopper        $substr_start = strpos($url, $source_ogv, $substr_start + strlen($source_webm));
119d09fc643SPhil Hopper        $this->assertNotSame(false, $substr_start, 'Substring not found.');
120d09fc643SPhil Hopper
121d09fc643SPhil Hopper        // find $a_webm in $url
122*058c6baeSAndreas Gohr        $a_webm = '<a href="' . DOKU_BASE . 'lib/exe/fetch.php?media=wiki:kind_zu_katze.webm" class="media mediafile mf_webm" title="wiki:kind_zu_katze.webm (99.1'."\xC2\xA0".'KB)">kind_zu_katze.webm</a>';
123d09fc643SPhil Hopper        $substr_start = strpos($url, $a_webm, $substr_start + strlen($source_ogv));
124d09fc643SPhil Hopper        $this->assertNotSame(false, $substr_start, 'Substring not found.');
125d09fc643SPhil Hopper
126d09fc643SPhil Hopper        // find $a_webm in $url
127*058c6baeSAndreas Gohr        $a_ogv = '<a href="' . DOKU_BASE . 'lib/exe/fetch.php?media=wiki:kind_zu_katze.ogv" class="media mediafile mf_ogv" title="wiki:kind_zu_katze.ogv (44.8'."\xC2\xA0".'KB)">kind_zu_katze.ogv</a>';
128d09fc643SPhil Hopper        $substr_start = strpos($url, $a_ogv, $substr_start + strlen($a_webm));
129d09fc643SPhil Hopper        $this->assertNotSame(false, $substr_start, 'Substring not found.');
130db42b6f8SMichael Große
131db42b6f8SMichael Große        $rest = '</video>'."\n";
132d09fc643SPhil Hopper        $substr_start = strlen($url) - strlen($rest);
133d09fc643SPhil Hopper        $this->assertEquals($rest, substr($url, $substr_start));
134db42b6f8SMichael Große    }
13579fdbafcSLarsDW223
1367d3a1fc9Sbleistivt    function testVideoInternalTitle() {
1377d3a1fc9Sbleistivt        $file = 'wiki:kind_zu_katze.ogv';
1387d3a1fc9Sbleistivt        $title = 'Single quote: \' Ampersand: &';
1397d3a1fc9Sbleistivt
1407d3a1fc9Sbleistivt        $Renderer = new Doku_Renderer_xhtml();
141d8ab8746SAndreas Gohr        $url = $Renderer->externalmedia($file, $title, null, null, null, 'cache', 'details', true);
1427d3a1fc9Sbleistivt
1437d3a1fc9Sbleistivt        // make sure the title is escaped just once
144f7711f2bSAndreas Gohr        $this->assertEquals(hsc($title), substr($url, 28, 37));
1457d3a1fc9Sbleistivt    }
1467d3a1fc9Sbleistivt
14779fdbafcSLarsDW223    function testSimpleLinkText() {
14879fdbafcSLarsDW223        $file = 'wiki:dokuwiki-128.png';
14979fdbafcSLarsDW223        $parser_response = p_get_instructions('{{' . $file . '|This is a simple text.}}');
15079fdbafcSLarsDW223
15179fdbafcSLarsDW223        $calls = array (
15279fdbafcSLarsDW223            array('document_start',array()),
15379fdbafcSLarsDW223            array('p_open',array()),
154d8ab8746SAndreas Gohr            array('internalmedia',array($file,'This is a simple text.',null,null,null,'cache','details')),
15579fdbafcSLarsDW223            array('cdata',array(null)),
15679fdbafcSLarsDW223            array('p_close',array()),
15779fdbafcSLarsDW223            array('document_end',array()),
15879fdbafcSLarsDW223        );
15979fdbafcSLarsDW223        $this->assertEquals(array_map('stripbyteindex',$parser_response),$calls);
16079fdbafcSLarsDW223    }
16179fdbafcSLarsDW223
16279fdbafcSLarsDW223    function testLinkTextWithWavedBrackets_1() {
16379fdbafcSLarsDW223        $file = 'wiki:dokuwiki-128.png';
16479fdbafcSLarsDW223        $parser_response = p_get_instructions('{{' . $file . '|We got a { here.}}');
16579fdbafcSLarsDW223
16679fdbafcSLarsDW223        $calls = array (
16779fdbafcSLarsDW223            array('document_start',array()),
16879fdbafcSLarsDW223            array('p_open',array()),
169d8ab8746SAndreas Gohr            array('internalmedia',array($file,'We got a { here.',null,null,null,'cache','details')),
17079fdbafcSLarsDW223            array('cdata',array(null)),
17179fdbafcSLarsDW223            array('p_close',array()),
17279fdbafcSLarsDW223            array('document_end',array()),
17379fdbafcSLarsDW223        );
17479fdbafcSLarsDW223        $this->assertEquals(array_map('stripbyteindex',$parser_response),$calls);
17579fdbafcSLarsDW223    }
17679fdbafcSLarsDW223
17779fdbafcSLarsDW223    function testLinkTextWithWavedBrackets_2() {
17879fdbafcSLarsDW223        $file = 'wiki:dokuwiki-128.png';
17979fdbafcSLarsDW223        $parser_response = p_get_instructions('{{' . $file . '|We got a } here.}}');
18079fdbafcSLarsDW223
18179fdbafcSLarsDW223        $calls = array (
18279fdbafcSLarsDW223            array('document_start',array()),
18379fdbafcSLarsDW223            array('p_open',array()),
184d8ab8746SAndreas Gohr            array('internalmedia',array($file,'We got a } here.',null,null,null,'cache','details')),
18579fdbafcSLarsDW223            array('cdata',array(null)),
18679fdbafcSLarsDW223            array('p_close',array()),
18779fdbafcSLarsDW223            array('document_end',array()),
18879fdbafcSLarsDW223        );
18979fdbafcSLarsDW223        $this->assertEquals(array_map('stripbyteindex',$parser_response),$calls);
19079fdbafcSLarsDW223    }
19179fdbafcSLarsDW223
19279fdbafcSLarsDW223    function testLinkTextWithWavedBrackets_3() {
19379fdbafcSLarsDW223        $file = 'wiki:dokuwiki-128.png';
19479fdbafcSLarsDW223        $parser_response = p_get_instructions('{{' . $file . '|We got a { and a } here.}}');
19579fdbafcSLarsDW223
19679fdbafcSLarsDW223        $calls = array (
19779fdbafcSLarsDW223            array('document_start',array()),
19879fdbafcSLarsDW223            array('p_open',array()),
199d8ab8746SAndreas Gohr            array('internalmedia',array($file,'We got a { and a } here.',null,null,null,'cache','details')),
20079fdbafcSLarsDW223            array('cdata',array(null)),
20179fdbafcSLarsDW223            array('p_close',array()),
20279fdbafcSLarsDW223            array('document_end',array()),
20379fdbafcSLarsDW223        );
20479fdbafcSLarsDW223        $this->assertEquals(array_map('stripbyteindex',$parser_response),$calls);
20579fdbafcSLarsDW223    }
206912be8f6SMichael Große}
207