1<?php
2
3namespace dokuwiki\plugin\struct\test;
4
5use dokuwiki\plugin\struct\meta\ValidationException;
6use dokuwiki\plugin\struct\types\Media;
7
8/**
9 * Testing the Media Type
10 *
11 * @group plugin_struct
12 * @group plugins
13 */
14class Type_Media_struct_test extends StructTest
15{
16
17    /**
18     * Provides failing validation data
19     *
20     * @return array
21     */
22    public function validateFailProvider()
23    {
24        return array(
25            array('image/jpeg, image/png', 'foo.gif'),
26            array('image/jpeg, image/png', 'http://www.example.com/foo.gif'),
27            array('application/octet-stream', 'hey:joe.jpeg'),
28            array('application/octet-stream', 'http://www.example.com/hey:joe.jpeg'),
29        );
30    }
31
32    /**
33     * Provides successful validation data
34     *
35     * @return array
36     */
37    public function validateSuccessProvider()
38    {
39        return array(
40            array('', 'foo.png'),
41            array('', 'http://www.example.com/foo.png'),
42            array('image/jpeg, image/png', 'foo.png'),
43            array('image/jpeg, image/png', 'http://www.example.com/foo.png'),
44            array('image/jpeg, image/png', 'http://www.example.com/dynamic?.png'),
45            array('application/octet-stream', 'hey:joe.exe'),
46            array('application/octet-stream', 'http://www.example.com/hey:joe.exe'),
47
48        );
49    }
50
51    /**
52     * @dataProvider validateFailProvider
53     */
54    public function test_validate_fail($mime, $value)
55    {
56        $this->expectException(ValidationException::class);
57        $integer = new Media(array('mime' => $mime));
58        $integer->validate($value);
59    }
60
61    /**
62     * @dataProvider validateSuccessProvider
63     */
64    public function test_validate_success($mime, $value)
65    {
66        $integer = new Media(array('mime' => $mime));
67        $integer->validate($value);
68        $this->assertTrue(true); // we simply check that no exceptions are thrown
69    }
70
71    public function test_render_page_img()
72    {
73        $R = new \Doku_Renderer_xhtml();
74
75        $media = new Media(array('width' => 150, 'height' => 160, 'agg_width' => 180, 'agg_height' => 190));
76        $media->renderValue('foo.png', $R, 'xhtml');
77        $pq = \phpQuery::newDocument($R->doc);
78
79        $a = $pq->find('a');
80        $img = $pq->find('img');
81
82        $this->assertStringContainsString('fetch.php', $a->attr('href')); // direct link goes to fetch
83        $this->assertEquals('lightbox', $a->attr('rel')); // lightbox single mode
84        $this->assertStringContainsString('w=150', $img->attr('src')); // fetch param
85        $this->assertEquals(150, $img->attr('width')); // img param
86        $this->assertStringContainsString('h=160', $img->attr('src')); // fetch param
87        $this->assertEquals(160, $img->attr('height')); // img param
88    }
89
90    public function test_render_aggregation_img()
91    {
92        $R = new \Doku_Renderer_xhtml();
93        $R->info['struct_table_hash'] = 'HASH';
94
95        $media = new Media(array('width' => 150, 'height' => 160, 'agg_width' => 180, 'agg_height' => 190));
96        $media->renderValue('foo.png', $R, 'xhtml');
97        $pq = \phpQuery::newDocument($R->doc);
98
99        $a = $pq->find('a');
100        $img = $pq->find('img');
101
102        $this->assertStringContainsString('fetch.php', $a->attr('href')); // direct link goes to fetch
103        $this->assertEquals('lightbox[gal-HASH]', $a->attr('rel')); // lightbox single mode
104        $this->assertStringContainsString('w=180', $img->attr('src')); // fetch param
105        $this->assertEquals(180, $img->attr('width')); // img param
106        $this->assertStringContainsString('h=190', $img->attr('src')); // fetch param
107        $this->assertEquals(190, $img->attr('height')); // img param
108    }
109
110    public function test_render_aggregation_pdf()
111    {
112        $R = new \Doku_Renderer_xhtml();
113
114        $media = new Media(array('width' => 150, 'height' => 160, 'agg_width' => 180, 'agg_height' => 190, 'mime' => ''));
115        $media->renderValue('foo.pdf', $R, 'xhtml');
116        $pq = \phpQuery::newDocument($R->doc);
117
118        $a = $pq->find('a');
119        $img = $pq->find('img');
120
121        $this->assertStringContainsString('fetch.php', $a->attr('href')); // direct link goes to fetch
122        $this->assertTrue($a->hasClass('mediafile')); // it's a media link
123        $this->assertEquals('', $a->attr('rel')); // no lightbox
124        $this->assertEquals(0, $img->length); // no image
125        $this->assertEquals('foo.pdf', $a->text()); // name is link name
126    }
127
128    public function test_render_aggregation_video()
129    {
130        $R = new \Doku_Renderer_xhtml();
131
132        // local video requires an existing file to be rendered. we fake one
133        $fake = mediaFN('foo.mp4');
134        touch($fake);
135
136        $media = new Media(array('width' => 150, 'height' => 160, 'agg_width' => 180, 'agg_height' => 190, 'mime' => ''));
137        $media->renderValue('foo.mp4', $R, 'xhtml');
138        $pq = \phpQuery::newDocument($R->doc);
139
140        $a = $pq->find('a');
141        $vid = $pq->find('video');
142        $src = $pq->find('source');
143
144        $this->assertStringContainsString('fetch.php', $a->attr('href')); // direct link goes to fetch
145        $this->assertStringContainsString('fetch.php', $src->attr('src')); // direct link goes to fetch
146        $this->assertEquals(150, $vid->attr('width')); // video param
147        $this->assertEquals(160, $vid->attr('height')); // video param
148    }
149
150}
151