2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Portfolio lib tests.
22 * @copyright 2014 Frédéric Massart
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 require_once($CFG->libdir
.'/portfoliolib.php');
30 require_once($CFG->libdir
.'/portfolio/formats.php');
33 * Portfolio lib testcase.
35 * @copyright 2014 Frédéric Massart
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class portfoliolib_test
extends advanced_testcase
{
40 public function test_portfolio_rewrite_pluginfile_urls() {
41 $this->resetAfterTest();
44 $context = context_system
::instance();
45 $component = 'core_test';
46 $filearea = 'fixture';
49 $filenameimg = 'file.png';
50 $filenamepdf = 'file.pdf';
52 // Store 2 test files in the pool.
53 $fs = get_file_storage();
55 'contextid' => $context->id
,
56 'component' => $component,
57 'filearea' => $filearea,
59 'filepath' => $filepath,
60 'filename' => $filenameimg,
62 $fileimg = $fs->create_file_from_string($filerecord, 'test');
64 $filerecord['filename'] = $filenamepdf;
65 $filepdf = $fs->create_file_from_string($filerecord, 'test');
67 // Test that nothing is matching.
70 $input = '<div>Here, the <a href="nowhere">@@PLUGINFILE@@' . $filepath . $filenamepdf .
71 ' is</a> not supposed to be an actual URL placeholder.</div>';
72 $output = portfolio_rewrite_pluginfile_urls($input, $context->id
, $component, $filearea, $itemid, $format, $options);
73 $this->assertSame($input, $output);
75 $input = '<div>Here, the <img src="nowhere" />@@PLUGINFILE@@' . $filepath . $filenameimg .
76 ' is</a> not supposed to be an actual URL placeholder.</div>';
77 $output = portfolio_rewrite_pluginfile_urls($input, $context->id
, $component, $filearea, $itemid, $format, $options);
78 $this->assertSame($input, $output);
80 // Now use our dummy format.
81 $format = new core_portfolio_format_dummy();
84 // Test that the link is matching.
85 $input = '<p>Come and <a href="@@PLUGINFILE@@' . $filepath . $filenamepdf . '">join us!</a>?</p>';
86 $expected = '<p>Come and <a href="files/' . $filenamepdf . '">' . $filenamepdf . '</a>?</p>';
87 $output = portfolio_rewrite_pluginfile_urls($input, $context->id
, $component, $filearea, $itemid, $format, $options);
88 $this->assertSame($expected, $output);
90 $input = '<p>Come and <a href="@@PLUGINFILE@@' . $filepath . $filenamepdf . '"><em>join us!</em></a>?</p>';
91 $expected = '<p>Come and <a href="files/' . $filenamepdf . '">' . $filenamepdf . '</a>?</p>';
92 $output = portfolio_rewrite_pluginfile_urls($input, $context->id
, $component, $filearea, $itemid, $format, $options);
93 $this->assertSame($expected, $output);
95 // Test that the image is matching.
96 $input = '<p>Here is an image <img src="@@PLUGINFILE@@' . $filepath . $filenameimg . '"></p>'; // No trailing slash.
97 $expected = '<p>Here is an image <img src="files/' . $filenameimg . '"/></p>';
98 $output = portfolio_rewrite_pluginfile_urls($input, $context->id
, $component, $filearea, $itemid, $format, $options);
99 $this->assertSame($expected, $output);
101 $input = '<p>Here is an image <img src="@@PLUGINFILE@@' . $filepath . $filenameimg . '" /></p>'; // Trailing slash.
102 $expected = '<p>Here is an image <img src="files/' . $filenameimg . '"/></p>';
103 $output = portfolio_rewrite_pluginfile_urls($input, $context->id
, $component, $filearea, $itemid, $format, $options);
104 $this->assertSame($expected, $output);
106 // Test that the attributes are kept.
107 $input = '<p><a title="hurray!" href="@@PLUGINFILE@@' . $filepath . $filenamepdf . '" target="_blank">join us!</a></p>';
108 $expected = '<p><a title="hurray!" href="files/' . $filenamepdf . '" target="_blank">' . $filenamepdf . '</a></p>';
109 $output = portfolio_rewrite_pluginfile_urls($input, $context->id
, $component, $filearea, $itemid, $format, $options);
110 $this->assertSame($expected, $output);
112 $input = '<p><img alt="before" src="@@PLUGINFILE@@' . $filepath . $filenameimg . '" title="after"/></p>';
113 $expected = '<p><img alt="before" src="files/' . $filenameimg . '" title="after"/></p>';
114 $output = portfolio_rewrite_pluginfile_urls($input, $context->id
, $component, $filearea, $itemid, $format, $options);
115 $this->assertSame($expected, $output);
117 // Test with more tags around.
118 $input = '<p><span title="@@PLUGINFILE/a.txt"><a href="@@PLUGINFILE@@' . $filepath . $filenamepdf . '">' .
119 '<em>join</em> <b>us!</b></a></span></p>';
120 $expected = '<p><span title="@@PLUGINFILE/a.txt"><a href="files/' . $filenamepdf . '">' . $filenamepdf . '</a></span></p>';
121 $output = portfolio_rewrite_pluginfile_urls($input, $context->id
, $component, $filearea, $itemid, $format, $options);
122 $this->assertSame($expected, $output);
124 $input = '<p><span title="@@PLUGINFILE/a.txt"><img src="@@PLUGINFILE@@' . $filepath . $filenameimg . '"/></span></p>';
125 $expected = '<p><span title="@@PLUGINFILE/a.txt"><img src="files/' . $filenameimg . '"/></span></p>';
126 $output = portfolio_rewrite_pluginfile_urls($input, $context->id
, $component, $filearea, $itemid, $format, $options);
127 $this->assertSame($expected, $output);
129 // Test multiple on same line.
130 $input = '<p><a rel="1" href="@@PLUGINFILE@@' . $filepath . $filenamepdf . '">join us!</a>' .
131 '<a rel="2" href="@@PLUGINFILE@@' . $filepath . $filenamepdf . '">join us!</a></p>';
132 $expected = '<p><a rel="1" href="files/' . $filenamepdf . '">' . $filenamepdf . '</a>' .
133 '<a rel="2" href="files/' . $filenamepdf . '">' . $filenamepdf . '</a></p>';
134 $output = portfolio_rewrite_pluginfile_urls($input, $context->id
, $component, $filearea, $itemid, $format, $options);
135 $this->assertSame($expected, $output);
137 $input = '<p><img rel="1" src="@@PLUGINFILE@@' . $filepath . $filenameimg . '"/>' .
138 '<img rel="2" src="@@PLUGINFILE@@' . $filepath . $filenameimg . '"/></p>';
139 $expected = '<p><img rel="1" src="files/' . $filenameimg . '"/><img rel="2" src="files/' . $filenameimg . '"/></p>';
140 $output = portfolio_rewrite_pluginfile_urls($input, $context->id
, $component, $filearea, $itemid, $format, $options);
141 $this->assertSame($expected, $output);
143 $input = '<p><a href="@@PLUGINFILE@@' . $filepath . $filenamepdf . '">join us!</a>' .
144 '<img src="@@PLUGINFILE@@' . $filepath . $filenameimg . '"/></p>';
145 $expected = '<p><a href="files/' . $filenamepdf . '">' . $filenamepdf . '</a>' .
146 '<img src="files/' . $filenameimg . '"/></p>';
147 $output = portfolio_rewrite_pluginfile_urls($input, $context->id
, $component, $filearea, $itemid, $format, $options);
148 $this->assertSame($expected, $output);
153 * Dummy portfolio format.
155 * @copyright 2014 Frédéric Massart
156 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
158 class core_portfolio_format_dummy
extends portfolio_format
{
160 public static function file_output($file, $options = null) {
161 if (isset($options['attributes']) && is_array($options['attributes'])) {
162 $attributes = $options['attributes'];
164 $attributes = array();
166 $path = 'files/' . $file->get_filename();
167 return self
::make_tag($file, $path, $attributes);