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 core_communication;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Trait communication_test_helper_trait to generate initial setup for communication providers.
|
|
|
21 |
*
|
|
|
22 |
* @package core_communication
|
|
|
23 |
* @category test
|
|
|
24 |
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
*/
|
|
|
27 |
trait communication_test_helper_trait {
|
|
|
28 |
/**
|
|
|
29 |
* Setup necessary configs for communication subsystem.
|
|
|
30 |
*
|
|
|
31 |
* @return void
|
|
|
32 |
*/
|
|
|
33 |
protected function setup_communication_configs(): void {
|
|
|
34 |
set_config('enablecommunicationsubsystem', 1);
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Disable configs for communication subsystem.
|
|
|
39 |
*
|
|
|
40 |
* @return void
|
|
|
41 |
*/
|
|
|
42 |
protected function disable_communication_configs(): void {
|
|
|
43 |
set_config('enablecommunicationsubsystem', 0);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Get or create course if it does not exist
|
|
|
48 |
*
|
|
|
49 |
* @param string $roomname The room name for the communication api
|
|
|
50 |
* @param string $provider The selected provider
|
|
|
51 |
* @return \stdClass
|
|
|
52 |
*/
|
|
|
53 |
protected function get_course(
|
|
|
54 |
string $roomname = 'Sampleroom',
|
|
|
55 |
string $provider = 'communication_matrix',
|
|
|
56 |
array $extrafields = [],
|
|
|
57 |
): \stdClass {
|
|
|
58 |
|
|
|
59 |
$this->setup_communication_configs();
|
|
|
60 |
$records = [
|
|
|
61 |
'selectedcommunication' => $provider,
|
|
|
62 |
'communicationroomname' => $roomname,
|
|
|
63 |
];
|
|
|
64 |
|
|
|
65 |
return $this->getDataGenerator()->create_course(array_merge($records, $extrafields));
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Get or create user if it does not exist.
|
|
|
70 |
*
|
|
|
71 |
* @param string $firstname The user's firstname for the communication api
|
|
|
72 |
* @param string $lastname The user's lastname for the communication api
|
|
|
73 |
* @param string $username The user's username for the communication api
|
|
|
74 |
* @return \stdClass
|
|
|
75 |
*/
|
|
|
76 |
protected function get_user(
|
|
|
77 |
string $firstname = 'Samplefn',
|
|
|
78 |
string $lastname = 'Sampleln',
|
|
|
79 |
string $username = 'sampleun'
|
|
|
80 |
): \stdClass {
|
|
|
81 |
|
|
|
82 |
$this->setup_communication_configs();
|
|
|
83 |
$records = [
|
|
|
84 |
'firstname' => $firstname,
|
|
|
85 |
'lastname' => $lastname,
|
|
|
86 |
'username' => $username,
|
|
|
87 |
];
|
|
|
88 |
|
|
|
89 |
return $this->getDataGenerator()->create_user($records);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Create a stored_file in a draft file area from a fixture file.
|
|
|
95 |
*
|
|
|
96 |
* @param string $filename The file name within the communication/tests/fixtures folder.
|
|
|
97 |
* @param string $storedname The name to use in the database.
|
|
|
98 |
* @return \stored_file
|
|
|
99 |
*/
|
|
|
100 |
protected function create_communication_file(
|
|
|
101 |
string $filename,
|
|
|
102 |
string $storedname,
|
|
|
103 |
): \stored_file {
|
|
|
104 |
global $CFG;
|
|
|
105 |
|
|
|
106 |
$fs = get_file_storage();
|
|
|
107 |
|
|
|
108 |
$itemid = file_get_unused_draft_itemid();
|
|
|
109 |
return $fs->create_file_from_pathname((object) [
|
|
|
110 |
'contextid' => \context_system::instance()->id,
|
|
|
111 |
'component' => 'user',
|
|
|
112 |
'filearea' => 'draftfile',
|
|
|
113 |
'itemid' => $itemid,
|
|
|
114 |
'filepath' => '/',
|
|
|
115 |
'filename' => $storedname,
|
|
|
116 |
], "{$CFG->dirroot}/communication/tests/fixtures/{$filename}");
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Helper to execute a particular task.
|
|
|
121 |
*
|
|
|
122 |
* @param string $task The task.
|
|
|
123 |
*/
|
|
|
124 |
private function execute_task(string $task): void {
|
|
|
125 |
// Run the scheduled task.
|
|
|
126 |
ob_start();
|
|
|
127 |
$task = \core\task\manager::get_scheduled_task($task);
|
|
|
128 |
$task->execute();
|
|
|
129 |
ob_end_clean();
|
|
|
130 |
}
|
|
|
131 |
}
|