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 shared drives 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_shared_drives_content_test extends \googledocs_content_testcase {
  33  
  34      /**
  35       * Test get_content_nodes().
  36       *
  37       * @dataProvider get_content_nodes_provider
  38       * @param array $shareddrives The array containing the existing shared drives
  39       * @param bool $sortcontent Whether the contents should be sorted in alphabetical order
  40       * @param array $expected The expected array which contains the generated repository content nodes
  41       */
  42      public function test_get_content_nodes(array $shareddrives, bool $sortcontent, array $expected) {
  43          // Mock the service object.
  44          $servicemock = $this->createMock(\repository_googledocs\rest::class);
  45  
  46          // Assert that the call() method is being called only once with the given arguments to fetch the existing
  47          // shared drives. Define the returned data object by this call.
  48          $servicemock->expects($this->once())
  49              ->method('call')
  50              ->with('shared_drives_list', [])
  51              ->willReturn((object)[
  52                  'kind' => 'drive#driveList',
  53                  'nextPageToken' => 'd838181f30b0f5',
  54                  'drives' => $shareddrives,
  55              ]);
  56  
  57          $path = \repository_googledocs::REPOSITORY_ROOT_ID . '|Google+Drive/' .
  58              \repository_googledocs::SHARED_DRIVES_ROOT_ID . '|Shared+Drives';
  59          $shareddrivesbrowser = new googledocs_shared_drives_content($servicemock, $path, $sortcontent);
  60          $contentnodes = $shareddrivesbrowser->get_content_nodes('', [$this, 'filter']);
  61  
  62          // Assert that the returned array of repository content nodes is equal to the expected one.
  63          $this->assertEquals($expected, $contentnodes);
  64      }
  65  
  66      /**
  67       * Data provider for test_get_content_nodes().
  68       *
  69       * @return array
  70       */
  71      public function get_content_nodes_provider(): array {
  72  
  73          $rootid = \repository_googledocs::REPOSITORY_ROOT_ID;
  74          $shareddrivesid = \repository_googledocs::SHARED_DRIVES_ROOT_ID;
  75          $shareddrivesstring = get_string('shareddrives', 'repository_googledocs');
  76  
  77          return [
  78              'Shared drives exist; ordering applied.' =>
  79                  [
  80                      [
  81                          $this->create_google_drive_shared_drive_object('d85b21c0f86cb5', 'Shared Drive 1'),
  82                          $this->create_google_drive_shared_drive_object('d85b21c0f86cb0', 'Shared Drive 3'),
  83                          $this->create_google_drive_shared_drive_object('bed5a0f08d412a', 'Shared Drive 2'),
  84                      ],
  85                      true,
  86                      [
  87                          $this->create_folder_content_node_array('d85b21c0f86cb5', 'Shared Drive 1',
  88                              "{$rootid}|Google+Drive/{$shareddrivesid}|" . urlencode($shareddrivesstring)),
  89                          $this->create_folder_content_node_array('bed5a0f08d412a', 'Shared Drive 2',
  90                              "{$rootid}|Google+Drive/{$shareddrivesid}|" . urlencode($shareddrivesstring)),
  91                          $this->create_folder_content_node_array('d85b21c0f86cb0', 'Shared Drive 3',
  92                              "{$rootid}|Google+Drive/{$shareddrivesid}|" . urlencode($shareddrivesstring)),
  93                      ],
  94                  ],
  95              'Shared drives exist; ordering not applied.' =>
  96                  [
  97                      [
  98                          $this->create_google_drive_shared_drive_object('0c4ad262c65333', 'Shared Drive 3'),
  99                          $this->create_google_drive_shared_drive_object('d85b21c0f86cb0', 'Shared Drive 1'),
 100                          $this->create_google_drive_shared_drive_object('bed5a0f08d412a', 'Shared Drive 2'),
 101                      ],
 102                      false,
 103                      [
 104                          $this->create_folder_content_node_array('0c4ad262c65333', 'Shared Drive 3',
 105                              "{$rootid}|Google+Drive/{$shareddrivesid}|" . urlencode($shareddrivesstring)),
 106                          $this->create_folder_content_node_array('d85b21c0f86cb0', 'Shared Drive 1',
 107                              "{$rootid}|Google+Drive/{$shareddrivesid}|" . urlencode($shareddrivesstring)),
 108                          $this->create_folder_content_node_array('bed5a0f08d412a', 'Shared Drive 2',
 109                              "{$rootid}|Google+Drive/{$shareddrivesid}|" . urlencode($shareddrivesstring)),
 110                      ],
 111                  ],
 112              'Shared drives do not exist.' =>
 113                  [
 114                      [],
 115                      false,
 116                      [],
 117                  ],
 118          ];
 119      }
 120  }