attempt_test.php 22.5 KB
Newer Older
0815-xyz's avatar
0815-xyz committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

namespace mod_adaptivequiz\local;

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->dirroot.'/mod/adaptivequiz/locallib.php');

use advanced_testcase;
use context_module;
use mod_adaptivequiz\local\attempt\attempt_state;
use stdClass;

/**
 * Tests for the attempt class.
 *
 * @package    mod_adaptivequiz
 * @copyright  2013 Remote-Learner {@link http://www.remote-learner.ca/}
 * @copyright  2022 onwards Vitaly Potenko <potenkov@gmail.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 *
 * @covers     \mod_adaptivequiz\local\attempt
 */
class attempt_test extends advanced_testcase {
    /** @var stdClass $activityinstance adaptivequiz activity instance object */
    protected $activityinstance = null;

    /** @var stdClass $cm a partially completed course module object */
    protected $cm = null;

    /** @var stdClass $user a user object */
    protected $user = null;

    /**
     * This function loads data into the PHPUnit tables for testing
     */
    protected function setup_test_data_xml() {
        $this->dataset_from_files(
            [__DIR__.'/../fixtures/mod_adaptivequiz_adaptiveattempt.xml']
        )->to_database();
    }

    /**
     * This function creates a default user and activity instance using generator classes
     * The activity parameters created are are follows:
     * lowest difficulty level: 1
     * highest difficulty level: 10
     * minimum question attempts: 2
     * maximum question attempts: 10
     * standard error: 1.1
     * starting level: 5
     * question category ids: 1
     * course id: 2
     * @return void
     */
    protected function setup_generator_data() {
        // Create test user.
        $this->user = $this->getDataGenerator()->create_user();
        $this->setUser($this->user);
        $this->setAdminUser();

        // Create activity.
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_adaptivequiz');
        $options = array(
            'highestlevel' => 10,
            'lowestlevel' => 1,
            'minimumquestions' => 2,
            'maximumquestions' => 10,
            'standarderror' => 1.1,
            'startinglevel' => 5,
            'questionpool' => array(1),
            'course' => 1
        );

        $this->activityinstance = $generator->create_instance($options);

        $this->cm = new stdClass();
        $this->cm->id = $this->activityinstance->cmid;
    }

    /**
     * This function tests retrieving an adaptivequiz attempt record
     */
    public function test_get_attempt() {
        $this->resetAfterTest(true);
        $this->setup_test_data_xml();

        $dummy = new stdClass();
        $dummy->id = 220;
        $userid = 2;

        $attempt = new attempt($dummy, $userid);
        $data = $attempt->get_attempt();

        $expected = new stdClass();
        $expected->id = '1';
        $expected->instance = '220';
        $expected->userid = '2';
        $expected->uniqueid = '1110';
        $expected->attemptstate = attempt_state::IN_PROGRESS;
        $expected->questionsattempted = '0';
        $expected->attemptstopcriteria = '';
        $expected->standarderror = '1.00000';
        $expected->difficultysum = '0.0000000';
        $expected->measure = '0.00000';
        $expected->timemodified = '0';
        $expected->timecreated = '0';

        $this->assertEquals($expected, $data);
    }

    /*
     * @test
     */
    public function it_fails_to_set_quba_for_an_attempt_with_an_invalid_argument(): void {
        $this->resetAfterTest(true);
        $this->setup_generator_data();

        $this->activityinstance->context = context_module::instance($this->cm->id);

        $adaptiveattempt = new attempt($this->activityinstance, $this->user->id);

        $this->expectException('coding_exception');
        $adaptiveattempt->set_quba(new stdClass());
    }

    /**
     * This function tests the accessor methods for question_usage_by_activity ($quba) property
     */
    public function test_set_get_quba() {
        $this->resetAfterTest(true);
        $this->setup_generator_data();

        $this->activityinstance->context = context_module::instance($this->cm->id);

        $adaptiveattempt = new attempt($this->activityinstance, $this->user->id);

        // Test a non initialized quba.
        $this->assertNull($adaptiveattempt->get_quba());

        // Test a property initializes quba.
        $mockquba = $this->getMockBuilder('question_usage_by_activity')
            ->disableOriginalConstructor()
            ->getMock();

        $adaptiveattempt->set_quba($mockquba);
        $this->assertInstanceOf('question_usage_by_activity', $adaptiveattempt->get_quba());
    }

