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 |
/**
|
|
|
18 |
* Base class for the googledoc repository unit tests.
|
|
|
19 |
*
|
|
|
20 |
* @package repository_googledocs
|
|
|
21 |
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
abstract class repository_googledocs_testcase extends \advanced_testcase {
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Create an array that would replicate the structure of a repository folder node array.
|
|
|
28 |
*
|
|
|
29 |
* @param string $id The ID of the folder
|
|
|
30 |
* @param string $name The name of the folder
|
|
|
31 |
* @param string $path The root path
|
|
|
32 |
* @param string $modified The date of the last modification
|
|
|
33 |
* @return array The repository folder node array
|
|
|
34 |
*/
|
1441 |
ariadna |
35 |
protected static function create_folder_content_node_array(
|
|
|
36 |
string $id,
|
|
|
37 |
string $name,
|
|
|
38 |
string $path,
|
|
|
39 |
string $modified = '',
|
|
|
40 |
): array {
|
1 |
efrain |
41 |
global $CFG, $OUTPUT;
|
|
|
42 |
|
|
|
43 |
return [
|
|
|
44 |
'id' => $id,
|
|
|
45 |
'title' => $name,
|
|
|
46 |
'path' => repository_googledocs\helper::build_node_path($id, $name, $path),
|
|
|
47 |
'date' => $modified,
|
|
|
48 |
'thumbnail' => "{$CFG->wwwroot}/theme/image.php/boost/core/1/" . file_folder_icon(),
|
|
|
49 |
'thumbnail_height' => 64,
|
|
|
50 |
'thumbnail_width' => 64,
|
|
|
51 |
'children' => [],
|
|
|
52 |
];
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Create an array that would replicate the structure of a repository file node array.
|
|
|
57 |
*
|
|
|
58 |
* @param string $id The ID of the file
|
|
|
59 |
* @param string $name The name of the file
|
|
|
60 |
* @param string $title The title of the file node
|
|
|
61 |
* @param string|null $size The size of the file
|
|
|
62 |
* @param string $modified The date of the last modification
|
|
|
63 |
* @param string $thumbnail The thumbnail of the file
|
|
|
64 |
* @param string|null $link The external link to the file
|
|
|
65 |
* @param string|null $exportformat The export format of the file
|
|
|
66 |
* @param string|null $googledoctype The type of the Google Doc file (if applicable)
|
|
|
67 |
* @return array The repository file node array
|
|
|
68 |
*/
|
1441 |
ariadna |
69 |
protected static function create_file_content_node_array(
|
|
|
70 |
string $id,
|
|
|
71 |
string $name,
|
|
|
72 |
string $title,
|
|
|
73 |
?string $size = null,
|
|
|
74 |
string $modified = '',
|
|
|
75 |
string $thumbnail = '',
|
|
|
76 |
string $link = '',
|
|
|
77 |
string $exportformat = '',
|
|
|
78 |
?string $googledoctype = null,
|
|
|
79 |
): array {
|
1 |
efrain |
80 |
return [
|
|
|
81 |
'id' => $id,
|
|
|
82 |
'title' => $title,
|
|
|
83 |
'source' => json_encode([
|
|
|
84 |
'id' => $id,
|
|
|
85 |
'name' => $name,
|
|
|
86 |
'link' => $link,
|
|
|
87 |
'exportformat' => $exportformat,
|
1441 |
ariadna |
88 |
'googledoctype' => $googledoctype,
|
1 |
efrain |
89 |
]),
|
|
|
90 |
'date' => $modified,
|
|
|
91 |
'size' => $size,
|
|
|
92 |
'thumbnail' => $thumbnail,
|
|
|
93 |
'thumbnail_height' => 64,
|
|
|
94 |
'thumbnail_width' => 64,
|
|
|
95 |
];
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Create an object that would replicate the metadata for a shared drive returned by the Google Drive API call.
|
|
|
100 |
*
|
|
|
101 |
* @param string $id The ID of the shared drive
|
|
|
102 |
* @param string $name The name of the shared drive
|
|
|
103 |
* @return \stdClass The shared drive object
|
|
|
104 |
*/
|
1441 |
ariadna |
105 |
protected static function create_google_drive_shared_drive_object(string $id, string $name): \stdClass {
|
|
|
106 |
return (object) [
|
1 |
efrain |
107 |
'kind' => 'drive#drive',
|
|
|
108 |
'id' => $id,
|
|
|
109 |
'name' => $name,
|
|
|
110 |
];
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Create an object that would replicate the metadata for a folder returned by the Google Drive API call.
|
|
|
115 |
*
|
|
|
116 |
* @param string $id The ID of the folder
|
|
|
117 |
* @param string $name The name of the folder
|
|
|
118 |
* @param string|null $modified The date of the last modification
|
|
|
119 |
* @return \stdClass The folder object
|
|
|
120 |
*/
|
1441 |
ariadna |
121 |
protected static function create_google_drive_folder_object(string $id, string $name, ?string $modified = null): \stdClass {
|
1 |
efrain |
122 |
return (object)[
|
|
|
123 |
'id' => $id,
|
|
|
124 |
'name' => $name,
|
|
|
125 |
'mimeType' => 'application/vnd.google-apps.folder',
|
|
|
126 |
'webViewLink' => "https://drive.google.com/drive/folders/{$id}",
|
|
|
127 |
'iconLink' => 'https://googleusercontent.com/16/type/application/vnd.google-apps.folder+shared',
|
|
|
128 |
'modifiedTime' => $modified ?? '',
|
|
|
129 |
];
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Create an object that would replicate the metadata for a file returned by the Google Drive API call.
|
|
|
134 |
*
|
|
|
135 |
* @param string $id The ID of the file
|
|
|
136 |
* @param string $name The name of the file
|
|
|
137 |
* @param string $mimetype The mimetype of the file
|
|
|
138 |
* @param string|null $extension The extension of the file
|
|
|
139 |
* @param string|null $size The size of the file
|
|
|
140 |
* @param string|null $modified The date of the last modification
|
|
|
141 |
* @param string|null $webcontentlink The link for downloading the content of the file
|
|
|
142 |
* @param string|null $webviewlink The link for opening the file in a browser
|
|
|
143 |
* @return \stdClass The file object
|
|
|
144 |
*/
|
1441 |
ariadna |
145 |
protected static function create_google_drive_file_object(
|
|
|
146 |
string $id,
|
|
|
147 |
string $name,
|
|
|
148 |
string $mimetype,
|
|
|
149 |
?string $extension = null,
|
|
|
150 |
?string $size = null,
|
|
|
151 |
?string $modified = null,
|
|
|
152 |
?string $webcontentlink = null,
|
|
|
153 |
?string $webviewlink = null,
|
|
|
154 |
): \stdClass {
|
1 |
efrain |
155 |
$googledrivefile = [
|
|
|
156 |
'id' => $id,
|
|
|
157 |
'name' => $name,
|
|
|
158 |
'mimeType' => $mimetype,
|
|
|
159 |
'size' => $size ?? '',
|
|
|
160 |
'webContentLink' => $webcontentlink ?? '',
|
|
|
161 |
'webViewLink' => $webviewlink ?? '',
|
|
|
162 |
'iconLink' => "https://googleusercontent.com/type/{$mimetype}",
|
|
|
163 |
'modifiedTime' => $modified ?? '',
|
|
|
164 |
];
|
|
|
165 |
// The returned metadata might not always have the 'fileExtension' property.
|
|
|
166 |
if ($extension) {
|
|
|
167 |
$googledrivefile['fileExtension'] = $extension;
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
return (object)$googledrivefile;
|
|
|
171 |
}
|
|
|
172 |
}
|