Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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;
  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 search content 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_search_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 bool $sortcontent Whether the contents should be sorted in alphabetical order
  40       * @param array $filterextensions The array containing file extensions that should be disallowed (filtered)
  41       * @param array $shareddrives The array containing the existing shared drives
  42       * @param array $searccontents The array containing the fetched google drive contents that match the search criteria
  43       * @param array $expected The expected array which contains the generated repository content nodes
  44       */
  45      public function test_get_content_nodes(string $query, bool $sortcontent, array $filterextensions,
  46              array $shareddrives, array $searccontents, array $expected) {
  47  
  48          // Mock the service object.
  49          $servicemock = $this->createMock(rest::class);
  50  
  51          $searchparams = [
  52              'q' => "fullText contains '" . str_replace("'", "\'", $query) . "' AND trashed = false",
  53              'fields' => 'files(id,name,mimeType,webContentLink,webViewLink,fileExtension,modifiedTime,size,iconLink)',
  54              'spaces' => 'drive',
  55          ];
  56  
  57          if (!empty($shareddrives)) {
  58              $searchparams['supportsAllDrives'] = 'true';
  59              $searchparams['includeItemsFromAllDrives'] = 'true';
  60              $searchparams['corpora'] = 'allDrives';
  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) that match the search criteria. Also, define the returned
  66          // data objects by these calls.
  67          $servicemock->expects($this->exactly(2))
  68              ->method('call')
  69              ->withConsecutive(
  70                  [
  71                      'shared_drives_list',
  72                      [],
  73                  ],
  74                  [
  75                      'list',
  76                      $searchparams,
  77                  ]
  78              )
  79              ->willReturnOnConsecutiveCalls(
  80                  (object)[
  81                      'kind' => 'drive#driveList',
  82                      'nextPageToken' => 'd838181f30b0f5',
  83                      'drives' => $shareddrives,
  84                  ],
  85                  (object)[
  86                      'files' => $searccontents,
  87                  ]
  88              );
  89  
  90          // Construct the node path.
  91          $path = \repository_googledocs::REPOSITORY_ROOT_ID . '|' . urlencode('Google Drive') . '/' .
  92              \repository_googledocs::SEARCH_ROOT_ID . '|' . urlencode(
  93              get_string('searchfor', 'repository_googledocs') . " '{$query}'");
  94  
  95          $searchcontentobj = new googledocs_content_search($servicemock, $path, $sortcontent);
  96          $this->disallowedextensions = $filterextensions;
  97          $contentnodes = $searchcontentobj->get_content_nodes($query, [$this, 'filter']);
  98  
  99          // Assert that the returned array of repository content nodes is equal to the expected one.
 100          $this->assertEquals($expected, $contentnodes);
 101      }
 102  
 103      /**
 104       * Data provider for test_get_content_nodes().
 105       *
 106       * @return array
 107       */
 108      public function get_content_nodes_provider(): array {
 109  
 110          $rootid = \repository_googledocs::REPOSITORY_ROOT_ID;
 111          $searchnodeid = \repository_googledocs::SEARCH_ROOT_ID;
 112          $searchforstring = get_string('searchfor', 'repository_googledocs');
 113  
 114          return [
 115              'Folders and files match the search criteria; shared drives exist; ordering applied.' =>
 116                  [
 117                      'test',
 118                      true,
 119                      [],
 120                      [
 121                          $this->create_google_drive_shared_drive_object('d85b21c0f86cb5', 'Shared Drive 1'),
 122                      ],
 123                      [
 124                          $this->create_google_drive_file_object('d85b21c0f86cb0', 'Test file 3.pdf',
 125                              'application/pdf', 'pdf', '1000', '',
 126                              'https://drive.google.com/uc?id=d85b21c0f86cb0&export=download'),
 127                          $this->create_google_drive_folder_object('0c4ad262c65333', 'Test folder 1'),
 128                          $this->create_google_drive_file_object('bed5a0f08d412a', 'Test file 1.pdf',
 129                              'application/pdf', 'pdf'),
 130                          $this->create_google_drive_folder_object('9c4ad262c65333', 'Test folder 2'),
 131                      ],
 132                      [
 133                          $this->create_folder_content_node_array('0c4ad262c65333', 'Test folder 1',
 134                              "{$rootid}|Google+Drive/{$searchnodeid}|" . urlencode("{$searchforstring} 'test'")),
 135                          $this->create_folder_content_node_array('9c4ad262c65333', 'Test folder 2',
 136                              "{$rootid}|Google+Drive/{$searchnodeid}|" . urlencode("{$searchforstring} 'test'")),
 137                          $this->create_file_content_node_array('bed5a0f08d412a', 'Test file 1.pdf',
 138                              'Test file 1.pdf', null, '', 'https://googleusercontent.com/type/application/pdf',
 139                              '', 'download'),
 140                          $this->create_file_content_node_array('d85b21c0f86cb0', 'Test file 3.pdf',
 141                              'Test file 3.pdf', '1000', '', 'https://googleusercontent.com/type/application/pdf',
 142                              'https://drive.google.com/uc?id=d85b21c0f86cb0&export=download', 'download'),
 143                      ],
 144                  ],
 145              'Only folders match the search criteria; shared drives do not exist; ordering not applied.' =>
 146                  [
 147                      'testing',
 148                      false,
 149                      [],
 150                      [],
 151                      [
 152                          $this->create_google_drive_folder_object('0c4ad262c65333', 'Testing folder 3'),
 153                          $this->create_google_drive_folder_object('d85b21c0f86cb0', 'Testing folder 1'),
 154                          $this->create_google_drive_folder_object('bed5a0f08d412a', 'Testing folder 2'),
 155                      ],
 156                      [
 157                          $this->create_folder_content_node_array('0c4ad262c65333', 'Testing folder 3',
 158                              "{$rootid}|Google+Drive/{$searchnodeid}|" . urlencode("{$searchforstring} 'testing'")),
 159                          $this->create_folder_content_node_array('d85b21c0f86cb0', 'Testing folder 1',
 160                              "{$rootid}|Google+Drive/{$searchnodeid}|" . urlencode("{$searchforstring} 'testing'")),
 161                          $this->create_folder_content_node_array('bed5a0f08d412a', 'Testing folder 2',
 162                              "{$rootid}|Google+Drive/{$searchnodeid}|" . urlencode("{$searchforstring} 'testing'")),
 163                      ],
 164                  ],
 165              'Only files match the search criteria; shared drives exist; ordering not applied; filter .doc and .txt.' =>
 166                  [
 167                      'root',
 168                      false,
 169                      ['doc', 'txt'],
 170                      [
 171                          $this->create_google_drive_shared_drive_object('d85b21c0f86cb5', 'Shared Drive 1'),
 172                      ],
 173                      [
 174                          $this->create_google_drive_file_object('d85b21c0f86cb0', 'Testing file 3.pdf',
 175                              'application/pdf', 'pdf', '1000'),
 176                          $this->create_google_drive_file_object('a85b21c0f86cb0', 'Testing file 1.txt',
 177                              'text/plain', 'txt', '3000'),
 178                          $this->create_google_drive_file_object('f85b21c0f86cb0', 'Testing file 2.doc',
 179                              'application/msword', 'doc', '2000'),
 180                      ],
 181                      [
 182                          $this->create_file_content_node_array('d85b21c0f86cb0', 'Testing file 3.pdf',
 183                              'Testing file 3.pdf', '1000', '',
 184                              'https://googleusercontent.com/type/application/pdf', '', 'download'),
 185                      ],
 186                  ],
 187              'No content that matches the search criteria; shared drives do not exist.' =>
 188                  [
 189                      'root',
 190                      false,
 191                      [],
 192                      [],
 193                      [],
 194                      [],
 195                  ],
 196          ];
 197      }
 198  }