diff --git a/_testing/README b/_testing/README index 32f2a93b58a6c6c63d7fc8ce82c6e60ba8afb650..253fb249aec5f2ea71e39ef596786079193595f1 100644 --- a/_testing/README +++ b/_testing/README @@ -55,6 +55,7 @@ the PHPUnit migration and are disabled by default: * inc/parser/parser_formatting * inc/parser/xhtml_htmlphp (runkit) * inc/parser/xhtml_links + * testing/inttests_plugins (enabling/disabling of plugins is borked) TODO for the test framework @@ -65,14 +66,10 @@ TODO for the test framework * unit test [ok] * integration test [ok] * plugins - * unit test + * unit test [ok] * integration test [untested] - * provide the possibility to enable/disable plugins - * cleanup dw globals like registered event hooks * cross platform compatibility: especially test windows (hint: tmp dir location) * update http://www.dokuwiki.org/devel:unittesting * optional: add helper methods to TestRequest for easy form submission * createForm(), ... - * optional: add helper methods to TestReponse for easy response parsing - * findElementById, findElementByXPath, ... diff --git a/_testing/bootstrap.php b/_testing/bootstrap.php index b4356fa7c98bc182ec17e74f2fd2e7d8cf802f07..7d57ba1d1dfc8f8baed82a9a8859b01971b9ef3a 100644 --- a/_testing/bootstrap.php +++ b/_testing/bootstrap.php @@ -6,6 +6,7 @@ if(!defined('DOKU_UNITTEST')) define('DOKU_UNITTEST',dirname(__FILE__).'/'); require_once DOKU_UNITTEST.'core/phpQuery-onefile.php'; require_once DOKU_UNITTEST.'core/DokuWikiTest.php'; +require_once DOKU_UNITTEST.'core/TestResponse.php'; require_once DOKU_UNITTEST.'core/TestRequest.php'; require_once DOKU_UNITTEST.'core/TestUtils.php'; @@ -27,16 +28,12 @@ define('DOKU_TMP_DATA', TMP_DIR.'/data/'); // default plugins $default_plugins = array( 'acl', - 'action', - 'admin', 'config', 'info', 'plugin', 'popularity', - 'remote', 'revert', 'safefnrecode', - 'syntax', 'usermanager' ); @@ -75,7 +72,7 @@ mkdir(TMP_DIR); // cleanup dir after exit register_shutdown_function(function() { - TestUtils::rdelete(TMP_DIR); + //TestUtils::rdelete(TMP_DIR); }); // populate default dirs @@ -85,40 +82,21 @@ TestUtils::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') { + if ($entry == '.' || $entry == '..') { continue; } - if (substr($entry, strlen($entry) - 4) == '.php') { - $plugin = substr($entry, 0, strlen($entry) - 4); - } else { - $plugin = $entry; + if (!is_dir(DOKU_INC.'lib/plugins/'.$entry)) { + continue; } - if (!in_array($plugin, $default_plugins)) { + if (!in_array($entry, $default_plugins)) { // disable this plugin - TestUtils::fappend(DOKU_CONF.'plugins.local.php', "\$plugins['$plugin'] = 0;\n"); + TestUtils::fappend(DOKU_CONF.'plugins.local.php', "\$plugins['$entry'] = 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; -} - - diff --git a/_testing/conf/plugins.php b/_testing/conf/plugins.php new file mode 100644 index 0000000000000000000000000000000000000000..b2c79970d8be8991d6bbe0eb95583a4d88d1e228 --- /dev/null +++ b/_testing/conf/plugins.php @@ -0,0 +1,6 @@ +<?php +/** + * This file configures the default states of available plugins. All settings in + * the plugins.*.php files will override those here. + */ +$plugins['testing'] = 0; diff --git a/_testing/core/DokuWikiTest.php b/_testing/core/DokuWikiTest.php index 8ae261a522374ba0687517325b1a2cb77163df6c..2517e25e3193b5e5bbf6f67464cae4cc9041a040 100644 --- a/_testing/core/DokuWikiTest.php +++ b/_testing/core/DokuWikiTest.php @@ -42,6 +42,16 @@ abstract class DokuWikiTest extends PHPUnit_Framework_TestCase { // reset loaded plugins global $plugin_controller_class, $plugin_controller; $plugin_controller = new $plugin_controller_class(); + + // disable all non-default plugins + global $default_plugins; + foreach ($plugin_controller->getList() as $plugin) { + if (!in_array($plugin, $default_plugins)) { + $plugin_controller->disable($plugin); + } + } + + // reset event handler global $EVENT_HANDLER; $EVENT_HANDLER = new Doku_Event_Handler(); diff --git a/_testing/core/TestRequest.php b/_testing/core/TestRequest.php index f77c494c3a32759c32af622be44ab83c344bd679..c9fc06bf21975d1c083773b0d6b7048a2bb0628e 100644 --- a/_testing/core/TestRequest.php +++ b/_testing/core/TestRequest.php @@ -4,10 +4,34 @@ * runtime inspection. */ +// 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 = array(); + var $session = array(); + var $get = array(); + var $post = array(); + + function getServer($key) { return $this->server[$key]; } + function getSession($key) { return $this->session[$key]; } + function getGet($key) { return $this->get[$key]; } + function getPost($key) { return $this->post[$key]; } + + function setServer($key, $value) { $this->server[$key] = $value; } + function setSession($key, $value) { $this->session[$key] = $value; } + function setGet($key, $value) { $this->get[$key] = $value; } + function setPost($key, $value) { $this->post[$key] = $value; } /** * Executes the request @@ -15,6 +39,22 @@ class TestRequest { * @return TestResponse response */ function execute() { + // save old environment + $server = $_SERVER; + $session = $_SESSION; + $get = $_GET; + $post = $_POST; + $request = $_REQUEST; + + // fake environment + global $default_server_vars; + $_SERVER = array_merge($default_server_vars, $this->server); + $_SESSION = $this->session; + $_GET = $this->get; + $_POST = $this->post; + $_REQUEST = array_merge($_GET, $_POST); + + // reset output buffer global $output_buffer; $output_buffer = ''; @@ -24,44 +64,19 @@ class TestRequest { 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 { - protected $content; - protected $headers; - protected $pq = null; - - function __construct($content, $headers) { - $this->content = $content; - $this->headers = $headers; - } + // create the response object + $response = new TestResponse( + $output_buffer, + headers_list() + ); - function getContent() { - return $this->content; - } + // reset environment + $_SERVER = $server; + $_SESSION = $session; + $_GET = $get; + $_POST = $post; + $_REQUEST = $request; - function getHeaders() { - return $this->headers; - } - - /** - * Query the response for a JQuery compatible CSS selector - * - * @link https://code.google.com/p/phpquery/wiki/Selectors - * @param string selector - * @returns object a PHPQuery object - */ - function queryHTML($selector){ - if(is_null($pq)) $pq = phpQuery::newDocument($this->content); - return pq($selector); + return $response; } } diff --git a/_testing/core/TestResponse.php b/_testing/core/TestResponse.php new file mode 100644 index 0000000000000000000000000000000000000000..ed112b42e351187a4d6eeb1860e0378852e06756 --- /dev/null +++ b/_testing/core/TestResponse.php @@ -0,0 +1,38 @@ +<?php +/** + * holds a copy of all produced outputs of a TestRequest + */ +class TestResponse { + protected $content; + protected $headers; + + /** + * @var phpQueryObject + */ + protected $pq = null; + + function __construct($content, $headers) { + $this->content = $content; + $this->headers = $headers; + } + + function getContent() { + return $this->content; + } + + function getHeaders() { + return $this->headers; + } + + /** + * Query the response for a JQuery compatible CSS selector + * + * @link https://code.google.com/p/phpquery/wiki/Selectors + * @param string selector + * @returns object a PHPQuery object + */ + function queryHTML($selector){ + if(is_null($this->pq)) $this->pq = phpQuery::newDocument($this->content); + return $this->pq->find($selector); + } +} diff --git a/_testing/tests/testing/inttests_globals.test.php b/_testing/tests/testing/inttests_globals.test.php index d3beb433d0c2b78ace8a4a392835912f84482109..b45b2bf83fbe835a76a37d9e0866f79913664fc9 100644 --- a/_testing/tests/testing/inttests_globals.test.php +++ b/_testing/tests/testing/inttests_globals.test.php @@ -4,19 +4,46 @@ * @group integration */ class InttestsGlobalsTest extends DokuWikiTest { + /** - * Global variables should be restored for every test case. + * every request should be with its own variables */ - function testFirstRun() { - $this->assertEquals('87.142.120.6', $_SERVER['REMOTE_ADDR'], 'Global var not set as expected'); + function testFirstRun() { + global $EVENT_HANDLER; - $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; - } + $request = new TestRequest(); + $request->setServer('testvar', true); - /** - * @depends testFirstRun - */ - function testSecondRun() { - $this->assertEquals('87.142.120.6', $_SERVER['REMOTE_ADDR'], 'Global var not set as expected'); + $self = $this; + $EVENT_HANDLER->register_hook('TPL_CONTENT_DISPLAY', 'AFTER', null, + function() use ($self) { + $self->assertTrue($_SERVER['testvar'], 'Server variable not set correctly: testvar'); + $self->assertEquals('87.142.120.6', $_SERVER['REMOTE_ADDR'], 'Server variable not set correctly: REMOTE_ADDR'); + $_SERVER['tmpvar'] = true; + } + ); + + $request->execute(); } + + /** + * @depends testFirstRun + */ + function testSecondRun() { + global $EVENT_HANDLER; + + $request = new TestRequest(); + $request->setServer('testvar', false); + + $self = $this; + $EVENT_HANDLER->register_hook('TPL_CONTENT_DISPLAY', 'AFTER', null, + function() use ($self) { + $self->assertFalse($_SERVER['testvar'], 'Server variable not set correctly: testvar'); + $self->assertEquals('87.142.120.6', $_SERVER['REMOTE_ADDR'], 'Server variable not set correctly: REMOTE_ADDR'); + $self->assertFalse(isset($_SERVER['tmpvar'])); + } + ); + + $request->execute(); + } } diff --git a/_testing/tests/testing/inttests_plugins.test.php b/_testing/tests/testing/inttests_plugins.test.php new file mode 100644 index 0000000000000000000000000000000000000000..bf3775b267c06ac8b86b9b9f7ef3c97146a8870d --- /dev/null +++ b/_testing/tests/testing/inttests_plugins.test.php @@ -0,0 +1,49 @@ +<?php + +/** + * @group integration + */ +class InttestsPluginsTest extends DokuWikiTest { + + function testTestingPluginEnabled() { + global $EVENT_HANDLER, $plugin_controller; + + $this->assertTrue( + $plugin_controller->enable('testing'), + 'Could not enable testing plugin.' + ); + + $request = new TestRequest(); + $hookTriggered = false; + + $EVENT_HANDLER->register_hook('TESTING_PLUGIN_INSTALLED', 'AFTER', null, + function() use (&$hookTriggered) { + $hookTriggered = true; + } + ); + + $request->execute(); + + $this->assertTrue($hookTriggered, 'Testing plugin did not trigger!'); + } + + /** + * @depends testTestingPluginEnabled + */ + function testTestingPluginDisabledDefault() { + global $EVENT_HANDLER; + + $request = new TestRequest(); + $hookTriggered = false; + + $EVENT_HANDLER->register_hook('TESTING_PLUGIN_INSTALLED', 'AFTER', null, + function() use (&$hookTriggered) { + $hookTriggered = true; + } + ); + + $request->execute(); + + $this->assertFalse($hookTriggered, 'Testing plugin did trigger!'); + } +} diff --git a/conf/plugins.php b/conf/plugins.php new file mode 100644 index 0000000000000000000000000000000000000000..b2c79970d8be8991d6bbe0eb95583a4d88d1e228 --- /dev/null +++ b/conf/plugins.php @@ -0,0 +1,6 @@ +<?php +/** + * This file configures the default states of available plugins. All settings in + * the plugins.*.php files will override those here. + */ +$plugins['testing'] = 0;