From 469aa911c12d1f90e9baf0ac99d3092c25ae6519 Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Thu, 21 Jul 2016 16:04:29 +0800 Subject: [PATCH] MDL-54847 weblib: Support basic HTML media tags --- lib/tests/htmlpurifier_test.php | 126 ++++++++++++++++++++++++++++++++ lib/weblib.php | 33 ++++++++- 2 files changed, 158 insertions(+), 1 deletion(-) diff --git a/lib/tests/htmlpurifier_test.php b/lib/tests/htmlpurifier_test.php index ae22daee0ded8..7246fe9ccf221 100644 --- a/lib/tests/htmlpurifier_test.php +++ b/lib/tests/htmlpurifier_test.php @@ -318,4 +318,130 @@ public function test_allowed_schemes() { $text = 'link'; $this->assertSame('link', purify_html($text)); } + + /** + * Tests media tags. + * + * @dataProvider media_tags_provider + * @param string $mediatag HTML media tag + * @param string $expected expected result + */ + public function test_media_tags($mediatag, $expected) { + $actual = format_text($mediatag, FORMAT_MOODLE, ['filter' => false, 'noclean' => true]); + $this->assertEquals($expected, $actual); + } + + /** + * Test cases for the test_media_tags test. + */ + public function media_tags_provider() { + // Given a 1D array, computes the power set of those elements. + $p = function(array $set) { + return array_reduce($set, function($carry, $element) { + return array_merge($carry, array_map(function($subset) use ($element) { + return array_merge([$element], $subset); + }, $carry)); + }, [[]]); + }; + + // Takes an array of attributes, then generates a test for every possible combination of them + // (i.e., every element of the power set). The testcases are named using $prefix and then + // a '/' delimited string describing the attributes being used. $templats is for the + // raw data and expected result. + $generatetestcases = function($prefix, array $attrs, array $templates) use ($p) { + $attrcombinations = $p($attrs); + return array_reduce($attrcombinations, function($carry, $attrset) use ($prefix, $templates) { + $testcase = [$prefix . '/' . join('/', $attrset) => [ + sprintf($templates[0], join(" ", $attrset)), + sprintf($templates[1], join(" ", $attrset)) + ]]; + return empty(array_values($carry)[0]) ? $testcase : $carry + $testcase; + }, [[]]); + }; + + $audioattrs = [ + 'preload="auto"', 'autoplay=""', 'loop=""', 'muted=""', 'controls=""', + 'crossorigin="anonymous"', 'crossorigin="use-credentials"' + ]; + $videoattrs = [ + 'crossorigin="anonymous"', 'crossorigin="use-credentials"', + 'poster="https://upload.wikimedia.org/wikipedia/en/1/14/Space_jam.jpg"', + 'preload=""', 'autoplay=""', 'playsinline=""', 'loop=""', 'muted=""', + 'controls=""', 'width="420px"', 'height="69px"' + ]; + return $generatetestcases('Plain audio', $audioattrs + ['src="http://example.com/jam.wav"'], [ + '', + '
' + ]) + $generatetestcases('Audio with one source', $audioattrs, [ + '', + '
' . + '' . + '
' + ]) + $generatetestcases('Audio with multiple sources', $audioattrs, [ + '', + '
' . + '' . + '
' + ]) + $generatetestcases('Plain video', $videoattrs + ['src="http://example.com/prettygood.mp4'], [ + '', + '
' + ]) + $generatetestcases('Video with one source', $videoattrs, [ + '', + '
' . + '' . + '
' + ]) + $generatetestcases('Video with multiple sources', $videoattrs, [ + '