From c5dcf21507338e88c9140241647ea962e1cc238e Mon Sep 17 00:00:00 2001 From: Tobias Sarnowski Date: Mon, 16 Apr 2012 18:19:20 +0000 Subject: [PATCH] provide some fake $_SERVER entries --- _testing/README | 61 ++++++++--- _testing/bootstrap.php | 103 ++++++++---------- .../tests/testing/inttests_basic.test.php | 2 +- .../tests/testing/inttests_globals.test.php | 4 +- 4 files changed, 98 insertions(+), 72 deletions(-) diff --git a/_testing/README b/_testing/README index 124e9ae8d3..13d3d3d840 100644 --- a/_testing/README +++ b/_testing/README @@ -1,23 +1,52 @@ -====== DokuWiki test suite ====== +DokuWiki Test Suite +============================================================================= +Content of this document: -===== Unit Tests ===== + * Requirements + * Installation of PHPUnit via Pear + * Running all Tests + * Create new Tests + * Known Bad Tests + * TODO for test framework -==== Requirements ==== - * PHP Unit 3.7 -Installation: -The easiest way to install phpunit is via pear: - pear config-set auto_discover 1 - pear upgrade - pear install pear.phpunit.de/PHPUnit -==== Run unit tests ==== +Requirements +----------------------------------------------------------------------------- + + * PHP Unit 3.7 + + +Installation of PHPUnit via Pear +----------------------------------------------------------------------------- + + > pear config-set auto_discover 1 + > pear upgrade + > pear install pear.phpunit.de/PHPUnit + + +Running all Tests +----------------------------------------------------------------------------- + > cd _testing/ > phpunit -==== Bad tests ==== -Bad tests are tests that do not run out of the box. + +Create new Tests +----------------------------------------------------------------------------- + +To create a test for DokuWiki, create a *.test.php file within the tests/ +folder. Please respect the folder structure and naming convention. Inside the +file, implement a class, extending 'DokuWikiTest'. Every method, starting +with 'test' will be called as a test (e.g. 'testIfThisIsValid'); + + +Known Bad Tests +----------------------------------------------------------------------------- + +Every test should be green on every run but the following were broken after +the PHPUnit migration and are disabled by default: * inc/DifferenceEngine (removed FS#2161, FS#2223) * inc/html_hilight (runkit) @@ -28,7 +57,13 @@ Bad tests are tests that do not run out of the box. * inc/parser/xhtml_links -==== TODO ==== +TODO for the test framework +----------------------------------------------------------------------------- + * plugin tests * cross platform compatible: especially test windows (hint: tmp dir location) + * 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 a97e6f0897..cb8a3b079d 100644 --- a/_testing/bootstrap.php +++ b/_testing/bootstrap.php @@ -58,6 +58,12 @@ function fappend($file, $text) { 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', @@ -74,11 +80,35 @@ function fappend($file, $text) { '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/'); +// 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); @@ -112,8 +142,13 @@ function fappend($file, $text) { } $dh->close(); -// TODO setup default global variables -$_SERVER['REMOTE_ADDR'] = '173.194.69.138'; +// 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'); @@ -126,39 +161,13 @@ function ob_start_callback($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 { - 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; @@ -169,22 +178,6 @@ 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'); @@ -216,6 +209,4 @@ function getContent() { function getHeaders() { return $this->headers; } - - // TODO provide findById, findBy... (https://github.com/cosmocode/dokuwiki-plugin-scrape/blob/master/phpQuery-onefile.php) } diff --git a/_testing/tests/testing/inttests_basic.test.php b/_testing/tests/testing/inttests_basic.test.php index ff4b2d5c1e..ca788a91d6 100644 --- a/_testing/tests/testing/inttests_basic.test.php +++ b/_testing/tests/testing/inttests_basic.test.php @@ -3,7 +3,7 @@ /** * @group integration */ -class InttestsBasicTest extends PHPUnit_Framework_TestCase { +class InttestsBasicTest extends DokuWikiTest { /** * Execute the simplest possible request and expect * a dokuwiki page which obviously has the word "DokuWiki" diff --git a/_testing/tests/testing/inttests_globals.test.php b/_testing/tests/testing/inttests_globals.test.php index 40237d7047..6608044c10 100644 --- a/_testing/tests/testing/inttests_globals.test.php +++ b/_testing/tests/testing/inttests_globals.test.php @@ -8,7 +8,7 @@ class InttestsGlobalsTest extends PHPUnit_Framework_TestCase { * Global variables should be restored for every test case. */ function testFirstRun() { - $this->assertEquals('173.194.69.138', $_SERVER['REMOTE_ADDR'], 'Global var not set as expected'); + $this->assertEquals('87.142.120.6', $_SERVER['REMOTE_ADDR'], 'Global var not set as expected'); $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; } @@ -17,6 +17,6 @@ function testFirstRun() { * @depends testFirstRun */ function testSecondRun() { - $this->assertEquals('173.194.69.138', $_SERVER['REMOTE_ADDR'], 'Global var not set as expected'); + $this->assertEquals('87.142.120.6', $_SERVER['REMOTE_ADDR'], 'Global var not set as expected'); } }