Proyectos de Subversion Moodle

Rev

Rev 11 | | 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 repository_googledocs\local\browser;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once($CFG->dirroot . '/repository/googledocs/tests/googledocs_content_testcase.php');
23
require_once($CFG->dirroot . '/repository/googledocs/lib.php');
24
 
25
/**
26
 * Class containing unit tests for the drive browser class.
27
 *
28
 * @package    repository_googledocs
29
 * @copyright  2021 Mihail Geshoski <mihail@moodle.com>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
1441 ariadna 32
final class googledocs_drive_content_test extends \googledocs_content_testcase {
1 efrain 33
 
34
    /**
35
     * Test get_content_nodes().
36
     *
37
     * @dataProvider get_content_nodes_provider
38
     * @param string $query The query string
39
     * @param string $path The path
40
     * @param bool $sortcontent Whether the contents should be sorted in alphabetical order
41
     * @param array $filterextensions The array containing file extensions that should be disallowed (filtered)
42
     * @param array $shareddrives The array containing the existing shared drives
43
     * @param array $drivecontents The array containing the fetched google drive contents
44
     * @param array $expected The expected array which contains the generated repository content nodes
45
     */
46
    public function test_get_content_nodes(string $query, string $path, bool $sortcontent, array $filterextensions,
11 efrain 47
            array $shareddrives, array $drivecontents, array $expected): void {
1 efrain 48
 
49
        // Mock the service object.
50
        $servicemock = $this->createMock(\repository_googledocs\rest::class);
51
 
52
        $listparams = [
53
            'q' => "'" . str_replace("'", "\'", $query) . "' in parents AND trashed = false",
54
            'fields' => 'files(id,name,mimeType,webContentLink,webViewLink,fileExtension,modifiedTime,size,iconLink)',
55
            'spaces' => 'drive',
56
        ];
57
 
58
        if (!empty($shareddrives)) {
59
            $listparams['supportsAllDrives'] = 'true';
60
            $listparams['includeItemsFromAllDrives'] = 'true';
61
        }
62
 
63
        // Assert that the call() method is being called twice with the given arguments consecutively. In the first
64
        // instance it is being called to fetch the shared drives (shared_drives_list), while in the second instance
65
        // to fetch the relevant drive contents (list). Also, define the returned data objects by these calls.
1441 ariadna 66
        $callinvocations = $this->exactly(2);
67
        $servicemock->expects($callinvocations)
1 efrain 68
            ->method('call')
1441 ariadna 69
            ->willReturnCallback(function(string $method, array $params) use (
70
                $callinvocations,
71
                $shareddrives,
72
                $listparams,
73
                $drivecontents,
74
            ) {
75
                switch (self::getInvocationCount($callinvocations)) {
76
                    case 1:
77
                        $this->assertEquals('shared_drives_list', $method);
78
                        $this->assertEquals([], $params);
1 efrain 79
 
1441 ariadna 80
                        return (object) [
81
                            'kind' => 'drive#driveList',
82
                            'nextPageToken' => 'd838181f30b0f5',
83
                            'drives' => $shareddrives,
84
                        ];
85
                    case 2:
86
                        $this->assertEquals('list', $method);
87
                        $this->assertEquals($listparams, $params);
88
                        return (object)[
89
                            'files' => $drivecontents,
90
                        ];
91
                    default:
92
                        $this->fail('Unexpected call to the call() method.');
93
                }
94
            });
95
 
1 efrain 96
        // Set the disallowed file types (extensions).
97
        $this->disallowedextensions = $filterextensions;
98
        $drivebrowser = new googledocs_drive_content($servicemock, $path, $sortcontent);
99
        $contentnodes = $drivebrowser->get_content_nodes($query, [$this, 'filter']);
100
 
101
        // Assert that the returned array of repository content nodes is equal to the expected one.
102
        $this->assertEquals($expected, $contentnodes);
103
    }
104
 
105
    /**
106
     * Data provider for test_get_content_nodes().
107
     *
108
     * @return array
109
     */
