| 1 | 
           efrain | 
           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 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           30 | 
           final class remote_resource_test extends \advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           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 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           40 | 
               public function test_getters($url, $metadata, $expectedextension): void {
  | 
        
        
           | 1 | 
           efrain | 
           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 | 
                */
  | 
        
        
           | 1441 | 
           ariadna | 
           56 | 
               public static function remote_resource_data_provider(): array {
  | 
        
        
           | 1 | 
           efrain | 
           57 | 
                   return [
  | 
        
        
            | 
            | 
           58 | 
                       'With filename and extension' => [
  | 
        
        
           | 1441 | 
           ariadna | 
           59 | 
                           self::getExternalTestFileUrl('/test.html'),
  | 
        
        
           | 1 | 
           efrain | 
           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 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           80 | 
               public function test_network_features(): void {
  | 
        
        
           | 1441 | 
           ariadna | 
           81 | 
                   $url = self::getExternalTestFileUrl('/test.html');
  | 
        
        
            | 
            | 
           82 | 
                   $nonexistenturl = self::getExternalTestFileUrl('/test.htmlzz');
  | 
        
        
           | 1 | 
           efrain | 
           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 | 
           }
  |