709f2cca536953b8703a4556d860662411ea2200
[moodle.git] / admin / tool / installaddon / tests / validator_test.php
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.
14 //
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/>.
18 /**
19  * Provides the unit tests class and some helper classes
20  *
21  * @package     tool_installaddon
22  * @category    test
23  * @copyright   2013 David Mudrak <david@moodle.com>
24  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 defined('MOODLE_INTERNAL') || die();
30 /**
31  * Unit tests for the {@link tool_installaddon_installer} class
32  *
33  * @copyright 2013 David Mudrak <david@moodle.com>
34  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  */
36 class tool_installaddon_validator_testcase extends basic_testcase {
38     public function test_validate_files_layout() {
39         $fixtures = dirname(__FILE__).'/fixtures';
41         // Non-existing directory.
42         $validator = testable_tool_installaddon_validator::instance($fixtures.'/nulldir', array(
43             'null/' => true,
44             'null/lang/' => true,
45             'null/lang/en/' => true,
46             'null/lang/en/null.php' => true));
47         $this->assertEquals('testable_tool_installaddon_validator', get_class($validator));
48         $this->assertFalse($validator->execute());
49         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR,
50             'filenotexists', array('file' => 'null/')));
52         // Missing expected file
53         $validator = testable_tool_installaddon_validator::instance($fixtures.'/plugindir', array(
54             'foobar/' => true,
55             'foobar/version.php' => true,
56             'foobar/index.php' => true,
57             'foobar/lang/' => true,
58             'foobar/lang/en/' => true,
59             'foobar/lang/en/local_foobar.php' => true,
60             'foobar/NOTEXISTS.txt' => true));
61         $this->assertFalse($validator->execute());
62         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR,
63             'filenotexists', array('file' => 'foobar/NOTEXISTS.txt')));
65         // Errors during ZIP extraction
66         $validator = testable_tool_installaddon_validator::instance($fixtures.'/multidir', array(
67             'one/' => true,
68             'one/version.php' => 'Can not write target file',
69             'two/' => true,
70             'two/README.txt' => true));
71         $this->assertFalse($validator->execute());
72         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'filestatus',
73             array('file' => 'one/version.php', 'status' => 'Can not write target file')));
75         // Insufficient number of extracted files
76         $validator = testable_tool_installaddon_validator::instance($fixtures.'/emptydir', array(
77             'emptydir/' => true,
78             'emptydir/README.txt' => true));
79         $this->assertFalse($validator->execute());
80         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'filesnumber'));
82         // No wrapping directory
83         $validator = testable_tool_installaddon_validator::instance($fixtures.'/nowrapdir', array(
84             'version.php' => true,
85             'index.php' => true,
86             'lang/' => true,
87             'lang/en/' => true,
88             'lang/en/foo.php' => true));
89         $this->assertFalse($validator->execute());
90         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'onedir'));
92         // Multiple directories
93         $validator = testable_tool_installaddon_validator::instance($fixtures.'/multidir', array(
94             'one/' => true,
95             'one/version.php' => true,
96             'two/' => true,
97             'two/README.txt' => true));
98         $this->assertFalse($validator->execute());
99         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'onedir'));
101         // Invalid root directory name
102         $validator = testable_tool_installaddon_validator::instance($fixtures.'/github', array(
103             'moodle-repository_mahara-master/' => true,
104             'moodle-repository_mahara-master/lang/' => true,
105             'moodle-repository_mahara-master/lang/en/' => true,
106             'moodle-repository_mahara-master/lang/en/repository_mahara.php' => true,
107             'moodle-repository_mahara-master/version.php' => true));
108         $this->assertFalse($validator->execute());
109         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'rootdirinvalid',
110             'moodle-repository_mahara-master'));
111     }
113     public function test_validate_version_php() {
114         $fixtures = dirname(__FILE__).'/fixtures';
116         $validator = testable_tool_installaddon_validator::instance($fixtures.'/noversiontheme', array(
117             'noversion/' => true,
118             'noversion/lang/' => true,
119             'noversion/lang/en/' => true,
120             'noversion/lang/en/theme_noversion.php' => true));
121         $validator->assert_plugin_type('theme');
122         $validator->assert_moodle_version(0);
123         $this->assertTrue($validator->execute());
124         $this->assertTrue($this->has_message($validator->get_messages(), $validator::DEBUG, 'missingversionphp'));
125         $this->assertTrue(is_null($validator->get_versionphp_info()));
127         $validator = testable_tool_installaddon_validator::instance($fixtures.'/noversionmod', array(
128             'noversion/' => true,
129             'noversion/lang/' => true,
130             'noversion/lang/en/' => true,
131             'noversion/lang/en/noversion.php' => true));
132         $validator->assert_plugin_type('mod');
133         $validator->assert_moodle_version(0);
134         $this->assertFalse($validator->execute());
135         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'missingversionphp'));
137         $validator = testable_tool_installaddon_validator::instance($fixtures.'/plugindir', array(
138             'legacymod/' => true,
139             'legacymod/version.php' => true,
140             'legacymod/lang/' => true,
141             'legacymod/lang/en/' => true,
142             'legacymod/lang/en/legacymod.php' => true));
143         $validator->assert_plugin_type('mod');
144         $validator->assert_moodle_version(0);
145         $this->assertFalse($validator->execute());
146         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'versionphpsyntax', '$module'));
148         $validator = testable_tool_installaddon_validator::instance($fixtures.'/plugindir', array(
149             'foobar/' => true,
150             'foobar/version.php' => true,
151             'foobar/index.php' => true,
152             'foobar/lang/' => true));
153         $validator->assert_plugin_type('block');
154         $validator->assert_moodle_version('2013031400.00');
155         $this->assertFalse($validator->execute());
156         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'componentmismatchtype',
157             array('expected' => 'block', 'found' => 'local')));
159         $validator = testable_tool_installaddon_validator::instance($fixtures.'/plugindir', array(
160             'foobar/' => true,
161             'foobar/version.php' => true,
162             'foobar/index.php' => true,
163             'foobar/lang/' => true,
164             'foobar/lang/en/' => true,
165             'foobar/lang/en/local_foobar.php' => true));
166         $validator->assert_plugin_type('local');
167         $validator->assert_moodle_version('2013031400.00');
168         $this->assertTrue($validator->execute());
169         $this->assertTrue($validator->get_result());
170         $this->assertEquals('foobar', $validator->get_rootdir());
171         $this->assertTrue($this->has_message($validator->get_messages(), $validator::INFO, 'rootdir', 'foobar'));
172         $versionphpinfo = $validator->get_versionphp_info();
173         $this->assertInternalType('array', $versionphpinfo);
174         $this->assertCount(4, $versionphpinfo);
175         $this->assertEquals(2013031900, $versionphpinfo['version']);
176         $this->assertEquals(2013031200, $versionphpinfo['requires']);
177         $this->assertEquals('local_foobar', $versionphpinfo['component']);
178         $this->assertEquals('MATURITY_ALPHA', $versionphpinfo['maturity']); // Note we get the constant name here.
179         $this->assertEquals(MATURITY_ALPHA, constant($versionphpinfo['maturity'])); // This is how to get the real value.
180         $this->assertTrue($this->has_message($validator->get_messages(), $validator::WARNING, 'maturity', 'MATURITY_ALPHA'));
181     }
183     public function test_validate_language_pack() {
184         $fixtures = dirname(__FILE__).'/fixtures';
186         $validator = testable_tool_installaddon_validator::instance($fixtures.'/nolang', array(
187             'bah/' => true,
188             'bah/index.php' => true,
189             'bah/view.php' => true,
190             'bah/version.php' => true));
191         $validator->assert_plugin_type('mod');
192         $validator->assert_moodle_version(0);
193         $this->assertFalse($validator->execute());
194         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'missinglangenfolder'));
196         $validator = testable_tool_installaddon_validator::instance($fixtures.'/nolang', array(
197             'bah/' => true,
198             'bah/version.php' => true,
199             'bah/lang/' => true,
200             'bah/lang/en/' => true));
201         $validator->assert_plugin_type('mod');
202         $validator->assert_moodle_version(0);
203         $this->assertFalse($validator->execute());
204         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'missinglangenfile'));
206         $validator = testable_tool_installaddon_validator::instance($fixtures.'/nolang', array(
207             'bah/' => true,
208             'bah/version.php' => true,
209             'bah/lang/' => true,
210             'bah/lang/en/' => true,
211             'bah/lang/en/bleh.php' => true,
212             'bah/lang/en/bah.php' => true));
213         $validator->assert_plugin_type('mod');
214         $validator->assert_moodle_version(0);
215         $this->assertTrue($validator->execute());
216         $this->assertTrue($this->has_message($validator->get_messages(), $validator::WARNING, 'multiplelangenfiles'));
217         $this->assertTrue(is_null($validator->get_language_file_name()));
219         $validator = testable_tool_installaddon_validator::instance($fixtures.'/nolang', array(
220             'bah/' => true,
221             'bah/version.php' => true,
222             'bah/lang/' => true,
223             'bah/lang/en/' => true,
224             'bah/lang/en/bah.php' => true));
225         $validator->assert_plugin_type('block');
226         $validator->assert_moodle_version(0);
227         $this->assertFalse($validator->execute());
228         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'missingexpectedlangenfile', 'block_bah.php'));
229         $this->assertEquals('bah', $validator->get_language_file_name());
231         $validator = testable_tool_installaddon_validator::instance($fixtures.'/noversiontheme', array(
232             'noversion/' => true,
233             'noversion/lang/' => true,
234             'noversion/lang/en/' => true,
235             'noversion/lang/en/theme_noversion.php' => true));
236         $validator->assert_plugin_type('theme');
237         $validator->assert_moodle_version(0);
238         $this->assertTrue($validator->execute());
239         $this->assertTrue($this->has_message($validator->get_messages(), $validator::DEBUG, 'foundlangfile', 'theme_noversion'));
240         $this->assertEquals('theme_noversion', $validator->get_language_file_name());
242         $validator = testable_tool_installaddon_validator::instance($fixtures.'/plugindir', array(
243             'foobar/' => true,
244             'foobar/version.php' => true,
245             'foobar/index.php' => true,
246             'foobar/lang/' => true,
247             'foobar/lang/en/' => true,
248             'foobar/lang/en/local_foobar.php' => true));
249         $validator->assert_plugin_type('local');
250         $validator->assert_moodle_version('2013031400.00');
251         $this->assertTrue($validator->execute());
252         $this->assertTrue($this->has_message($validator->get_messages(), $validator::DEBUG, 'foundlangfile', 'local_foobar'));
253         $this->assertEquals('local_foobar', $validator->get_language_file_name());
254     }
256     public function test_validate_target_location() {
257         $fixtures = dirname(__FILE__).'/fixtures';
259         $validator = testable_tool_installaddon_validator::instance($fixtures.'/installed', array(
260             'greenbar/' => true,
261             'greenbar/version.php' => true,
262             'greenbar/index.php' => true,
263             'greenbar/lang/' => true,
264             'greenbar/lang/en/' => true,
265             'greenbar/lang/en/local_greenbar.php' => true));
266         $validator->assert_plugin_type('local');
267         $validator->assert_moodle_version('2013031400.00');
268         $this->assertFalse($validator->execute());
269         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'targetexists',
270             $validator->get_plugintype_location('local').'/greenbar'));
272         $validator = testable_tool_installaddon_validator::instance($fixtures.'/plugindir', array(
273             'foobar/' => true,
274             'foobar/version.php' => true,
275             'foobar/index.php' => true,
276             'foobar/lang/' => true,
277             'foobar/lang/en/' => true,
278             'foobar/lang/en/local_foobar.php' => true));
279         $validator->assert_plugin_type('local');
280         $validator->assert_moodle_version('2013031400.00');
281         $this->assertTrue($validator->execute());
282         $this->assertTrue($this->has_message($validator->get_messages(), $validator::INFO, 'pathwritable',
283             $validator->get_plugintype_location('local')));
284     }
286     public function test_parse_version_php() {
287         $fixtures = dirname(__FILE__).'/fixtures/versionphp';
289         $validator = testable_tool_installaddon_validator::instance($fixtures, array());
290         $this->assertEquals('testable_tool_installaddon_validator', get_class($validator));
292         $info = $validator->testable_parse_version_php($fixtures.'/version1.php');
293         $this->assertInternalType('array', $info);
294         $this->assertCount(7, $info);
295         $this->assertEquals('block_foobar', $info['plugin->component']);    // Later in the file.
296         $this->assertEquals('2013010100', $info['plugin->version']);        // Numeric wins over strings.
297         $this->assertEquals('2012122401', $info['plugin->requires']);       // Commented.
298         $this->assertEquals('MATURITY_STABLE', $info['module->maturity']);  // Constant wins regardless the order (non-PHP behaviour).
299         $this->assertEquals('MATURITY_ALPHA', $info['plugin->maturity']);   // Constant wins regardless the order (non-PHP behaviour).
300         $this->assertEquals('v2.3', $info['module->release']);              // String wins over numeric (non-PHP behaviour).
301         $this->assertEquals('v2.4', $info['plugin->release']);              // String wins over numeric (non-PHP behaviour).
302     }
304     // Helper methods //////////////////////////////////////////////////////////
306     protected function has_message(array $messages, $level, $msgcode, $addinfo = null) {
307         foreach ($messages as $message) {
308             if ($message->level === $level and $message->msgcode === $msgcode and $message->addinfo === $addinfo) {
309                 return true;
310             }
311         }
312         return false;
313     }
317 /**
318  * Provides access to protected methods we want to explicitly test
319  *
320  * @copyright 2013 David Mudrak <david@moodle.com>
321  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
322  */
323 class testable_tool_installaddon_validator extends tool_installaddon_validator {
325     public function testable_parse_version_php($fullpath) {
326         return parent::parse_version_php($fullpath);
327     }
329     public function get_plugintype_location($plugintype) {
331         $testableroot = make_temp_directory('testable_tool_installaddon_validator/plugintypes');
332         if (!is_dir($testableroot.'/'.$plugintype)) {
333             make_temp_directory('testable_tool_installaddon_validator/plugintypes/'.$plugintype);
334         }
336         if ($plugintype === 'local') {
337             // We need the following for the test_validate_target_location() method
338             make_temp_directory('testable_tool_installaddon_validator/plugintypes/local/greenbar');
339         }
341         return $testableroot.'/'.$plugintype;
342     }