1441 |
ariadna |
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 |
namespace mod_feedback\external\questions;
|
|
|
18 |
|
|
|
19 |
use core_external\external_api;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Unit tests of external class for re-ordering feedback question items
|
|
|
23 |
*
|
|
|
24 |
* @package mod_feedback
|
|
|
25 |
* @covers \mod_feedback\external\questions\reorder
|
|
|
26 |
* @copyright 2024 Mikel Martín <mikel@moodle.com>
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
final class reorder_test extends \advanced_testcase {
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Text execute method
|
|
|
33 |
*/
|
|
|
34 |
public function test_execute(): void {
|
|
|
35 |
$this->resetAfterTest();
|
|
|
36 |
$this->setAdminUser();
|
|
|
37 |
|
|
|
38 |
// Create a course with a feedback activity and some questions.
|
|
|
39 |
$course = $this->getDataGenerator()->create_course();
|
|
|
40 |
$feedback = $this->getDataGenerator()->create_module('feedback', ['course' => $course->id]);
|
|
|
41 |
$cm = get_coursemodule_from_instance('feedback', $feedback->id);
|
|
|
42 |
|
|
|
43 |
$feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback');
|
|
|
44 |
$item1 = $feedbackgenerator->create_item_label($feedback);
|
|
|
45 |
$item2 = $feedbackgenerator->create_item_info($feedback);
|
|
|
46 |
$item3 = $feedbackgenerator->create_item_numeric($feedback);
|
|
|
47 |
|
|
|
48 |
// Check initial items order.
|
|
|
49 |
$this->assertEquals([$item1->id, $item2->id, $item3->id], $this->get_feedback_item_order($feedback));
|
|
|
50 |
|
|
|
51 |
// Call the execute method to invert the items order.
|
|
|
52 |
$result = reorder::execute($cm->id, "$item3->id,$item2->id,$item1->id");
|
|
|
53 |
$result = external_api::clean_returnvalue(reorder::execute_returns(), $result);
|
|
|
54 |
$this->assertTrue($result);
|
|
|
55 |
|
|
|
56 |
// Check items order is inverted.
|
|
|
57 |
$this->assertEquals([$item3->id, $item2->id, $item1->id], $this->get_feedback_item_order($feedback));
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Get the order of the feedback items.
|
|
|
62 |
*
|
|
|
63 |
* @param object $feedback The feedback activity.
|
|
|
64 |
* @return array
|
|
|
65 |
*/
|
|
|
66 |
private function get_feedback_item_order($feedback) {
|
|
|
67 |
global $DB;
|
|
|
68 |
return $DB->get_fieldset_select(
|
|
|
69 |
'feedback_item',
|
|
|
70 |
'id',
|
|
|
71 |
'feedback = :feedbackid ORDER BY position ASC',
|
|
|
72 |
['feedbackid' => $feedback->id]
|
|
|
73 |
);
|
|
|
74 |
}
|
|
|
75 |
}
|