Merge branch 'MDL-67995-37' of git://github.com/cescobedo/moodle into MOODLE_37_STABLE
[moodle.git] / grade / tests / export_test.php
blobb2cc79adb1f7a575683f878e285521d1d9c6ca1c
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.
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/>.
17 /**
18 * Unit tests for grade/report/lib.php.
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
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot.'/grade/lib.php');
30 require_once($CFG->dirroot.'/grade/export/lib.php');
32 /**
33 * A test class used to test grade_report, the abstract grade report parent class
35 class core_grade_export_test extends advanced_testcase {
37 /**
38 * Ensure that feedback is correct formatted. Test the default implementation of format_feedback
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.
45 public function test_format_feedback($input, $inputformat, $expected) {
46 $feedback = $this->getMockForAbstractClass(
47 \grade_export::class,
48 [],
49 '',
50 false
53 $this->assertEquals(
54 $expected,
55 $feedback->format_feedback((object) [
56 'feedback' => $input,
57 'feedbackformat' => $inputformat,
62 /**
63 * Ensure that feedback is correctly formatted. Test augmented functionality to handle file links
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;
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: "
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"
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'
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"
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'
104 $feedback = $this->getMockForAbstractClass(
105 \grade_export::class,
108 false
111 foreach ($tests as $key => $testdetails) {
112 $expected = $testdetails[2];
113 $input = $testdetails[0];
114 $inputformat = $testdetails[1];
116 $this->assertEquals(
117 $expected,
118 $feedback->format_feedback((object) [
119 'feedback' => $input,
120 'feedbackformat' => $inputformat,
121 ], $gi1a),
122 $key
128 * Data provider for the format_feedback tests.
130 * @return array
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',
139 'Basic string (HTML)' => [
140 '<p>This is an example string</p>',
141 FORMAT_HTML,
142 'This is an example string',
144 'Has image (HTML)' => [
145 '<p>See this reference: <img src="http://example.com/myimage.jpg"></p>',
146 FORMAT_HTML,
147 'See this reference: ',
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',
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',
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'
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;',