fetchquestion_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
<?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/>.

/**
 * fetch question PHPUnit tests
 *
 * @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
 */

namespace mod_adaptivequiz\local;

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

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

use advanced_testcase;
use coding_exception;
use mod_adaptivequiz\local\repository\questions_number_per_difficulty;
use stdClass;

/**
 * @group mod_adaptivequiz
 * @covers \mod_adaptivequiz\local\fetchquestion
 */
class fetchquestion_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
     *
     * @return void
     * @throws coding_exception
     */
    protected function setup_test_data_xml() {
        $this->dataset_from_files(
            [__DIR__.'/../fixtures/mod_adaptivequiz_findquestion.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' => 2
        );
        $this->activityinstance = $generator->create_instance($options);

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

    /**
     * This function creates a default user and activity instance using generator classes (using a different question category)
     * 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_two() {
        // 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(4),
                'course' => 2
        );
        $this->activityinstance = $generator->create_instance($options);

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

    /**
     * This function tests the retrieval of using illegible tag ids.
     * @see setup_generator_data() for detail of activity instance.
     */
    public function test_find_questions_fail_tag_ids() {
        $this->resetAfterTest(true);
        $this->setup_test_data_xml();
        $this->setup_generator_data();

        $attempt = $this
            ->getMockBuilder(fetchquestion::class)
            ->onlyMethods(
                ['retrieve_question_categories']
            )
            ->setConstructorArgs(
                [$this->activityinstance, 1, 1, 100]
            )
            ->getMock();
        $attempt->expects($this->exactly(2))
            ->method('retrieve_question_categories')
            ->willReturn(
                [1 => 1, 2 => 2, 3 => 3]
            );

        $data = $attempt->find_questions_with_tags(
            [99]
        );
        $this->assertEquals(0, count($data));

        $data = $attempt->find_questions_with_tags([]);
        $this->assertEquals(0, count($data));
    }

    /**
     * This function tests the retrieval of questions using an empty set of question categories.
     * @see setup_generator_data() for detail of activity instance.
     */
    public function test_find_questions_fail_question_cat() {
        $this->resetAfterTest(true);
        $this->setup_test_data_xml();
        $this->setup_generator_data();

        $mockclass = $this
            ->getMockBuilder(fetchquestion::class)
            ->onlyMethods(
                ['retrieve_question_categories']
            )
            ->setConstructorArgs(
                [$this->activityinstance, 1, 1, 100]
            )
            ->getMock();
        $mockclass->expects($this->exactly(2))
            ->method('retrieve_question_categories')
            ->willReturn([]);

        // Call class method with illegible tag id.
        $data = $mockclass->find_questions_with_tags(
            [99]
        );
        $this->assertEquals(0, count($data));

        // Call class method with legit tag id.
        $data = $mockclass->find_questions_with_tags(
            [1]
        );
        $this->assertEquals(0, count($data));
    }

    /**
     * This function tests the retrieval of questions using the exclude parameter
     * @see setup_generator_data() for detail of activity instance
     * @return void
     */
    public function test_find_questions_exclude() {
        $this->resetAfterTest(true);
        $this->setup_test_data_xml();
        $this->setup_generator_data();

        $mockclass = $this
            ->getMockBuilder(fetchquestion::class)
            ->onlyMethods(
                ['retrieve_question_categories']
            )
            ->setConstructorArgs(
                [$this->activityinstance, 1, 1, 100]
            )
            ->getMock();
        $mockclass->expects($this->once())
            ->method('retrieve_question_categories')
            ->willReturn(
                [1 => 1, 2 => 2, 3 => 3]
            );

        $data = $mockclass->find_questions_with_tags([1], [1]);
        $this->assertEquals(0, count($data));
    }

    /**
     * This functions tests the accessor methods for the $level class variable.
     */
    public function test_get_set_level() {
        $this->resetAfterTest(true);
        $dummyclass = new stdClass();

        $fetchquestion = new fetchquestion($dummyclass, 99, 1, 100);
        $this->assertEquals(99, $fetchquestion->get_level());

        $fetchquestion->set_level(22);
        $this->assertEquals(22, $fetchquestion->get_level());

        $this->expectException('coding_exception');
        $fetchquestion->set_level(-22);
    }

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

        $this->expectException('coding_exception');
        (new fetchquestion(new stdClass(), 0, 1, 100, ['phpunittag_']));
    }

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

        $this->expectException('coding_exception');
        (new fetchquestion(new stdClass(), -11, 1, 100, ['phpunittag_']));
    }

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

        $this->expectException('coding_exception');
        (new fetchquestion(new stdClass(), 'asdf', 1, 100, ['phpunittag_']));
    }

    /**
     * This functions tests the retrevial of tag ids with an associated difficulty level
     * but using legit data.
     */
    public function test_retrieve_tag() {
        $this->resetAfterTest();
        $this->setup_test_data_xml();

        $dummyclass = new stdClass();

        $fetchquestion = new fetchquestion($dummyclass, 5, 1, 100, ['phpunittag_']);
        $data = $fetchquestion->retrieve_tag(5);

        $this->assertEquals(2, count($data));
        $this->assertEquals([0 => 1, 1 => 2], $data);

        $fetchquestion2 = new fetchquestion($dummyclass, 888, 1, 100, ['phpunittag_']);
        $data = $fetchquestion2->retrieve_tag(888);

        $this->assertEquals(0, count($data));
    }

    /**
     * This function test output from fetch_question() where initalize_tags_with_quest_count() returns an empty array
     */
    public function test_fetch_question_initalize_tags_with_quest_count_return_empty_array() {
        $this->resetAfterTest(true);

        $mockclass = $this
            ->getMockBuilder(fetchquestion::class)
            ->onlyMethods(
                ['initalize_tags_with_quest_count', 'retrieve_tag', 'find_questions_with_tags']
            )
            ->setConstructorArgs(
                [new stdClass(), 5, 1, 100]
            )
            ->getMock();
        $mockclass->expects($this->once())
            ->method('initalize_tags_with_quest_count')
            ->willReturn([]);
        $mockclass->expects($this->never())
            ->method('retrieve_tag');
        $mockclass->expects($this->never())
            ->method('find_questions_with_tags');

        $result = $mockclass->fetch_questions();
        $this->assertEquals([], $result);
    }

    /**
     * This function test output from fetch_question() where the initial requested level has available questions
     */
    public function test_fetch_question_requested_level_has_questions() {
        $this->resetAfterTest(true);

        $mockclass = $this
            ->getMockBuilder(fetchquestion::class)
            ->onlyMethods(
                ['initalize_tags_with_quest_count', 'retrieve_tag', 'find_questions_with_tags']
            )
            ->setConstructorArgs(
                [new stdClass(), 5, 1, 100]
            )
            ->getMock();
        $mockclass->expects($this->once())
            ->method('initalize_tags_with_quest_count')
            ->with([], ['adpq_'], '1', '100')
            ->willReturn(
                    [5 => 2]
            );
        $mockclass->expects($this->once())
            ->method('retrieve_tag')
            ->with(5)
            ->willReturn(
                [11]
            );
        $mockclass->expects($this->once())
            ->method('find_questions_with_tags')
            ->with(array(11), array())
            ->willReturn(
                [22]
            );

        $result = $mockclass->fetch_questions();
        $this->assertEquals([22], $result);
    }

    /**
     * This function test output from fetch_question() where one level higher than requested level has available
     * questions.
     */
    public function test_fetch_question_one_level_higher_has_questions() {
        $this->resetAfterTest(true);

        $mockclass = $this
            ->getMockBuilder(fetchquestion::class)
            ->onlyMethods(
                ['initalize_tags_with_quest_count', 'retrieve_tag', 'find_questions_with_tags']
            )
            ->setConstructorArgs(
                [new stdClass(), 5, 1, 100]
            )
            ->getMock();
        $mockclass->expects($this->once())
            ->method('initalize_tags_with_quest_count')
            ->with([], ['adpq_'], '1', '100')
            ->willReturn(
                [5 => 0, 6 => 1]
            );
        $mockclass->expects($this->once())
            ->method('retrieve_tag')
            ->with(6)
            ->willReturn(
                [11]
            );
        $mockclass->expects($this->once())
            ->method('find_questions_with_tags')
            ->with([11], [])
            ->willReturn(
                [22]
            );

        $result = $mockclass->fetch_questions();
        $this->assertEquals([22], $result);
    }

    /**
     * This function test output from fetch_question() where five levels higher than requested level has available
     * questions.
     */
    public function test_fetch_question_five_levels_higher_has_questions() {
        $this->resetAfterTest(true);

        $mockclass = $this
            ->getMockBuilder(fetchquestion::class)
            ->onlyMethods(
                ['initalize_tags_with_quest_count', 'retrieve_tag', 'find_questions_with_tags']
            )
            ->setConstructorArgs(
                [new stdClass(), 5, 1, 100]
            )
            ->getMock();
        $mockclass->expects($this->once())
            ->method('initalize_tags_with_quest_count')
            ->with([], ['adpq_'], '1', '100')
            ->willReturn(
                [1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0, 9 => 0, 10 => 2]
            );
        $mockclass->expects($this->once())
            ->method('retrieve_tag')
            ->with(10)
            ->willReturn(
                [11]
            );
        $mockclass->expects($this->once())
            ->method('find_questions_with_tags')
            ->with([11], [])
            ->willReturn(
                [22]
            );

        $result = $mockclass->fetch_questions();
        $this->assertEquals([22], $result);
    }

    /**
     * This function test output from fetch_question() where four levels lower than requested level has available
     * questions.
     */
    public function test_fetch_question_four_levels_lower_has_questions() {
        $this->resetAfterTest(true);

        $mockclass = $this
            ->getMockBuilder(fetchquestion::class)
            ->onlyMethods(
                ['initalize_tags_with_quest_count', 'retrieve_tag', 'find_questions_with_tags']
            )
            ->setConstructorArgs(
                [new stdClass(), 5, 1, 100]
            )
            ->getMock();
        $mockclass->expects($this->once())
            ->method('initalize_tags_with_quest_count')
            ->with([], ['adpq_'], '1', '100')
            ->willReturn(
                [1 => 1, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0, 9 => 0, 10 => 0]
            );
        $mockclass->expects($this->once())
            ->method('retrieve_tag')
            ->with(1)
            ->willReturn(
                [11]
            );
        $mockclass->expects($this->once())
            ->method('find_questions_with_tags')
            ->with([11], [])
            ->willReturn([22]);

        $result = $mockclass->fetch_questions();
        $this->assertEquals([22], $result);
    }

    /**
     * This function test output from fetch_question() where searching for a question goes outside
     * the min and max boundaries and stops the searching.
     */
    public function test_fetch_question_search_outside_min_max_bounds() {
        $this->resetAfterTest(true);

        $mockclass = $this
            ->getMockBuilder(fetchquestion::class)
            ->onlyMethods(
                ['initalize_tags_with_quest_count', 'retrieve_tag', 'find_questions_with_tags']
            )
            ->setConstructorArgs(
                [new stdClass(),  50, 49, 51]
            )
            ->getMock();
        $mockclass->expects($this->once())
            ->method('initalize_tags_with_quest_count')
            ->with([], ['adpq_'], 49, 51)
            ->willReturn(
                [48 => 1, 52 => 1]
            );
        $mockclass->expects($this->never())
            ->method('retrieve_tag');
        $mockclass->expects($this->never())
            ->method('find_questions_with_tags');

        $result = $mockclass->fetch_questions();
        $this->assertEquals([], $result);
    }

    /**
     * @test
     */
    public function it_retrieves_all_tag_ids(): void {
        $this->resetAfterTest();
        $this->setup_test_data_xml();

        $fetchquestion = new fetchquestion(new stdClass(), 5, 1, 100);
        $result = $fetchquestion->retrieve_all_tag_ids(1, 100, ADAPTIVEQUIZ_QUESTION_TAG);

        $this->assertEquals(
            [5 => '1', 6 => '4', 7 => '5', 8 => '6', 9 => '7', 10 => '8'],
            $result
        );
    }

    /**
     * @test
     */
    public function it_throws_an_exception_when_retrieves_all_tag_ids_for_an_empty_tag_prefix(): void {
        $fetchquestion = new fetchquestion(new stdClass(), 5, 1, 100);

        $this->expectException('invalid_parameter_exception');
        $fetchquestion->retrieve_all_tag_ids(1, 5, '');
    }

    /**
     * This is a data provider for
     * @return $data - an array with arrays of data
     */
    public function constructor_throw_coding_exception_provider() {
        $data = array(
            array(0, 1, 100),
            array(1, 100, 100),
            array(1, 100, 99)
        );

        return $data;
    }

    /**
     * This function tests throwing an exception by passing incorrect parameters
     *
     * @param int $level the difficulty level
     * @param int $min the minimum level of the attempt
     * @param int $max the maximum level of the attempt
     * @dataProvider constructor_throw_coding_exception_provider
     */
    public function test_constructor_throw_coding_exception($level, $min, $max) {
        $this->resetAfterTest(true);

        $this->expectException('coding_exception');
        (new fetchquestion(new stdClass(), $level, $min, $max));
    }

    /**
     * This function tests the output from initalize_tags_with_quest_count()
     */
    public function test_initalize_tags_with_quest_count() {
        $this->resetAfterTest();

        $mockclass = $this
            ->getMockBuilder(fetchquestion::class)
            ->onlyMethods(
                ['retrieve_question_categories', 'retrieve_all_tag_ids', 'retrieve_tags_with_question_count']
            )
            ->setConstructorArgs(
                [new stdClass(), 1, 1, 100]
            )
            ->getMock();
        $mockclass->expects($this->once())
            ->method('retrieve_question_categories')
            ->willReturn(
                [1 => 1, 2 => 2, 3 => 3]
            );
        $mockclass->expects($this->exactly(2))
            ->method('retrieve_all_tag_ids')
            ->withAnyParameters()
            ->willReturn(
                [4 => 4, 5 => 5, 6 => 6]
            );
        $mockclass->expects($this->exactly(2))
            ->method('retrieve_tags_with_question_count')
            ->withAnyParameters()
            ->willReturn(
                [
                    new questions_number_per_difficulty(1, 8),
                    new questions_number_per_difficulty(2, 3),
                    new questions_number_per_difficulty(5, 10),
                ]
            );

        $result = $mockclass->initalize_tags_with_quest_count([], ['test1_', 'test2_'], 1, 100);
        $this->assertEquals([1 => 16, 2 => 6, 5 => 20], $result);
    }

    /**
     * This function tests the output from initalize_tags_with_quest_count(), passing an already built difficulty question
     * sum structure, forcing a rebuild.
     */
    public function test_initalize_tags_with_quest_count_pre_built_quest_sum_struct_rebuild_true() {
        $this->resetAfterTest();

        $mockclass = $this
            ->getMockBuilder(fetchquestion::class)
            ->onlyMethods(
                ['retrieve_question_categories', 'retrieve_all_tag_ids', 'retrieve_tags_with_question_count']
            )
            ->setConstructorArgs(
                [new stdClass(), 1, 1, 100]
            )
            ->getMock();
        $mockclass->expects($this->once())
            ->method('retrieve_question_categories')
            ->willReturn(
                [1 => 1, 2 => 2, 3 => 3]
            );
        $mockclass->expects($this->exactly(2))
            ->method('retrieve_all_tag_ids')
            ->withAnyParameters()
            ->willReturn(
                [4 => 4, 5 => 5, 6 => 6]
            );
        $mockclass->expects($this->exactly(2))
            ->method('retrieve_tags_with_question_count')
            ->withAnyParameters()
            ->willReturn(
                [
                    new questions_number_per_difficulty(1, 8),
                    new questions_number_per_difficulty(2, 3),
                    new questions_number_per_difficulty(5, 10),
                ]
            );

        $result = $mockclass->initalize_tags_with_quest_count([1, 2, 3, 4], ['test1_', 'test2_'], 1, 100, true);
        $this->assertEquals([1 => 16, 2 => 6, 5 => 20], $result);
    }

    /**
     * This function tests the output from decrement_question_sum_from_difficulty().
     */
    public function test_decrement_question_sum_from_difficulty() {
        $this->resetAfterTest(true);

        $dummyclass = new stdClass();
        $result = array(1 => 12);
        $expected = array(1 => 11);

        $fetchquestion = new fetchquestion($dummyclass, 1, 1, 2);
        $result = $fetchquestion->decrement_question_sum_from_difficulty($result, 1);
        $this->assertEquals($expected, $result);
    }

    /**
     * This function tests the output from decrement_question_sum_from_difficulty(), using a key that doesn't exist.
     */
    public function test_decrement_question_sum_from_difficulty_user_missing_key() {
        $this->resetAfterTest(true);

        $dummyclass = new stdClass();
        $result = array(1 => 12);
        $expected = array(1 => 12);

        $fetchquestion = new fetchquestion($dummyclass, 1, 1, 2);
        $result = $fetchquestion->decrement_question_sum_from_difficulty($result, 2);
        $this->assertEquals($expected, $result);
    }
}