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 310 and 401] [Versions 39 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  /**
  18   * Unit tests for /lib/componentlib.class.php.
  19   *
  20   * @package   core
  21   * @category  test
  22   * @copyright 2011 Tomasz Muras
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core;
  27  
  28  use component_installer;
  29  use lang_installer;
  30  use lang_installer_exception;
  31  
  32  defined('MOODLE_INTERNAL') || die();
  33  
  34  global $CFG;
  35  require_once($CFG->libdir.'/componentlib.class.php');
  36  
  37  /**
  38   * Unit tests for /lib/componentlib.class.php.
  39   *
  40   * @package   core
  41   * @category  test
  42   * @copyright 2011 Tomasz Muras
  43   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   */
  45  class componentlib_test extends \advanced_testcase {
  46  
  47      public function test_component_installer() {
  48          global $CFG;
  49  
  50          $url = $this->getExternalTestFileUrl('');
  51          $ci = new component_installer($url, '', 'downloadtests.zip');
  52          $this->assertTrue($ci->check_requisites());
  53  
  54          $destpath = $CFG->dataroot.'/downloadtests';
  55  
  56          // Carefully remove component files to enforce fresh installation.
  57          @unlink($destpath.'/'.'downloadtests.md5');
  58          @unlink($destpath.'/'.'test.html');
  59          @unlink($destpath.'/'.'test.jpg');
  60          @rmdir($destpath);
  61  
  62          $this->assertSame(COMPONENT_NEEDUPDATE, $ci->need_upgrade());
  63  
  64          $status = $ci->install();
  65          $this->assertSame(COMPONENT_INSTALLED, $status);
  66          $this->assertSame('9e94f74b3efb1ff6cf075dc6b2abf15c', $ci->get_component_md5());
  67  
  68          // It's already installed, so Moodle should detect it's up to date.
  69          $this->assertSame(COMPONENT_UPTODATE, $ci->need_upgrade());
  70          $status = $ci->install();
  71          $this->assertSame(COMPONENT_UPTODATE, $status);
  72  
  73          // Check if correct files were downloaded.
  74          $this->assertSame('2af180e813dc3f446a9bb7b6af87ce24', md5_file($destpath.'/'.'test.jpg'));
  75          $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5_file($destpath.'/'.'test.html'));
  76      }
  77  
  78      /**
  79       * Test the public API of the {@link lang_installer} class.
  80       */
  81      public function test_lang_installer() {
  82  
  83          // Test the manipulation with the download queue.
  84          $installer = new testable_lang_installer();
  85          $this->assertFalse($installer->protected_is_queued());
  86          $installer->protected_add_to_queue('cs');
  87          $installer->protected_add_to_queue(array('cs', 'sk'));
  88          $this->assertTrue($installer->protected_is_queued());
  89          $this->assertTrue($installer->protected_is_queued('cs'));
  90          $this->assertTrue($installer->protected_is_queued('sk'));
  91          $this->assertFalse($installer->protected_is_queued('de_kids'));
  92          $installer->set_queue('de_kids');
  93          $this->assertFalse($installer->protected_is_queued('cs'));
  94          $this->assertFalse($installer->protected_is_queued('sk'));
  95          $this->assertFalse($installer->protected_is_queued('de'));
  96          $this->assertFalse($installer->protected_is_queued('de_du'));
  97          $this->assertTrue($installer->protected_is_queued('de_kids'));
  98          $installer->set_queue(array('cs', 'de_kids'));
  99          $this->assertTrue($installer->protected_is_queued('cs'));
 100          $this->assertFalse($installer->protected_is_queued('sk'));
 101          $this->assertFalse($installer->protected_is_queued('de'));
 102          $this->assertFalse($installer->protected_is_queued('de_du'));
 103          $this->assertTrue($installer->protected_is_queued('de_kids'));
 104          $installer->set_queue(array());
 105          $this->assertFalse($installer->protected_is_queued());
 106          unset($installer);
 107  
 108          // Install a set of lang packs.
 109          $installer = new testable_lang_installer(array('cs', 'de_kids', 'xx'));
 110          $result = $installer->run();
 111          $this->assertSame($result['cs'], lang_installer::RESULT_UPTODATE);
 112          $this->assertSame($result['de_kids'], lang_installer::RESULT_INSTALLED);
 113          $this->assertSame($result['xx'], lang_installer::RESULT_DOWNLOADERROR);
 114  
 115          // The following two were automatically added to the queue.
 116          $this->assertSame($result['de_du'], lang_installer::RESULT_INSTALLED);
 117          $this->assertSame($result['de'], lang_installer::RESULT_UPTODATE);
 118  
 119          // Exception throwing.
 120          $installer = new testable_lang_installer(array('yy'));
 121          try {
 122              $installer->run();
 123              $this->fail('lang_installer_exception exception expected');
 124          } catch (\moodle_exception $e) {
 125              $this->assertInstanceOf('lang_installer_exception', $e);
 126          }
 127      }
 128  }
 129  
 130  
 131  /**
 132   * Testable lang_installer subclass that does not actually install anything
 133   * and provides access to the protected methods of the parent class
 134   *
 135   * @copyright 2011 David Mudrak <david@moodle.com>
 136   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 137   */
 138  class testable_lang_installer extends lang_installer {
 139  
 140      /**
 141       * @see parent::is_queued()
 142       */
 143      public function protected_is_queued($langcode = '') {
 144          return $this->is_queued($langcode);
 145      }
 146  
 147      /**
 148       * @see parent::add_to_queue()
 149       */
 150      public function protected_add_to_queue($langcodes) {
 151          return $this->add_to_queue($langcodes);
 152      }
 153  
 154      /**
 155       * Simulate lang pack installation via component_installer.
 156       *
 157       * Language packages 'de_du' and 'de_kids' reported as installed
 158       * Language packages 'cs' and 'de' reported as up-to-date
 159       * Language package 'xx' returns download error
 160       * All other language packages will throw an unknown exception
 161       *
 162       * @see parent::install_language_pack()
 163       */
 164      protected function install_language_pack($langcode) {
 165  
 166          switch ($langcode) {
 167              case 'de_du':
 168              case 'de_kids':
 169                  return self::RESULT_INSTALLED;
 170  
 171              case 'cs':
 172              case 'de':
 173                  return self::RESULT_UPTODATE;
 174  
 175              case 'xx':
 176                  return self::RESULT_DOWNLOADERROR;
 177  
 178              default:
 179                  throw new lang_installer_exception('testing-unknown-exception', $langcode);
 180          }
 181      }
 182  
 183      /**
 184       * Simulate detection of parent language.
 185       *
 186       * @see parent::get_parent_language()
 187       */
 188      protected function get_parent_language($langcode) {
 189  
 190          switch ($langcode) {
 191              case 'de_kids':
 192                  return 'de_du';
 193              case 'de_du':
 194                  return 'de';
 195              default:
 196                  return '';
 197          }
 198      }
 199  }