Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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 search_solr;
18
 
19
/**
20
 * Solr search engine unit tests that can operate using a mock http_client and without creating a
21
 * search manager instance.
22
 *
23
 * These tests can run without the solr PHP extension.
24
 *
25
 * All 'realistic' tests of searching (e.g. index something then see if it is found by search)
26
 * require a real Solr instance for testing and should be placed in {@see engine_test}.
27
 * Tests that don't rely heavily on the real search functionality, or where we need to simulate
28
 * multiple different ways of configuring the search infrastructure, or unusual failures in
29
 * communication, may be better suited for this mock test approach.
30
 *
31
 * @package search_solr
32
 * @category test
33
 * @copyright 2024 The Open University
34
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 * @covers \search_solr\engine
36
 */
37
final class mock_engine_test extends \advanced_testcase {
38
 
39
    protected function setUp(): void {
40
        parent::setUp();
41
        $this->resetAfterTest();
42
 
43
        // Minimal configuration.
44
        set_config('server_hostname', 'host.invalid', 'search_solr');
45
        set_config('indexname', 'myindex', 'search_solr');
46
 
47
        // This is not necessary on my setup but in GitHub Actions, the server_port is set to ''
48
        // instead of default 8983.
49
        set_config('server_port', '8983', 'search_solr');
50
    }
51
 
52
    /**
53
     * Tests {@see engine::get_server_url}.
54
     */
55
    public function test_get_server_url(): void {
56
        // Basic URL.
57
        $engine = new engine();
58
        $this->assertEquals(
59
            'http://host.invalid:8983/solr/',
60
            $engine->get_server_url('')->out(false),
61
        );
62
 
63
        // Same but with specified path.
64
        $this->assertEquals(
65
            'http://host.invalid:8983/solr/twiddle',
66
            $engine->get_server_url('twiddle')->out(false),
67
        );
68
        // Slash at start of path will be stripped.
69
        $this->assertEquals(
70
            'http://host.invalid:8983/solr/twiddle',
71
            $engine->get_server_url('/twiddle')->out(false),
72
        );
73
 
74
        // Turn on https. Due to the way the port setting works, which is bad, this will still have
75
        // the default not-secure port (even though the 'default' on the setting page will now be
76
        // shown as 8443, hmm). User has to change it manually.
77
        set_config('secure', '1', 'search_solr');
78
        $engine = new engine();
79
        $this->assertEquals(
80
            'https://host.invalid:8983/solr/',
81
            $engine->get_server_url('')->out(false),
82
        );
83
 
84
        // Change port from default. User has to do this manually when enabling secure.
85
        set_config('server_port', '8443', 'search_solr');
86
        $engine = new engine();
87
        $this->assertEquals(
88
            'https://host.invalid:8443/solr/',
89
            $engine->get_server_url('')->out(false),
90
        );
91
    }
92
 
93
    /**
94
     * Tests {@see engine::get_connection_url}.
95
     */
96
    public function test_get_connection_url(): void {
97
        // Basic URL.
98
        $engine = new engine();
99
        $this->assertEquals(
100
            'http://host.invalid:8983/solr/myindex/',
101
            $engine->get_connection_url('')->out(false),
102
        );
103
    }
104
 
105
    /**
106
     * Tests {@see engine::raw_get_request()} with no auth settings.
107
     */
108
    public function test_raw_get_request_no_auth(): void {
109
        $engine = new engine();
110
 
111
        $response = $this->createStub(\Psr\Http\Message\ResponseInterface::class);
112
 
113
        // When there is no auth, there aren't many options, just timeout.
114
        $mockedclient = $this->createMock(\core\http_client::class);
115
        \core\di::set(\core\http_client::class, $mockedclient);
116
        $mockedclient->expects($this->once())->method('get')->with(
117
            'http://host.invalid:8983/solr/frog',
118
            [
119
                'connect_timeout' => 30,
120
                'read_timeout' => 30,
121
            ],
122
        )->willReturn($response);
123
        $this->assertEquals($response, $engine->raw_get_request('frog'));
124
 
125
        // Timeout can be changed in config.
126
        set_config('server_timeout', '10', 'search_solr');
127
        $engine = new engine();
128
        $mockedclient = $this->createMock(\core\http_client::class);
129
        \core\di::set(\core\http_client::class, $mockedclient);
130
        $mockedclient->expects($this->once())->method('get')->with(
131
            'http://host.invalid:8983/solr/frog',
132
            [
133
                'connect_timeout' => 10,
134
                'read_timeout' => 10,
135
            ],
136
        )->willReturn($response);
137
        $this->assertEquals($response, $engine->raw_get_request('frog'));
138
    }
139
 
140
    /**
141
     * Tests {@see engine::raw_get_request()} with basic auth settings.
142
     */
143
    public function test_raw_get_request_basic_auth(): void {
144
        set_config('server_username', 'u', 'search_solr');
145
        set_config('server_password', 'p', 'search_solr');
146
        $engine = new engine();
147
 
148
        $response = $this->createStub(\Psr\Http\Message\ResponseInterface::class);
149
 
150
        // Basic auth works with an 'auth' option.
151
        $mockedclient = $this->createMock(\core\http_client::class);
152
        \core\di::set(\core\http_client::class, $mockedclient);
153
        $mockedclient->expects($this->once())->method('get')->with(
154
            'http://host.invalid:8983/solr/frog',
155
            [
156
                'connect_timeout' => 30,
157
                'read_timeout' => 30,
158
                'auth' => ['u', 'p'],
159
            ],
160
        )->willReturn($response);
161
        $this->assertEquals($response, $engine->raw_get_request('frog'));
162
    }
163
 
164
    /**
165
     * Tests {@see engine::raw_get_request()} with a supplied user certificate.
166
     */
167
    public function test_raw_get_request_user_cert(): void {
168
        set_config('secure', '1', 'search_solr');
169
        set_config('ssl_cert', '/tmp/cert.pem', 'search_solr');
170
        $engine = new engine();
171
 
172
        $response = $this->createStub(\Psr\Http\Message\ResponseInterface::class);
173
 
174
        // User cert auth uses the 'cert' parameter, with or without a key.
175
        $mockedclient = $this->createMock(\core\http_client::class);
176
        \core\di::set(\core\http_client::class, $mockedclient);
177
        $mockedclient->expects($this->once())->method('get')->with(
178
            'https://host.invalid:8983/solr/frog',
179
            [
180
                'connect_timeout' => 30,
181
                'read_timeout' => 30,
182
                'cert' => '/tmp/cert.pem',
183
            ],
184
        )->willReturn($response);
185
        $this->assertEquals($response, $engine->raw_get_request('frog'));
186
    }
187
 
188
    /**
189
     * Tests {@see engine::raw_get_request()} with a user key (with or without password).
190
     */
191
    public function test_raw_get_request_user_key(): void {
192
        set_config('secure', '1', 'search_solr');
193
        set_config('ssl_key', '/tmp/key.pem', 'search_solr');
194
        $engine = new engine();
195
 
196
        $response = $this->createStub(\Psr\Http\Message\ResponseInterface::class);
197
 
198
        // User cert auth uses the 'cert' parameter, with or without a key.
199
        $mockedclient = $this->createMock(\core\http_client::class);
200
        \core\di::set(\core\http_client::class, $mockedclient);
201
        $mockedclient->expects($this->once())->method('get')->with(
202
            'https://host.invalid:8983/solr/frog',
203
            [
204
                'connect_timeout' => 30,
205
                'read_timeout' => 30,
206
                'ssl_key' => '/tmp/key.pem',
207
            ],
208
        )->willReturn($response);
209
        $this->assertEquals($response, $engine->raw_get_request('frog'));
210
 
211
        set_config('ssl_keypassword', 'frog', 'search_solr');
212
        $engine = new engine();
213
        $mockedclient = $this->createMock(\core\http_client::class);
214
        \core\di::set(\core\http_client::class, $mockedclient);
215
        $mockedclient->expects($this->once())->method('get')->with(
216
            'https://host.invalid:8983/solr/frog',
217
            [
218
                'connect_timeout' => 30,
219
                'read_timeout' => 30,
220
                'ssl_key' => ['/tmp/key.pem', 'frog'],
221
            ],
222
        )->willReturn($response);
223
        $this->assertEquals($response, $engine->raw_get_request('frog'));
224
    }
225
 
226
    /**
227
     * Tests {@see engine::raw_get_request()} with a certificate bundle for verifying the server.
228
     */
229
    public function test_raw_get_request_certificate_bundle(): void {
230
        set_config('secure', '1', 'search_solr');
231
        set_config('ssl_cainfo', '/tmp/allthecerts.pem', 'search_solr');
232
        $engine = new engine();
233
 
234
        $response = $this->createStub(\Psr\Http\Message\ResponseInterface::class);
235
 
236
        // User cert auth uses the 'cert' parameter, with or without a key.
237
        $mockedclient = $this->createMock(\core\http_client::class);
238
        \core\di::set(\core\http_client::class, $mockedclient);
239
        $mockedclient->expects($this->once())->method('get')->with(
240
            'https://host.invalid:8983/solr/frog',
241
            [
242
                'connect_timeout' => 30,
243
                'read_timeout' => 30,
244
                'verify' => '/tmp/allthecerts.pem',
245
            ],
246
        )->willReturn($response);
247
        $this->assertEquals($response, $engine->raw_get_request('frog'));
248
    }
249
 
250
    /**
251
     * Tests {@see engine::raw_get_request()} with a certificate folder for verifying the server.
252
     * Guzzle doesn't support a certificate folder (curl does) so this code makes a bundle in the
253
     * localcache area.
254
     */
255
    public function test_raw_get_request_certificate_folder(): void {
256
        global $CFG;
257
 
258
        // Make a directory full of fake .pem files.
259
        $temp = make_request_directory();
260
        file_put_contents($temp . '/0.pem', "PEM0\n");
261
        file_put_contents($temp . '/1.pem', "PEM1\n");
262
        file_put_contents($temp . '/2.txt', "TXT2\n");
263
 
264
        set_config('secure', '1', 'search_solr');
265
        set_config('ssl_capath', $temp, 'search_solr');
266
 
267
        $response = $this->createStub(\Psr\Http\Message\ResponseInterface::class);
268
 
269
        // Party like it's 13 February 2009.
270
        $time = 1234567890;
271
        $this->mock_clock_with_frozen($time);
272
 
273
        // User cert auth uses the 'cert' parameter, with or without a key.
274
        $mockedclient = $this->createMock(\core\http_client::class);
275
        \core\di::set(\core\http_client::class, $mockedclient);
276
 
277
        // The filename is the hash of the capath setting plus current time.
278
        $combinedfile = $CFG->dataroot .
279
            '/localcache/search_solr/capath.' .
280
            sha1($temp) .
281
            '.1234567890';
282
 
283
        $mockedclient->expects($this->once())->method('get')->with(
284
            'https://host.invalid:8983/solr/frog',
285
            [
286
                'connect_timeout' => 30,
287
                'read_timeout' => 30,
288
                'verify' => $combinedfile,
289
            ],
290
        )->willReturn($response);
291
        $engine = new engine();
292
        $this->assertEquals($response, $engine->raw_get_request('frog'));
293
 
294
        // Check the file actually is the .pem files concatenated.
295
        $this->assertEquals("PEM0\n\n\nPEM1\n\n\n", file_get_contents($combinedfile));
296
 
297
        // Let's add another .pem file.
298
        file_put_contents($temp . '/3.pem', "PEM3\n");
299
 
300
        // 9 minutes 59 seconds later, it will still use the cached version (same file).
301
        $time += 599;
302
        $this->mock_clock_with_frozen($time);
303
 
304
        $mockedclient = $this->createMock(\core\http_client::class);
305
        \core\di::set(\core\http_client::class, $mockedclient);
306
        $mockedclient->expects($this->once())->method('get')->with(
307
            'https://host.invalid:8983/solr/frog',
308
            [
309
                'connect_timeout' => 30,
310
                'read_timeout' => 30,
311
                'verify' => $combinedfile,
312
            ],
313
        )->willReturn($response);
314
        $engine = new engine();
315
        $this->assertEquals($response, $engine->raw_get_request('frog'));
316
 
317
        $this->assertEquals("PEM0\n\n\nPEM1\n\n\n", file_get_contents($combinedfile));
318
 
319
        // 10 minutes later, it will make a new cached version.
320
        $time += 1;
321
        $this->mock_clock_with_frozen($time);
322
 
323
        $combinedfile2 = $CFG->dataroot .
324
            '/localcache/search_solr/capath.' .
325
            sha1($temp) .
326
            '.1234568490';
327
 
328
        $mockedclient = $this->createMock(\core\http_client::class);
329
        \core\di::set(\core\http_client::class, $mockedclient);
330
        $mockedclient->expects($this->once())->method('get')->with(
331
            'https://host.invalid:8983/solr/frog',
332
            [
333
                'connect_timeout' => 30,
334
                'read_timeout' => 30,
335
                'verify' => $combinedfile2,
336
            ],
337
        )->willReturn($response);
338
        $engine = new engine();
339
        $this->assertEquals($response, $engine->raw_get_request('frog'));
340
 
341
        $this->assertEquals("PEM0\n\n\nPEM1\n\n\nPEM3\n\n\n", file_get_contents($combinedfile2));
342
 
343
        // The old file is still there.
344
        $this->assertEquals("PEM0\n\n\nPEM1\n\n\n", file_get_contents($combinedfile));
345
 
346
        // Go another minute. We're still using the same combined file...
347
        $time += 60;
348
        $this->mock_clock_with_frozen($time);
349
 
350
        $mockedclient = $this->createMock(\core\http_client::class);
351
        \core\di::set(\core\http_client::class, $mockedclient);
352
        $mockedclient->expects($this->once())->method('get')->with(
353
            'https://host.invalid:8983/solr/frog',
354
            [
355
                'connect_timeout' => 30,
356
                'read_timeout' => 30,
357
                'verify' => $combinedfile2,
358
            ],
359
        )->willReturn($response);
360
        $engine = new engine();
361
        $this->assertEquals($response, $engine->raw_get_request('frog'));
362
 
363
        $this->assertEquals("PEM0\n\n\nPEM1\n\n\nPEM3\n\n\n", file_get_contents($combinedfile2));
364
 
365
        // But now it will delete the old one.
366
        $this->assertFalse(file_exists($combinedfile));
367
    }
368
 
369
    /**
370
     * Tests the {@see engine::get_status()} function when there is an exception connecting.
371
     */
372
    public function test_get_status_exception_connecting(): void {
373
        $mockedclient = $this->createMock(\core\http_client::class);
374
        \core\di::set(\core\http_client::class, $mockedclient);
375
        $mockedclient->expects($this->once())->method('get')->with(
376
            'http://host.invalid:8983/solr/admin/cores',
377
            [
378
                'connect_timeout' => 30,
379
                'read_timeout' => 30,
380
            ],
381
        )->willThrowException(new \coding_exception('ex'));
382
        $engine = new engine();
383
        $status = $engine->get_status();
384
        $this->assertFalse($status['connected']);
385
        $this->assertFalse($status['foundcore']);
386
        $this->assertEquals(
387
            'Exception occurred: Coding error detected, it must be fixed by a programmer: ex',
388
            $status['error'],
389
        );
390
        $this->assertInstanceOf(\coding_exception::class, $status['exception']);
391
    }
392
 
393
    /**
394
     * Tests the {@see engine::get_status()} function when the server returns 404.
395
     */
396
    public function test_get_status_bad_http_status(): void {
397
        $response = $this->createStub(\Psr\Http\Message\ResponseInterface::class);
398
        $response->method('getStatusCode')->willReturn(404);
399
 
400
        $mockedclient = $this->createMock(\core\http_client::class);
401
        \core\di::set(\core\http_client::class, $mockedclient);
402
        $mockedclient->expects($this->once())->method('get')->with(
403
            'http://host.invalid:8983/solr/admin/cores',
404
            [
405
                'connect_timeout' => 30,
406
                'read_timeout' => 30,
407
            ],
408
        )->willReturn($response);
409
        $engine = new engine();
410
        $status = $engine->get_status();
411
        $this->assertFalse($status['connected']);
412
        $this->assertFalse($status['foundcore']);
413
        $this->assertEquals('Unsuccessful status code: 404', $status['error']);
414
    }
415
 
416
    /**
417
     * Creates a mock ResponseInterface with a body containing the specified string.
418
     *
419
     * @param string $body Body content
420
     * @return \Psr\Http\Message\ResponseInterface Interface
421
     */
422
    protected function get_fake_response(string $body): \Psr\Http\Message\ResponseInterface {
423
        $response = $this->createStub(\Psr\Http\Message\ResponseInterface::class);
424
        $response->method('getStatusCode')->willReturn(200);
425
        $stream = $this->createStub(\Psr\Http\Message\StreamInterface::class);
426
        $response->method('getBody')->willReturn($stream);
427
        $stream->method('getContents')->willReturn($body);
428
        return $response;
429
    }
430
 
431
    /**
432
     * Tests the {@see engine::get_status()} function when the server returns invalid JSON.
433
     * In real life this would only be likely to happen if the server is down and a load balancer
434
     * in front of it for some crazy reason interposes a page with status 200.
435
     */
436
    public function test_get_status_not_json(): void {
437
        $response = $this->get_fake_response('notjson');
438
 
439
        $mockedclient = $this->createMock(\core\http_client::class);
440
        \core\di::set(\core\http_client::class, $mockedclient);
441
        $mockedclient->expects($this->once())->method('get')->with(
442
            'http://host.invalid:8983/solr/admin/cores',
443
            [
444
                'connect_timeout' => 30,
445
                'read_timeout' => 30,
446
            ],
447
        )->willReturn($response);
448
        $engine = new engine();
449
        $status = $engine->get_status();
450
        $this->assertFalse($status['connected']);
451
        $this->assertFalse($status['foundcore']);
452
        $this->assertEquals('Invalid JSON', $status['error']);
453
    }
454
 
455
    /**
456
     * Tests the {@see engine::get_status()} function when the server returns an empty response.
457
     *
458
     * This could maybe happen if the server has been configured, but not fully initialised.
459
     */
460
    public function test_get_status_no_cores(): void {
461
        $response = $this->get_fake_response('{}');
462
 
463
        $mockedclient = $this->createMock(\core\http_client::class);
464
        \core\di::set(\core\http_client::class, $mockedclient);
465
        $mockedclient->expects($this->once())->method('get')->with(
466
            'http://host.invalid:8983/solr/admin/cores',
467
            [
468
                'connect_timeout' => 30,
469
                'read_timeout' => 30,
470
            ],
471
        )->willReturn($response);
472
        $engine = new engine();
473
        $status = $engine->get_status();
474
        $this->assertTrue($status['connected']);
475
        $this->assertFalse($status['foundcore']);
476
        $this->assertEquals('Unexpected JSON: no core status', $status['error']);
477
    }
478
 
479
    /**
480
     * Tests the {@see engine::get_status()} function when the server returns a core without a name
481
     * we can read.
482
     *
483
     * In real usage this should only happen if the Solr REST interface changes unexpectedly.
484
     */
485
    public function test_get_status_core_no_name(): void {
486
        // A core with no name (in its 'name' field, the 'frog' key is ignored).
487
        $response = $this->get_fake_response('{"status":{"frog":{}}}');
488
 
489
        $mockedclient = $this->createMock(\core\http_client::class);
490
        \core\di::set(\core\http_client::class, $mockedclient);
491
        $mockedclient->expects($this->once())->method('get')->with(
492
            'http://host.invalid:8983/solr/admin/cores',
493
            [
494
                'connect_timeout' => 30,
495
                'read_timeout' => 30,
496
            ],
497
        )->willReturn($response);
498
        $engine = new engine();
499
        $status = $engine->get_status();
500
        $this->assertTrue($status['connected']);
501
        $this->assertFalse($status['foundcore']);
502
        $this->assertEquals('Unexpected JSON: core has no name', $status['error']);
503
    }
504
 
505
    /**
506
     * Tests the {@see engine::get_status()} function when the server doesn't return status for a
507
     * core that matches the index name in Moodle config.
508
     *
509
     * In real usage this could happen if the index got wiped from search or something.
510
     */
511
    public function test_get_status_no_matching_core(): void {
512
        // Core is not the one we're looking for.
513
        $response = $this->get_fake_response('{"status":{"frog":{"name":"frog"}}}');
514
 
515
        $mockedclient = $this->createMock(\core\http_client::class);
516
        \core\di::set(\core\http_client::class, $mockedclient);
517
        $mockedclient->expects($this->once())->method('get')->with(
518
            'http://host.invalid:8983/solr/admin/cores',
519
            [
520
                'connect_timeout' => 30,
521
                'read_timeout' => 30,
522
            ],
523
        )->willReturn($response);
524
        $engine = new engine();
525
        $status = $engine->get_status();
526
        $this->assertTrue($status['connected']);
527
        $this->assertFalse($status['foundcore']);
528
        $this->assertEquals('Could not find core matching myindex', $status['error']);
529
    }
530
 
531
    /**
532
     * Tests the {@see engine::get_status()} function when the server returns a core without index
533
     * information.
534
     *
535
     * In real usage this should only happen if the Solr REST interface changes unexpectedly. There
536
     * is a parameter to not receive index information, but we don't use it.
537
     */
538
    public function test_get_status_core_no_index(): void {
539
        // Core exists but has no index object.
540
        $response = $this->get_fake_response('{"status":{"myindex":{"name":"myindex"}}}');
541
 
542
        $mockedclient = $this->createMock(\core\http_client::class);
543
        \core\di::set(\core\http_client::class, $mockedclient);
544
        $mockedclient->expects($this->once())->method('get')->with(
545
            'http://host.invalid:8983/solr/admin/cores',
546
            [
547
                'connect_timeout' => 30,
548
                'read_timeout' => 30,
549
            ],
550
        )->willReturn($response);
551
        $engine = new engine();
552
        $status = $engine->get_status();
553
        $this->assertTrue($status['connected']);
554
        $this->assertTrue($status['foundcore']);
555
        $this->assertEquals('Unexpected JSON: core has no index', $status['error']);
556
    }
557
 
558
    /**
559
     * Tests the {@see engine::get_status()} function when the server returns index information
560
     * without size.
561
     *
562
     * In real usage this should only happen if the Solr REST interface changes unexpectedly.
563
     */
564
    public function test_get_status_core_index_no_size(): void {
565
        // Core index objects doesn't have a size.
566
        $response = $this->get_fake_response('{"status":{"myindex":{"name":"myindex","index":{}}}}');
567
 
568
        $mockedclient = $this->createMock(\core\http_client::class);
569
        \core\di::set(\core\http_client::class, $mockedclient);
570
        $mockedclient->expects($this->once())->method('get')->with(
571
            'http://host.invalid:8983/solr/admin/cores',
572
            [
573
                'connect_timeout' => 30,
574
                'read_timeout' => 30,
575
            ],
576
        )->willReturn($response);
577
        $engine = new engine();
578
        $status = $engine->get_status();
579
        $this->assertTrue($status['connected']);
580
        $this->assertTrue($status['foundcore']);
581
        $this->assertEquals('Unexpected JSON: core index has no sizeInBytes', $status['error']);
582
    }
583
 
584
    /**
585
     * Tests the {@see engine::get_status()} function when all desired data is present, using a
586
     * single-instance Solr configuration.
587
     */
588
    public function test_get_status_success_single_server(): void {
589
        // Core index complete with size.
590
        $response = $this->get_fake_response('{"status":{"myindex":{"name":"myindex",' .
591
            '"index":{"sizeInBytes":123}}}}');
592
 
593
        $mockedclient = $this->createMock(\core\http_client::class);
594
        \core\di::set(\core\http_client::class, $mockedclient);
595
        $mockedclient->expects($this->once())->method('get')->with(
596
            'http://host.invalid:8983/solr/admin/cores',
597
            [
598
                'connect_timeout' => 30,
599
                'read_timeout' => 30,
600
            ],
601
        )->willReturn($response);
602
        $engine = new engine();
603
        $status = $engine->get_status();
604
        $this->assertTrue($status['connected']);
605
        $this->assertTrue($status['foundcore']);
606
        $this->assertEquals(123, $status['indexsize']);
607
    }
608
 
609
    /**
610
     * Tests the {@see engine::get_status()} function when all desired data is present, using a
611
     * multiple-instance (SolrCloud) configuration.
612
     */
613
    public function test_get_status_success_solr_cloud(): void {
614
        // Index with size, in cloud replica. These have a different name for each node but a
615
        // 'collection' field with the original index name.
616
        $response = $this->get_fake_response('{"status":{"replica1":{"name":"replica1",' .
617
            '"cloud":{"collection":"myindex"},"index":{"sizeInBytes":123}}}}');
618
 
619
        $mockedclient = $this->createMock(\core\http_client::class);
620
        \core\di::set(\core\http_client::class, $mockedclient);
621
        $mockedclient->expects($this->once())->method('get')->with(
622
            'http://host.invalid:8983/solr/admin/cores',
623
            [
624
                'connect_timeout' => 30,
625
                'read_timeout' => 30,
626
            ],
627
        )->willReturn($response);
628
        $engine = new engine();
629
        $status = $engine->get_status();
630
        $this->assertTrue($status['connected']);
631
        $this->assertTrue($status['foundcore']);
632
        $this->assertEquals(123, $status['indexsize']);
633
    }
634
}