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 communication_matrix; 18 19 use communication_matrix\local\spec\{v1p1, v1p2, v1p3, v1p4, v1p5, v1p6, v1p7}; 20 use communication_matrix\tests\fixtures\mocked_matrix_client; 21 use core\http_client; 22 use GuzzleHttp\Handler\MockHandler; 23 use GuzzleHttp\HandlerStack; 24 use GuzzleHttp\Middleware; 25 use GuzzleHttp\Psr7\Response; 26 27 /** 28 * A trait with shared tooling for handling matrix_client tests. 29 * 30 * @package communication_matrix 31 * @category test 32 * @copyright 2023 Andrew Lyons <andrew@nicols.co.uk> 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 trait matrix_client_test_trait { 36 public static function setUpBeforeClass(): void { 37 parent::setUpBeforeClass(); 38 39 // Ensure that the mocked client is available. 40 require_once (__DIR__ . '/fixtures/mocked_matrix_client.php'); 41 } 42 43 public function setUp(): void { 44 parent::setUp(); 45 46 // Reset the test client. 47 mocked_matrix_client::reset_client(); 48 } 49 50 public function tearDown(): void { 51 parent::tearDown(); 52 53 // Reset the test client. 54 mocked_matrix_client::reset_client(); 55 } 56 57 /** 58 * Get a mocked instance for a specific Matrix API version, 59 * 60 * @param string $version 61 * @param array $historycontainer An array which will be filled with history for the mocked client. 62 * @param MockHandler|null $mock A MockHandler object that can be appended to 63 * @return matrix_client 64 */ 65 protected function get_mocked_instance_for_version( 66 string $version, 67 array &$historycontainer = [], 68 ?MockHandler $mock = null, 69 ): matrix_client { 70 if ($mock === null) { 71 $mock = new MockHandler(); 72 } 73 // Add the version response. 74 $mock->append($this->get_mocked_version_response([$version])); 75 76 $handlerstack = HandlerStack::create($mock); 77 $history = Middleware::history($historycontainer); 78 $handlerstack->push($history); 79 $client = new http_client(['handler' => $handlerstack]); 80 mocked_matrix_client::set_client($client); 81 82 $client = mocked_matrix_client::instance( 83 'https://example.com', 84 'testtoken', 85 ); 86 87 // Remove the request that is required to fetch the version from the history. 88 array_shift($historycontainer); 89 90 return $client; 91 } 92 93 /** 94 * Get a mocked response for the /versions well-known URI. 95 * 96 * @param array|null $versions 97 * @param array|null $unstablefeatures 98 * @return Response 99 */ 100 protected function get_mocked_version_response( 101 array $versions = null, 102 array $unstablefeatures = null, 103 ): Response { 104 $data = (object) [ 105 "versions" => array_values(self::get_current_versions()), 106 "unstable_features" => self::get_current_unstable_features(), 107 ]; 108 109 if ($versions) { 110 $data->versions = array_values($versions); 111 } 112 113 if ($unstablefeatures) { 114 $data->unstable_features = $unstablefeatures; 115 } 116 117 return new Response(200, [], json_encode($data)); 118 } 119 120 /** 121 * A helper to get the current versions returned by synapse. 122 * 123 * @return array 124 */ 125 protected static function get_current_versions(): array { 126 return [ 127 v1p1::class => "v1.1", 128 v1p2::class => "v1.2", 129 v1p3::class => "v1.3", 130 v1p4::class => "v1.4", 131 v1p5::class => "v1.5", 132 v1p6::class => "v1.6", 133 v1p7::class => "v1.7", 134 ]; 135 } 136 137 /** 138 * A helper to get the current unstable features returned by synapse. 139 * @return array 140 */ 141 protected static function get_current_unstable_features(): array { 142 return [ 143 "org.matrix.label_based_filtering" => true, 144 "org.matrix.e2e_cross_signing" => true, 145 "org.matrix.msc2432" => true, 146 "uk.half-shot.msc2666.query_mutual_rooms" => true, 147 "io.element.e2ee_forced.public" => false, 148 "io.element.e2ee_forced.private" => false, 149 "io.element.e2ee_forced.trusted_private" => false, 150 "org.matrix.msc3026.busy_presence" => false, 151 "org.matrix.msc2285.stable" => true, 152 "org.matrix.msc3827.stable" => true, 153 "org.matrix.msc2716" => false, 154 "org.matrix.msc3440.stable" => true, 155 "org.matrix.msc3771" => true, 156 "org.matrix.msc3773" => false, 157 "fi.mau.msc2815" => false, 158 "fi.mau.msc2659.stable" => true, 159 "org.matrix.msc3882" => false, 160 "org.matrix.msc3881" => false, 161 "org.matrix.msc3874" => false, 162 "org.matrix.msc3886" => false, 163 "org.matrix.msc3912" => false, 164 "org.matrix.msc3952_intentional_mentions" => false, 165 "org.matrix.msc3981" => false, 166 "org.matrix.msc3391" => false, 167 ]; 168 } 169 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body