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
|
<?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');
// 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/');
// default plugins
$default_plugins = array(
'acl',
'action',
'admin',
'config',
'info',
'plugin',
'popularity',
'remote',
'revert',
'safefnrecode',
'syntax',
'usermanager'
);
// default server variables
$default_server_vars = array(
'QUERY_STRING' => '?id=',
'REQUEST_METHOD' => 'GET',
'CONTENT_TYPE' => '',
'CONTENT_LENGTH' => '',
'SCRIPT_NAME' => '/doku.php',
'REQUEST_URI' => '/doku.php?id=',
'DOCUMENT_URI' => '/doku.php',
'DOCUMENT_ROOT' => DOKU_INC,
'SERVER_PROTOCOL' => 'HTTP/1.1',
'SERVER_SOFTWARE' => 'nginx/0.7.67',
'REMOTE_ADDR' => '87.142.120.6',
'REMOTE_PORT' => '21418',
'SERVER_ADDR' => '46.38.241.24',
'SERVER_PORT' => '443',
'SERVER_NAME' => 'wiki.example.com',
'REDIRECT_STATUS' => '200',
'SCRIPT_FILENAME' => DOKU_INC.'doku.php',
'HTTP_HOST' => 'wiki.example.com',
'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; OpenBSD amd64; rv:11.0) Gecko/20100101 Firefox/11.0',
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.5',
'HTTP_ACCEPT_ENCODING' => 'gzip, deflate',
'HTTP_CONNECTION' => 'keep-alive',
'HTTP_CACHE_CONTROL' => 'max-age=0',
'PHP_SELF' => '/doku.php',
'REQUEST_TIME' => time(),
);
// 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();
// setup default global variables
$_GET = array('id' => '');
$_POST = array();
$_REQUEST = array('id' => '');
foreach ($default_server_vars as $key => $value) {
$_SERVER[$key] = $value;
}
// 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 provide basic functionality for tests
abstract class DokuWikiTest extends PHPUnit_Framework_TestCase {
// nothing for now, makes migration easy
}
// Helper class to execute a fake request
class TestRequest {
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 = '';
// 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;
}
}
|