Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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_quiz\external;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
require_once(__DIR__ . '/../../../../webservice/tests/helpers.php');
22
 
23
use core_question_generator;
24
use externallib_advanced_testcase;
25
use mod_quiz\quiz_settings;
26
use required_capability_exception;
27
 
28
/**
29
 * Test for the update_slots service.
30
 *
31
 * @package   mod_quiz
32
 * @category  external
33
 * @copyright 2023 The Open University
34
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 * @covers \mod_quiz\external\update_slots
36
 */
37
final class update_slots_test extends externallib_advanced_testcase {
38
 
39
    public function test_update_slots_service_works(): void {
40
        global $DB;
41
 
42
        $quizobj = $this->create_quiz_with_two_shortanswer_questions();
43
        $this->setAdminUser();
44
        $structure = $quizobj->get_structure();
45
 
46
        // No changes to slot 1.
47
        $slot1data = [
48
            'id' => $structure->get_slot_by_number(1)->id,
49
        ];
50
        // Change everything in slot 2.
51
        $slot2data = [
52
            'id' => $structure->get_slot_by_number(2)->id,
53
            'displaynumber' => '1b',
54
            'requireprevious' => true,
55
            'maxmark' => 7,
56
            'quizgradeitemid' => 123,
57
        ];
58
 
59
        update_slots::execute($quizobj->get_quizid(), [$slot1data, $slot2data]);
60
 
61
        $slot = $DB->get_record('quiz_slots', ['id' => $slot2data['id']]);
62
        $this->assertEquals('1b', $slot->displaynumber);
63
        $this->assertTrue((bool) $slot->requireprevious);
64
        $this->assertEquals(7, $slot->maxmark);
65
        $this->assertEquals(123, $slot->quizgradeitemid);
66
    }
67
 
68
    public function test_update_slots_checks_permissions(): void {
69
        $quizobj = $this->create_quiz_with_two_shortanswer_questions();
70
 
71
        $unprivilegeduser = $this->getDataGenerator()->create_user();
72
        $this->setUser($unprivilegeduser);
73
 
74
        $this->expectException(required_capability_exception::class);
75
        update_slots::execute($quizobj->get_quizid(), []);
76
    }
77
 
78
    /**
79
     * Create a quiz of two shortanswer questions.
80
     *
81
     * @return quiz_settings the newly created quiz.
82
     */
83
    protected function create_quiz_with_two_shortanswer_questions(): quiz_settings {
84
        global $SITE;
85
        $this->resetAfterTest();
86
 
87
        // Make a quiz.
88
        $timeclose = time() + HOURSECS;
89
        $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
90
 
91
        $quiz = $quizgenerator->create_instance([
92
            'course' => $SITE->id,
93
            'timeclose' => $timeclose,
94
            'overduehandling' => 'autoabandon',
95
        ]);
96
 
97
        // Create a question.
98
        /** @var core_question_generator $questiongenerator */
99
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
100
        $cat = $questiongenerator->create_question_category();
101
        $saq1 = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
102
        $saq2 = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
103
 
104
        // Add them to the quiz.
105
        $quizobj = quiz_settings::create($quiz->id);
106
        quiz_add_quiz_question($saq1->id, $quiz, 0, 1);
107
        quiz_add_quiz_question($saq2->id, $quiz, 0, 1);
108
        $quizobj->get_grade_calculator()->recompute_quiz_sumgrades();
109
 
110
        return $quizobj;
111
    }
112
}