Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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  /**
  19   * Provides the unit tests class and some helper classes
  20   *
  21   * @package     tool_installaddon
  22   * @category    test
  23   * @copyright   2013 David Mudrak <david@moodle.com>
  24   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once (__DIR__.'/fixtures/testable_installer.php');
  31  
  32  /**
  33   * Unit tests for the {@link tool_installaddon_installer} class
  34   *
  35   * @copyright 2013 David Mudrak <david@moodle.com>
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class tool_installaddon_installer_testcase extends advanced_testcase {
  39  
  40      public function test_get_addons_repository_url() {
  41          $installer = testable_tool_installaddon_installer::instance();
  42          $url = $installer->get_addons_repository_url();
  43          $query = parse_url($url, PHP_URL_QUERY);
  44          $this->assertEquals(1, preg_match('~^site=(.+)$~', $query, $matches));
  45          $site = rawurldecode($matches[1]);
  46          $site = json_decode(base64_decode($site), true);
  47          $this->assertIsArray($site);
  48          $this->assertEquals(3, count($site));
  49          $this->assertSame('Nasty site', $site['fullname']);
  50          $this->assertSame('file:///etc/passwd', $site['url']);
  51          $this->assertSame("2.5'; DROP TABLE mdl_user; --", $site['majorversion']);
  52      }
  53  
  54      public function test_decode_remote_request() {
  55          $installer = testable_tool_installaddon_installer::instance();
  56  
  57          $request = base64_encode(json_encode(array(
  58              'name' => '<h1>Stamp collection</h1>"; DELETE FROM mdl_users; --',
  59              'component' => 'mod_stampcoll',
  60              'version' => 2013032800,
  61          )));
  62          $request = $installer->testable_decode_remote_request($request);
  63          $this->assertTrue(is_object($request));
  64          // One, my little hobbit, never trusts the input parameters!
  65          $this->assertEquals('Stamp collection&quot;; DELETE FROM mdl_users; --', $request->name);
  66          $this->assertEquals('mod_stampcoll', $request->component);
  67          $this->assertEquals(2013032800, $request->version);
  68  
  69          $request = base64_encode(json_encode(array(
  70              'name' => 'Theme with invalid version number',
  71              'component' => 'theme_invalid',
  72              'version' => '1.0',
  73          )));
  74          $this->assertSame(false, $installer->testable_decode_remote_request($request));
  75  
  76          $request = base64_encode(json_encode(array(
  77              'name' => 'Invalid activity name',
  78              'component' => 'mod_invalid_activity',
  79              'version' => 2013032800,
  80          )));
  81          $this->assertSame(false, $installer->testable_decode_remote_request($request));
  82  
  83          $request = base64_encode(json_encode(array(
  84              'name' => 'Moodle 3.0',
  85              'component' => 'core',
  86              'version' => 2022010100,
  87          )));
  88          $this->assertSame(false, $installer->testable_decode_remote_request($request));
  89  
  90          $request = base64_encode(json_encode(array(
  91              'name' => 'Invalid core subsystem',
  92              'component' => 'core_cache',
  93              'version' => 2014123400,
  94          )));
  95          $this->assertSame(false, $installer->testable_decode_remote_request($request));
  96  
  97          $request = base64_encode(json_encode(array(
  98              'name' => 'Non-existing plugintype',
  99              'component' => 'david_mudrak',
 100              'version' => 2012123199,
 101          )));
 102          $this->assertSame(false, $installer->testable_decode_remote_request($request));
 103  
 104          $request = base64_encode(json_encode(array(
 105              'name' => 'Bogus module name',
 106              'component' => 'mod_xxx_yyy',
 107              'version' => 2012123190,
 108          )));
 109          $this->assertSame(false, $installer->testable_decode_remote_request($request));
 110      }
 111  
 112      public function test_detect_plugin_component() {
 113          global $CFG;
 114  
 115          $installer = tool_installaddon_installer::instance();
 116  
 117          $zipfile = $CFG->libdir.'/tests/fixtures/update_validator/zips/bar.zip';
 118          $this->assertEquals('foo_bar', $installer->detect_plugin_component($zipfile));
 119  
 120          $zipfile = $CFG->libdir.'/tests/fixtures/update_validator/zips/invalidroot.zip';
 121          $this->assertFalse($installer->detect_plugin_component($zipfile));
 122      }
 123  
 124      public function test_detect_plugin_component_from_versionphp() {
 125          global $CFG;
 126  
 127          $installer = testable_tool_installaddon_installer::instance();
 128          $fixtures = $CFG->libdir.'/tests/fixtures/update_validator/';
 129  
 130          $this->assertEquals('bar_bar_conan', $installer->testable_detect_plugin_component_from_versionphp('
 131  $plugin->version  = 2014121300;
 132    $plugin->component=   "bar_bar_conan"  ; // Go Arnie go!'));
 133  
 134          $versionphp = file_get_contents($fixtures.'/github/moodle-repository_mahara-master/version.php');
 135          $this->assertEquals('repository_mahara', $installer->testable_detect_plugin_component_from_versionphp($versionphp));
 136  
 137          $versionphp = file_get_contents($fixtures.'/nocomponent/baz/version.php');
 138          $this->assertFalse($installer->testable_detect_plugin_component_from_versionphp($versionphp));
 139      }
 140  
 141      public function test_make_installfromzip_storage() {
 142          $installer = testable_tool_installaddon_installer::instance();
 143  
 144          // Check we get writable directory.
 145          $storage1 = $installer->make_installfromzip_storage();
 146          $this->assertTrue(is_dir($storage1));
 147          $this->assertTrue(is_writable($storage1));
 148          file_put_contents($storage1.'/hello.txt', 'Find me if you can!');
 149  
 150          // Check we get unique directory on each call.
 151          $storage2 = $installer->make_installfromzip_storage();
 152          $this->assertTrue(is_dir($storage2));
 153          $this->assertTrue(is_writable($storage2));
 154          $this->assertFalse(file_exists($storage2.'/hello.txt'));
 155  
 156          // Check both are in the same parent directory.
 157          $this->assertEquals(dirname($storage1), dirname($storage2));
 158      }
 159  }