Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
11 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_h5pactivity\external;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
23
 
24
use mod_h5pactivity\local\manager;
25
use core_external\external_api;
26
use externallib_advanced_testcase;
27
 
28
/**
29
 * External function test for log_report_viewed.
30
 *
31
 * @package    mod_h5pactivity
32
 * @category   external
33
 * @covers     \mod_h5pactivity\external\log_report_viewed
34
 * @since      Moodle 3.11
35
 * @copyright  2021 Ilya Tregubov <ilya@moodle.com>
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
final class log_report_viewed_test extends externallib_advanced_testcase {
39
 
40
    /**
41
     * Test the behaviour of log_report_viewed.
42
     *
43
     * @dataProvider execute_data
44
     * @param int $enabletracking the activity tracking enable
45
     * @param int $reviewmode the activity review mode
46
     * @param string $loginuser the user which calls the webservice
47
     * @param string|null $participant the user to log the data
48
     */
49
    public function test_execute(int $enabletracking, int $reviewmode, string $loginuser, ?string $participant): void {
50
        $this->resetAfterTest();
51
        $this->setAdminUser();
52
 
53
        // Create a course.
54
        $course = $this->getDataGenerator()->create_course();
55
 
56
        // Enrol users: 1 teacher, 1 student.
57
        $users = [
58
            'editingteacher' => $this->getDataGenerator()->create_and_enrol($course, 'editingteacher'),
59
            'student' => $this->getDataGenerator()->create_and_enrol($course, 'student'),
60
        ];
61
 
62
        // Add h5p activity.
63
        $activity = $this->getDataGenerator()->create_module('h5pactivity',
64
            ['course' => $course, 'enabletracking' => $enabletracking, 'reviewmode' => $reviewmode]);
65
 
66
        // Create attempt for h5p activity.
67
        $attempts = [];
68
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity');
69
        $user = $users['student'];
70
        $manager = manager::create_from_instance($activity);
71
        $cm = $manager->get_coursemodule();
72
        $params = ['cmid' => $cm->id, 'userid' => $user->id];
73
        $attempts['student'] = $generator->create_content($activity, $params);
74
 
75
        // Redirect events to the sink, so we can recover them later.
76
        $sink = $this->redirectEvents();
77
 
78
        // Execute external method.
79
        $this->setUser($users[$loginuser]);
80
        $attemptid = $attempts[$participant]->id ?? 0;
81
        $result = log_report_viewed::execute($activity->id, $user->id, $attemptid);
82
        $result = external_api::clean_returnvalue(
83
            log_report_viewed::execute_returns(),
84
            $result
85
        );
86
 
87
        // Validate general structure.
88
        $this->assertArrayHasKey('status', $result);
89
 
90
        $events = $sink->get_events();
91
        $event = end($events);
92
 
93
        // Check the event details are correct.
94
        $this->assertInstanceOf('mod_h5pactivity\event\report_viewed', $event);
95
        $this->assertEquals(\context_module::instance($cm->id), $event->get_context());
96
 
97
        $this->assertEquals($cm->instance, $event->other['instanceid']);
98
        $this->assertEquals($user->id, $event->other['userid']);
99
        $this->assertEquals($attemptid, $event->other['attemptid']);
100
    }
101
 
102
    /**
103
     * Data provider for the test_execute tests.
104
     *
105
     * @return  array
106
     */
107
    public static function execute_data(): array {
108
        return [
109
            'Student reviewing own attempt' => [
110
                1, manager::REVIEWCOMPLETION, 'student', 'student'
111
            ],
112
            'Teacher reviewing student attempts' => [
113
                1, manager::REVIEWCOMPLETION, 'editingteacher', 'student'
114
            ],
115
        ];
116
    }
117
}