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 * Unit tests for grade/report/lib.php.
19 *
20 * @package  core_grades
21 * @category phpunit
22 * @copyright   Andrew Nicols <andrew@nicols.co.uk>
23 * @license  http://www.gnu.org/copyleft/gpl.html GNU Public License
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->dirroot.'/grade/lib.php');
30require_once($CFG->dirroot.'/grade/export/lib.php');
31
32/**
33 * A test class used to test grade_report, the abstract grade report parent class
34 */
35class core_grade_export_test extends advanced_testcase {
36
37    /**
38     * Ensure that feedback is correct formatted. Test the default implementation of format_feedback
39     *
40     * @dataProvider    format_feedback_provider
41     * @param   string  $input The input string to test
42     * @param   int     $inputformat The format of the input string
43     * @param   string  $expected The expected result of the format.
44     */
45    public function test_format_feedback($input, $inputformat, $expected) {
46        $feedback = $this->getMockForAbstractClass(
47                \grade_export::class,
48                [],
49                '',
50                false
51            );
52
53        $this->assertEquals(
54            $expected,
55            $feedback->format_feedback((object) [
56                    'feedback' => $input,
57                    'feedbackformat' => $inputformat,
58                ])
59            );
60    }
61
62    /**
63     * Ensure that feedback is correctly formatted. Test augmented functionality to handle file links
64     */
65    public function test_format_feedback_with_grade() {
66        $this->resetAfterTest();
67        $dg = $this->getDataGenerator();
68        $c1 = $dg->create_course();
69        $u1 = $dg->create_user();
70        $gi1a = new grade_item($dg->create_grade_item(['courseid' => $c1->id]), false);
71        $gi1a->update_final_grade($u1->id, 1, 'test');
72        $contextid = $gi1a->get_context()->id;
73        $gradeid = $gi1a->id;
74
75        $tests = [
76            'Has server based image (HTML)' => [
77                '<p>See this reference: <img src="@@PLUGINFILE@@/test.img"></p>',
78                FORMAT_HTML,
79                "See this reference: "
80            ],
81            'Has server based image and more (HTML)' => [
82                '<p>See <img src="@@PLUGINFILE@@/test.img"> for <em>reference</em></p>',
83                FORMAT_HTML,
84                "See  for reference"
85            ],
86            'Has server based video and more (HTML)' => [
87                '<p>See <video src="@@PLUGINFILE@@/test.img">video of a duck</video> for <em>reference</em></p>',
88                FORMAT_HTML,
89                'See video of a duck for reference'
90            ],
91            'Has server based video with text and more (HTML)' => [
92                '<p>See <video src="@@PLUGINFILE@@/test.img">@@PLUGINFILE@@/test.img</video> for <em>reference</em></p>',
93                FORMAT_HTML,
94                "See https://www.example.com/moodle/pluginfile.php/$contextid/grade/feedback/$gradeid/test.img for reference"
95            ],
96            'Multiple videos (HTML)' => [
97                '<p>See <video src="@@PLUGINFILE@@/test.img">video of a duck</video> and '.
98                '<video src="http://example.com/myimage.jpg">video of a cat</video> for <em>reference</em></p>',
99                FORMAT_HTML,
100                'See video of a duck and video of a cat for reference'
101            ],
102        ];
103
104        $feedback = $this->getMockForAbstractClass(
105            \grade_export::class,
106            [],
107            '',
108            false
109        );
110
111        foreach ($tests as $key => $testdetails) {
112            $expected = $testdetails[2];
113            $input = $testdetails[0];
114            $inputformat = $testdetails[1];
115
116            $this->assertEquals(
117                $expected,
118                $feedback->format_feedback((object) [
119                    'feedback' => $input,
120                    'feedbackformat' => $inputformat,
121                ], $gi1a),
122                $key
123            );
124        }
125    }
126
127    /**
128     * Data provider for the format_feedback tests.
129     *
130     * @return  array
131     */
132    public function format_feedback_provider() : array {
133        return [
134            'Basic string (PLAIN)' => [
135                'This is an example string',
136                FORMAT_PLAIN,
137                'This is an example string',
138            ],
139            'Basic string (HTML)' => [
140                '<p>This is an example string</p>',
141                FORMAT_HTML,
142                'This is an example string',
143            ],
144            'Has image (HTML)' => [
145                '<p>See this reference: <img src="http://example.com/myimage.jpg"></p>',
146                FORMAT_HTML,
147                'See this reference: ',
148            ],
149            'Has image and more (HTML)' => [
150                '<p>See <img src="http://example.com/myimage.jpg"> for <em>reference</em></p>',
151                FORMAT_HTML,
152                'See  for reference',
153            ],
154            'Has video and more (HTML)' => [
155                '<p>See <video src="http://example.com/myimage.jpg">video of a duck</video> for <em>reference</em></p>',
156                FORMAT_HTML,
157                'See video of a duck for reference',
158            ],
159            'Multiple videos (HTML)' => [
160                '<p>See <video src="http://example.com/myimage.jpg">video of a duck</video> and '.
161                '<video src="http://example.com/myimage.jpg">video of a cat</video> for <em>reference</em></p>',
162                FORMAT_HTML,
163                'See video of a duck and video of a cat for reference'
164            ],
165            'HTML Looking tags in PLAIN' => [
166                'The way you have written the <img thing looks pretty fun >',
167                FORMAT_PLAIN,
168                'The way you have written the &lt;img thing looks pretty fun &gt;',
169            ],
170
171        ];
172    }
173}
174