Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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  /**
  18   * Unit tests for the remote_resource class.
  19   *
  20   * @package    tool_moodlenet
  21   * @category   test
  22   * @copyright  2020 Jake Dallimore <jrhdallimore@gmail.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace tool_moodlenet\local\tests;
  26  
  27  use tool_moodlenet\local\remote_resource;
  28  use tool_moodlenet\local\url;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  /**
  33   * Class tool_moodlenet_remote_resource_testcase, providing test cases for the remote_resource class.
  34   */
  35  class tool_moodlenet_remote_resource_testcase extends \advanced_testcase {
  36  
  37      /**
  38       * Test getters.
  39       *
  40       * @dataProvider remote_resource_data_provider
  41       * @param string $url the url of the resource.
  42       * @param string $metadata the resource metadata like name, description, etc.
  43       * @param string $expectedextension the extension we expect to find when querying the remote resource.
  44       */
  45      public function test_getters($url, $metadata, $expectedextension) {
  46          $this->resetAfterTest();
  47  
  48          $remoteres = new remote_resource(new \curl(), new url($url), $metadata);
  49  
  50          $this->assertEquals(new url($url), $remoteres->get_url());
  51          $this->assertEquals($metadata->name, $remoteres->get_name());
  52          $this->assertEquals($metadata->description, $remoteres->get_description());
  53          $this->assertEquals($expectedextension, $remoteres->get_extension());
  54      }
  55  
  56      /**
  57       * Data provider generating remote urls.
  58       *
  59       * @return array
  60       */
  61      public function remote_resource_data_provider() {
  62          return [
  63              'With filename and extension' => [
  64                  $this->getExternalTestFileUrl('/test.html'),
  65                  (object) [
  66                      'name' => 'Test html file',
  67                      'description' => 'Full description of the html file'
  68                  ],
  69                  'html'
  70              ],
  71              'With filename only' => [
  72                  'http://example.com/path/file',
  73                  (object) [
  74                      'name' => 'Test html file',
  75                      'description' => 'Full description of the html file'
  76                  ],
  77                  ''
  78              ]
  79          ];
  80      }
  81  
  82      /**
  83       * Test confirming the network based operations of a remote_resource.
  84       */
  85      public function test_network_features() {
  86          $url = $this->getExternalTestFileUrl('/test.html');
  87          $nonexistenturl = $this->getExternalTestFileUrl('/test.htmlzz');
  88  
  89          $remoteres = new remote_resource(
  90              new \curl(),
  91              new url($url),
  92              (object) [
  93                  'name' => 'Test html file',
  94                  'description' => 'Some description'
  95              ]
  96          );
  97          $nonexistentremoteres = new remote_resource(
  98              new \curl(),
  99              new url($nonexistenturl),
 100              (object) [
 101                  'name' => 'Test html file',
 102                  'description' => 'Some description'
 103              ]
 104          );
 105  
 106          $this->assertGreaterThan(0, $remoteres->get_download_size());
 107          [$path, $name] = $remoteres->download_to_requestdir();
 108          $this->assertIsString($path);
 109          $this->assertEquals('test.html', $name);
 110          $this->assertFileExists($path . '/' . $name);
 111  
 112          $this->expectException(\coding_exception::class);
 113          $nonexistentremoteres->get_download_size();
 114      }
 115  }