Skip to content

Commit 4f82677

Browse files
committed
MDL-41296 gradingform_rubrics: Unit tests for import and export.
1 parent 351d101 commit 4f82677

File tree

2 files changed

+213
-0
lines changed

2 files changed

+213
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
namespace gradingform_rubric;
19+
20+
use advanced_testcase;
21+
22+
/**
23+
* Unit tests for the rubric export manager;
24+
*
25+
* @package gradingform_rubric
26+
* @category test
27+
* @copyright 2022 Adrian Greeve <adrian@moodle.com>
28+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29+
*/
30+
class export_ims_mapper_test extends advanced_testcase {
31+
32+
/**
33+
* Rubric data in a format taken from advanced grading: rubric.
34+
* This is setup as an array for ease of use rather than casting it to a stdClass and then back again.
35+
*/
36+
protected function rubric_data(): array {
37+
$data = [
38+
'id' => 30,
39+
'name' => 'Test rubric',
40+
'description' => 'This is a test rubric',
41+
'timecreated' => time(),
42+
'timemodified' => time(),
43+
'options' => [],
44+
'rubric_criteria' => [
45+
3 => [
46+
'id' => 3,
47+
'sortorder' => 1,
48+
'description' => 'Spelling',
49+
'descriptionformat' => 0,
50+
'levels' => [
51+
334 => [
52+
'id' => 334,
53+
'score' => 0,
54+
'definition' => '4+ spelling mistakes',
55+
'definitionformat' => 0
56+
],
57+
335 => [
58+
'id' => 335,
59+
'score' => 1.67,
60+
'definition' => '3 spelling mistakes',
61+
'definitionformat' => 0
62+
],
63+
336 => [
64+
'id' => 336,
65+
'score' => 2,
66+
'definition' => '2 spelling mistakes',
67+
'definitionformat' => 0
68+
],
69+
337 => [
70+
'id' => 337,
71+
'score' => 3,
72+
'definition' => '1 spelling mistake',
73+
'definitionformat' => 0
74+
],
75+
338 => [
76+
'id' => 338,
77+
'score' => 4,
78+
'definition' => 'No mistakes',
79+
'definitionformat' => 0
80+
]
81+
]
82+
],
83+
56 => [
84+
'id' => 56,
85+
'sortorder' => 2,
86+
'description' => 'Pictures',
87+
'descriptionformat' => 0,
88+
'levels' => [
89+
339 => [
90+
'id' => 339,
91+
'score' => 0,
92+
'definition' => 'No pictures',
93+
'definitionformat' => 0
94+
],
95+
340 => [
96+
'id' => 340,
97+
'score' => 5,
98+
'definition' => 'One picture',
99+
'definitionformat' => 0,
100+
],
101+
341 => [
102+
'id' => 341,
103+
'score' => 10,
104+
'definition' => 'Two pictures',
105+
'definitionformat' => 0
106+
]
107+
]
108+
]
109+
]
110+
];
111+
return $data;
112+
}
113+
114+
/**
115+
* Test that an array of data from the advanced grading method: rubric, can be transformed into a format acceptable by IMS.
116+
*/
117+
public function test_translate_data(): void {
118+
$temp = $this->rubric_data();
119+
$manager = new \gradingform_rubric\local\export\ims_mapper();
120+
$result = $manager->translate_data($temp);
121+
// Check base level information.
122+
$resultobject = json_decode($result);
123+
$this->assertTrue(is_a($resultobject, 'stdClass'));
124+
$this->assertTrue(isset($resultobject->Title));
125+
$this->assertTrue(isset($resultobject->URI));
126+
$this->assertTrue(isset($resultobject->Identifier));
127+
$this->assertTrue(isset($resultobject->Description));
128+
$this->assertTrue(isset($resultobject->lastChangeDateTime));
129+
$this->assertTrue(isset($resultobject->CFRubricCriterion));
130+
131+
// Check the format of the Identifier
132+
// @see https://www.imsglobal.org/sites/default/files/CASE/casev1p0/information_model/caseservicev1p0_infomodelv1p0.html#DerivedAttribute_UUID_pattern
133+
$expression = "/[0-9a-f]{8}-[0-9a-f]{4}-[1-5]{1}[0-9a-f]{3}-[8-9a-b]{1}[0-9a-f]{3}-[0-9a-f]{12}/s";
134+
$this->assertMatchesRegularExpression($expression, $resultobject->Identifier);
135+
136+
foreach ($resultobject->CFRubricCriterion as $element) {
137+
$this->assertTrue(isset($element->URI));
138+
$this->assertTrue(isset($element->Identifier));
139+
$this->assertTrue(isset($element->Description));
140+
$this->assertTrue(isset($element->position));
141+
$this->assertTrue(isset($element->CFRubricCriterionLevels));
142+
$this->assertMatchesRegularExpression($expression, $element->Identifier);
143+
foreach ($element->CFRubricCriterionLevels as $criterionlevel) {
144+
$this->assertTrue(isset($criterionlevel->Description));
145+
$this->assertTrue(isset($criterionlevel->URI));
146+
$this->assertTrue(isset($criterionlevel->Identifier));
147+
$this->assertTrue(isset($criterionlevel->score));
148+
$this->assertTrue(isset($criterionlevel->position));
149+
$this->assertTrue(isset($criterionlevel->lastChangeDateTime));
150+
$this->assertMatchesRegularExpression($expression, $criterionlevel->Identifier);
151+
}
152+
}
153+
}
154+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
namespace gradingform_rubric;
19+
20+
use advanced_testcase;
21+
22+
/**
23+
* Unit tests for the rubric import manager;
24+
*
25+
* @package gradingform_rubric
26+
* @category test
27+
* @copyright 2022 Adrian Greeve <adrian@moodle.com>
28+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29+
*/
30+
class import_ims_mapper_test extends advanced_testcase {
31+
32+
/**
33+
* Test that a file in IMS specification can be imported and transformed into a format that Moodle will accept.
34+
*/
35+
public function test_translate_data() {
36+
$this->resetAfterTest();
37+
$this->setAdminUser();
38+
$importstring = file_get_contents(__DIR__ . '/fixtures/rubric-import.json');
39+
$data = json_decode($importstring);
40+
$manager = new \gradingform_rubric\local\import\ims_mapper($data, 1);
41+
$result = $manager->translate_data();
42+
43+
$this->assertTrue(isset($result->areaid));
44+
$this->assertTrue(isset($result->status));
45+
$this->assertTrue(isset($result->name));
46+
$this->assertTrue(isset($result->rubric));
47+
$this->assertCount(4, $result->rubric['criteria']);
48+
foreach ($result->rubric['criteria'] as $criteria) {
49+
$this->assertTrue(isset($criteria['sortorder']));
50+
$this->assertTrue(isset($criteria['description']));
51+
$this->assertTrue(isset($criteria['levels']));
52+
foreach ($criteria['levels'] as $level) {
53+
$this->assertTrue(isset($level['score']));
54+
$this->assertTrue(isset($level['definition']));
55+
}
56+
}
57+
}
58+
59+
}

0 commit comments

Comments
 (0)