MDL-48494 admin: Fail validation of plugins with no component declared
[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.'/nocomponent', array(
149             'baz/' => true,
150             'baz/version.php' => true,
151             'baz/lang/' => true,
152             'baz/lang/en/' => true,
153             'baz/lang/en/auth_baz.php' => true));
154         $validator->assert_plugin_type('auth');
155         $validator->assert_moodle_version(0);
156         $this->assertFalse($validator->execute());
157         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'missingcomponent'));
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         $validator->assert_plugin_type('block');
165         $validator->assert_moodle_version('2013031400.00');
166         $this->assertFalse($validator->execute());
167         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'componentmismatchtype',
168             array('expected' => 'block', 'found' => 'local')));
170         $validator = testable_tool_installaddon_validator::instance($fixtures.'/plugindir', array(
171             'foobar/' => true,
172             'foobar/version.php' => true,
173             'foobar/index.php' => true,
174             'foobar/lang/' => true,
175             'foobar/lang/en/' => true,
176             'foobar/lang/en/local_foobar.php' => true));
177         $validator->assert_plugin_type('local');
178         $validator->assert_moodle_version('2013031400.00');
179         $this->assertTrue($validator->execute());
180         $this->assertTrue($validator->get_result());
181         $this->assertEquals('foobar', $validator->get_rootdir());
182         $this->assertTrue($this->has_message($validator->get_messages(), $validator::INFO, 'rootdir', 'foobar'));
183         $versionphpinfo = $validator->get_versionphp_info();
184         $this->assertInternalType('array', $versionphpinfo);
185         $this->assertCount(4, $versionphpinfo);
186         $this->assertEquals(2013031900, $versionphpinfo['version']);
187         $this->assertEquals(2013031200, $versionphpinfo['requires']);
188         $this->assertEquals('local_foobar', $versionphpinfo['component']);
189         $this->assertEquals('MATURITY_ALPHA', $versionphpinfo['maturity']); // Note we get the constant name here.
190         $this->assertEquals(MATURITY_ALPHA, constant($versionphpinfo['maturity'])); // This is how to get the real value.
191         $this->assertTrue($this->has_message($validator->get_messages(), $validator::WARNING, 'maturity', 'MATURITY_ALPHA'));
192     }
194     public function test_validate_language_pack() {
195         $fixtures = dirname(__FILE__).'/fixtures';
197         $validator = testable_tool_installaddon_validator::instance($fixtures.'/nolang', array(
198             'bah/' => true,
199             'bah/index.php' => true,
200             'bah/view.php' => true,
201             'bah/version.php' => true));
202         $validator->assert_plugin_type('mod');
203         $validator->assert_moodle_version(0);
204         $this->assertFalse($validator->execute());
205         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'missinglangenfolder'));
207         $validator = testable_tool_installaddon_validator::instance($fixtures.'/nolang', array(
208             'bah/' => true,
209             'bah/version.php' => true,
210             'bah/lang/' => true,
211             'bah/lang/en/' => true));
212         $validator->assert_plugin_type('mod');
213         $validator->assert_moodle_version(0);
214         $this->assertFalse($validator->execute());
215         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'missinglangenfile'));
217         $validator = testable_tool_installaddon_validator::instance($fixtures.'/nolang', array(
218             'bah/' => true,
219             'bah/version.php' => true,
220             'bah/lang/' => true,
221             'bah/lang/en/' => true,
222             'bah/lang/en/bleh.php' => true,
223             'bah/lang/en/bah.php' => true));
224         $validator->assert_plugin_type('mod');
225         $validator->assert_moodle_version(0);
226         $this->assertTrue($validator->execute());
227         $this->assertTrue($this->has_message($validator->get_messages(), $validator::WARNING, 'multiplelangenfiles'));
228         $this->assertTrue(is_null($validator->get_language_file_name()));
230         $validator = testable_tool_installaddon_validator::instance($fixtures.'/wronglang', array(
231             'bah/' => true,
232             'bah/version.php' => true,
233             'bah/lang/' => true,
234             'bah/lang/en/' => true,
235             'bah/lang/en/bah.php' => true));
236         $validator->assert_plugin_type('block');
237         $validator->assert_moodle_version(0);
238         $this->assertFalse($validator->execute());
239         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'missingexpectedlangenfile', 'block_bah.php'));
240         $this->assertEquals('bah', $validator->get_language_file_name());
242         $validator = testable_tool_installaddon_validator::instance($fixtures.'/noversiontheme', array(
243             'noversion/' => true,
244             'noversion/lang/' => true,
245             'noversion/lang/en/' => true,
246             'noversion/lang/en/theme_noversion.php' => true));
247         $validator->assert_plugin_type('theme');
248         $validator->assert_moodle_version(0);
249         $this->assertTrue($validator->execute());
250         $this->assertTrue($this->has_message($validator->get_messages(), $validator::DEBUG, 'foundlangfile', 'theme_noversion'));
251         $this->assertEquals('theme_noversion', $validator->get_language_file_name());
253         $validator = testable_tool_installaddon_validator::instance($fixtures.'/plugindir', array(
254             'foobar/' => true,
255             'foobar/version.php' => true,
256             'foobar/index.php' => true,
257             'foobar/lang/' => true,
258             'foobar/lang/en/' => true,
259             'foobar/lang/en/local_foobar.php' => true));
260         $validator->assert_plugin_type('local');
261         $validator->assert_moodle_version('2013031400.00');
262         $this->assertTrue($validator->execute());
263         $this->assertTrue($this->has_message($validator->get_messages(), $validator::DEBUG, 'foundlangfile', 'local_foobar'));
264         $this->assertEquals('local_foobar', $validator->get_language_file_name());
265     }
267     public function test_validate_target_location() {
268         $fixtures = dirname(__FILE__).'/fixtures';
270         $validator = testable_tool_installaddon_validator::instance($fixtures.'/installed', array(
271             'greenbar/' => true,
272             'greenbar/version.php' => true,
273             'greenbar/index.php' => true,
274             'greenbar/lang/' => true,
275             'greenbar/lang/en/' => true,
276             'greenbar/lang/en/local_greenbar.php' => true));
277         $validator->assert_plugin_type('local');
278         $validator->assert_moodle_version('2013031400.00');
279         $this->assertFalse($validator->execute());
280         $this->assertTrue($this->has_message($validator->get_messages(), $validator::ERROR, 'targetexists',
281             $validator->get_plugintype_location('local').'/greenbar'));
283         $validator = testable_tool_installaddon_validator::instance($fixtures.'/plugindir', array(
284             'foobar/' => true,
285             'foobar/version.php' => true,
286             'foobar/index.php' => true,
287             'foobar/lang/' => true,
288             'foobar/lang/en/' => true,
289             'foobar/lang/en/local_foobar.php' => true));
290         $validator->assert_plugin_type('local');
291         $validator->assert_moodle_version('2013031400.00');
292         $this->assertTrue($validator->execute());
293         $this->assertTrue($this->has_message($validator->get_messages(), $validator::INFO, 'pathwritable',
294             $validator->get_plugintype_location('local')));
295     }
297     public function test_parse_version_php() {
298         $fixtures = dirname(__FILE__).'/fixtures/versionphp';
300         $validator = testable_tool_installaddon_validator::instance($fixtures, array());
301         $this->assertEquals('testable_tool_installaddon_validator', get_class($validator));
303         $info = $validator->testable_parse_version_php($fixtures.'/version1.php');
304         $this->assertInternalType('array', $info);
305         $this->assertCount(7, $info);
306         $this->assertEquals('block_foobar', $info['plugin->component']);    // Later in the file.
307         $this->assertEquals('2013010100', $info['plugin->version']);        // Numeric wins over strings.
308         $this->assertEquals('2012122401', $info['plugin->requires']);       // Commented.
309         $this->assertEquals('MATURITY_STABLE', $info['module->maturity']);  // Constant wins regardless the order (non-PHP behaviour).
310         $this->assertEquals('MATURITY_ALPHA', $info['plugin->maturity']);   // Constant wins regardless the order (non-PHP behaviour).
311         $this->assertEquals('v2.3', $info['module->release']);              // String wins over numeric (non-PHP behaviour).
312         $this->assertEquals('v2.4', $info['plugin->release']);              // String wins over numeric (non-PHP behaviour).
313     }
315     // Helper methods //////////////////////////////////////////////////////////
317     protected function has_message(array $messages, $level, $msgcode, $addinfo = null) {
318         foreach ($messages as $message) {
319             if ($message->level === $level and $message->msgcode === $msgcode and $message->addinfo === $addinfo) {
320                 return true;
321             }
322         }
323         return false;
324     }
328 /**
329  * Provides access to protected methods we want to explicitly test
330  *
331  * @copyright 2013 David Mudrak <david@moodle.com>
332  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
333  */
334 class testable_tool_installaddon_validator extends tool_installaddon_validator {
336     public function testable_parse_version_php($fullpath) {
337         return parent::parse_version_php($fullpath);
338     }
340     public function get_plugintype_location($plugintype) {
342         $testableroot = make_temp_directory('testable_tool_installaddon_validator/plugintypes');
343         if (!is_dir($testableroot.'/'.$plugintype)) {
344             make_temp_directory('testable_tool_installaddon_validator/plugintypes/'.$plugintype);
345         }
347         if ($plugintype === 'local') {
348             // We need the following for the test_validate_target_location() method
349             make_temp_directory('testable_tool_installaddon_validator/plugintypes/local/greenbar');
350         }
352         return $testableroot.'/'.$plugintype;
353     }