From 4993701ac4ac5be9197756289d38f05f8c6bd048 Mon Sep 17 00:00:00 2001 From: Andreas Gohr <gohr@cosmocode.de> Date: Tue, 17 Apr 2012 09:44:53 +0200 Subject: [PATCH] reset configs and other inits between tests --- _testing/bootstrap.php | 287 ++++++++++-------- .../tests/testing/inttests_reset.test.php | 40 +++ 2 files changed, 205 insertions(+), 122 deletions(-) create mode 100644 _testing/tests/testing/inttests_reset.test.php diff --git a/_testing/bootstrap.php b/_testing/bootstrap.php index cb8a3b079..5b828141f 100644 --- a/_testing/bootstrap.php +++ b/_testing/bootstrap.php @@ -8,45 +8,45 @@ // 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(); - } + 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); - } + 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); + $fh = fopen($file, 'a'); + fwrite($fh, $text); + fclose($fh); } // if someone really wants a special handling during tests @@ -66,48 +66,48 @@ define('DOKU_TMP_DATA', TMP_DIR.'/data/'); // default plugins $default_plugins = array( - 'acl', - 'action', - 'admin', - 'config', - 'info', - 'plugin', - 'popularity', - 'remote', - 'revert', - 'safefnrecode', - 'syntax', - 'usermanager' + '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(), + '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 @@ -115,7 +115,7 @@ mkdir(TMP_DIR); // cleanup dir after exit register_shutdown_function(function() { - rdelete(TMP_DIR); + rdelete(TMP_DIR); }); // populate default dirs @@ -125,20 +125,20 @@ 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"); - } + 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(); @@ -147,7 +147,7 @@ $_GET = array('id' => ''); $_POST = array(); $_REQUEST = array('id' => ''); foreach ($default_server_vars as $key => $value) { - $_SERVER[$key] = $value; + $_SERVER[$key] = $value; } // load dw @@ -157,56 +157,99 @@ require_once(DOKU_INC.'inc/init.php'); $output_buffer = ''; function ob_start_callback($buffer) { - global $output_buffer; - $output_buffer .= $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 + // nothing for now, makes migration easy + + function setUp() { + // reload config + global $conf, $config_cascade; + $conf = array(); + foreach (array('default','local','protected') as $config_group) { + if (empty($config_cascade['main'][$config_group])) continue; + foreach ($config_cascade['main'][$config_group] as $config_file) { + if (@file_exists($config_file)) { + include($config_file); + } + } + } + + // reload license config + global $license; + $license = array(); + + // load the license file(s) + foreach (array('default','local') as $config_group) { + if (empty($config_cascade['license'][$config_group])) continue; + foreach ($config_cascade['license'][$config_group] as $config_file) { + if(@file_exists($config_file)){ + include($config_file); + } + } + } + + // make real paths and check them + init_paths(); + init_files(); + + // reset loaded plugins + global $plugin_controller_class, $plugin_controller; + $plugin_controller = new $plugin_controller_class(); + global $EVENT_HANDLER; + $EVENT_HANDLER = new Doku_Event_Handler(); + + // reload language + $local = $conf['lang']; + trigger_event('INIT_LANG_LOAD', $local, 'init_lang', true); + } + } // 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() - ); - } + 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; + var $content; + var $headers; - function __construct($content, $headers) { - $this->content = $content; - $this->headers = $headers; - } + function __construct($content, $headers) { + $this->content = $content; + $this->headers = $headers; + } - function getContent() { - return $this->content; - } + function getContent() { + return $this->content; + } - function getHeaders() { - return $this->headers; - } + function getHeaders() { + return $this->headers; + } } diff --git a/_testing/tests/testing/inttests_reset.test.php b/_testing/tests/testing/inttests_reset.test.php new file mode 100644 index 000000000..2241421f5 --- /dev/null +++ b/_testing/tests/testing/inttests_reset.test.php @@ -0,0 +1,40 @@ +<?php + +/** + * @group integration + */ +class InttestsScopeTest extends DokuWikiTest { + + public $triggered = false; + + function testFirstRun(){ + global $conf; + $conf['foo'] = 'bar'; + + global $EVENT_HANDLER; + $null = null; + $self = $this; + $EVENT_HANDLER->register_hook('DOKUWIKI_STARTED', 'AFTER', $null, + function() use ($self) { + $self->triggered = true; + } + ); + $request = new TestRequest(); + $request->execute(); + $this->assertTrue($this->triggered); + } + + /** + * @depends testFirstRun + */ + function testSecondRun(){ + global $conf; + $this->assertFalse(isset($conf['foo']), 'conf setting'); + + $request = new TestRequest(); + $request->execute(); + + $this->assertFalse($this->triggered, 'trigger'); + } + +} -- GitLab