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\local\node;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->dirroot . '/repository/googledocs/tests/repository_googledocs_testcase.php');
  23  
  24  /**
  25   * Class containing unit tests for the repository file node class.
  26   *
  27   * @package    repository_googledocs
  28   * @copyright  2021 Mihail Geshoski <mihail@moodle.com>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class file_node_test extends \repository_googledocs_testcase {
  32  
  33      /**
  34       * Test create_node_array().
  35       *
  36       * @dataProvider create_node_array_provider
  37       * @param \stdClass $gdfile The Google Drive file object
  38       * @param array $configsettings The googledoc repository config settings that should be set
  39       * @param array|null $expected The expected repository file node array
  40       */
  41      public function test_create_node_array(\stdClass $gdfile, array $configsettings, ?array $expected) {
  42          $this->resetAfterTest();
  43          // Set the required config settings.
  44          array_walk($configsettings, function($value, $name) {
  45              set_config($name, $value, 'googledocs');
  46          });
  47  
  48          $filenode = new file_node($gdfile);
  49          $filenodearray = $filenode->create_node_array();
  50          // Assert that the returned repository file node array by create_node_array() is equal to the expected one.
  51          $this->assertEquals($expected, $filenodearray);
  52      }
  53  
  54      /**
  55       * Data provider for test_create_node_array().
  56       *
  57       * @return array
  58       */
  59      public function create_node_array_provider(): array {
  60  
  61          return [
  62              'Google Drive file with an extension.' =>
  63                  [
  64                      $this->create_google_drive_file_object('d85b21c0f86cb0', 'File.pdf',
  65                          'application/pdf', 'pdf', '1000', '01/01/21 0:30'),
  66                      [],
  67                      $this->create_file_content_node_array('d85b21c0f86cb0', 'File.pdf', 'File.pdf', '1000',
  68                          '1609432200', 'https://googleusercontent.com/type/application/pdf', '',
  69                          'download'),
  70                  ],
  71              'Google Drive file that has webContentLink and webViewLink.' =>
  72                  [
  73                      $this->create_google_drive_file_object('d85b21c0f86cb0', 'File.pdf',
  74                          'application/pdf', 'pdf', null, '',
  75                          'https://drive.google.com/uc?id=d85b21c0f86cb0&export=download',
  76                          'https://drive.google.com/file/d/d85b21c0f86cb0/view?usp=drivesdk'),
  77                      [
  78                          'documentformat' => 'rtf',
  79                      ],
  80                      $this->create_file_content_node_array('d85b21c0f86cb0', 'File.pdf', 'File.pdf', null,
  81                          '', 'https://googleusercontent.com/type/application/pdf',
  82                          'https://drive.google.com/file/d/d85b21c0f86cb0/view?usp=drivesdk', 'download'),
  83                  ],
  84              'Google Drive file that has webContentLink and no webViewLink.' =>
  85                  [
  86                      $this->create_google_drive_file_object('d85b21c0f86cb0', 'File.pdf',
  87                          'application/pdf', 'pdf', null, '',
  88                          'https://drive.google.com/uc?id=d85b21c0f86cb0&export=download', ''),
  89                      [],
  90                      $this->create_file_content_node_array('d85b21c0f86cb0', 'File.pdf', 'File.pdf', null,
  91                          '', 'https://googleusercontent.com/type/application/pdf',
  92                          'https://drive.google.com/uc?id=d85b21c0f86cb0&export=download', 'download'),
  93                  ],
  94              'Google Drive file without an extension (Google document file; documentformat config set to rtf).' =>
  95                  [
  96                      $this->create_google_drive_file_object('d85b21c0f86cb0', 'File',
  97                          'application/vnd.google-apps.document', null),
  98                      [
  99                          'documentformat' => 'rtf',
 100                      ],
 101                      $this->create_file_content_node_array('d85b21c0f86cb0', 'File', 'File.gdoc', '', '',
 102                          'https://googleusercontent.com/type/application/vnd.google-apps.document', '',
 103                          'application/rtf', 'document'),
 104                  ],
 105              'Google Drive file without an extension (Google presentation file; presentationformat config not set).' =>
 106                  [
 107                      $this->create_google_drive_file_object('d85b21c0f86cb0', 'File',
 108                          'application/vnd.google-apps.presentation', null),
 109                      [
 110                          'documentformat' => 'rtf',
 111                      ],
 112                      null,
 113                  ],
 114              'Google Drive file without an extension (File type not supported).' =>
 115                  [
 116                      $this->create_google_drive_file_object('d85b21c0f86cb0', 'File',
 117                          'application/vnd.google-apps.unknownmimetype', null),
 118                      [],
 119                      null,
 120                  ],
 121          ];
 122      }
 123  }