From f9b8008a4ccae56009894e4052dba80752d562bc Mon Sep 17 00:00:00 2001 From: Tobias Sarnowski Date: Tue, 17 Apr 2012 17:27:27 +0200 Subject: BROKEN added enable/disable feature for plugins --- _testing/README | 7 +- _testing/bootstrap.php | 36 ++-------- _testing/conf/plugins.php | 6 ++ _testing/core/DokuWikiTest.php | 10 +++ _testing/core/TestRequest.php | 89 ++++++++++++++---------- _testing/core/TestResponse.php | 38 ++++++++++ _testing/tests/testing/inttests_globals.test.php | 47 ++++++++++--- _testing/tests/testing/inttests_plugins.test.php | 49 +++++++++++++ conf/plugins.php | 6 ++ 9 files changed, 207 insertions(+), 81 deletions(-) create mode 100644 _testing/conf/plugins.php create mode 100644 _testing/core/TestResponse.php create mode 100644 _testing/tests/testing/inttests_plugins.test.php create mode 100644 conf/plugins.php diff --git a/_testing/README b/_testing/README index 32f2a93b5..253fb249a 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 b4356fa7c..7d57ba1d1 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 000000000..b2c79970d --- /dev/null +++ b/_testing/conf/plugins.php @@ -0,0 +1,6 @@ +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 f77c494c3..c9fc06bf2 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 000000000..ed112b42e --- /dev/null +++ b/_testing/core/TestResponse.php @@ -0,0 +1,38 @@ +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 d3beb433d..b45b2bf83 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 000000000..bf3775b26 --- /dev/null +++ b/_testing/tests/testing/inttests_plugins.test.php @@ -0,0 +1,49 @@ +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 000000000..b2c79970d --- /dev/null +++ b/conf/plugins.php @@ -0,0 +1,6 @@ +