3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * PHPUnit tests for the mdeploy.php utility
21 * Because the mdeploy.php can't be part of the Moodle code itself, this tests must be
22 * executed using something like:
24 * $ phpunit --no-configuration mdeploytest
27 * @copyright 2012 David Mudrak <david@moodle.com>
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 require(__DIR__
.'/mdeploy.php');
34 * Provides testable input options.
36 * @copyright 2012 David Mudrak <david@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class input_fake_provider
extends input_provider
{
42 protected $fakeoptions = array();
45 * Sets fake raw options.
47 * @param array $options
49 public function set_fake_options(array $options) {
50 $this->fakeoptions
= $options;
51 $this->populate_options();
55 * Returns the explicitly set fake options.
59 protected function parse_raw_options() {
60 return $this->fakeoptions
;
67 * @copyright 2012 David Mudrak <david@moodle.com>
68 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
70 class testable_input_manager
extends input_manager
{
73 * Provides access to the protected method so we can test it explicitly.
75 public function cast_value($raw, $type) {
76 return parent
::cast_value($raw, $type);
80 * Sets the fake input provider.
82 protected function initialize() {
83 $this->inputprovider
= input_fake_provider
::instance();
91 * @copyright 2012 David Mudrak <david@moodle.com>
92 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
94 class testable_worker
extends worker
{
97 * Provides access to the protected method.
99 public function move_directory($source, $target, $keepsourceroot = false) {
100 return parent
::move_directory($source, $target, $keepsourceroot);
104 * Provides access to the protected method.
106 public function remove_directory($path, $keeppathroot = false) {
107 return parent
::remove_directory($path, $keeppathroot);
113 * Test cases for the mdeploy utility
115 * @copyright 2012 David Mudrak <david@moodle.com>
116 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
118 class mdeploytest
extends PHPUnit_Framework_TestCase
{
120 public function test_same_singletons() {
121 $a = input_manager
::instance();
122 $b = input_manager
::instance();
123 $this->assertSame($a, $b);
127 * @dataProvider data_for_cast_value
129 public function test_cast_value($raw, $type, $result) {
130 $input = testable_input_manager
::instance();
131 $this->assertSame($input->cast_value($raw, $type), $result);
134 public function data_for_cast_value() {
136 array('3', input_manager
::TYPE_INT
, 3),
137 array(4, input_manager
::TYPE_INT
, 4),
138 array('', input_manager
::TYPE_INT
, 0),
140 array(true, input_manager
::TYPE_FLAG
, true),
141 array(false, input_manager
::TYPE_FLAG
, true),
142 array(0, input_manager
::TYPE_FLAG
, true),
143 array('1', input_manager
::TYPE_FLAG
, true),
144 array('0', input_manager
::TYPE_FLAG
, true),
145 array('muhehe', input_manager
::TYPE_FLAG
, true),
147 array('C:\\WINDOWS\\user.dat', input_manager
::TYPE_PATH
, 'C/WINDOWS/user.dat'),
148 array('../../../etc/passwd', input_manager
::TYPE_PATH
, '/etc/passwd'),
149 array('///////.././public_html/test.php', input_manager
::TYPE_PATH
, '/public_html/test.php'),
151 array("!@#$%|/etc/qwerty\n\n\t\n\r", input_manager
::TYPE_RAW
, "!@#$%|/etc/qwerty\n\n\t\n\r"),
153 array("\nrock'n'roll.mp3\t.exe", input_manager
::TYPE_FILE
, 'rocknroll.mp3.exe'),
155 array('http://localhost/moodle/dev/plugin.zip', input_manager
::TYPE_URL
, 'http://localhost/moodle/dev/plugin.zip'),
157 'https://moodle.org/plugins/download.php/1292/mod_stampcoll_moodle23_2012062201.zip',
158 input_manager
::TYPE_URL
,
159 'https://moodle.org/plugins/download.php/1292/mod_stampcoll_moodle23_2012062201.zip'
162 array('5e8d2ea4f50d154730100b1645fbad67', input_manager
::TYPE_MD5
, '5e8d2ea4f50d154730100b1645fbad67'),
167 * @expectedException invalid_coding_exception
169 public function test_cast_array_argument() {
170 $input = testable_input_manager
::instance();
171 $input->cast_value(array(1, 2, 3), input_manager
::TYPE_INT
); // must throw exception
175 * @expectedException invalid_coding_exception
177 public function test_cast_object_argument() {
178 $input = testable_input_manager
::instance();
180 $input->cast_value($o, input_manager
::TYPE_INT
); // must throw exception
184 * @expectedException invalid_option_exception
186 public function test_cast_invalid_url_value() {
187 $input = testable_input_manager
::instance();
188 $invalid = 'file:///etc/passwd';
189 $input->cast_value($invalid, input_manager
::TYPE_URL
); // must throw exception
193 * @expectedException invalid_option_exception
195 public function test_cast_invalid_md5_value() {
196 $input = testable_input_manager
::instance();
197 $invalid = 'this is not a valid md5 hash';
198 $input->cast_value($invalid, input_manager
::TYPE_MD5
); // must throw exception
202 * @expectedException invalid_option_exception
204 public function test_cast_tilde_in_path() {
205 $input = testable_input_manager
::instance();
206 $invalid = '~/public_html/moodle_dev';
207 $input->cast_value($invalid, input_manager
::TYPE_PATH
); // must throw exception
210 public function test_has_option() {
211 $provider = input_fake_provider
::instance();
213 $provider->set_fake_options(array());
214 $this->assertFalse($provider->has_option('foo')); // foo not passed
216 $provider->set_fake_options(array('foo' => 1));
217 $this->assertFalse($provider->has_option('foo')); // foo passed but not a known option
219 $provider->set_fake_options(array('foo' => 1, 'help' => false));
220 $this->assertTrue($provider->has_option('help')); // help passed and it is a flag (value ignored)
221 $this->assertTrue($provider->has_option('h')); // 'h' is a shortname for 'help'
224 public function test_get_option() {
225 $input = testable_input_manager
::instance();
226 $provider = input_fake_provider
::instance();
228 $provider->set_fake_options(array('help' => false, 'passfile' => '_mdeploy.123456'));
229 $this->assertTrue($input->get_option('h'));
230 $this->assertEquals($input->get_option('passfile'), '_mdeploy.123456');
231 $this->assertEquals($input->get_option('password', 'admin123'), 'admin123');
233 $this->assertEquals($input->get_option('password'), 'admin123'); // must throw exception (not passed but required)
234 $this->assertTrue(false);
235 } catch (missing_option_exception
$e) {
236 $this->assertTrue(true);
240 public function test_moving_and_removing_directories() {
241 $worker = testable_worker
::instance();
243 $root = sys_get_temp_dir().'/'.uniqid('mdeploytest', true);
244 mkdir($root.'/a', 0777, true);
245 touch($root.'/a/a.txt');
247 $this->assertTrue(file_exists($root.'/a/a.txt'));
248 $this->assertFalse(file_exists($root.'/b/a.txt'));
249 $this->assertTrue($worker->move_directory($root.'/a', $root.'/b'));
250 $this->assertFalse(is_dir($root.'/a'));
251 $this->assertTrue(file_exists($root.'/b/a.txt'));
252 $this->assertTrue($worker->move_directory($root.'/b', $root.'/c', true));
253 $this->assertTrue(file_exists($root.'/c/a.txt'));
254 $this->assertFalse(file_exists($root.'/b/a.txt'));
255 $this->assertTrue(is_dir($root.'/b'));
256 $this->assertTrue($worker->remove_directory($root.'/c', true));
257 $this->assertFalse(file_exists($root.'/c/a.txt'));
258 $this->assertTrue($worker->remove_directory($root.'/c'));
259 $this->assertFalse(is_dir($root.'/c'));