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_bigbluebuttonbn\task;
18
 
19
use advanced_testcase;
20
use mod_bigbluebuttonbn\task\completion_update_state;
21
 
22
/**
23
 * Completion_update_state task tests.
24
 *
25
 * @package   mod_bigbluebuttonbn
26
 * @copyright 2024 Catalyst IT
27
 * @author    Matthew Hilton <matthewhilton@catalyst-au.net>
28
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 * @covers    \mod_bigbluebuttonbn\task\completion_update_state
30
 */
31
final class completion_update_state_task_test extends advanced_testcase {
32
 
33
    /**
34
     * Providers data to test_invalid_customdata test
35
     * @return array
36
     */
37
    public static function invalid_customdata_provider(): array {
38
        return [
39
            'empty' => [
40
                'customdata' => [],
41
                'expectoutput' => "",
42
                'expectexceptionmessage' => "Task customdata was missing bigbluebuttonbn->id or userid",
43
            ],
44
            'bbb id set but is invalid' => [
45
                'customdata' => [
46
                    'bigbluebuttonbn' => -1,
47
                ],
48
                'expectoutput' => "",
49
                'expectexceptionmessage' => "Task customdata was missing bigbluebuttonbn->id or userid",
50
            ],
51
            'bbb id is valid, but there is no user' => [
52
                'customdata' => [
53
                    'bigbluebuttonbn' => ':bbb',
54
                ],
55
                'expectoutput' => "",
56
                'expectexceptionmessage' => "Task customdata was missing bigbluebuttonbn->id or userid",
57
            ],
58
            'bbb id is valid, but the user is not given' => [
59
                'customdata' => [
60
                    'bigbluebuttonbn' => ':bbb',
61
                ],
62
                'expectoutput' => "",
63
                'expectexceptionmessage' => "Task customdata was missing bigbluebuttonbn->id or userid",
64
            ],
65
            'bbb id is valid, but the user given is invalid' => [
66
                'customdata' => [
67
                    'bigbluebuttonbn' => ':bbb',
68
                    'userid' => -1,
69
                ],
70
                'expectoutput' => "User does not exist, ignoring.\n",
71
                'expectexceptionmessage' => "",
72
            ],
73
            'bbb and userid is valid' => [
74
                'customdata' => [
75
                    'bigbluebuttonbn' => ':bbb',
76
                    'userid' => ':userid',
77
                ],
78
                // Expects this output, since all the necessary data is there.
79
                'expectoutput' => "Task completion_update_state running for user :userid\nCompletion not enabled\n",
80
                'expectexceptionmessage' => "",
81
            ],
82
        ];
83
    }
84
    /**
85
     * Tests the task handles an invalid cmid gracefully.
86
     * @param array $customdata customdata to set (with placeholders to replace with real data).
87
     * @param string $expectoutput any output expected from the test, or empty to not expect output.
88
     * @param string $expectexceptionmessage exception message expected from test, or empty to expect nothing.
89
     * @dataProvider invalid_customdata_provider
90
     */
91
    public function test_invalid_customdata(array $customdata, string $expectoutput, string $expectexceptionmessage): void {
92
        $this->resetAfterTest();
93
        $customdata = (object) $customdata;
94
 
95
        // Replace any placeholders in the customdata.
96
        if (!empty($customdata->bigbluebuttonbn) && $customdata->bigbluebuttonbn == ':bbb') {
97
            $course = $this->getDataGenerator()->create_course();
98
            $module = $this->getDataGenerator()->create_module('bigbluebuttonbn', ['course' => $course->id]);
99
            $customdata->bigbluebuttonbn = $module;
100
        }
101
 
102
        $user = $this->getDataGenerator()->create_user();
103
 
104
        // Replace userid placeholders.
105
        if (!empty($customdata->userid) && $customdata->userid == ':userid') {
106
            $customdata->userid = $user->id;
107
        }
108
 
109
        $task = new completion_update_state();
110
        $task->set_custom_data($customdata);
111
 
112
        if (!empty($expectoutput)) {
113
            $expectoutput = str_replace(':userid', $user->id, $expectoutput);
114
            $this->expectOutputString($expectoutput);
115
        }
116
 
117
        if (!empty($expectexceptionmessage)) {
118
            $this->expectExceptionMessage($expectexceptionmessage);
119
        }
120
        $task->execute();
121
    }
122
}