MDL-53409 question: Add tests for core_question_update_flag
[moodle.git] / question / tests / externallib_test.php
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/>.
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  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32 require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
34 /**
35  * Question external functions tests
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 {
45     /**
46      * Set up for every test
47      */
48     public function setUp() {
49         global $DB;
50         $this->resetAfterTest();
51         $this->setAdminUser();
53         // Setup test data.
54         $this->course = $this->getDataGenerator()->create_course();
56         // Create users.
57         $this->student = self::getDataGenerator()->create_user();
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');
62     }
64     /**
65      * Test update question flag
66      */
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.
95         try {
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);
103         }
104     }