    /**
     * This function tests whether the user's attempt has exceeded the maximum number of questions attempted
     */
    public function test_max_questions_answered() {
        $this->resetAfterTest(true);
        $this->setup_test_data_xml();

        $dummy = new stdClass();
        $dummy->id = 220;
        $dummy->maximumquestions = 20;
        $userid = 2;

        $attempt = new attempt($dummy, $userid);
        $attempt->get_attempt();
        $this->assertEquals(false, $attempt->max_questions_answered());

        $dummy->id = 221;
        $attempt = new attempt($dummy, $userid);
        $attempt->get_attempt();
        $this->assertEquals(false, $attempt->max_questions_answered());

        $dummy->id = 222;
        $attempt = new attempt($dummy, $userid);
        $attempt->get_attempt();
        $this->assertEquals(true, $attempt->max_questions_answered());

        $dummy->id = 223;
        $attempt = new attempt($dummy, $userid);
        $attempt->get_attempt();
        $this->assertEquals(true, $attempt->max_questions_answered());
    }

    /**
     * This function tests whether the user's attempt has met the minimum number of questions attempted
     */
    public function test_min_questions_answered() {
        $this->resetAfterTest(true);
        $this->setup_test_data_xml();

        $dummy = new stdClass();
        $dummy->id = 220;
        $dummy->minimumquestions = 21;
        $userid = 2;

        $attempt = new attempt($dummy, $userid);
        $attempt->get_attempt();
        $this->assertEquals(false, $attempt->min_questions_answered());

        $dummy->id = 221;
        $attempt = new attempt($dummy, $userid);
        $attempt->get_attempt();
        $this->assertEquals(false, $attempt->min_questions_answered());

        $dummy->id = 222;
        $attempt = new attempt($dummy, $userid);
        $attempt->get_attempt();
        $this->assertEquals(false, $attempt->min_questions_answered());

        $dummy->id = 223;
        $attempt = new attempt($dummy, $userid);
        $attempt->get_attempt();
        $this->assertEquals(true, $attempt->min_questions_answered());
    }

    /**
     * This function test that one value is returned or an empty array is returned
     */
    public function test_return_random_question() {
        $this->resetAfterTest(true);

        $dummy = new stdClass();
        $dummy->id = 220;
        $dummy->minimumquestions = 21;
        $userid = 2;

        $adaptiveattempt = new attempt($dummy, $userid);

        $result = $adaptiveattempt->return_random_question(array());
        $this->assertEquals(0, $result);

        $result = (string) $adaptiveattempt->return_random_question(
            [1 => 'quest 1', 2 => 'quest 2', 3 => 'quest 3', 4 => 'quest 4']
        );
        $this->assertMatchesRegularExpression('/[1-4]/', $result);
    }

    /**
     * This function tests the creation of a question_usage_by_activity object for an attempt
     */
    public function test_initialize_quba() {
        $this->resetAfterTest(true);
        $this->setup_generator_data();

        $this->activityinstance->context = context_module::instance($this->cm->id);

        $adaptiveattempt = new attempt($this->activityinstance, $this->user->id);
        $adaptiveattempt->get_attempt();
        $quba = $adaptiveattempt->initialize_quba();

        $this->assertInstanceOf('question_usage_by_activity', $quba);
    }

    /**
     * This function tests the re-using of the question_usage_by_activity object, due to an incomplete attempt
     */
    public function test_initialize_quba_with_existing_attempt_uniqueid() {
        global $DB;

        $this->resetAfterTest(true);
        $this->setup_test_data_xml();

        $param = array('id' => 330);
        $activityinstance = $DB->get_record('adaptivequiz', $param);

        $adaptiveattempt = new attempt($activityinstance, 2);
        $adaptiveattempt->get_attempt();
        $quba = $adaptiveattempt->initialize_quba();

        $this->assertInstanceOf('question_usage_by_activity', $quba);

        // Check if the uniqueid of the adaptive attempt equals the id of the object loaded by quba class.
        $this->assertEquals(330, $quba->get_id());
    }

