MDL-75553 mod_data: Fix wording of data fields in Behat tests
[moodle.git] / lib / tests / componentlib_test.php
blob7e6ac394b86ce16e1bd0f8a909caa5acfa372eb1
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 test
22 * @copyright 2011 Tomasz Muras
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 namespace core;
28 use component_installer;
29 use lang_installer;
30 use lang_installer_exception;
32 defined('MOODLE_INTERNAL') || die();
34 global $CFG;
35 require_once($CFG->libdir.'/componentlib.class.php');
37 /**
38 * Unit tests for /lib/componentlib.class.php.
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
45 class componentlib_test extends \advanced_testcase {
47 public function test_component_installer() {
48 global $CFG;
50 $url = $this->getExternalTestFileUrl('');
51 $ci = new component_installer($url, '', 'downloadtests.zip');
52 $this->assertTrue($ci->check_requisites());
54 $destpath = $CFG->dataroot.'/downloadtests';
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);
62 $this->assertSame(COMPONENT_NEEDUPDATE, $ci->need_upgrade());
64 $status = $ci->install();
65 $this->assertSame(COMPONENT_INSTALLED, $status);
66 $this->assertSame('9e94f74b3efb1ff6cf075dc6b2abf15c', $ci->get_component_md5());
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);
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'));
78 /**
79 * Test the public API of the {@link lang_installer} class.
81 public function test_lang_installer() {
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);
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);
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);
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);
132 * Testable lang_installer subclass that does not actually install anything
133 * and provides access to the protected methods of the parent class
135 * @copyright 2011 David Mudrak <david@moodle.com>
136 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
138 class testable_lang_installer extends lang_installer {
141 * @see parent::is_queued()
143 public function protected_is_queued($langcode = '') {
144 return $this->is_queued($langcode);
148 * @see parent::add_to_queue()
150 public function protected_add_to_queue($langcodes) {
151 return $this->add_to_queue($langcodes);
155 * Simulate lang pack installation via component_installer.
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
162 * @see parent::install_language_pack()
164 protected function install_language_pack($langcode) {
166 switch ($langcode) {
167 case 'de_du':
168 case 'de_kids':
169 return self::RESULT_INSTALLED;
171 case 'cs':
172 case 'de':
173 return self::RESULT_UPTODATE;
175 case 'xx':
176 return self::RESULT_DOWNLOADERROR;
178 default:
179 throw new lang_installer_exception('testing-unknown-exception', $langcode);
184 * Simulate detection of parent language.
186 * @see parent::get_parent_language()
188 protected function get_parent_language($langcode) {
190 switch ($langcode) {
191 case 'de_kids':
192 return 'de_du';
193 case 'de_du':
194 return 'de';
195 default:
196 return '';