Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 21... Línea 21...
21
 *
21
 *
22
 * @package     repository_dropbox
22
 * @package     repository_dropbox
23
 * @copyright   Andrew Nicols <andrew@nicols.co.uk>
23
 * @copyright   Andrew Nicols <andrew@nicols.co.uk>
24
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
25
 */
26
class api_test extends \advanced_testcase {
26
final class api_test extends \advanced_testcase {
27
    /**
27
    /**
28
     * Data provider for has_additional_results.
28
     * Data provider for has_additional_results.
29
     *
29
     *
30
     * @return array
30
     * @return array
31
     */
31
     */
32
    public function has_additional_results_provider() {
32
    public static function has_additional_results_provider(): array {
33
        return [
33
        return [
34
            'No more results' => [
34
            'No more results' => [
35
                (object) [
35
                (object) [
36
                    'has_more'  => false,
36
                    'has_more'  => false,
37
                    'cursor'    => '',
37
                    'cursor'    => '',
Línea 86... Línea 86...
86
    /**
86
    /**
87
     * Data provider for check_and_handle_api_errors.
87
     * Data provider for check_and_handle_api_errors.
88
     *
88
     *
89
     * @return array
89
     * @return array
90
     */
90
     */
91
    public function check_and_handle_api_errors_provider() {
91
    public static function check_and_handle_api_errors_provider(): array {
92
        return [
92
        return [
93
            '200 http_code' => [
93
            '200 http_code' => [
94
                ['http_code' => 200],
94
                ['http_code' => 200],
95
                '',
95
                '',
96
                null,
96
                null,
Línea 177... Línea 177...
177
    /**
177
    /**
178
     * Data provider for the supports_thumbnail function.
178
     * Data provider for the supports_thumbnail function.
179
     *
179
     *
180
     * @return array
180
     * @return array
181
     */
181
     */
182
    public function supports_thumbnail_provider() {
182
    public static function supports_thumbnail_provider(): array {
183
        $tests = [
183
        $tests = [
184
            'Only files support thumbnails' => [
184
            'Only files support thumbnails' => [
185
                (object) ['.tag' => 'folder'],
185
                (object) ['.tag' => 'folder'],
186
                false,
186
                false,
187
            ],
187
            ],
Línea 401... Línea 401...
401
            ])
401
            ])
402
            ->getMock();
402
            ->getMock();
Línea 403... Línea 403...
403
 
403
 
Línea 404... Línea 404...
404
        $endpoint = 'testEndpoint';
404
        $endpoint = 'testEndpoint';
405
 
-
 
406
        // We can't detect if fetch_dropbox_data was called twice because
405
 
407
        // we can'
406
        $requestinvocations = $this->exactly(3);
408
        $mock->expects($this->exactly(3))
407
        $mock->expects($requestinvocations)
-
 
408
            ->method('request')
409
            ->method('request')
409
            ->willReturnCallback(function () use ($requestinvocations): string {
410
            ->will($this->onConsecutiveCalls(
410
                return match (self::getInvocationCount($requestinvocations)) {
411
                json_encode(['has_more' => true, 'cursor' => 'Example', 'matches' => ['foo', 'bar']]),
411
                    1 => json_encode(['has_more' => true, 'cursor' => 'Example', 'matches' => ['foo', 'bar']]),
-
 
412
                    2 => json_encode(['has_more' => true, 'cursor' => 'Example', 'matches' => ['baz']]),
-
 
413
                    3 => json_encode(['has_more' => false, 'cursor' => '', 'matches' => ['bum']]),
412
                json_encode(['has_more' => true, 'cursor' => 'Example', 'matches' => ['baz']]),
414
                    default => $this->fail('Unexpected call to the call() method.'),
Línea 413... Línea 415...
413
                json_encode(['has_more' => false, 'cursor' => '', 'matches' => ['bum']])
415
                };
-
 
416
            });
414
            ));
417
 
415
 
418
        // We automatically adjust for the /continue endpoint.
416
        // We automatically adjust for the /continue endpoint.
419
        $apiinvocations = $this->exactly(3);
417
        $mock->expects($this->exactly(3))
420
        $mock->expects($apiinvocations)
-
 
421
            ->method('get_api_endpoint')
-
 
422
            ->willReturnCallback(function ($endpoint) use ($apiinvocations): string {
418
            ->method('get_api_endpoint')
423
                switch (self::getInvocationCount($apiinvocations)) {
-
 
424
                    case 1:
-
 
425
                        $this->assertEquals('testEndpoint', $endpoint);
419
            ->withConsecutive(['testEndpoint'], ['testEndpoint/continue'], ['testEndpoint/continue'])
426
                        return 'https://example.com/api/2/testEndpoint';
-
 
427
                    case 2:
-
 
428
                        $this->assertEquals('testEndpoint/continue', $endpoint);
420
            ->willReturn($this->onConsecutiveCalls(
429
                        return 'https://example.com/api/2/testEndpoint/continue';
-
 
430
                    case 3:
-
 
431
                        $this->assertEquals('testEndpoint/continue', $endpoint);
-
 
432
                        return 'https://example.com/api/2/testEndpoint/continue';
421
                'https://example.com/api/2/testEndpoint',
433
                    default:
Línea 422... Línea 434...
422
                'https://example.com/api/2/testEndpoint/continue',
434
                        $this->fail('Unexpected call to the get_api_endpoint() method.');
423
                'https://example.com/api/2/testEndpoint/continue'
435
                }
424
            ));
436
            });
425
 
437
 
Línea 466... Línea 478...
466
            ->will($this->returnValue($url));
478
            ->will($this->returnValue($url));
Línea 467... Línea 479...
467
 
479
 
468
        $mock->expects($this->never())
480
        $mock->expects($this->never())
Línea -... Línea 481...
-
 
481
            ->method('get_api_endpoint');
469
            ->method('get_api_endpoint');
482
 
470
 
483
        $headerinvocations = $this->exactly(2);
-
 
484
        $mock->expects($headerinvocations)
-
 
485
            ->method('setHeader')
471
        $mock->expects($this->exactly(2))
486
            ->willReturnCallback(function ($header) use ($data, $headerinvocations): void {
472
            ->method('setHeader')
487
                switch (self::getInvocationCount($headerinvocations)) {
-
 
488
                    case 1:
-
 
489
                        $this->assertEquals('Content-Type: ', $header);
473
            ->withConsecutive(
490
                        break;
-
 
491
                    case 2:
-
 
492
                        $this->assertEquals('Dropbox-API-Arg: ' . json_encode($data), $header);
-
 
493
                        break;
-
 
494
                    default:
474
                [$this->equalTo('Content-Type: ')],
495
                        $this->fail('Unexpected call to the setHeader() method.');
Línea 475... Línea 496...
475
                [$this->equalTo('Dropbox-API-Arg: ' . json_encode($data))]
496
                }
476
            );
497
            });
477
 
498
 
478
        // Only one request should be made, and it should forcibly be a POST.
499
        // Only one request should be made, and it should forcibly be a POST.
Línea 544... Línea 565...
544
        $id = 'LifeTheUniverseAndEverything';
565
        $id = 'LifeTheUniverseAndEverything';
545
        $file = (object) ['.tag' => 'file', 'id' => $id, 'path_lower' => 'SomeValue'];
566
        $file = (object) ['.tag' => 'file', 'id' => $id, 'path_lower' => 'SomeValue'];
546
        $sharelink = 'https://example.com/share/link';
567
        $sharelink = 'https://example.com/share/link';
Línea 547... Línea 568...
547
 
568
 
-
 
569
        // Mock fetch_dropbox_data to return an existing file.
548
        // Mock fetch_dropbox_data to return an existing file.
570
        $fetchinvocations = $this->exactly(2);
549
        $mock->expects($this->exactly(2))
571
        $mock->expects($fetchinvocations)
-
 
572
            ->method('fetch_dropbox_data')
-
 
573
            ->willReturnCallback(function ($path, $values) use ($fetchinvocations, $id, $file): object {
550
            ->method('fetch_dropbox_data')
574
                switch (self::getInvocationCount($fetchinvocations)) {
551
            ->withConsecutive(
575
                    case 1:
552
                [$this->equalTo('sharing/list_shared_links'), $this->equalTo(['path' => $id])],
576
                        $this->assertEquals('sharing/list_shared_links', $path);
553
                [$this->equalTo('sharing/create_shared_link_with_settings'), $this->equalTo([
577
                        $this->assertEquals(['path' => $id], $values);
554
                    'path' => $id,
578
                        return (object) ['links' => []];
-
 
579
                    case 2:
555
                    'settings' => [
580
                        $this->assertEquals('sharing/create_shared_link_with_settings', $path);
556
                        'requested_visibility' => 'public',
581
                        $this->assertEquals(['path' => $id, 'settings' => ['requested_visibility' => 'public']], $values);
557
                    ]
582
                        return $file;
558
                ])]
-
 
559
            )
583
                    default:
560
            ->will($this->onConsecutiveCalls(
-
 
561
                (object) ['links' => []],
584
                        $this->fail('Unexpected call to the fetch_dropbox_data() method.');
562
                $file
585
                }
Línea 563... Línea 586...
563
            ));
586
            });
564
 
587
 
565
        $mock->expects($this->once())
588
        $mock->expects($this->once())
566
            ->method('normalize_file_share_info')
589
            ->method('normalize_file_share_info')
Línea 585... Línea 608...
585
        $id = 'LifeTheUniverseAndEverything';
608
        $id = 'LifeTheUniverseAndEverything';
Línea 586... Línea 609...
586
 
609
 
587
        // Mock fetch_dropbox_data to return an existing file.
610
        // Mock fetch_dropbox_data to return an existing file.
588
        $mock->expects($this->exactly(2))
611
        $mock->expects($this->exactly(2))
-
 
612
            ->method('fetch_dropbox_data')
589
            ->method('fetch_dropbox_data')
613
            ->willReturnCallback(function ($path, $values) use ($id): ?object {
590
            ->withConsecutive(
614
                switch ($path) {
591
                [$this->equalTo('sharing/list_shared_links'), $this->equalTo(['path' => $id])],
615
                    case 'sharing/list_shared_links':
592
                [$this->equalTo('sharing/create_shared_link_with_settings'), $this->equalTo([
616
                        $this->assertEquals(['path' => $id], $values);
593
                    'path' => $id,
617
                        return (object) ['links' => []];
594
                    'settings' => [
618
                    case 'sharing/create_shared_link_with_settings':
595
                        'requested_visibility' => 'public',
619
                        $this->assertEquals(['path' => $id, 'settings' => ['requested_visibility' => 'public']], $values);
596
                    ]
620
                        return null;
597
                ])]
-
 
598
            )
621
                    default:
599
            ->will($this->onConsecutiveCalls(
-
 
600
                (object) ['links' => []],
622
                        $this->fail('Unexpected call to the fetch_dropbox_data() method.');
601
                null
623
                }
Línea 602... Línea 624...
602
            ));
624
            });
603
 
625
 
Línea 604... Línea 626...
604
        $mock->expects($this->never())
626
        $mock->expects($this->never())