summaryrefslogtreecommitdiff
path: root/_testing/bootstrap.php
blob: a97e6f089784a2ef2a67619b4a2ea5141bf72ff6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
 * Test Library for DokuWiki
 *
 * Simulates a full DokuWiki HTTP Request and allows
 * runtime inspection.
 */

// helper for recursive copy()
function rcopy($destdir, $source) {
	if (!is_dir($source)) {
		copy($source, $destdir.'/'.basename($source));
	} else {
		$newdestdir = $destdir.'/'.basename($source);
		mkdir($newdestdir);

		$dh = dir($source);
		while (false !== ($entry = $dh->read())) {
			if ($entry == '.' || $entry == '..') {
				continue;
			}
			rcopy($newdestdir, $source.'/'.$entry);
		}
		$dh->close();
	}
}

// helper for recursive rmdir()/unlink()
function rdelete($target) {
	if (!is_dir($target)) {
		unlink($target);
	} else {
		$dh = dir($target);
		while (false !== ($entry = $dh->read())) {
			if ($entry == '.' || $entry == '..') {
				continue;
			}
			rdelete("$target/$entry");
		}
		$dh->close();
		rmdir($target);
	}
}

// helper to append text to a file
function fappend($file, $text) {
	$fh = fopen($file, 'a');
	fwrite($fh, $text);
	fclose($fh);
}

// if someone really wants a special handling during tests
define('DOKU_UNITTEST', true);
define('SIMPLE_TEST', true);

// basic behaviours
error_reporting(E_ALL);
set_time_limit(0);
ini_set('memory_limit','2048M');

// default plugins
$default_plugins = array(
	'acl',
	'action',
	'admin',
	'config',
	'info',
	'plugin',
	'popularity',
	'remote',
	'revert',
	'safefnrecode',
	'syntax',
	'usermanager'
);

// prepare temporary directories
define('DOKU_INC', dirname(dirname(__FILE__)).'/');
define('TMP_DIR', '/tmp/dwtests-'.microtime(true));
define('DOKU_CONF', TMP_DIR.'/conf/');
define('DOKU_TMP_DATA', TMP_DIR.'/data/');

// create temp directories
mkdir(TMP_DIR);

// cleanup dir after exit
register_shutdown_function(function() {
	rdelete(TMP_DIR);
});

// populate default dirs
rcopy(TMP_DIR, dirname(__FILE__).'/conf');
rcopy(TMP_DIR, dirname(__FILE__).'/data');

// disable all non-default plugins by default
$dh = dir(DOKU_INC.'lib/plugins/');
while (false !== ($entry = $dh->read())) {
	if ($entry == '.' || $entry == '..' || $entry == 'index.html') {
		continue;
	}

	if (substr($entry, strlen($entry) - 4) == '.php') {
		$plugin = substr($entry, 0, strlen($entry) - 4);
	} else {
		$plugin = $entry;
	}

	if (!in_array($plugin, $default_plugins)) {
		// disable this plugin
		fappend(DOKU_CONF.'plugins.local.php', "\$plugins['$plugin'] = 0;\n");
	}
}
$dh->close();

// TODO setup default global variables
$_SERVER['REMOTE_ADDR'] = '173.194.69.138';

// load dw
require_once(DOKU_INC.'inc/init.php');

// output buffering
$output_buffer = '';

function ob_start_callback($buffer) {
	global $output_buffer;
	$output_buffer .= $buffer;
}

// Helper class to execute a fake request
class TestRequest {
	var $server_vars = array(
			'REMOTE_ADDR' => '127.0.0.1',
		);

	var $get_vars = array();
	var $post_vars = array();

	function __construct($page = '') {
		$this->setPage($page);
	}

	function setServerVar($varName, $varValue) {
		$this->sevrer_vars[$varName] = $varValue;
	}

	function setGetVar($varName, $varValue) {
		$this->get_vars[$varName] = $varValue;
	}

	function setPostVar($varName, $varValue) {
		$this->post_vars[$varName] = $varValue;
	}

	function setPage($pageName) {
		$this->setGetVar('id', $pageName);
	}

	function addLocalConf($text) {
		$this->conf_local[] = $text;
	}

	function hook($hook, $step, $function) {
		global $EVENT_HANDLER;
		$null = null;
		$EVENT_HANDLER->register_hook($hook, $step, $null, $function);
	}

	function execute() {
		global $output_buffer;
		$output_buffer = '';

		// fake php environment
		foreach ($this->server_vars as $key => $value) {
			$_SERVER[$key] = $value;
		}
		$_REQUEST = array();
		$_GET = array();
		foreach ($this->get_vars as $key => $value) {
			$_GET[$key] = $value;
			$_REQUEST[$key] = $value;
		}
		$_POST = array();
		foreach ($this->post_vars as $key => $value) {
			$_POST[$key] = $value;
			$_REQUEST[$key] = $value;
		}

		// now execute dokuwiki and grep the output
		header_remove();
		ob_start('ob_start_callback');
		include(DOKU_INC.'doku.php');
		ob_end_flush();

		// it's done, return the page result
		return new TestResponse(
				$output_buffer,
				headers_list()
			);
	}
}

// holds a copy of all produced outputs of a TestRequest
class TestResponse {
	var $content;
	var $headers;

	function __construct($content, $headers) {
		$this->content = $content;
		$this->headers = $headers;
	}

	function getContent() {
		return $this->content;
	}

	function getHeaders() {
		return $this->headers;
	}

	// TODO provide findById, findBy... (https://github.com/cosmocode/dokuwiki-plugin-scrape/blob/master/phpQuery-onefile.php)
}