    /**
     * This function tests retrieving the last question viewed by the student for a given attempt
     */
    public function test_find_last_quest_used_by_attempt() {
        global $DB;

        $this->resetAfterTest(true);
        $this->setup_test_data_xml();

        $param = array('id' => 330);
        $activityinstance = $DB->get_record('adaptivequiz', $param);

        $adaptiveattempt = new attempt($activityinstance, 2);
        $adaptiveattempt->get_attempt();
        $quba = $adaptiveattempt->initialize_quba();

        $result = $adaptiveattempt->find_last_quest_used_by_attempt($quba);

        $this->assertEquals(2, $result);
    }

    /**
     * @test
     */
    public function it_fails_to_find_the_last_question_used_by_attempt_with_an_invalid_argument() {
        $this->resetAfterTest(true);

        $dummy = new stdClass();

        $adaptiveattempt = new attempt($dummy, 2);

        $this->expectException('coding_exception');
        $adaptiveattempt->find_last_quest_used_by_attempt($dummy);
    }

    /**
     * This function tests retrieving the last question viewed by the student for a given attempt, but using failing data
     */
    public function test_find_last_umarked_question_using_bad_data() {
        $this->resetAfterTest(true);

        $dummy = new stdClass();

        $adaptiveattempt = $this->getMockBuilder(attempt::class)
            ->disableOriginalConstructor()
            ->getMock();

        // Test passing an invalid object instance.
        $result = $adaptiveattempt->find_last_quest_used_by_attempt($dummy);
        $this->assertEquals(0, $result);

        // Test getting an empty slot value.
        $mockquba = $this->getMockBuilder('question_usage_by_activity')
            ->disableOriginalConstructor()
            ->getMock();

        $result = $adaptiveattempt->find_last_quest_used_by_attempt($mockquba);

        $this->assertEquals(0, $result);
    }

    /**
     * This function tests whether the user submitted an answer to the question
     */
    public function test_was_answer_submitted_to_question_with_graded_right() {
        $this->resetAfterTest(true);
        $dummy = new stdClass();
        $adaptiveattempt = new attempt($dummy, 1);

        $mockquba = $this->getMockBuilder('question_usage_by_activity')
            ->disableOriginalConstructor()
            ->getMock();

        $mockstate = $this->createMock('question_state_gradedright', array(), array(), '', false);

        $mockquba->expects($this->once())
            ->method('get_question_state')
            ->with(1)
            ->will($this->returnValue($mockstate));

        $mockquba->expects($this->never())
            ->method('get_id');

        $result = $adaptiveattempt->was_answer_submitted_to_question($mockquba, 1);
        $this->assertTrue($result);
    }

    /**
     * This function tests whether the user submitted an answer to the question
     */
    public function test_was_answer_submitted_to_question_with_graded_wrong() {
        $this->resetAfterTest(true);
        $dummy = new stdClass();
        $adaptiveattempt = new attempt($dummy, 1);

        $mockquba = $this->getMockBuilder('question_usage_by_activity')
            ->disableOriginalConstructor()
            ->getMock();

        $mockstate = $this->createMock('question_state_gradedwrong', array(), array(), '', false);

        $mockquba->expects($this->once())
            ->method('get_question_state')
            ->with(1)
            ->will($this->returnValue($mockstate));

        $mockquba->expects($this->never())
            ->method('get_id');

        $result = $adaptiveattempt->was_answer_submitted_to_question($mockquba, 1);
        $this->assertTrue($result);
    }

    /**
     * This function tests whether the user submitted an answer to the question
     */
    public function test_was_answer_submitted_to_question_with_graded_partial() {
        $this->resetAfterTest(true);
        $dummy = new stdClass();
        $adaptiveattempt = new attempt($dummy, 1);

        $mockquba = $this->getMockBuilder('question_usage_by_activity')
            ->disableOriginalConstructor()
            ->getMock();

        $mockstate = $this->createMock('question_state_gradedpartial', array(), array(), '', false);

        $mockquba->expects($this->once())
            ->method('get_question_state')
            ->with(1)
            ->will($this->returnValue($mockstate));

        $mockquba->expects($this->never())
            ->method('get_id');

        $result = $adaptiveattempt->was_answer_submitted_to_question($mockquba, 1);
        $this->assertTrue($result);
    }

