Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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 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 {
1441 ariadna 70
        // If no mock is provided, use get_mocked_http_client to create the mock and client.
1 efrain 71
        if ($mock === null) {
1441 ariadna 72
            ['mock' => $mock, 'client' => $client] = $this->get_mocked_http_client(
73
                history: $historycontainer
74
            );
75
        } else {
76
            // If mock is provided, create the handlerstack and history middleware.
77
            $handlerstack = HandlerStack::create($mock);
78
            $history = Middleware::history($historycontainer);
79
            $handlerstack->push($history);
80
            $client = new http_client(['handler' => $handlerstack]);
1 efrain 81
        }
82
        // Add the version response.
83
        $mock->append($this->get_mocked_version_response([$version]));
84
 
85
        mocked_matrix_client::set_client($client);
86
 
1441 ariadna 87
        $instance = mocked_matrix_client::instance(
1 efrain 88
            'https://example.com',
89
            'testtoken',
90
        );
91
 
92
        // Remove the request that is required to fetch the version from the history.
93
        array_shift($historycontainer);
94
 
1441 ariadna 95
        return $instance;
1 efrain 96
    }
97
 
98
    /**
99
     * Get a mocked response for the /versions well-known URI.
100
     *
101
     * @param array|null $versions
102
     * @param array|null $unstablefeatures
103
     * @return Response
104
     */
105
    protected function get_mocked_version_response(
1441 ariadna 106
        ?array $versions = null,
107
        ?array $unstablefeatures = null,
1 efrain 108
    ): Response {
109
        $data = (object) [
110
            "versions" => array_values(self::get_current_versions()),
111
            "unstable_features" => self::get_current_unstable_features(),
112
        ];
113
 
114
        if ($versions) {
115
            $data->versions = array_values($versions);
116
        }
117
 
118
        if ($unstablefeatures) {
119
            $data->unstable_features = $unstablefeatures;
120
        }
121
 
122
        return new Response(200, [], json_encode($data));
123
    }
124
 
125
    /**
126
     * A helper to get the current versions returned by synapse.
127
     *
128
     * @return array
129
     */
130
    protected static function get_current_versions(): array {
131
        return [
132
            v1p1::class => "v1.1",
133
            v1p2::class => "v1.2",
134
            v1p3::class => "v1.3",
135
            v1p4::class => "v1.4",
136
            v1p5::class => "v1.5",
137
            v1p6::class => "v1.6",
138
            v1p7::class => "v1.7",
139
        ];
140
    }
141
 
142
    /**
143
     * A helper to get the current unstable features returned by synapse.
144
     * @return array
145
     */
146
    protected static function get_current_unstable_features(): array {
147
        return [
148
            "org.matrix.label_based_filtering" => true,
149
            "org.matrix.e2e_cross_signing" => true,
150
            "org.matrix.msc2432" => true,
151
            "uk.half-shot.msc2666.query_mutual_rooms" => true,
152
            "io.element.e2ee_forced.public" => false,
153
            "io.element.e2ee_forced.private" => false,
154
            "io.element.e2ee_forced.trusted_private" => false,
155
            "org.matrix.msc3026.busy_presence" => false,
156
            "org.matrix.msc2285.stable" => true,
157
            "org.matrix.msc3827.stable" => true,
158
            "org.matrix.msc2716" => false,
159
            "org.matrix.msc3440.stable" => true,
160
            "org.matrix.msc3771" => true,
161
            "org.matrix.msc3773" => false,
162
            "fi.mau.msc2815" => false,
163
            "fi.mau.msc2659.stable" => true,
164
            "org.matrix.msc3882" => false,
165
            "org.matrix.msc3881" => false,
166
            "org.matrix.msc3874" => false,
167
            "org.matrix.msc3886" => false,
168
            "org.matrix.msc3912" => false,
169
            "org.matrix.msc3952_intentional_mentions" => false,
170
            "org.matrix.msc3981" => false,
171
            "org.matrix.msc3391" => false,
172
        ];
173
    }
174
}