Merge branch 'MDL-29276' of git://github.com/mouneyrac/moodle
[moodle.git] / lib / simpletest / testcomponentlib.php
blob13d15438efa5929163eec6ecd3c8a6c0b9eafd9a
1 <?php
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.
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/>.
19 /**
20 * Unit tests for /lib/componentlib.class.php.
22 * @package moodlecore
23 * @copyright 2011 Tomasz Muras
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
29 require_once($CFG->libdir.'/componentlib.class.php');
31 class componentlib_test extends UnitTestCase {
33 public function test_component_installer() {
34 global $CFG;
36 $ci = new component_installer('http://download.moodle.org', 'unittest', 'downloadtests.zip');
37 $this->assertTrue($ci->check_requisites());
39 $destpath = $CFG->dataroot.'/downloadtests';
41 //carefully remove component files to enforce fresh installation
42 @unlink($destpath.'/'.'downloadtests.md5');
43 @unlink($destpath.'/'.'test.html');
44 @unlink($destpath.'/'.'test.jpg');
45 @rmdir($destpath);
47 $this->assertEqual(COMPONENT_NEEDUPDATE, $ci->need_upgrade());
49 $status = $ci->install();
50 $this->assertEqual(COMPONENT_INSTALLED, $status);
51 $this->assertEqual('9e94f74b3efb1ff6cf075dc6b2abf15c', $ci->get_component_md5());
53 //it's already installed, so Moodle should detect it's up to date
54 $this->assertEqual(COMPONENT_UPTODATE, $ci->need_upgrade());
55 $status = $ci->install();
56 $this->assertEqual(COMPONENT_UPTODATE, $status);
58 //check if correct files were downloaded
59 $this->assertEqual('2af180e813dc3f446a9bb7b6af87ce24', md5_file($destpath.'/'.'test.jpg'));
60 $this->assertEqual('47250a973d1b88d9445f94db4ef2c97a', md5_file($destpath.'/'.'test.html'));
63 /**
64 * Test the public API of the {@link lang_installer} class
66 public function test_lang_installer() {
68 // test the manipulation with the download queue
69 $installer = new testable_lang_installer();
70 $this->assertFalse($installer->protected_is_queued());
71 $installer->protected_add_to_queue('cs');
72 $installer->protected_add_to_queue(array('cs', 'sk'));
73 $this->assertTrue($installer->protected_is_queued());
74 $this->assertTrue($installer->protected_is_queued('cs'));
75 $this->assertTrue($installer->protected_is_queued('sk'));
76 $this->assertFalse($installer->protected_is_queued('de_kids'));
77 $installer->set_queue('de_kids');
78 $this->assertFalse($installer->protected_is_queued('cs'));
79 $this->assertFalse($installer->protected_is_queued('sk'));
80 $this->assertFalse($installer->protected_is_queued('de'));
81 $this->assertFalse($installer->protected_is_queued('de_du'));
82 $this->assertTrue($installer->protected_is_queued('de_kids'));
83 $installer->set_queue(array('cs', 'de_kids'));
84 $this->assertTrue($installer->protected_is_queued('cs'));
85 $this->assertFalse($installer->protected_is_queued('sk'));
86 $this->assertFalse($installer->protected_is_queued('de'));
87 $this->assertFalse($installer->protected_is_queued('de_du'));
88 $this->assertTrue($installer->protected_is_queued('de_kids'));
89 $installer->set_queue(array());
90 $this->assertFalse($installer->protected_is_queued());
91 unset($installer);
93 // install a set of lang packs
94 $installer = new testable_lang_installer(array('cs', 'de_kids', 'xx'));
95 $result = $installer->run();
96 $this->assertEqual($result['cs'], lang_installer::RESULT_UPTODATE);
97 $this->assertEqual($result['de_kids'], lang_installer::RESULT_INSTALLED);
98 $this->assertEqual($result['xx'], lang_installer::RESULT_DOWNLOADERROR);
99 // the following two were automatically added to the queue
100 $this->assertEqual($result['de_du'], lang_installer::RESULT_INSTALLED);
101 $this->assertEqual($result['de'], lang_installer::RESULT_UPTODATE);
103 // exception throwing
104 $installer = new testable_lang_installer(array('yy'));
105 $this->expectException('lang_installer_exception');
106 $installer->run();
111 * Testable lang_installer subclass that does not actually install anything
112 * and provides access to the protected methods of the parent class
114 * @copyright 2011 David Mudrak <david@moodle.com>
115 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
117 class testable_lang_installer extends lang_installer {
120 * @see parent::is_queued()
122 public function protected_is_queued($langcode = '') {
123 return $this->is_queued($langcode);
127 * @see parent::add_to_queue()
129 public function protected_add_to_queue($langcodes) {
130 return $this->add_to_queue($langcodes);
134 * Simulate lang pack installation via component_installer
136 * Language packages 'de_du' and 'de_kids' reported as installed
137 * Language packages 'cs' and 'de' reported as up-to-date
138 * Language package 'xx' returns download error
139 * All other language packages will throw an unknown exception
141 * @see parent::install_language_pack()
143 protected function install_language_pack($langcode) {
145 switch ($langcode) {
146 case 'de_du':
147 case 'de_kids':
148 return self::RESULT_INSTALLED;
150 case 'cs':
151 case 'de':
152 return self::RESULT_UPTODATE;
154 case 'xx':
155 return self::RESULT_DOWNLOADERROR;
157 default:
158 throw new lang_installer_exception('testing-unknown-exception', $langcode);
163 * Simulate detection of parent languge
165 * @see parent::get_parent_language()
167 protected function get_parent_language($langcode) {
169 switch ($langcode) {
170 case 'de_kids':
171 return 'de_du';
172 case 'de_du':
173 return 'de';
174 default:
175 return '';