Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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   */
  32  class googledocs_drive_content_test extends \googledocs_content_testcase {
  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,
  47              array $shareddrives, array $drivecontents, array $expected) {
  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.
  66          $servicemock->expects($this->exactly(2))
  67              ->method('call')
  68              ->withConsecutive(
  69                  [
  70                      'shared_drives_list',
  71                      [],
  72                  ],
  73                  [
  74                      'list',
  75                      $listparams,
  76                  ]
  77              )
  78              ->willReturnOnConsecutiveCalls(
  79                  (object)[
  80                      'kind' => 'drive#driveList',
  81                      'nextPageToken' => 'd838181f30b0f5',
  82                      'drives' => $shareddrives,
  83                  ],
  84                  (object)[
  85                      'files' => $drivecontents,
  86                  ]
  87              );
  88  
  89          // Set the disallowed file types (extensions).
  90          $this->disallowedextensions = $filterextensions;
  91          $drivebrowser = new googledocs_drive_content($servicemock, $path, $sortcontent);
  92          $contentnodes = $drivebrowser->get_content_nodes($query, [$this, 'filter']);
  93  
  94          // Assert that the returned array of repository content nodes is equal to the expected one.
  95          $this->assertEquals($expected, $contentnodes);
  96      }
  97  
  98      /**
  99       * Data provider for test_get_content_nodes().
 100       *
 101       * @return array
 102       */
 103      public function get_content_nodes_provider(): array {
 104  
 105          $rootid = \repository_googledocs::REPOSITORY_ROOT_ID;
 106          $mydriveid = \repository_googledocs::MY_DRIVE_ROOT_ID;
 107  
 108          return [
 109              'Folders and files exist in the drive; shared drives exist; ordering applied.' =>
 110                  [
 111                      $mydriveid,
 112                      "{$rootid}|Google+Drive/{$mydriveid}|My+Drive",
 113                      true,
 114                      [],
 115                      [
 116                          $this->create_google_drive_shared_drive_object('d85b21c0f86cb5', 'Shared Drive 1'),
 117                      ],
 118                      [
 119                          $this->create_google_drive_folder_object('1c4ad262c65333', 'Folder 2'),
 120                          $this->create_google_drive_file_object('d85b21c0f86cb0', 'File 3.pdf',
 121                              'application/pdf', 'pdf', '1000'),
 122                          $this->create_google_drive_folder_object('0c4ad262c65333', 'Folder 1'),
 123                          $this->create_google_drive_file_object('bed5a0f08d412a', 'File 1.pdf',
 124                              'application/pdf', 'pdf'),
 125                      ],
 126                      [
 127                          $this->create_folder_content_node_array('0c4ad262c65333', 'Folder 1',
 128                              "{$rootid}|Google+Drive/{$mydriveid}|My+Drive"),
 129                          $this->create_folder_content_node_array('1c4ad262c65333', 'Folder 2',
 130                              "{$rootid}|Google+Drive/{$mydriveid}|My+Drive"),
 131                          $this->create_file_content_node_array('bed5a0f08d412a', 'File 1.pdf', 'File 1.pdf',
 132                              null, '', 'https://googleusercontent.com/type/application/pdf', '',
 133                              'download'),
 134                          $this->create_file_content_node_array('d85b21c0f86cb0', 'File 3.pdf', 'File 3.pdf',
 135                              '1000', '', 'https://googleusercontent.com/type/application/pdf', '',
 136                              'download'),
 137                      ],
 138                  ],
 139              'Only folders exist in the drive; shared drives do not exist; ordering not applied.' =>
 140                  [
 141                      $mydriveid,
 142                      "{$rootid}|Google+Drive/{$mydriveid}|My+Drive",
 143                      false,
 144                      [],
 145                      [],
 146                      [
 147                          $this->create_google_drive_folder_object('0c4ad262c65333', 'Folder 3'),
 148                          $this->create_google_drive_folder_object('d85b21c0f86cb0', 'Folder 1'),
 149                          $this->create_google_drive_folder_object('bed5a0f08d412a', 'Folder 2'),
 150                      ],
 151                      [
 152                          $this->create_folder_content_node_array('0c4ad262c65333', 'Folder 3',
 153                              "{$rootid}|Google+Drive/{$mydriveid}|My+Drive"),
 154                          $this->create_folder_content_node_array('d85b21c0f86cb0', 'Folder 1',
 155                              "{$rootid}|Google+Drive/{$mydriveid}|My+Drive"),
 156                          $this->create_folder_content_node_array('bed5a0f08d412a', 'Folder 2',
 157                              "{$rootid}|Google+Drive/{$mydriveid}|My+Drive"),
 158                      ],
 159                  ],
 160              'Only files exist in the drive; shared drives do not exist; ordering not applied; filter .txt.' =>
 161                  [
 162                      $mydriveid,
 163                      "{$rootid}|Google+Drive/{$mydriveid}|My+Drive",
 164                      false,
 165                      ['txt'],
 166                      [],
 167                      [
 168                          $this->create_google_drive_file_object('d85b21c0f86cb0', 'File 3.pdf',
 169                              'application/pdf', 'pdf', '1000'),
 170                          $this->create_google_drive_file_object('a85b21c0f86cb0', 'File 1.txt',
 171                              'text/plain', 'txt', '3000'),
 172                          $this->create_google_drive_file_object('f85b21c0f86cb0', 'File 2.doc',
 173                              'application/msword', 'doc', '2000'),
 174                      ],
 175                      [
 176                          $this->create_file_content_node_array('d85b21c0f86cb0', 'File 3.pdf', 'File 3.pdf',
 177                              '1000', '', 'https://googleusercontent.com/type/application/pdf', '',
 178                              'download'),
 179                          $this->create_file_content_node_array('f85b21c0f86cb0', 'File 2.doc', 'File 2.doc',
 180                              '2000', '', 'https://googleusercontent.com/type/application/msword', '',
 181                              'download'),
 182                      ],
 183                  ],
 184              'Contents do not exist in the drive; shared drives do not exist.' =>
 185                  [
 186                      $mydriveid,
 187                      "{$rootid}|Google+Drive/{$mydriveid}|My+Drive",
 188                      false,
 189                      [],
 190                      [],
 191                      [],
 192                      [],
 193                  ],
 194          ];
 195      }
 196  
 197      /**
 198       * Test get_navigation().
 199       *
 200       * @dataProvider get_navigation_provider
 201       * @param string $nodepath The node path string
 202       * @param array $expected The expected array containing the repository navigation nodes
 203       */
 204      public function test_get_navigation(string $nodepath, array $expected) {
 205          // Mock the service object.
 206          $servicemock = $this->createMock(\repository_googledocs\rest::class);
 207  
 208          $drivebrowser = new googledocs_drive_content($servicemock, $nodepath);
 209          $navigation = $drivebrowser->get_navigation();
 210  
 211          // Assert that the returned array containing the navigation nodes is equal to the expected one.
 212          $this->assertEquals($expected, $navigation);
 213      }
 214  
 215      /**
 216       * Data provider for test_get_navigation().
 217       *
 218       * @return array
 219       */
 220      public function get_navigation_provider(): array {
 221  
 222          $rootid = \repository_googledocs::REPOSITORY_ROOT_ID;
 223          $mydriveid = \repository_googledocs::MY_DRIVE_ROOT_ID;
 224  
 225          return [
 226              'Return navigation nodes array from path where all nodes have a name.' =>
 227                  [
 228                      "{$rootid}|Google+Drive/{$mydriveid}|My+Drive/bed5a0f08d|Test+Folder",
 229                      [
 230                          [
 231                              'name' => 'Google Drive',
 232                              'path' => "{$rootid}|Google+Drive",
 233                          ],
 234                          [
 235                              'name' => 'My Drive',
 236                              'path' => "{$rootid}|Google+Drive/{$mydriveid}|My+Drive",
 237                          ],
 238                          [
 239                              'name' => 'Test Folder',
 240                              'path' => "{$rootid}|Google+Drive/{$mydriveid}|My+Drive/bed5a0f08d|Test+Folder",
 241                          ],
 242                      ],
 243                  ],
 244              'Return navigation nodes array from path where some nodes do not have a name.' =>
 245                  [
 246                      "{$rootid}|Google+Drive/{$mydriveid}|My+Drive/bed5a0f08d/d85b21c0f8|Test+Folder",
 247                      [
 248                          [
 249                              'name' => 'Google Drive',
 250                              'path' => "{$rootid}|Google+Drive",
 251                          ],
 252                          [
 253                              'name' => 'My Drive',
 254                              'path' => "{$rootid}|Google+Drive/{$mydriveid}|My+Drive",
 255                          ],
 256                          [
 257                              'name' => 'bed5a0f08d',
 258                              'path' => "{$rootid}|Google+Drive/{$mydriveid}|My+Drive/bed5a0f08d|bed5a0f08d",
 259                          ],
 260                          [
 261                              'name' => 'Test Folder',
 262                              'path' => "{$rootid}|Google+Drive/{$mydriveid}|My+Drive/bed5a0f08d|bed5a0f08d/" .
 263                                  "d85b21c0f8|Test+Folder",
 264                          ],
 265                      ],
 266                  ],
 267          ];
 268      }
 269  }