MDL-34762 course Hide system archetypes from mod chooser
[moodle.git] / mdeploytest.php
CommitLineData
89af1765
DM
1<?php
2
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/>.
17
18/**
19 * PHPUnit tests for the mdeploy.php utility
20 *
21 * Because the mdeploy.php can't be part of the Moodle code itself, this tests must be
22 * executed using something like:
23 *
24 * $ phpunit --no-configuration mdeploytest
25 *
26 * @package core
27 * @copyright 2012 David Mudrak <david@moodle.com>
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 */
30
31require(__DIR__.'/mdeploy.php');
32
33/**
34 * Provides testable input options.
35 *
36 * @copyright 2012 David Mudrak <david@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class input_fake_provider extends input_provider {
40
41 /** @var array */
42 protected $fakeoptions = array();
43
44 /**
45 * Sets fake raw options.
46 *
47 * @param array $options
48 */
49 public function set_fake_options(array $options) {
50 $this->fakeoptions = $options;
51 $this->populate_options();
52 }
53
54 /**
55 * Returns the explicitly set fake options.
56 *
57 * @return array
58 */
59 protected function parse_raw_options() {
60 return $this->fakeoptions;
61 }
62}
63
64/**
65 * Testable subclass.
66 *
67 * @copyright 2012 David Mudrak <david@moodle.com>
68 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
69 */
70class testable_input_manager extends input_manager {
71
72 /**
73 * Provides access to the protected method so we can test it explicitly.
74 */
75 public function cast_value($raw, $type) {
76 return parent::cast_value($raw, $type);
77 }
78
79 /**
80 * Sets the fake input provider.
81 */
82 protected function initialize() {
83 $this->inputprovider = input_fake_provider::instance();
84 }
85}
86
87
88/**
89 * Test cases for the mdeploy utility
90 *
91 * @copyright 2012 David Mudrak <david@moodle.com>
92 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
93 */
94class mdeploytest extends PHPUnit_Framework_TestCase {
95
96 public function test_same_singletons() {
97 $a = input_manager::instance();
98 $b = input_manager::instance();
99 $this->assertSame($a, $b);
100 }
101
102 /**
103 * @dataProvider data_for_cast_value
104 */
105 public function test_cast_value($raw, $type, $result) {
106 $input = testable_input_manager::instance();
107 $this->assertSame($input->cast_value($raw, $type), $result);
108 }
109
110 public function data_for_cast_value() {
111 return array(
112 array('3', input_manager::TYPE_INT, 3),
113 array(4, input_manager::TYPE_INT, 4),
114 array('', input_manager::TYPE_INT, 0),
115
116 array(true, input_manager::TYPE_FLAG, true),
117 array(false, input_manager::TYPE_FLAG, true),
118 array(0, input_manager::TYPE_FLAG, true),
119 array('1', input_manager::TYPE_FLAG, true),
120 array('0', input_manager::TYPE_FLAG, true),
121 array('muhehe', input_manager::TYPE_FLAG, true),
c57f18ad
DM
122
123 array('C:\\WINDOWS\\user.dat', input_manager::TYPE_PATH, 'C/WINDOWS/user.dat'),
124 array('../../../etc/passwd', input_manager::TYPE_PATH, '/etc/passwd'),
125 array('///////.././public_html/test.php', input_manager::TYPE_PATH, '/public_html/test.php'),
126
127 array("!@#$%|/etc/qwerty\n\n\t\n\r", input_manager::TYPE_RAW, "!@#$%|/etc/qwerty\n\n\t\n\r"),
128
129 array("\nrock'n'roll.mp3\t.exe", input_manager::TYPE_FILE, 'rocknroll.mp3.exe'),
4c72f555
DM
130
131 array('http://localhost/moodle/dev/plugin.zip', input_manager::TYPE_URL, 'http://localhost/moodle/dev/plugin.zip'),
132 array(
133 'https://moodle.org/plugins/download.php/1292/mod_stampcoll_moodle23_2012062201.zip',
134 input_manager::TYPE_URL,
135 'https://moodle.org/plugins/download.php/1292/mod_stampcoll_moodle23_2012062201.zip'
136 ),
6b75106a
DM
137
138 array('5e8d2ea4f50d154730100b1645fbad67', input_manager::TYPE_MD5, '5e8d2ea4f50d154730100b1645fbad67'),
89af1765
DM
139 );
140 }
141
142 /**
143 * @expectedException invalid_coding_exception
144 */
145 public function test_cast_array_argument() {
146 $input = testable_input_manager::instance();
147 $input->cast_value(array(1, 2, 3), input_manager::TYPE_INT); // must throw exception
148 }
149
150 /**
151 * @expectedException invalid_coding_exception
152 */
153 public function test_cast_object_argument() {
154 $input = testable_input_manager::instance();
155 $o = new stdClass();
156 $input->cast_value($o, input_manager::TYPE_INT); // must throw exception
157 }
158
5bd9b0ae
DM
159 /**
160 * @expectedException invalid_option_exception
161 */
162 public function test_cast_invalid_url_value() {
163 $input = testable_input_manager::instance();
164 $invalid = 'file:///etc/passwd';
165 $input->cast_value($invalid, input_manager::TYPE_URL); // must throw exception
166 }
167
6b75106a
DM
168 /**
169 * @expectedException invalid_option_exception
170 */
171 public function test_cast_invalid_md5_value() {
172 $input = testable_input_manager::instance();
173 $invalid = 'this is not a valid md5 hash';
174 $input->cast_value($invalid, input_manager::TYPE_MD5); // must throw exception
175 }
176
8ffa8d7e
DM
177 /**
178 * @expectedException invalid_option_exception
179 */
180 public function test_cast_tilde_in_path() {
181 $input = testable_input_manager::instance();
182 $invalid = '~/public_html/moodle_dev';
183 $input->cast_value($invalid, input_manager::TYPE_PATH); // must throw exception
184 }
185
89af1765
DM
186 public function test_has_option() {
187 $provider = input_fake_provider::instance();
188
189 $provider->set_fake_options(array());
190 $this->assertFalse($provider->has_option('foo')); // foo not passed
191
192 $provider->set_fake_options(array('foo' => 1));
193 $this->assertFalse($provider->has_option('foo')); // foo passed but not a known option
194
195 $provider->set_fake_options(array('foo' => 1, 'help' => false));
196 $this->assertTrue($provider->has_option('help')); // help passed and it is a flag (value ignored)
197 $this->assertTrue($provider->has_option('h')); // 'h' is a shortname for 'help'
198 }
11c3c579
DM
199
200 public function test_get_option() {
201 $input = testable_input_manager::instance();
202 $provider = input_fake_provider::instance();
203
204 $provider->set_fake_options(array('help' => false, 'passfile' => '_mdeploy.123456'));
205 $this->assertTrue($input->get_option('h'));
206 $this->assertEquals($input->get_option('passfile'), '_mdeploy.123456');
207 $this->assertEquals($input->get_option('password', 'admin123'), 'admin123');
208 try {
209 $this->assertEquals($input->get_option('password'), 'admin123'); // must throw exception (not passed but required)
210 $this->assertTrue(false);
211 } catch (missing_option_exception $e) {
212 $this->assertTrue(true);
213 }
214 }
89af1765 215}