Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  namespace tool_installaddon;
  19  
  20  use testable_tool_installaddon_installer;
  21  use tool_installaddon_installer;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  global $CFG;
  26  require_once (__DIR__.'/fixtures/testable_installer.php');
  27  
  28  /**
  29   * Unit tests for the {@link tool_installaddon_installer} class
  30   *
  31   * @package     tool_installaddon
  32   * @category    test
  33   * @copyright 2013 David Mudrak <david@moodle.com>
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class installer_test extends \advanced_testcase {
  37  
  38      public function test_get_addons_repository_url() {
  39          $installer = testable_tool_installaddon_installer::instance();
  40          $url = $installer->get_addons_repository_url();
  41          $query = parse_url($url, PHP_URL_QUERY);
  42          $this->assertEquals(1, preg_match('~^site=(.+)$~', $query, $matches));
  43          $site = rawurldecode($matches[1]);
  44          $site = json_decode(base64_decode($site), true);
  45          $this->assertIsArray($site);
  46          $this->assertEquals(3, count($site));
  47          $this->assertSame('Nasty site', $site['fullname']);
  48          $this->assertSame('file:///etc/passwd', $site['url']);
  49          $this->assertSame("2.5'; DROP TABLE mdl_user; --", $site['majorversion']);
  50      }
  51  
  52      public function test_decode_remote_request() {
  53          $installer = testable_tool_installaddon_installer::instance();
  54  
  55          $request = base64_encode(json_encode(array(
  56              'name' => '<h1>Stamp collection</h1>"; DELETE FROM mdl_users; --',
  57              'component' => 'mod_stampcoll',
  58              'version' => 2013032800,
  59          )));
  60          $request = $installer->testable_decode_remote_request($request);
  61          $this->assertTrue(is_object($request));
  62          // One, my little hobbit, never trusts the input parameters!
  63          $this->assertEquals('Stamp collection&quot;; DELETE FROM mdl_users; --', $request->name);
  64          $this->assertEquals('mod_stampcoll', $request->component);
  65          $this->assertEquals(2013032800, $request->version);
  66  
  67          $request = base64_encode(json_encode(array(
  68              'name' => 'Theme with invalid version number',
  69              'component' => 'theme_invalid',
  70              'version' => '1.0',
  71          )));
  72          $this->assertSame(false, $installer->testable_decode_remote_request($request));
  73  
  74          $request = base64_encode(json_encode(array(
  75              'name' => 'Invalid activity name',
  76              'component' => 'mod_invalid_activity',
  77              'version' => 2013032800,
  78          )));
  79          $this->assertSame(false, $installer->testable_decode_remote_request($request));
  80  
  81          $request = base64_encode(json_encode(array(
  82              'name' => 'Moodle 3.0',
  83              'component' => 'core',
  84              'version' => 2022010100,
  85          )));
  86          $this->assertSame(false, $installer->testable_decode_remote_request($request));
  87  
  88          $request = base64_encode(json_encode(array(
  89              'name' => 'Invalid core subsystem',
  90              'component' => 'core_cache',
  91              'version' => 2014123400,
  92          )));
  93          $this->assertSame(false, $installer->testable_decode_remote_request($request));
  94  
  95          $request = base64_encode(json_encode(array(
  96              'name' => 'Non-existing plugintype',
  97              'component' => 'david_mudrak',
  98              'version' => 2012123199,
  99          )));
 100          $this->assertSame(false, $installer->testable_decode_remote_request($request));
 101  
 102          $request = base64_encode(json_encode(array(
 103              'name' => 'Bogus module name',
 104              'component' => 'mod_xxx_yyy',
 105              'version' => 2012123190,
 106          )));
 107          $this->assertSame(false, $installer->testable_decode_remote_request($request));
 108      }
 109  
 110      public function test_detect_plugin_component() {
 111          global $CFG;
 112  
 113          $installer = tool_installaddon_installer::instance();
 114  
 115          $zipfile = $CFG->libdir.'/tests/fixtures/update_validator/zips/bar.zip';
 116          $this->assertEquals('foo_bar', $installer->detect_plugin_component($zipfile));
 117  
 118          $zipfile = $CFG->libdir.'/tests/fixtures/update_validator/zips/invalidroot.zip';
 119          $this->assertFalse($installer->detect_plugin_component($zipfile));
 120      }
 121  
 122      public function test_detect_plugin_component_from_versionphp() {
 123          global $CFG;
 124  
 125          $installer = testable_tool_installaddon_installer::instance();
 126          $fixtures = $CFG->libdir.'/tests/fixtures/update_validator/';
 127  
 128          $this->assertEquals('bar_bar_conan', $installer->testable_detect_plugin_component_from_versionphp('
 129  $plugin->version  = 2014121300;
 130    $plugin->component=   "bar_bar_conan"  ; // Go Arnie go!'));
 131  
 132          $versionphp = file_get_contents($fixtures.'/github/moodle-repository_mahara-master/version.php');
 133          $this->assertEquals('repository_mahara', $installer->testable_detect_plugin_component_from_versionphp($versionphp));
 134  
 135          $versionphp = file_get_contents($fixtures.'/nocomponent/baz/version.php');
 136          $this->assertFalse($installer->testable_detect_plugin_component_from_versionphp($versionphp));
 137      }
 138  
 139      public function test_make_installfromzip_storage() {
 140          $installer = testable_tool_installaddon_installer::instance();
 141  
 142          // Check we get writable directory.
 143          $storage1 = $installer->make_installfromzip_storage();
 144          $this->assertTrue(is_dir($storage1));
 145          $this->assertTrue(is_writable($storage1));
 146          file_put_contents($storage1.'/hello.txt', 'Find me if you can!');
 147  
 148          // Check we get unique directory on each call.
 149          $storage2 = $installer->make_installfromzip_storage();
 150          $this->assertTrue(is_dir($storage2));
 151          $this->assertTrue(is_writable($storage2));
 152          $this->assertFalse(file_exists($storage2.'/hello.txt'));
 153  
 154          // Check both are in the same parent directory.
 155          $this->assertEquals(dirname($storage1), dirname($storage2));
 156      }
 157  }