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.

Differences Between: [Versions 311 and 401] [Versions 400 and 401]

   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 tool_moodlenet\local;
  18  
  19  use tool_moodlenet\local\remote_resource;
  20  use tool_moodlenet\local\url;
  21  
  22  /**
  23   * Class tool_moodlenet_remote_resource_testcase, providing test cases for the remote_resource class.
  24   *
  25   * @package    tool_moodlenet
  26   * @category   test
  27   * @copyright  2020 Jake Dallimore <jrhdallimore@gmail.com>
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class remote_resource_test extends \advanced_testcase {
  31  
  32      /**
  33       * Test getters.
  34       *
  35       * @dataProvider remote_resource_data_provider
  36       * @param string $url the url of the resource.
  37       * @param string $metadata the resource metadata like name, description, etc.
  38       * @param string $expectedextension the extension we expect to find when querying the remote resource.
  39       */
  40      public function test_getters($url, $metadata, $expectedextension) {
  41          $this->resetAfterTest();
  42  
  43          $remoteres = new remote_resource(new \curl(), new url($url), $metadata);
  44  
  45          $this->assertEquals(new url($url), $remoteres->get_url());
  46          $this->assertEquals($metadata->name, $remoteres->get_name());
  47          $this->assertEquals($metadata->description, $remoteres->get_description());
  48          $this->assertEquals($expectedextension, $remoteres->get_extension());
  49      }
  50  
  51      /**
  52       * Data provider generating remote urls.
  53       *
  54       * @return array
  55       */
  56      public function remote_resource_data_provider() {
  57          return [
  58              'With filename and extension' => [
  59                  $this->getExternalTestFileUrl('/test.html'),
  60                  (object) [
  61                      'name' => 'Test html file',
  62                      'description' => 'Full description of the html file'
  63                  ],
  64                  'html'
  65              ],
  66              'With filename only' => [
  67                  'http://example.com/path/file',
  68                  (object) [
  69                      'name' => 'Test html file',
  70                      'description' => 'Full description of the html file'
  71                  ],
  72                  ''
  73              ]
  74          ];
  75      }
  76  
  77      /**
  78       * Test confirming the network based operations of a remote_resource.
  79       */
  80      public function test_network_features() {
  81          $url = $this->getExternalTestFileUrl('/test.html');
  82          $nonexistenturl = $this->getExternalTestFileUrl('/test.htmlzz');
  83  
  84          $remoteres = new remote_resource(
  85              new \curl(),
  86              new url($url),
  87              (object) [
  88                  'name' => 'Test html file',
  89                  'description' => 'Some description'
  90              ]
  91          );
  92          $nonexistentremoteres = new remote_resource(
  93              new \curl(),
  94              new url($nonexistenturl),
  95              (object) [
  96                  'name' => 'Test html file',
  97                  'description' => 'Some description'
  98              ]
  99          );
 100  
 101          // We need to handle size of -1 (missing "Content-Length" header), or where it is set and greater than zero.
 102          $this->assertThat(
 103              $remoteres->get_download_size(),
 104              $this->logicalOr(
 105                  $this->equalTo(-1),
 106                  $this->greaterThan(0),
 107              ),
 108          );
 109  
 110          [$path, $name] = $remoteres->download_to_requestdir();
 111          $this->assertIsString($path);
 112          $this->assertEquals('test.html', $name);
 113          $this->assertFileExists($path . '/' . $name);
 114  
 115          $this->expectException(\coding_exception::class);
 116          $nonexistentremoteres->get_download_size();
 117      }
 118  }