1441 ariadna 110
    public static function get_content_nodes_provider(): array {
1 efrain 111
        $rootid = \repository_googledocs::REPOSITORY_ROOT_ID;
112
        $mydriveid = \repository_googledocs::MY_DRIVE_ROOT_ID;
113
 
114
        return [
115
            'Folders and files exist in the drive; shared drives exist; ordering applied.' =>
116
                [
117
                    $mydriveid,
118
                    "{$rootid}|Google+Drive/{$mydriveid}|My+Drive",
119
                    true,
120
                    [],
121
                    [
1441 ariadna 122
                        self::create_google_drive_shared_drive_object('d85b21c0f86cb5', 'Shared Drive 1'),
1 efrain 123
                    ],
124
                    [
1441 ariadna 125
                        self::create_google_drive_folder_object('1c4ad262c65333', 'Folder 2'),
126
                        self::create_google_drive_file_object('d85b21c0f86cb0', 'File 3.pdf',
1 efrain 127
                            'application/pdf', 'pdf', '1000'),
1441 ariadna 128
                        self::create_google_drive_folder_object('0c4ad262c65333', 'Folder 1'),
129
                        self::create_google_drive_file_object('bed5a0f08d412a', 'File 1.pdf',
1 efrain 130
                            'application/pdf', 'pdf'),
131
                    ],
132
                    [
1441 ariadna 133
                        self::create_folder_content_node_array('0c4ad262c65333', 'Folder 1',
1 efrain 134
                            "{$rootid}|Google+Drive/{$mydriveid}|My+Drive"),
1441 ariadna 135
                        self::create_folder_content_node_array('1c4ad262c65333', 'Folder 2',
1 efrain 136
                            "{$rootid}|Google+Drive/{$mydriveid}|My+Drive"),
1441 ariadna 137
                        self::create_file_content_node_array('bed5a0f08d412a', 'File 1.pdf', 'File 1.pdf',
1 efrain 138
                            null, '', 'https://googleusercontent.com/type/application/pdf', '',
139
                            'download'),
1441 ariadna 140
                        self::create_file_content_node_array('d85b21c0f86cb0', 'File 3.pdf', 'File 3.pdf',
1 efrain 141
                            '1000', '', 'https://googleusercontent.com/type/application/pdf', '',
142
                            'download'),
143
                    ],
144
                ],
145
            'Only folders exist in the drive; shared drives do not exist; ordering not applied.' =>
146
                [
147
                    $mydriveid,
148
                    "{$rootid}|Google+Drive/{$mydriveid}|My+Drive",
149
                    false,
150
                    [],
151
                    [],
152
                    [
1441 ariadna 153
                        self::create_google_drive_folder_object('0c4ad262c65333', 'Folder 3'),
154
                        self::create_google_drive_folder_object('d85b21c0f86cb0', 'Folder 1'),
155
                        self::create_google_drive_folder_object('bed5a0f08d412a', 'Folder 2'),
1 efrain 156
                    ],
157
                    [
1441 ariadna 158
                        self::create_folder_content_node_array('0c4ad262c65333', 'Folder 3',
1 efrain 159
                            "{$rootid}|Google+Drive/{$mydriveid}|My+Drive"),
1441 ariadna 160
                        self::create_folder_content_node_array('d85b21c0f86cb0', 'Folder 1',
1 efrain 161
                            "{$rootid}|Google+Drive/{$mydriveid}|My+Drive"),
1441 ariadna 162
                        self::create_folder_content_node_array('bed5a0f08d412a', 'Folder 2',
1 efrain 163
                            "{$rootid}|Google+Drive/{$mydriveid}|My+Drive"),
164
                    ],
165
                ],
166
            'Only files exist in the drive; shared drives do not exist; ordering not applied; filter .txt.' =>
167
                [
168
                    $mydriveid,
169
                    "{$rootid}|Google+Drive/{$mydriveid}|My+Drive",
170
                    false,
171
                    ['txt'],
172
                    [],
173
                    [
1441 ariadna 174
                        self::create_google_drive_file_object('d85b21c0f86cb0', 'File 3.pdf',
1 efrain 175
                            'application/pdf', 'pdf', '1000'),
1441 ariadna 176
                        self::create_google_drive_file_object('a85b21c0f86cb0', 'File 1.txt',
1 efrain 177
                            'text/plain', 'txt', '3000'),
1441 ariadna 178
                        self::create_google_drive_file_object('f85b21c0f86cb0', 'File 2.doc',
1 efrain 179
                            'application/msword', 'doc', '2000'),
180
                    ],
181
                    [
1441 ariadna 182
                        self::create_file_content_node_array('d85b21c0f86cb0', 'File 3.pdf', 'File 3.pdf',
1 efrain 183
                            '1000', '', 'https://googleusercontent.com/type/application/pdf', '',
184
                            'download'),
1441 ariadna 185
                        self::create_file_content_node_array('f85b21c0f86cb0', 'File 2.doc', 'File 2.doc',
1 efrain 186
                            '2000', '', 'https://googleusercontent.com/type/application/msword', '',
187
                            'download'),
188
                    ],
189
                ],
190
            'Contents do not exist in the drive; shared drives do not exist.' =>
191
                [
192
                    $mydriveid,
193
                    "{$rootid}|Google+Drive/{$mydriveid}|My+Drive",
194
                    false,
195
                    [],
196
                    [],
197
                    [],
198
                    [],
199
                ],
200
        ];
201
    }
