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