Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
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.
8
//
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.
13
//
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/>.
16
 
17
/**
18
 * Portfolio lib tests.
19
 *
20
 * @package    core
21
 * @subpackage phpunit
22
 * @copyright  2014 Frédéric Massart
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
defined('MOODLE_INTERNAL') || die();
27
global $CFG;
28
 
29
require_once($CFG->libdir.'/portfoliolib.php');
30
require_once($CFG->libdir.'/portfolio/formats.php');
31
 
32
/**
33
 * Portfolio lib testcase.
34
 *
35
 * @copyright  2014 Frédéric Massart
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class portfoliolib_test extends advanced_testcase {
39
 
11 efrain 40
    public function test_portfolio_rewrite_pluginfile_urls(): void {
1 efrain 41
        $this->resetAfterTest();
42
 
43
        // File info.
44
        $context = context_system::instance();
45
        $component = 'core_test';
46
        $filearea = 'fixture';
47
        $filepath = '/';
48
        $itemid = 0;
49
        $filenameimg = 'file.png';
50
        $filenamepdf = 'file.pdf';
51
 
52
        // Store 2 test files in the pool.
53
        $fs = get_file_storage();
54
        $filerecord = array(
55
            'contextid' => $context->id,
56
            'component' => $component,
57
            'filearea'  => $filearea,
58
            'itemid'    => $itemid,
59
            'filepath'  => $filepath,
60
            'filename'  => $filenameimg,
61
        );
62
        $fileimg = $fs->create_file_from_string($filerecord, 'test');
63
 
64
        $filerecord['filename']  = $filenamepdf;
65
        $filepdf = $fs->create_file_from_string($filerecord, 'test');
66
 
67
        // Test that nothing is matching.
68
        $format = '';
69
        $options = null;
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);
74
 
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);
79
 
80
        // Now use our dummy format.
81
        $format = new core_portfolio_format_dummy();
82
        $options = null;
83
 
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);
89
 
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);
94
 
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);
100
 
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);
105
 
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);
111
 
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);
116
 
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);
123
 
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);
128
 
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);
136
 
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);
142
 
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);
149
    }
150
}
151
 
152
/**
153
 * Dummy portfolio format.
154
 *
155
 * @copyright  2014 Frédéric Massart
156
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
157
 */
158
class core_portfolio_format_dummy extends portfolio_format {
159
 
160
    public static function file_output($file, $options = null) {
161
        if (isset($options['attributes']) && is_array($options['attributes'])) {
162
            $attributes = $options['attributes'];
163
        } else {
164
            $attributes = array();
165
        }
166
        $path = 'files/' . $file->get_filename();
167
        return self::make_tag($file, $path, $attributes);
168
    }
169
 
170
}