    /**
     * This function tests whether the user submitted an answer to the question
     */
    public function test_was_answer_submitted_to_question_with_graded_gaveup() {
        $this->resetAfterTest(true);
        $dummy = new stdClass();
        $adaptiveattempt = new attempt($dummy, 1);

        $mockquba = $this->getMockBuilder('question_usage_by_activity')
            ->disableOriginalConstructor()
            ->getMock();

        $mockstate = $this->createMock('question_state_gaveup', array(), array(), '', false);

        $mockquba->expects($this->once())
            ->method('get_question_state')
            ->with(1)
            ->will($this->returnValue($mockstate));

        $mockquba->expects($this->never())
            ->method('get_id');

        $result = $adaptiveattempt->was_answer_submitted_to_question($mockquba, 1);
        $this->assertTrue($result);
    }

    /**
     * This function tests whether the user submitted an answer to the question
     */
    public function test_was_answer_submitted_to_question_with_graded_todo() {
        $this->resetAfterTest(true);
        $dummy = new stdClass();
        $adaptiveattempt = new attempt($dummy, 1);

        $mockquba = $this->getMockBuilder('question_usage_by_activity')
            ->disableOriginalConstructor()
            ->getMock();

        $mockstate = $this->createMock('question_state_todo', array(), array(), '', false);

        $mockquba->expects($this->once())
            ->method('get_question_state')
            ->with(1)
            ->will($this->returnValue($mockstate));

        $mockquba->expects($this->once())
            ->method('get_id')
            ->will($this->returnValue(1));

        $result = $adaptiveattempt->was_answer_submitted_to_question($mockquba, 1);
        $this->assertFalse($result);
    }

    /**
     * This function tests the accessor method for $slot
     */
    public function test_set_get_question_slot_number() {
        $this->resetAfterTest(true);

        $dummy = new stdClass();
        $userid = 1;
        $adaptiveattempt = new attempt($dummy, $userid);

        $adaptiveattempt->set_question_slot_number(2);
        $result = $adaptiveattempt->get_question_slot_number();

        $this->assertEquals(2, $result);
    }

    /**
     * This function tests the accessor method for $slot
     */
    public function test_set_get_question_level() {
        $this->resetAfterTest(true);

        $dummy = new stdClass();
        $userid = 1;
        $adaptiveattempt = new attempt($dummy, $userid);

        $adaptiveattempt->set_level(2);
        $result = $adaptiveattempt->get_level();

        $this->assertEquals(2, $result);
    }

    /**
     * This function tests results returned from get_question_mark()
     */
    public function test_get_question_mark_with_quba_return_float() {
        $this->resetAfterTest(true);

        // Test quba returning a mark of 1.0.
        $mockquba = $this->createMock('question_usage_by_activity', array(), array(), '', false);

        $mockquba->expects($this->once())
            ->method('get_question_mark')
            ->will($this->returnValue(1.0));

        $dummy = new stdClass();
        $userid = 1;
        $adaptiveattempt = new attempt($dummy, $userid);
        $result = $adaptiveattempt->get_question_mark($mockquba, 1);
        $this->assertEquals(1.0, $result);
    }

    /**
     * This function tests results returned from get_question_mark()
     */
    public function test_get_question_mark_with_quba_return_non_float() {
        $this->resetAfterTest(true);

        // Test quba returning a non float value.
        $mockqubatwo = $this->createMock('question_usage_by_activity', array(), array(), '', false);

        $mockqubatwo->expects($this->once())
            ->method('get_question_mark')
            ->will($this->returnValue(0));

        $dummy = new stdClass();
        $userid = 1;
        $adaptiveattempttwo = new attempt($dummy, $userid);
        $result = $adaptiveattempttwo->get_question_mark($mockqubatwo, 1);
        $this->assertEquals(0, $result);
    }

