[FEATURE] Render additionalAttributes and data-* attr in media renderer
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / Rendering / AudioTagRendererTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource\Rendering;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Class AudioTagRendererTest
19 */
20 class AudioTagRendererTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
21 {
22 /**
23 * @test
24 */
25 public function getPriorityReturnsCorrectValue()
26 {
27 $audioTagRenderer = new \TYPO3\CMS\Core\Resource\Rendering\AudioTagRenderer();
28
29 $this->assertSame(1, $audioTagRenderer->getPriority());
30 }
31
32 /**
33 * @test
34 */
35 public function canRenderReturnsTrueOnCorrectFile()
36 {
37 $audioTagRenderer = new \TYPO3\CMS\Core\Resource\Rendering\AudioTagRenderer();
38
39 $fileResourceMock1 = $this->createMock(\TYPO3\CMS\Core\Resource\File::class);
40 $fileResourceMock1->expects($this->any())->method('getMimeType')->will($this->returnValue('audio/mpeg'));
41 $fileResourceMock2 = $this->createMock(\TYPO3\CMS\Core\Resource\File::class);
42 $fileResourceMock2->expects($this->any())->method('getMimeType')->will($this->returnValue('audio/wav'));
43 $fileResourceMock3 = $this->createMock(\TYPO3\CMS\Core\Resource\File::class);
44 $fileResourceMock3->expects($this->any())->method('getMimeType')->will($this->returnValue('audio/ogg'));
45
46 $this->assertTrue($audioTagRenderer->canRender($fileResourceMock1));
47 $this->assertTrue($audioTagRenderer->canRender($fileResourceMock2));
48 $this->assertTrue($audioTagRenderer->canRender($fileResourceMock3));
49 }
50
51 /**
52 * @test
53 */
54 public function canRenderReturnsFalseOnCorrectFile()
55 {
56 $audioTagRenderer = new \TYPO3\CMS\Core\Resource\Rendering\AudioTagRenderer();
57
58 $fileResourceMock = $this->createMock(\TYPO3\CMS\Core\Resource\File::class);
59 $fileResourceMock->expects($this->any())->method('getMimeType')->will($this->returnValue('video/mp4'));
60
61 $this->assertFalse($audioTagRenderer->canRender($fileResourceMock));
62 }
63
64 /**
65 * Array of configurations
66 */
67 public function renderArgumentsDataProvider()
68 {
69 return [
70 [
71 '//:path/myAudioFile?foo=bar&baz=true',
72 [],
73 '<audio controls><source src="//:path/myAudioFile?foo=bar&amp;baz=true" type="audio/mpeg"></audio>',
74 ],
75 [
76 '//:path/myAudioFile',
77 ['loop' => 1],
78 '<audio controls loop><source src="//:path/myAudioFile" type="audio/mpeg"></audio>',
79 ],
80 [
81 '//:path/myAudioFile',
82 ['autoplay' => 1],
83 '<audio controls autoplay><source src="//:path/myAudioFile" type="audio/mpeg"></audio>',
84 ],
85 [
86 '//:path/myAudioFile',
87 ['controls' => 0, 'autoplay' => 1],
88 '<audio autoplay><source src="//:path/myAudioFile" type="audio/mpeg"></audio>',
89 ],
90 [
91 '//:path/myAudioFile',
92 ['controls' => 1, 'controlsList' => 'nodownload'],
93 '<audio controls controlsList="nodownload"><source src="//:path/myAudioFile" type="audio/mpeg"></audio>',
94 ],
95 [
96 '//:path/myAudioFile',
97 ['additionalAttributes' => ['muted' => 'muted', 'foo' => 'bar']],
98 '<audio muted="muted" foo="bar" controls><source src="//:path/myAudioFile" type="audio/mpeg"></audio>',
99 ],
100 [
101 '//:path/myAudioFile',
102 ['data' => ['js-required' => 'yes', 'custom-id' => 'audio-123']],
103 '<audio data-js-required="yes" data-custom-id="audio-123" controls><source src="//:path/myAudioFile" type="audio/mpeg"></audio>',
104 ]
105 ,
106 [
107 '//:path/myAudioFile',
108 ['data' => ['js-required' => 'yes', 'custom-id' => 'audio-123'], 'additionalAttributes' => ['muted' => 'muted', 'foo' => 'bar']],
109 '<audio muted="muted" foo="bar" data-js-required="yes" data-custom-id="audio-123" controls><source src="//:path/myAudioFile" type="audio/mpeg"></audio>',
110 ]
111 ];
112 }
113
114 /**
115 * @test
116 * @dataProvider renderArgumentsDataProvider
117 * @param string $url
118 * @param array $arguments
119 * @param string $expected
120 */
121 public function renderOutputIsCorrect($url, $arguments, $expected)
122 {
123 $audioTagRenderer = new \TYPO3\CMS\Core\Resource\Rendering\AudioTagRenderer();
124
125 $fileResourceMock = $this->createMock(\TYPO3\CMS\Core\Resource\File::class);
126 $fileResourceMock->expects($this->any())->method('getMimeType')->will($this->returnValue('audio/mpeg'));
127 $fileResourceMock->expects($this->any())->method('getPublicUrl')->will($this->returnValue($url));
128
129 $this->assertSame(
130 $expected,
131 $audioTagRenderer->render($fileResourceMock, '300m', '200', $arguments)
132 );
133 }
134 }