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/repository_googledocs_testcase.php');
  23  require_once($CFG->dirroot . '/repository/googledocs/lib.php');
  24  
  25  /**
  26   * Class containing unit tests for the helper 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 helper_test extends \repository_googledocs_testcase {
  33  
  34      /**
  35       * Test build_node_path().
  36       *
  37       * @dataProvider build_node_path_provider
  38       * @param string $id The ID of the node
  39       * @param string $name The name of the node
  40       * @param string $rootpath The path to append the node on
  41       * @param string $expected The expected node path
  42       */
  43      public function test_build_node_path(string $id, string $name, string $rootpath, string $expected) {
  44          // Assert that the returned node path is equal to the expected one.
  45          $this->assertEquals($expected, helper::build_node_path($id, $name, $rootpath));
  46      }
  47  
  48      /**
  49       * Data provider for test_build_node_path().
  50       *
  51       * @return array
  52       */
  53      public function build_node_path_provider(): array {
  54  
  55          $rootid = \repository_googledocs::REPOSITORY_ROOT_ID;
  56          $mydriveid = \repository_googledocs::MY_DRIVE_ROOT_ID;
  57          $shareddrivesid = \repository_googledocs::SHARED_DRIVES_ROOT_ID;
  58  
  59          return [
  60              'Generate the path for a node without a root path.' =>
  61                  [
  62                      $rootid,
  63                      'Google Drive',
  64                      '',
  65                      "{$rootid}|Google+Drive",
  66                  ],
  67              'Generate the path for a node without a name and root path.' =>
  68                  [
  69                      $rootid,
  70                      '',
  71                      '',
  72                      $rootid,
  73                  ],
  74              'Generate the path for a node without a name.' =>
  75                  [
  76                      $mydriveid,
  77                      '',
  78                      "{$rootid}|Google+Drive",
  79                      "{$rootid}|Google+Drive/{$mydriveid}",
  80                  ],
  81              'Generate the path for a node which has a name and root path.' =>
  82                  [
  83                      '092cdf4732b9d5',
  84                      'Shared Drive 5',
  85                      "{$rootid}|Google+Drive/{$shareddrivesid}|Shared+Drives",
  86                      "{$rootid}|Google+Drive/{$shareddrivesid}|Shared+Drives/092cdf4732b9d5|Shared+Drive+5",
  87                  ],
  88          ];
  89      }
  90  
  91      /**
  92       * Test explode_node_path().
  93       *
  94       * @dataProvider explode_node_path_provider
  95       * @param string $node The node string to extract information from
  96       * @param array $expected The expected array containing the information about the node
  97       */
  98      public function test_explode_node_path(string $node, array $expected) {
  99          // Assert that the returned array is equal to the expected one.
 100          $this->assertEquals($expected, helper::explode_node_path($node));
 101      }
 102  
 103      /**
 104       * Data provider for test_explode_node_path().
 105       *
 106       * @return array
 107       */
 108      public function explode_node_path_provider(): array {
 109  
 110          $rootid = \repository_googledocs::REPOSITORY_ROOT_ID;
 111  
 112          return [
 113              'Return the information for a path node that has a name.' =>
 114                  [
 115                      "{$rootid}|Google+Drive",
 116                      [
 117                          0 => $rootid,
 118                          1 => 'Google Drive',
 119                          'id' => $rootid,
 120                          'name' => 'Google Drive',
 121                      ],
 122                  ],
 123              'Return the information for a path node that does not have a name.' =>
 124                  [
 125                      $rootid,
 126                      [
 127                          0 => $rootid,
 128                          1 => '',
 129                          'id' => $rootid,
 130                          'name' => '',
 131                      ],
 132                  ],
 133          ];
 134      }
 135  
 136      /**
 137       * Test get_browser().
 138       *
 139       * @dataProvider get_browser_provider
 140       * @param string $nodepath The node path string
 141       * @param string $expected The expected browser class
 142       */
 143      public function test_get_browser(string $nodepath, string $expected) {
 144          // The service (rest API) object is required by get_browser(), but not being used to determine which browser
 145          // object should be returned. Therefore, we can simply mock this object in this test.
 146          $servicemock = $this->createMock(rest::class);
 147          $browser = helper::get_browser($servicemock, $nodepath);
 148  
 149          // Assert that the returned browser class by get_browser() is equal to the expected one.
 150          $this->assertEquals($expected, get_class($browser));
 151      }
 152  
 153      /**
 154       * Data provider for test_get_browser().
 155       *
 156       * @return array
 157       */
 158      public function get_browser_provider(): array {
 159  
 160          $rootid = \repository_googledocs::REPOSITORY_ROOT_ID;
 161          $mydriveid = \repository_googledocs::MY_DRIVE_ROOT_ID;
 162          $shareddrivesid = \repository_googledocs::SHARED_DRIVES_ROOT_ID;
 163  
 164          return [
 165              'Repository root level path.' =>
 166                  [
 167                      "{$rootid}|Google+Drive",
 168                      \repository_googledocs\local\browser\googledocs_root_content::class,
 169                  ],
 170              'My drive path.' =>
 171                  [
 172                      "{$rootid}|Google+Drive/{$mydriveid}|My+Drive",
 173                      \repository_googledocs\local\browser\googledocs_drive_content::class,
 174                  ],
 175              'Shared drives root path.' =>
 176                  [
 177                      "{$rootid}|Google+Drive/{$shareddrivesid}|Shared+Drives",
 178                      \repository_googledocs\local\browser\googledocs_shared_drives_content::class,
 179                  ],
 180              'Path within a shared drive.' =>
 181                  [
 182                      "{$rootid}|Google+Drive/{$shareddrivesid}|Shared+Drives/092cdf4732b9d5|Shared+Drive+5",
 183                      \repository_googledocs\local\browser\googledocs_drive_content::class,
 184                  ],
 185          ];
 186      }
 187  
 188      /**
 189       * Test get_node().
 190       *
 191       * @dataProvider get_node_provider
 192       * @param \stdClass $gdcontent The Google Drive content (file/folder) object
 193       * @param string $expected The expected content node class
 194       */
 195      public function test_get_node(\stdClass $gdcontent, string $expected) {
 196          // The path is required by get_content_node(), but not being used to determine which content node
 197          // object should be returned. Therefore, we can just generate a dummy path.
 198          $path = \repository_googledocs::REPOSITORY_ROOT_ID . '|Google+Drive|' .
 199              \repository_googledocs::MY_DRIVE_ROOT_ID . '|My+Drive';
 200          $node = helper::get_node($gdcontent, $path);
 201  
 202          // Assert that the returned content node class by get_node() is equal to the expected one.
 203          $this->assertEquals($expected, get_class($node));
 204      }
 205  
 206      /**
 207       * Data provider for test_get_node().
 208       *
 209       * @return array
 210       */
 211      public function get_node_provider(): array {
 212  
 213          return [
 214              'The content object represents a Google Drive folder.' =>
 215                  [
 216                      $this->create_google_drive_folder_object('e3b0c44298fc1c149', 'Folder', ''),
 217                      \repository_googledocs\local\node\folder_node::class,
 218                  ],
 219              'The content object represents a Google Drive file.' =>
 220                  [
 221                      $this->create_google_drive_file_object('de04d58dc5ccc', 'File.pdf',
 222                          'application/pdf'),
 223                      \repository_googledocs\local\node\file_node::class,
 224                  ],
 225          ];
 226      }
 227  
 228      /**
 229       * Test request() when an exception is thrown by the API call.
 230       *
 231       * @dataProvider request_exception_provider
 232       * @param \Exception $exception The exception thrown by the API call
 233       * @param \Exception $expected The expected exception thrown by request()
 234       */
 235      public function test_request_exception(\Exception $exception, \Exception $expected) {
 236          // Mock the service object.
 237          $servicemock = $this->createMock(rest::class);
 238  
 239          // Assert that the call() method is being called only once with the given arguments.
 240          // Define the thrown exception by this call.
 241          $servicemock->expects($this->once())
 242              ->method('call')
 243              ->with('list', [])
 244              ->willThrowException($exception);
 245  
 246          $this->expectExceptionObject($expected);
 247  
 248          helper::request($servicemock, 'list', []);
 249      }
 250  
 251      /**
 252       * Data provider for test_request_exception().
 253       *
 254       * @return array
 255       */
 256      public function request_exception_provider(): array {
 257  
 258          return [
 259              'The API call throws exception (status: 403; message: Access Not Configured).' =>
 260                  [
 261                      new \Exception('Access Not Configured', 403),
 262                      new \repository_exception('servicenotenabled', 'repository_googledocs'),
 263                  ],
 264              'The API call throws exception (status: 405; message: Access Not Configured).' =>
 265                  [
 266                      new \Exception('Access Not Configured', 405),
 267                      new \Exception('Access Not Configured', 405),
 268                  ],
 269              'The API call throws exception (status: 403; message: Access Forbidden).' =>
 270                  [
 271                      new \Exception('Access Forbidden', 403),
 272                      new \Exception('Access Forbidden', 403),
 273                  ],
 274              'The API call throws exception (status: 404; message: Not Found).' =>
 275                  [
 276                      new \Exception('Not Found', 404),
 277                      new \Exception('Not Found', 404),
 278                  ],
 279          ];
 280      }
 281  }