Merge branch 'wip-mdl-52007' of https://github.com/rajeshtaneja/moodle
[moodle.git] / lib / tests / componentlib_test.php
blob53a401c4b5ba0b3dd50015e7bb6762ef2fe35e25
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 core_componentlib_testcase extends advanced_testcase {
33 public function test_component_installer() {
34 global $CFG;
36 $url = $this->getExternalTestFileUrl('');
37 $ci = new component_installer($url, '', 'downloadtests.zip');
38 $this->assertTrue($ci->check_requisites());
40 $destpath = $CFG->dataroot.'/downloadtests';
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);
48 $this->assertSame(COMPONENT_NEEDUPDATE, $ci->need_upgrade());
50 $status = $ci->install();
51 $this->assertSame(COMPONENT_INSTALLED, $status);
52 $this->assertSame('9e94f74b3efb1ff6cf075dc6b2abf15c', $ci->get_component_md5());
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);
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'));
64 /**
65 * Test the public API of the {@link lang_installer} class.
67 public function test_lang_installer() {
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);
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);
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);
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);
118 * Testable lang_installer subclass that does not actually install anything
119 * and provides access to the protected methods of the parent class
121 * @copyright 2011 David Mudrak <david@moodle.com>
122 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
124 class testable_lang_installer extends lang_installer {
127 * @see parent::is_queued()
129 public function protected_is_queued($langcode = '') {
130 return $this->is_queued($langcode);
134 * @see parent::add_to_queue()
136 public function protected_add_to_queue($langcodes) {
137 return $this->add_to_queue($langcodes);
141 * Simulate lang pack installation via component_installer.
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
148 * @see parent::install_language_pack()
150 protected function install_language_pack($langcode) {
152 switch ($langcode) {
153 case 'de_du':
154 case 'de_kids':
155 return self::RESULT_INSTALLED;
157 case 'cs':
158 case 'de':
159 return self::RESULT_UPTODATE;
161 case 'xx':
162 return self::RESULT_DOWNLOADERROR;
164 default:
165 throw new lang_installer_exception('testing-unknown-exception', $langcode);
170 * Simulate detection of parent language.
172 * @see parent::get_parent_language()
174 protected function get_parent_language($langcode) {
176 switch ($langcode) {
177 case 'de_kids':
178 return 'de_du';
179 case 'de_du':
180 return 'de';
181 default:
182 return '';