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]

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