Merge branch 'MDL-38424_m23' of git://github.com/kordan/moodle into MOODLE_23_STABLE
[moodle.git] / lib / tests / componentlib_test.php
blobcbe0713dd0783dab09da325ae00cc7c7d2651897
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.
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/>.
17 /**
18 * Unit tests for /lib/componentlib.class.php.
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
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->libdir.'/componentlib.class.php');
31 class componentlib_testcase extends advanced_testcase {
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->assertEquals(COMPONENT_NEEDUPDATE, $ci->need_upgrade());
49 $status = $ci->install();
50 $this->assertEquals(COMPONENT_INSTALLED, $status);
51 $this->assertEquals('9e94f74b3efb1ff6cf075dc6b2abf15c', $ci->get_component_md5());
53 //it's already installed, so Moodle should detect it's up to date
54 $this->assertEquals(COMPONENT_UPTODATE, $ci->need_upgrade());
55 $status = $ci->install();
56 $this->assertEquals(COMPONENT_UPTODATE, $status);
58 //check if correct files were downloaded
59 $this->assertEquals('2af180e813dc3f446a9bb7b6af87ce24', md5_file($destpath.'/'.'test.jpg'));
60 $this->assertEquals('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->assertEquals($result['cs'], lang_installer::RESULT_UPTODATE);
97 $this->assertEquals($result['de_kids'], lang_installer::RESULT_INSTALLED);
98 $this->assertEquals($result['xx'], lang_installer::RESULT_DOWNLOADERROR);
99 // the following two were automatically added to the queue
100 $this->assertEquals($result['de_du'], lang_installer::RESULT_INSTALLED);
101 $this->assertEquals($result['de'], lang_installer::RESULT_UPTODATE);
103 // exception throwing
104 $installer = new testable_lang_installer(array('yy'));
105 try {
106 $installer->run();
107 $this->fail('lang_installer_exception exception expected');
108 } catch (Exception $e) {
109 $this->assertEquals('lang_installer_exception', get_class($e));
115 * Testable lang_installer subclass that does not actually install anything
116 * and provides access to the protected methods of the parent class
118 * @copyright 2011 David Mudrak <david@moodle.com>
119 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
121 class testable_lang_installer extends lang_installer {
124 * @see parent::is_queued()
126 public function protected_is_queued($langcode = '') {
127 return $this->is_queued($langcode);
131 * @see parent::add_to_queue()
133 public function protected_add_to_queue($langcodes) {
134 return $this->add_to_queue($langcodes);
138 * Simulate lang pack installation via component_installer
140 * Language packages 'de_du' and 'de_kids' reported as installed
141 * Language packages 'cs' and 'de' reported as up-to-date
142 * Language package 'xx' returns download error
143 * All other language packages will throw an unknown exception
145 * @see parent::install_language_pack()
147 protected function install_language_pack($langcode) {
149 switch ($langcode) {
150 case 'de_du':
151 case 'de_kids':
152 return self::RESULT_INSTALLED;
154 case 'cs':
155 case 'de':
156 return self::RESULT_UPTODATE;
158 case 'xx':
159 return self::RESULT_DOWNLOADERROR;
161 default:
162 throw new lang_installer_exception('testing-unknown-exception', $langcode);
167 * Simulate detection of parent languge
169 * @see parent::get_parent_language()
171 protected function get_parent_language($langcode) {
173 switch ($langcode) {
174 case 'de_kids':
175 return 'de_du';
176 case 'de_du':
177 return 'de';
178 default:
179 return '';