diff --git a/lib/portfoliolib.php b/lib/portfoliolib.php index 1b42e436a64..7a3483261d5 100644 --- a/lib/portfoliolib.php +++ b/lib/portfoliolib.php @@ -1224,13 +1224,25 @@ function portfolio_format_text_options() { * @return object|array|string */ function portfolio_rewrite_pluginfile_url_callback($contextid, $component, $filearea, $itemid, $format, $options, $matches) { - $matches = $matches[0]; // no internal matching + $matches = $matches[0]; // No internal matching. + + // Loads the HTML. $dom = new DomDocument(); - if (!$dom->loadXML($matches)) { + if (!$dom->loadHTML($matches)) { return $matches; } + + // Navigates to the node. + $xpath = new DOMXPath($dom); + $nodes = $xpath->query('/html/body/child::*'); + if (empty($nodes) || count($nodes) > 1) { + // Unexpected sequence, none or too many nodes. + return $matches; + } + $dom = $nodes->item(0); + $attributes = array(); - foreach ($dom->documentElement->attributes as $attr => $node) { + foreach ($dom->attributes as $attr => $node) { $attributes[$attr] = $node->value; } // now figure out the file @@ -1360,7 +1372,11 @@ function portfolio_include_callback_file($component, $class = null) { * @return mixed */ function portfolio_rewrite_pluginfile_urls($text, $contextid, $component, $filearea, $itemid, $format, $options=null) { - $pattern = '/(<[^<]*?="@@PLUGINFILE@@\/[^>]*?(?:\/>|>.*?<\/[^>]*?>))/'; + $patterns = array( + '(<(a|A)[^<]*?href="@@PLUGINFILE@@/[^>]*?>.*?(a|A)>)', + '(<(img|IMG)\s[^<]*?src="@@PLUGINFILE@@/[^>]*?/?>)', + ); + $pattern = '~' . implode('|', $patterns) . '~'; $callback = partial('portfolio_rewrite_pluginfile_url_callback', $contextid, $component, $filearea, $itemid, $format, $options); return preg_replace_callback($pattern, $callback, $text); } diff --git a/lib/tests/portfoliolib_test.php b/lib/tests/portfoliolib_test.php new file mode 100644 index 00000000000..1d8a0517a64 --- /dev/null +++ b/lib/tests/portfoliolib_test.php @@ -0,0 +1,170 @@ +. + +/** + * Portfolio lib tests. + * + * @package core + * @subpackage phpunit + * @copyright 2014 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); +global $CFG; + +require_once($CFG->libdir.'/portfoliolib.php'); +require_once($CFG->libdir.'/portfolio/formats.php'); + +/** + * Portfolio lib testcase. + * + * @copyright 2014 Frédéric Massart + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class core_portfoliolib_testcase extends advanced_testcase { + + public function test_portfolio_rewrite_pluginfile_urls() { + $this->resetAfterTest(); + + // File info. + $context = context_system::instance(); + $component = 'core_test'; + $filearea = 'fixture'; + $filepath = '/'; + $itemid = 0; + $filenameimg = 'file.png'; + $filenamepdf = 'file.pdf'; + + // Store 2 test files in the pool. + $fs = get_file_storage(); + $filerecord = array( + 'contextid' => $context->id, + 'component' => $component, + 'filearea' => $filearea, + 'itemid' => $itemid, + 'filepath' => $filepath, + 'filename' => $filenameimg, + ); + $fileimg = $fs->create_file_from_string($filerecord, 'test'); + + $filerecord['filename'] = $filenamepdf; + $filepdf = $fs->create_file_from_string($filerecord, 'test'); + + // Test that nothing is matching. + $format = ''; + $options = null; + $input = '
Come and join us!?
'; + $expected = 'Come and ' . $filenamepdf . '?
'; + $output = portfolio_rewrite_pluginfile_urls($input, $context->id, $component, $filearea, $itemid, $format, $options); + $this->assertSame($expected, $output); + + $input = 'Come and join us!?
'; + $expected = 'Come and ' . $filenamepdf . '?
'; + $output = portfolio_rewrite_pluginfile_urls($input, $context->id, $component, $filearea, $itemid, $format, $options); + $this->assertSame($expected, $output); + + // Test that the image is matching. + $input = 'Here is an image
Here is an image
Here is an image
Here is an image
' . $filenamepdf . '' . + '' . $filenamepdf . '
'; + $output = portfolio_rewrite_pluginfile_urls($input, $context->id, $component, $filearea, $itemid, $format, $options); + $this->assertSame($expected, $output); + + $input = '' .
+ '
join us!' .
+ '
' . $filenamepdf . '' .
+ '