| 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;
  | 
        
        
            | 
            | 
           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 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           32 | 
           final class googledocs_search_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 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,
  | 
        
        
           | 11 | 
           efrain | 
           46 | 
                       array $shareddrives, array $searccontents, array $expected): void {
  | 
        
        
           | 1 | 
           efrain | 
           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.
  | 
        
        
           | 1441 | 
           ariadna | 
           67 | 
                   $callinvocations = $this->exactly(2);
  | 
        
        
            | 
            | 
           68 | 
                   $servicemock->expects($callinvocations)
  | 
        
        
           | 1 | 
           efrain | 
           69 | 
                       ->method('call')
  | 
        
        
           | 1441 | 
           ariadna | 
           70 | 
                       ->willReturnCallback(function(string $method, array $params) use (
  | 
        
        
            | 
            | 
           71 | 
                           $callinvocations,
  | 
        
        
            | 
            | 
           72 | 
                           $shareddrives,
  | 
        
        
            | 
            | 
           73 | 
                           $searccontents,
  | 
        
        
            | 
            | 
           74 | 
                           $searchparams,
  | 
        
        
            | 
            | 
           75 | 
                       ) {
  | 
        
        
            | 
            | 
           76 | 
                           switch (self::getInvocationCount($callinvocations)) {
  | 
        
        
            | 
            | 
           77 | 
                               case 1:
  | 
        
        
            | 
            | 
           78 | 
                                   $this->assertEquals('shared_drives_list', $method);
  | 
        
        
           | 1 | 
           efrain | 
           79 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           80 | 
                                   $this->assertEmpty($params);
  | 
        
        
            | 
            | 
           81 | 
                                   return (object) [
  | 
        
        
            | 
            | 
           82 | 
                                       'kind' => 'drive#driveList',
  | 
        
        
            | 
            | 
           83 | 
                                       'nextPageToken' => 'd838181f30b0f5',
  | 
        
        
            | 
            | 
           84 | 
                                       'drives' => $shareddrives,
  | 
        
        
            | 
            | 
           85 | 
                                   ];
  | 
        
        
            | 
            | 
           86 | 
                               case 2:
  | 
        
        
            | 
            | 
           87 | 
                                   $this->assertEquals('list', $method);
  | 
        
        
            | 
            | 
           88 | 
                                   $this->assertEquals($searchparams, $params);
  | 
        
        
            | 
            | 
           89 | 
              | 
        
        
            | 
            | 
           90 | 
                                   return (object) [
  | 
        
        
            | 
            | 
           91 | 
                                       'files' => $searccontents,
  | 
        
        
            | 
            | 
           92 | 
                                   ];
  | 
        
        
            | 
            | 
           93 | 
                           }
  | 
        
        
            | 
            | 
           94 | 
                       });
  | 
        
        
            | 
            | 
           95 | 
              | 
        
        
           | 1 | 
           efrain | 
           96 | 
                   // Construct the node path.
  | 
        
        
            | 
            | 
           97 | 
                   $path = \repository_googledocs::REPOSITORY_ROOT_ID . '|' . urlencode('Google Drive') . '/' .
  | 
        
        
            | 
            | 
           98 | 
                       \repository_googledocs::SEARCH_ROOT_ID . '|' . urlencode(
  | 
        
        
            | 
            | 
           99 | 
                       get_string('searchfor', 'repository_googledocs') . " '{$query}'");
  | 
        
        
            | 
            | 
           100 | 
              | 
        
        
            | 
            | 
           101 | 
                   $searchcontentobj = new googledocs_content_search($servicemock, $path, $sortcontent);
  | 
        
        
            | 
            | 
           102 | 
                   $this->disallowedextensions = $filterextensions;
  | 
        
        
            | 
            | 
           103 | 
                   $contentnodes = $searchcontentobj->get_content_nodes($query, [$this, 'filter']);
  | 
        
        
            | 
            | 
           104 | 
              | 
        
        
            | 
            | 
           105 | 
                   // Assert that the returned array of repository content nodes is equal to the expected one.
  | 
        
        
            | 
            | 
           106 | 
                   $this->assertEquals($expected, $contentnodes);
  | 
        
        
            | 
            | 
           107 | 
               }
  | 
        
        
            | 
            | 
           108 | 
              | 
        
        
            | 
            | 
           109 | 
               /**
  | 
        
        
            | 
            | 
           110 | 
                * Data provider for test_get_content_nodes().
  | 
        
        
            | 
            | 
           111 | 
                *
  | 
        
        
            | 
            | 
           112 | 
                * @return array
  | 
        
        
            | 
            | 
           113 | 
                */
  | 
        
        
           | 1441 | 
           ariadna | 
           114 | 
               public static function get_content_nodes_provider(): array {
  | 
        
        
           | 1 | 
           efrain | 
           115 | 
              | 
        
        
            | 
            | 
           116 | 
                   $rootid = \repository_googledocs::REPOSITORY_ROOT_ID;
  | 
        
        
            | 
            | 
           117 | 
                   $searchnodeid = \repository_googledocs::SEARCH_ROOT_ID;
  | 
        
        
            | 
            | 
           118 | 
                   $searchforstring = get_string('searchfor', 'repository_googledocs');
  | 
        
        
            | 
            | 
           119 | 
              | 
        
        
            | 
            | 
           120 | 
                   return [
  | 
        
        
            | 
            | 
           121 | 
                       'Folders and files match the search criteria; shared drives exist; ordering applied.' =>
  | 
        
        
            | 
            | 
           122 | 
                           [
  | 
        
        
            | 
            | 
           123 | 
                               'test',
  | 
        
        
            | 
            | 
           124 | 
                               true,
  | 
        
        
            | 
            | 
           125 | 
                               [],
  | 
        
        
            | 
            | 
           126 | 
                               [
  | 
        
        
           | 1441 | 
           ariadna | 
           127 | 
                                   self::create_google_drive_shared_drive_object('d85b21c0f86cb5', 'Shared Drive 1'),
  | 
        
        
           | 1 | 
           efrain | 
           128 | 
                               ],
  | 
        
        
            | 
            | 
           129 | 
                               [
  | 
        
        
           | 1441 | 
           ariadna | 
           130 | 
                                   self::create_google_drive_file_object('d85b21c0f86cb0', 'Test file 3.pdf',
  | 
        
        
           | 1 | 
           efrain | 
           131 | 
                                       'application/pdf', 'pdf', '1000', '',
  | 
        
        
            | 
            | 
           132 | 
                                       'https://drive.google.com/uc?id=d85b21c0f86cb0&export=download'),
  | 
        
        
           | 1441 | 
           ariadna | 
           133 | 
                                   self::create_google_drive_folder_object('0c4ad262c65333', 'Test folder 1'),
  | 
        
        
            | 
            | 
           134 | 
                                   self::create_google_drive_file_object('bed5a0f08d412a', 'Test file 1.pdf',
  | 
        
        
           | 1 | 
           efrain | 
           135 | 
                                       'application/pdf', 'pdf'),
  | 
        
        
           | 1441 | 
           ariadna | 
           136 | 
                                   self::create_google_drive_folder_object('9c4ad262c65333', 'Test folder 2'),
  | 
        
        
           | 1 | 
           efrain | 
           137 | 
                               ],
  | 
        
        
            | 
            | 
           138 | 
                               [
  | 
        
        
           | 1441 | 
           ariadna | 
           139 | 
                                   self::create_folder_content_node_array('0c4ad262c65333', 'Test folder 1',
  | 
        
        
           | 1 | 
           efrain | 
           140 | 
                                       "{$rootid}|Google+Drive/{$searchnodeid}|" . urlencode("{$searchforstring} 'test'")),
  | 
        
        
           | 1441 | 
           ariadna | 
           141 | 
                                   self::create_folder_content_node_array('9c4ad262c65333', 'Test folder 2',
  | 
        
        
           | 1 | 
           efrain | 
           142 | 
                                       "{$rootid}|Google+Drive/{$searchnodeid}|" . urlencode("{$searchforstring} 'test'")),
  | 
        
        
           | 1441 | 
           ariadna | 
           143 | 
                                   self::create_file_content_node_array('bed5a0f08d412a', 'Test file 1.pdf',
  | 
        
        
           | 1 | 
           efrain | 
           144 | 
                                       'Test file 1.pdf', null, '', 'https://googleusercontent.com/type/application/pdf',
  | 
        
        
            | 
            | 
           145 | 
                                       '', 'download'),
  | 
        
        
           | 1441 | 
           ariadna | 
           146 | 
                                   self::create_file_content_node_array('d85b21c0f86cb0', 'Test file 3.pdf',
  | 
        
        
           | 1 | 
           efrain | 
           147 | 
                                       'Test file 3.pdf', '1000', '', 'https://googleusercontent.com/type/application/pdf',
  | 
        
        
            | 
            | 
           148 | 
                                       'https://drive.google.com/uc?id=d85b21c0f86cb0&export=download', 'download'),
  | 
        
        
            | 
            | 
           149 | 
                               ],
  | 
        
        
            | 
            | 
           150 | 
                           ],
  | 
        
        
            | 
            | 
           151 | 
                       'Only folders match the search criteria; shared drives do not exist; ordering not applied.' =>
  | 
        
        
            | 
            | 
           152 | 
                           [
  | 
        
        
            | 
            | 
           153 | 
                               'testing',
  | 
        
        
            | 
            | 
           154 | 
                               false,
  | 
        
        
            | 
            | 
           155 | 
                               [],
  | 
        
        
            | 
            | 
           156 | 
                               [],
  | 
        
        
            | 
            | 
           157 | 
                               [
  | 
        
        
           | 1441 | 
           ariadna | 
           158 | 
                                   self::create_google_drive_folder_object('0c4ad262c65333', 'Testing folder 3'),
  | 
        
        
            | 
            | 
           159 | 
                                   self::create_google_drive_folder_object('d85b21c0f86cb0', 'Testing folder 1'),
  | 
        
        
            | 
            | 
           160 | 
                                   self::create_google_drive_folder_object('bed5a0f08d412a', 'Testing folder 2'),
  | 
        
        
           | 1 | 
           efrain | 
           161 | 
                               ],
  | 
        
        
            | 
            | 
           162 | 
                               [
  | 
        
        
           | 1441 | 
           ariadna | 
           163 | 
                                   self::create_folder_content_node_array('0c4ad262c65333', 'Testing folder 3',
  | 
        
        
           | 1 | 
           efrain | 
           164 | 
                                       "{$rootid}|Google+Drive/{$searchnodeid}|" . urlencode("{$searchforstring} 'testing'")),
  | 
        
        
           | 1441 | 
           ariadna | 
           165 | 
                                   self::create_folder_content_node_array('d85b21c0f86cb0', 'Testing folder 1',
  | 
        
        
           | 1 | 
           efrain | 
           166 | 
                                       "{$rootid}|Google+Drive/{$searchnodeid}|" . urlencode("{$searchforstring} 'testing'")),
  | 
        
        
           | 1441 | 
           ariadna | 
           167 | 
                                   self::create_folder_content_node_array('bed5a0f08d412a', 'Testing folder 2',
  | 
        
        
           | 1 | 
           efrain | 
           168 | 
                                       "{$rootid}|Google+Drive/{$searchnodeid}|" . urlencode("{$searchforstring} 'testing'")),
  | 
        
        
            | 
            | 
           169 | 
                               ],
  | 
        
        
            | 
            | 
           170 | 
                           ],
  | 
        
        
            | 
            | 
           171 | 
                       'Only files match the search criteria; shared drives exist; ordering not applied; filter .doc and .txt.' =>
  | 
        
        
            | 
            | 
           172 | 
                           [
  | 
        
        
            | 
            | 
           173 | 
                               'root',
  | 
        
        
            | 
            | 
           174 | 
                               false,
  | 
        
        
            | 
            | 
           175 | 
                               ['doc', 'txt'],
  | 
        
        
            | 
            | 
           176 | 
                               [
  | 
        
        
           | 1441 | 
           ariadna | 
           177 | 
                                   self::create_google_drive_shared_drive_object('d85b21c0f86cb5', 'Shared Drive 1'),
  | 
        
        
           | 1 | 
           efrain | 
           178 | 
                               ],
  | 
        
        
            | 
            | 
           179 | 
                               [
  | 
        
        
           | 1441 | 
           ariadna | 
           180 | 
                                   self::create_google_drive_file_object('d85b21c0f86cb0', 'Testing file 3.pdf',
  | 
        
        
           | 1 | 
           efrain | 
           181 | 
                                       'application/pdf', 'pdf', '1000'),
  | 
        
        
           | 1441 | 
           ariadna | 
           182 | 
                                   self::create_google_drive_file_object('a85b21c0f86cb0', 'Testing file 1.txt',
  | 
        
        
           | 1 | 
           efrain | 
           183 | 
                                       'text/plain', 'txt', '3000'),
  | 
        
        
           | 1441 | 
           ariadna | 
           184 | 
                                   self::create_google_drive_file_object('f85b21c0f86cb0', 'Testing file 2.doc',
  | 
        
        
           | 1 | 
           efrain | 
           185 | 
                                       'application/msword', 'doc', '2000'),
  | 
        
        
            | 
            | 
           186 | 
                               ],
  | 
        
        
            | 
            | 
           187 | 
                               [
  | 
        
        
           | 1441 | 
           ariadna | 
           188 | 
                                   self::create_file_content_node_array('d85b21c0f86cb0', 'Testing file 3.pdf',
  | 
        
        
           | 1 | 
           efrain | 
           189 | 
                                       'Testing file 3.pdf', '1000', '',
  | 
        
        
            | 
            | 
           190 | 
                                       'https://googleusercontent.com/type/application/pdf', '', 'download'),
  | 
        
        
            | 
            | 
           191 | 
                               ],
  | 
        
        
            | 
            | 
           192 | 
                           ],
  | 
        
        
            | 
            | 
           193 | 
                       'No content that matches the search criteria; shared drives do not exist.' =>
  | 
        
        
            | 
            | 
           194 | 
                           [
  | 
        
        
            | 
            | 
           195 | 
                               'root',
  | 
        
        
            | 
            | 
           196 | 
                               false,
  | 
        
        
            | 
            | 
           197 | 
                               [],
  | 
        
        
            | 
            | 
           198 | 
                               [],
  | 
        
        
            | 
            | 
           199 | 
                               [],
  | 
        
        
            | 
            | 
           200 | 
                               [],
  | 
        
        
            | 
            | 
           201 | 
                           ],
  | 
        
        
            | 
            | 
           202 | 
                   ];
  | 
        
        
            | 
            | 
           203 | 
               }
  | 
        
        
            | 
            | 
           204 | 
           }
  |