Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 26... Línea 26...
26
namespace mod_quiz\event;
26
namespace mod_quiz\event;
Línea 27... Línea 27...
27
 
27
 
28
use mod_quiz\quiz_attempt;
28
use mod_quiz\quiz_attempt;
29
use mod_quiz\quiz_settings;
29
use mod_quiz\quiz_settings;
-
 
30
use context_module;
Línea 30... Línea 31...
30
use context_module;
31
use mod_quiz\external\submit_question_version;
31
 
32
 
32
/**
33
/**
33
 * Unit tests for quiz events.
34
 * Unit tests for quiz events.
34
 *
35
 *
35
 * @package    mod_quiz
36
 * @package    mod_quiz
36
 * @category   phpunit
37
 * @category   phpunit
-
 
38
 * @copyright  2013 Adrian Greeve
37
 * @copyright  2013 Adrian Greeve
39
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 * @covers \mod_quiz\external\submit_question_version
Línea 39... Línea 41...
39
 */
41
 */
40
class events_test extends \advanced_testcase {
42
final class events_test extends \advanced_testcase {
41
 
43
 
42
    /**
44
    /**
Línea 66... Línea 68...
66
 
68
 
67
        $cat = $questiongenerator->create_question_category();
69
        $cat = $questiongenerator->create_question_category();
68
        $saq = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
70
        $saq = $questiongenerator->create_question('shortanswer', null, ['category' => $cat->id]);
Línea -... Línea 71...
-
 
71
        $numq = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]);
-
 
72
 
-
 
73
        // Update the numq question so it has multiple versions. Needed to test
-
 
74
        // the version updated event.
69
        $numq = $questiongenerator->create_question('numerical', null, ['category' => $cat->id]);
75
        $questiongenerator->update_question($numq, null, ['name' => 'Second version of numq']);
70
 
76
 
71
        // Add them to the quiz.
77
        // Add them to the quiz.
Línea 72... Línea 78...
72
        quiz_add_quiz_question($saq->id, $quiz);
78
        quiz_add_quiz_question($saq->id, $quiz);
Línea 110... Línea 116...
110
        return $this->prepare_quiz_attempt($quizobj, $ispreview);
116
        return $this->prepare_quiz_attempt($quizobj, $ispreview);
111
    }
117
    }
Línea 112... Línea 118...
112
 
118
 
Línea 113... Línea 119...
113
    public function test_attempt_submitted(): void {
119
    public function test_attempt_submitted(): void {
114
 
120
 
Línea 115... Línea 121...
115
        list($quizobj, $quba, $attempt) = $this->prepare_quiz_data();
121
        [$quizobj, , $attempt] = $this->prepare_quiz_data();
116
        $attemptobj = quiz_attempt::create($attempt->id);
122
        $attemptobj = quiz_attempt::create($attempt->id);
Línea 117... Línea 123...
117
 
123
 
118
        // Catch the event.
124
        // Catch the event.
-
 
125
        $sink = $this->redirectEvents();
-
 
126
 
-
 
127
        $timefinish = time();
-
 
128
        $attemptobj->process_submit($timefinish, false);
-
 
129
        $events = $sink->get_events();
-
 
130
        $sink->close();
-
 
131
 
-
 
132
        // Validate the event.
-
 
133
        $this->assertCount(1, $events);
-
 
134
        $event = $events[0];
-
 
135
        $this->assertInstanceOf('\mod_quiz\event\attempt_submitted', $event);
-
 
136
        $this->assertEquals('quiz_attempts', $event->objecttable);
-
 
137
        $this->assertEquals($quizobj->get_context(), $event->get_context());
-
 
138
        $this->assertEquals($attempt->userid, $event->relateduserid);
-
 
139
        $this->assertEquals(null, $event->other['submitterid']); // Should be the user, but PHP Unit complains...
-
 
140
        $this->assertEventContextNotUsed($event);
-
 
141
    }
-
 
142
 
-
 
143
    /**
-
 
144
     * The \mod_quiz\event\attempt_graded event should be fired when an attempt is graded.
-
 
145
     *
-
 
146
     * @return void
-
 
147
     * @covers \mod_quiz\quiz_attempt::process_grade_submission
-
 
148
     */
-
 
149
    public function test_attempt_graded(): void {
-
 
150
 
-
 
151
        [$quizobj, , $attempt] = $this->prepare_quiz_data();
-
 
152
        $attemptobj = quiz_attempt::create($attempt->id);
-
 
153
        $timefinish = time();
-
 
154
        $attemptobj->process_submit($timefinish, false);
119
        $sink = $this->redirectEvents();
155
 
120
 
156
        // Catch the event.
Línea 121... Línea 157...
121
        $timefinish = time();
157
        $sink = $this->redirectEvents();
122
        $attemptobj->process_finish($timefinish, false);
158
        $attemptobj->process_grade_submission($timefinish);
123
        $events = $sink->get_events();
159
        $events = $sink->get_events();
124
        $sink->close();
160
        $sink->close();
125
 
161
 
126
        // Validate the event.
162
        // Validate the event.
127
        $this->assertCount(3, $events);
163
        $this->assertCount(3, $events);
128
        $event = $events[2];
164
        $event = $events[2];
129
        $this->assertInstanceOf('\mod_quiz\event\attempt_submitted', $event);
165
        $this->assertInstanceOf('\mod_quiz\event\attempt_graded', $event);
Línea 1200... Línea 1236...
1200
        $this->assertInstanceOf('\mod_quiz\event\slot_mark_updated', $event);
1236
        $this->assertInstanceOf('\mod_quiz\event\slot_mark_updated', $event);
1201
        $this->assertEquals($quizobj->get_context(), $event->get_context());
1237
        $this->assertEquals($quizobj->get_context(), $event->get_context());
1202
        $this->assertEventContextNotUsed($event);
1238
        $this->assertEventContextNotUsed($event);
1203
    }
1239
    }
Línea -... Línea 1240...
-
 
1240
 
-
 
1241
    public function test_slot_version_updated(): void {
-
 
1242
        $quizobj = $this->prepare_quiz();
-
 
1243
        $this->setAdminUser();
-
 
1244
 
-
 
1245
        $quizobj->preload_questions();
-
 
1246
        [, $numqslotid] = array_column($quizobj->get_questions(null, false), 'slotid');
-
 
1247
 
-
 
1248
        $sink = $this->redirectEvents();
-
 
1249
        submit_question_version::execute($numqslotid, 2);
-
 
1250
        $events = $sink->get_events();
-
 
1251
        $event = reset($events);
-
 
1252
 
-
 
1253
        // Check that the event data is valid.
-
 
1254
        $expecteddesc = "The user with id '2' updated the slot with id '$numqslotid' " .
-
 
1255
            "belonging to the quiz with course module id '{$quizobj->get_cmid()}'. " .
-
 
1256
            "Its question version was changed from 'Always latest' to '2'.";
-
 
1257
        $this->assertInstanceOf('\mod_quiz\event\slot_version_updated', $event);
-
 
1258
        $this->assertEquals($quizobj->get_context(), $event->get_context());
-
 
1259
        $this->assertEquals($expecteddesc, $event->get_description());
-
 
1260
        $this->assertEventContextNotUsed($event);
-
 
1261
    }
1204
 
1262
 
1205
    /**
1263
    /**
1206
     * Test quiz_grade_item_created.
1264
     * Test quiz_grade_item_created.
1207
     *
1265
     *
1208
     * @covers \mod_quiz\event\quiz_grade_item_created
1266
     * @covers \mod_quiz\event\quiz_grade_item_created