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