Merge branch 'MDL-80072-main' of https://github.com/andrewnicols/moodle
[moodle.git] / repository / googledocs / tests / repository_googledocs_testcase.php
blob8b9c0707b1d9348d56b2bef2c04b9090ef54f158
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.
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/>.
17 /**
18 * Base class for the googledoc repository unit tests.
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
24 abstract class repository_googledocs_testcase extends \advanced_testcase {
26 /**
27 * Create an array that would replicate the structure of a repository folder node array.
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
35 protected function create_folder_content_node_array(string $id, string $name, string $path,
36 string $modified = ''): array {
38 global $CFG, $OUTPUT;
40 return [
41 'id' => $id,
42 'title' => $name,
43 'path' => repository_googledocs\helper::build_node_path($id, $name, $path),
44 'date' => $modified,
45 'thumbnail' => "{$CFG->wwwroot}/theme/image.php/boost/core/1/" . file_folder_icon(),
46 'thumbnail_height' => 64,
47 'thumbnail_width' => 64,
48 'children' => [],
52 /**
53 * Create an array that would replicate the structure of a repository file node array.
55 * @param string $id The ID of the file
56 * @param string $name The name of the file
57 * @param string $title The title of the file node
58 * @param string|null $size The size of the file
59 * @param string $modified The date of the last modification
60 * @param string $thumbnail The thumbnail of the file
61 * @param string|null $link The external link to the file
62 * @param string|null $exportformat The export format of the file
63 * @param string|null $googledoctype The type of the Google Doc file (if applicable)
64 * @return array The repository file node array
66 protected function create_file_content_node_array(string $id, string $name, string $title, ?string $size = null,
67 string $modified = '', string $thumbnail = '' , string $link = '', string $exportformat = '',
68 ?string $googledoctype = null): array {
70 return [
71 'id' => $id,
72 'title' => $title,
73 'source' => json_encode([
74 'id' => $id,
75 'name' => $name,
76 'link' => $link,
77 'exportformat' => $exportformat,
78 'googledoctype' => $googledoctype
79 ]),
80 'date' => $modified,
81 'size' => $size,
82 'thumbnail' => $thumbnail,
83 'thumbnail_height' => 64,
84 'thumbnail_width' => 64,
88 /**
89 * Create an object that would replicate the metadata for a shared drive returned by the Google Drive API call.
91 * @param string $id The ID of the shared drive
92 * @param string $name The name of the shared drive
93 * @return \stdClass The shared drive object
95 protected function create_google_drive_shared_drive_object(string $id, string $name): \stdClass {
96 return (object)[
97 'kind' => 'drive#drive',
98 'id' => $id,
99 'name' => $name,
104 * Create an object that would replicate the metadata for a folder returned by the Google Drive API call.
106 * @param string $id The ID of the folder
107 * @param string $name The name of the folder
108 * @param string|null $modified The date of the last modification
109 * @return \stdClass The folder object
111 protected function create_google_drive_folder_object(string $id, string $name, ?string $modified = null): \stdClass {
112 return (object)[
113 'id' => $id,
114 'name' => $name,
115 'mimeType' => 'application/vnd.google-apps.folder',
116 'webViewLink' => "https://drive.google.com/drive/folders/{$id}",
117 'iconLink' => 'https://googleusercontent.com/16/type/application/vnd.google-apps.folder+shared',
118 'modifiedTime' => $modified ?? '',
123 * Create an object that would replicate the metadata for a file returned by the Google Drive API call.
125 * @param string $id The ID of the file
126 * @param string $name The name of the file
127 * @param string $mimetype The mimetype of the file
128 * @param string|null $extension The extension of the file
129 * @param string|null $size The size of the file
130 * @param string|null $modified The date of the last modification
131 * @param string|null $webcontentlink The link for downloading the content of the file
132 * @param string|null $webviewlink The link for opening the file in a browser
133 * @return \stdClass The file object
135 protected function create_google_drive_file_object(string $id, string $name, string $mimetype,
136 ?string $extension = null, ?string $size = null, ?string $modified = null, ?string $webcontentlink = null,
137 ?string $webviewlink = null): \stdClass {
139 $googledrivefile = [
140 'id' => $id,
141 'name' => $name,
142 'mimeType' => $mimetype,
143 'size' => $size ?? '',
144 'webContentLink' => $webcontentlink ?? '',
145 'webViewLink' => $webviewlink ?? '',
146 'iconLink' => "https://googleusercontent.com/type/{$mimetype}",
147 'modifiedTime' => $modified ?? '',
149 // The returned metadata might not always have the 'fileExtension' property.
150 if ($extension) {
151 $googledrivefile['fileExtension'] = $extension;
154 return (object)$googledrivefile;