MDL-78738 core_communication: Set avatar from a stored_file
[moodle.git] / communication / provider / matrix / tests / matrix_test_helper_trait.php
blob80d69b4f30a203f3bbf90f23bbf3d97f4cd0c8ee
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.
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/>.
17 namespace communication_matrix;
19 /**
20 * Trait matrix_helper_trait to generate initial setup for matrix mock and associated helpers.
22 * @package communication_matrix
23 * @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 trait matrix_test_helper_trait {
28 /**
29 * @var string $accesstoken The token for matrix connection
31 protected string $accesstoken;
33 /**
34 * @var string $matrixhomeserverurl The server url of matrix synapse server
36 protected string $matrixhomeserverurl;
38 /**
39 * Initialize the mock configs in settings.
41 * @return void
43 protected function initialise_mock_configs(): void {
44 $this->matrixhomeserverurl = TEST_COMMUNICATION_MATRIX_MOCK_SERVER;
45 set_config('matrixhomeserverurl', $this->matrixhomeserverurl, 'communication_matrix');
46 $request = $this->request();
47 $response = $request->post($this->matrixhomeserverurl. '/backoffice/create-admin');
48 $admindata = json_decode($response->getBody());
49 $json = [
50 'identifier' => [
51 'type' => 'm.id.user',
52 'user' => $admindata->user_id,
54 'type' => 'm.login.password',
55 'password' => $admindata->password,
57 $request = $this->request($json);
58 $response = $request->post($this->matrixhomeserverurl. '/_matrix/client/r0/login');
59 $response = json_decode($response->getBody());
60 if (empty($response->access_token)) {
61 $this->markTestSkipped(
62 'The matrix mock server is not responsive, can not continue the tests'
65 $this->accesstoken = $response->access_token;
66 set_config('matrixaccesstoken', $this->accesstoken, 'communication_matrix');
69 /**
70 * Get the mock server url.
72 * @return string
74 public function get_matrix_server_url(): string {
75 if (empty($this->matrixhomeserverurl)) {
76 throw new \coding_exception('Can not get this information without initializing the mock server.');
78 return $this->matrixhomeserverurl;
81 /**
82 * Get the matrix access token.
84 * @return string
86 public function get_matrix_access_token(): string {
87 if (empty($this->accesstoken)) {
88 throw new \coding_exception('Can not get this information without initializing the mock server.');
90 return $this->accesstoken;
93 /**
94 * This test requires mock server to be present.
96 * @return void
98 protected function initialise_mock_server(): void {
99 if (!defined('TEST_COMMUNICATION_MATRIX_MOCK_SERVER')) {
100 $this->markTestSkipped(
101 'The TEST_COMMUNICATION_MATRIX_MOCK_SERVER constant must be defined to run communication_matrix tests'
104 $this->reset_mock();
105 $this->initialise_mock_configs();
109 * Get matrix room data from matrix server.
111 * @param string $roomid The id of the room
112 * @return \stdClass
114 public function get_matrix_room_data(string $roomid): \stdClass {
115 $matrixeventmanager = new matrix_events_manager($roomid);
116 $response = $matrixeventmanager->request()->get($matrixeventmanager->get_room_info_endpoint());
117 return json_decode($response->getBody(), false, 512, JSON_THROW_ON_ERROR);
121 * Get matrix user data from matrix server.
123 * @param string $roomid The id of the room
124 * @param string $matrixuserid The id of the user
125 * @return \stdClass
127 public function get_matrix_user_data(string $roomid, string $matrixuserid): \stdClass {
128 $matrixeventmanager = new matrix_events_manager($roomid);
129 $response = $matrixeventmanager->request()->get($matrixeventmanager->get_user_info_endpoint($matrixuserid));
130 return json_decode($response->getBody(), false, 512, JSON_THROW_ON_ERROR);
134 * The http request for the api call.
136 * @param array $jsonarray The array of json
137 * @param array $headers The array of headers
138 * @return \core\http_client
140 public function request(array $jsonarray = [], array $headers = []): \core\http_client {
141 $response = new \core\http_client([
142 'headers' => $headers,
143 'json' => $jsonarray,
145 return $response;
149 * Get the URI of a backoffice endpoint on the mock server.
151 * @param string $endpoint
152 * @return string
154 protected function get_backoffice_uri(string $endpoint): string {
155 return $this->get_matrix_server_url() . '/backoffice/' . $endpoint;
159 * Fetch all rooms from the back office.
161 * @return array
163 public function backoffice_get_all_rooms(): array {
164 $client = new \core\http_client();
166 return json_decode($client->get($this->get_backoffice_uri('rooms'))->getBody())->rooms;
170 * Return the first room from the server.
172 * In most cases there is only one room.
173 * @return \stdClass
175 public function backoffice_get_room(): \stdClass {
176 // Fetch the room information from the server.
177 $rooms = $this->backoffice_get_all_rooms();
178 $this->assertCount(1, $rooms);
179 $room = reset($rooms);
180 return $room;
184 * Reset the mock server
186 * @return void
188 public function reset_mock(): void {
189 if (defined('TEST_COMMUNICATION_MATRIX_MOCK_SERVER')) {
190 $request = $this->request();
191 $response = $request->post(TEST_COMMUNICATION_MATRIX_MOCK_SERVER. '/backoffice/reset');
192 $response = json_decode($response->getBody());
193 if (empty($response->reset)) {
194 $this->markTestSkipped(
195 'The matrix mock server is not responsive, can not continue the tests'
202 * Helper to create a room.
204 * @param null|string $component
205 * @param null|string $itemtype
206 * @param null|int $itemid
207 * @param null|string $roomname
208 * @param null|string $roomtopic
209 * @param null|stored_file $roomavatar
210 * @param array $members
211 * @return api
213 protected function create_matrix_room(
214 ?string $component = 'communication_matrix',
215 ?string $itemtype = 'example',
216 ?int $itemid = 1,
217 ?string $roomname = null,
218 ?string $roomtopic = null,
219 ?\stored_file $roomavatar = null,
220 array $members = [],
221 ): \core_communication\api {
222 // Create a new room.
223 $communication = \core_communication\api::load_by_instance(
224 component: $component,
225 instancetype: $itemtype,
226 instanceid: $itemid,
229 $communication->create_and_configure_room(
230 selectedcommunication: 'communication_matrix',
231 communicationroomname: $roomname ?? 'Room name',
232 avatar: $roomavatar,
233 instance: (object) [
234 'matrixroomtopic' => $roomtopic ?? 'A fun topic',
238 $communication->add_members_to_room($members);
240 // Run the adhoc task.
241 $this->run_all_adhoc_tasks();
243 return \core_communication\api::load_by_instance(
244 component: $component,
245 instancetype: $itemtype,
246 instanceid: $itemid,