Commit | Line | Data |
---|---|---|
7361b968 PFO |
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 | * Question external functions tests. | |
19 | * | |
20 | * @package core_question | |
21 | * @category external | |
22 | * @copyright 2016 Pau Ferrer <pau@moodle.com> | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | * @since Moodle 3.1 | |
25 | */ | |
26 | ||
27 | defined('MOODLE_INTERNAL') || die(); | |
28 | ||
29 | global $CFG; | |
30 | ||
31 | require_once($CFG->dirroot . '/webservice/tests/helpers.php'); | |
176bcaa9 | 32 | require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); |
7361b968 PFO |
33 | |
34 | /** | |
176bcaa9 | 35 | * Question external functions tests |
7361b968 PFO |
36 | * |
37 | * @package core_question | |
38 | * @category external | |
39 | * @copyright 2016 Pau Ferrer <pau@moodle.com> | |
40 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
41 | * @since Moodle 3.1 | |
42 | */ | |
43 | class core_question_external_testcase extends externallib_advanced_testcase { | |
44 | ||
45 | /** | |
46 | * Set up for every test | |
47 | */ | |
48 | public function setUp() { | |
49 | global $DB; | |
50 | $this->resetAfterTest(); | |
176bcaa9 | 51 | $this->setAdminUser(); |
7361b968 PFO |
52 | |
53 | // Setup test data. | |
54 | $this->course = $this->getDataGenerator()->create_course(); | |
7361b968 PFO |
55 | |
56 | // Create users. | |
57 | $this->student = self::getDataGenerator()->create_user(); | |
58 | ||
59 | // Users enrolments. | |
60 | $this->studentrole = $DB->get_record('role', array('shortname' => 'student')); | |
61 | $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual'); | |
7361b968 PFO |
62 | } |
63 | ||
64 | /** | |
65 | * Test update question flag | |
66 | */ | |
67 | public function test_core_question_update_flag() { | |
68 | ||
176bcaa9 PFO |
69 | $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); |
70 | ||
71 | // Create a question category. | |
72 | $cat = $questiongenerator->create_question_category(); | |
73 | ||
74 | $quba = question_engine::make_questions_usage_by_activity('core_question_update_flag', context_system::instance()); | |
75 | $quba->set_preferred_behaviour('deferredfeedback'); | |
76 | $questiondata = $questiongenerator->create_question('numerical', null, array('category' => $cat->id)); | |
77 | $question = question_bank::load_question($questiondata->id); | |
7361b968 | 78 | $slot = $quba->add_question($question); |
7361b968 PFO |
79 | $qa = $quba->get_question_attempt($slot); |
80 | ||
176bcaa9 PFO |
81 | self::setUser($this->student); |
82 | ||
83 | $quba->start_all_questions(); | |
84 | question_engine::save_questions_usage_by_activity($quba); | |
85 | ||
7361b968 PFO |
86 | $qubaid = $quba->get_id(); |
87 | $questionid = $question->id; | |
176bcaa9 | 88 | $qaid = $qa->get_database_id(); |
7361b968 PFO |
89 | $checksum = md5($qubaid . "_" . $this->student->secret . "_" . $questionid . "_" . $qaid . "_" . $slot); |
90 | ||
91 | $flag = core_question_external::update_flag($qubaid, $questionid, $qaid, $slot, $checksum, true); | |
92 | $this->assertTrue($flag['status']); | |
176bcaa9 PFO |
93 | |
94 | // Test invalid checksum. | |
95 | try { | |
96 | // Using random_string to force failing. | |
97 | $checksum = md5($qubaid . "_" . random_string(11) . "_" . $questionid . "_" . $qaid . "_" . $slot); | |
98 | ||
99 | core_question_external::update_flag($qubaid, $questionid, $qaid, $slot, $checksum, true); | |
100 | $this->fail('Exception expected due to invalid checksum.'); | |
101 | } catch (moodle_exception $e) { | |
102 | $this->assertEquals('errorsavingflags', $e->errorcode); | |
103 | } | |
7361b968 | 104 | } |
176bcaa9 | 105 | } |