202
 
203
    /**
204
     * Test get_navigation().
205
     *
206
     * @dataProvider get_navigation_provider
207
     * @param string $nodepath The node path string
208
     * @param array $expected The expected array containing the repository navigation nodes
209
     */
11 efrain 210
    public function test_get_navigation(string $nodepath, array $expected): void {
1 efrain 211
        // Mock the service object.
212
        $servicemock = $this->createMock(\repository_googledocs\rest::class);
213
 
214
        $drivebrowser = new googledocs_drive_content($servicemock, $nodepath);
215
        $navigation = $drivebrowser->get_navigation();
216
 
217
        // Assert that the returned array containing the navigation nodes is equal to the expected one.
218
        $this->assertEquals($expected, $navigation);
219
    }
220
 
221
    /**
222
     * Data provider for test_get_navigation().
223
     *
224
     * @return array
225
     */
1441 ariadna 226
    public static function get_navigation_provider(): array {
1 efrain 227
 
228
        $rootid = \repository_googledocs::REPOSITORY_ROOT_ID;
229
        $mydriveid = \repository_googledocs::MY_DRIVE_ROOT_ID;
230
 
231
        return [
232
            'Return navigation nodes array from path where all nodes have a name.' =>
233
                [
234
                    "{$rootid}|Google+Drive/{$mydriveid}|My+Drive/bed5a0f08d|Test+Folder",
235
                    [
236
                        [
237
                            'name' => 'Google Drive',
238
                            'path' => "{$rootid}|Google+Drive",
239
                        ],
240
                        [
241
                            'name' => 'My Drive',
242
                            'path' => "{$rootid}|Google+Drive/{$mydriveid}|My+Drive",
243
                        ],
244
                        [
245
                            'name' => 'Test Folder',
246
                            'path' => "{$rootid}|Google+Drive/{$mydriveid}|My+Drive/bed5a0f08d|Test+Folder",
247
                        ],
248
                    ],
249
                ],
250
            'Return navigation nodes array from path where some nodes do not have a name.' =>
251
                [
252
                    "{$rootid}|Google+Drive/{$mydriveid}|My+Drive/bed5a0f08d/d85b21c0f8|Test+Folder",
253
                    [
254
                        [
255
                            'name' => 'Google Drive',
256
                            'path' => "{$rootid}|Google+Drive",
257
                        ],
258
                        [
259
                            'name' => 'My Drive',
260
                            'path' => "{$rootid}|Google+Drive/{$mydriveid}|My+Drive",
261
                        ],
262
                        [
263
                            'name' => 'bed5a0f08d',
264
                            'path' => "{$rootid}|Google+Drive/{$mydriveid}|My+Drive/bed5a0f08d|bed5a0f08d",
265
                        ],
266
                        [
267
                            'name' => 'Test Folder',
268
                            'path' => "{$rootid}|Google+Drive/{$mydriveid}|My+Drive/bed5a0f08d|bed5a0f08d/" .
269
                                "d85b21c0f8|Test+Folder",
270
                        ],
271
                    ],
272
                ],
273
        ];
274
    }
275
}