2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Question external functions tests.
20 * @package core_question
22 * @copyright 2016 Pau Ferrer <pau@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32 require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
35 * Question external functions tests
37 * @package core_question
39 * @copyright 2016 Pau Ferrer <pau@moodle.com>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class core_question_external_testcase extends externallib_advanced_testcase {
46 * Set up for every test
48 public function setUp() {
50 $this->resetAfterTest();
51 $this->setAdminUser();
54 $this->course = $this->getDataGenerator()->create_course();
57 $this->student = self::getDataGenerator()->create_user();
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');
65 * Test update question flag
67 public function test_core_question_update_flag() {
69 $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
71 // Create a question category.
72 $cat = $questiongenerator->create_question_category();
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);
78 $slot = $quba->add_question($question);
79 $qa = $quba->get_question_attempt($slot);
81 self::setUser($this->student);
83 $quba->start_all_questions();
84 question_engine::save_questions_usage_by_activity($quba);
86 $qubaid = $quba->get_id();
87 $questionid = $question->id;
88 $qaid = $qa->get_database_id();
89 $checksum = md5($qubaid . "_" . $this->student->secret . "_" . $questionid . "_" . $qaid . "_" . $slot);
91 $flag = core_question_external::update_flag($qubaid, $questionid, $qaid, $slot, $checksum, true);
92 $this->assertTrue($flag['status']);
94 // Test invalid checksum.
96 // Using random_string to force failing.
97 $checksum = md5($qubaid . "_" . random_string(11) . "_" . $questionid . "_" . $qaid . "_" . $slot);
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);