    /**
     * This function tests results returned from get_question_mark()
     */
    public function test_get_question_mark_with_quba_return_non_null() {
        $this->resetAfterTest(true);

        // Test quba returning null.
        $mockqubathree = $this->createMock('question_usage_by_activity', array(), array(), '', false);

        $mockqubathree->expects($this->once())
            ->method('get_question_mark')
            ->will($this->returnValue(null));

        $dummy = new stdClass();
        $userid = 1;
        $adaptiveattemptthree = new attempt($dummy, $userid);
        $result = $adaptiveattemptthree->get_question_mark($mockqubathree, 1);
        $this->assertEquals(0, $result);
    }

    /**
     * This function tests what happens when the maximum number of questions have been answered
     */
    public function test_start_attempt_max_num_of_quest_answered() {
        $this->resetAfterTest(true);

        $attempt = $this->createPartialMock(attempt::class,
            ['get_attempt', 'level_in_bounds', 'max_questions_answered']);
        $attempt->method('get_attempt')->willReturn(new stdClass());
        $attempt->method('level_in_bounds')->willReturn(true);
        $attempt->method('max_questions_answered')->willReturn(true);

        $this->assertFalse($attempt->start_attempt());
    }

    /**
     * This function tests what happens when a question slot number is not found, but the number of submitted answers is greater
     * than zero.
     */
    public function test_start_attempt_quest_slot_empty_quest_submit_greater_than_one() {
        $dummyadaptivequiz = new stdClass();
        $dummyadaptivequiz->lowestlevel = 1;
        $dummyadaptivequiz->highestlevel = 100;

        $mockattemptthree = $this
            ->getMockBuilder(attempt::class)
            ->onlyMethods(
                ['get_attempt', 'max_questions_answered', 'initialize_quba', 'find_last_quest_used_by_attempt',
                    'level_in_bounds']
            )
            ->setConstructorArgs(
                [$dummyadaptivequiz, 1]
            )
            ->getMock();

        $dummyattempt = new stdClass();
        $dummyattempt->questionsattempted = 1;

        $mockattemptthree->expects($this->once())
            ->method('get_attempt')
            ->will($this->returnValue($dummyattempt));
        $mockattemptthree->expects($this->once())
            ->method('level_in_bounds')
            ->will($this->returnValue(true));
        $mockattemptthree->expects($this->once())
            ->method('max_questions_answered')
            ->will($this->returnValue(false));
        $mockattemptthree->expects($this->once())
            ->method('initialize_quba');
        $mockattemptthree->expects($this->once())
            ->method('find_last_quest_used_by_attempt')
            ->will($this->returnValue(0));

        $this->assertFalse($mockattemptthree->start_attempt());
    }

    /**
     * This function tests the return values for level_in_bounds()
     */
    public function test_level_in_bounds() {
        $this->resetAfterTest(true);

        $dummy = new stdClass();
        $dummy->lowestlevel = 5;
        $dummy->highestlevel = 10;

        $userid = 1;
        $adaptiveattempt = new attempt($dummy, $userid);

        $adaptivequiz = $adaptiveattempt->get_adaptivequiz();

        $result = $adaptiveattempt->level_in_bounds(6, $adaptivequiz);

        $this->assertTrue($result);
    }

    /**
     * This function tests retrieving of question ids
     */
    public function test_get_all_questions_in_attempt() {
        $this->resetAfterTest(true);
        $this->setup_test_data_xml();

        $dummy = new stdClass();
        $userid = 1;
        $adaptiveattempt = new attempt($dummy, $userid);
        $questids = $adaptiveattempt->get_all_questions_in_attempt(330);

        $this->assertEquals(2, count($questids));
        $this->assertEquals(array('1' => 1, '2' => 2), $questids);
    }

    /**
     * @test
     */
    public function it_can_check_if_a_user_has_a_completed_attempt_on_a_quiz(): void {
        global $DB;

        $this->resetAfterTest();
        $this->setup_test_data_xml();

        $uniqueid = 330;
        $adaptivequizid = 330;
        $cmid = 5;
        $userid = 2;

        $adaptivequiz = $DB->get_record('adaptivequiz', ['id' => $adaptivequizid]);
        $context = context_module::instance($cmid);

        adaptivequiz_complete_attempt($uniqueid, $adaptivequiz, $context, $userid, '', '');

        $this->assertTrue(attempt::user_has_completed_on_quiz($adaptivequizid, $userid));
    }
}