1f8369d7dSTobias Sarnowski<?php 2f8369d7dSTobias Sarnowski/** 3f8369d7dSTobias Sarnowski * Helper class to provide basic functionality for tests 4f8369d7dSTobias Sarnowski */ 5f8369d7dSTobias Sarnowskiabstract class DokuWikiTest extends PHPUnit_Framework_TestCase { 6f8369d7dSTobias Sarnowski 7f8369d7dSTobias Sarnowski /** 8f8369d7dSTobias Sarnowski * tests can override this 9f8369d7dSTobias Sarnowski * 10f8369d7dSTobias Sarnowski * @var array plugins to enable for test class 11f8369d7dSTobias Sarnowski */ 12f8369d7dSTobias Sarnowski protected $pluginsEnabled = array(); 13f8369d7dSTobias Sarnowski 14f8369d7dSTobias Sarnowski /** 15f8369d7dSTobias Sarnowski * tests can override this 16f8369d7dSTobias Sarnowski * 17f8369d7dSTobias Sarnowski * @var array plugins to disable for test class 18f8369d7dSTobias Sarnowski */ 19f8369d7dSTobias Sarnowski protected $pluginsDisabled = array(); 20f8369d7dSTobias Sarnowski 21f8369d7dSTobias Sarnowski /** 220644090aSAndreas Gohr * Setup the data directory 230644090aSAndreas Gohr * 240644090aSAndreas Gohr * This is ran before each test class 250644090aSAndreas Gohr */ 260644090aSAndreas Gohr public static function setUpBeforeClass() { 270644090aSAndreas Gohr // just to be safe not to delete something undefined later 280644090aSAndreas Gohr if(!defined('TMP_DIR')) die('no temporary directory'); 290644090aSAndreas Gohr if(!defined('DOKU_TMP_DATA')) die('no temporary data directory'); 300644090aSAndreas Gohr 310644090aSAndreas Gohr // remove any leftovers from the last run 320644090aSAndreas Gohr if(is_dir(DOKU_TMP_DATA)){ 334f708321SMichael Hamann // clear indexer data and cache 344f708321SMichael Hamann idx_get_indexer()->clear(); 350644090aSAndreas Gohr TestUtils::rdelete(DOKU_TMP_DATA); 360644090aSAndreas Gohr } 370644090aSAndreas Gohr 380644090aSAndreas Gohr // populate default dirs 390644090aSAndreas Gohr TestUtils::rcopy(TMP_DIR, dirname(__FILE__).'/../data/'); 400644090aSAndreas Gohr } 410644090aSAndreas Gohr 420644090aSAndreas Gohr /** 43f8369d7dSTobias Sarnowski * Reset the DokuWiki environment before each test run. Makes sure loaded config, 44f8369d7dSTobias Sarnowski * language and plugins are correct. 45f8369d7dSTobias Sarnowski * 46f8369d7dSTobias Sarnowski * @throws Exception if plugin actions fail 47f8369d7dSTobias Sarnowski * @return void 48f8369d7dSTobias Sarnowski */ 49f8369d7dSTobias Sarnowski public function setUp() { 500644090aSAndreas Gohr 51f8369d7dSTobias Sarnowski // reload config 52f8369d7dSTobias Sarnowski global $conf, $config_cascade; 53f8369d7dSTobias Sarnowski $conf = array(); 54f8369d7dSTobias Sarnowski foreach (array('default','local','protected') as $config_group) { 55f8369d7dSTobias Sarnowski if (empty($config_cascade['main'][$config_group])) continue; 56f8369d7dSTobias Sarnowski foreach ($config_cascade['main'][$config_group] as $config_file) { 5779e79377SAndreas Gohr if (file_exists($config_file)) { 58f8369d7dSTobias Sarnowski include($config_file); 59f8369d7dSTobias Sarnowski } 60f8369d7dSTobias Sarnowski } 61f8369d7dSTobias Sarnowski } 62f8369d7dSTobias Sarnowski 63f8369d7dSTobias Sarnowski // reload license config 64f8369d7dSTobias Sarnowski global $license; 65f8369d7dSTobias Sarnowski $license = array(); 66f8369d7dSTobias Sarnowski 67f8369d7dSTobias Sarnowski // load the license file(s) 68f8369d7dSTobias Sarnowski foreach (array('default','local') as $config_group) { 69f8369d7dSTobias Sarnowski if (empty($config_cascade['license'][$config_group])) continue; 70f8369d7dSTobias Sarnowski foreach ($config_cascade['license'][$config_group] as $config_file) { 7179e79377SAndreas Gohr if(file_exists($config_file)){ 72f8369d7dSTobias Sarnowski include($config_file); 73f8369d7dSTobias Sarnowski } 74f8369d7dSTobias Sarnowski } 75f8369d7dSTobias Sarnowski } 76f48e16abSGerrit Uitslag // reload some settings 77f48e16abSGerrit Uitslag $conf['gzip_output'] &= (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false); 78f8369d7dSTobias Sarnowski 79f48e16abSGerrit Uitslag if($conf['compression'] == 'bz2' && !DOKU_HAS_BZIP) { 80f48e16abSGerrit Uitslag $conf['compression'] = 'gz'; 81f48e16abSGerrit Uitslag } 82f48e16abSGerrit Uitslag if($conf['compression'] == 'gz' && !DOKU_HAS_GZIP) { 83f48e16abSGerrit Uitslag $conf['compression'] = 0; 84f48e16abSGerrit Uitslag } 85f8369d7dSTobias Sarnowski // make real paths and check them 86f8369d7dSTobias Sarnowski init_paths(); 87f8369d7dSTobias Sarnowski init_files(); 88f8369d7dSTobias Sarnowski 89f8369d7dSTobias Sarnowski // reset loaded plugins 90f8369d7dSTobias Sarnowski global $plugin_controller_class, $plugin_controller; 91e3ab6fc5SMichael Hamann /** @var Doku_Plugin_Controller $plugin_controller */ 92f8369d7dSTobias Sarnowski $plugin_controller = new $plugin_controller_class(); 93f8369d7dSTobias Sarnowski 94f8369d7dSTobias Sarnowski // disable all non-default plugins 95f8369d7dSTobias Sarnowski global $default_plugins; 96f8369d7dSTobias Sarnowski foreach ($plugin_controller->getList() as $plugin) { 97f8369d7dSTobias Sarnowski if (!in_array($plugin, $default_plugins)) { 98f8369d7dSTobias Sarnowski if (!$plugin_controller->disable($plugin)) { 99f8369d7dSTobias Sarnowski throw new Exception('Could not disable plugin "'.$plugin.'"!'); 100f8369d7dSTobias Sarnowski } 101f8369d7dSTobias Sarnowski } 102f8369d7dSTobias Sarnowski } 103f8369d7dSTobias Sarnowski 104f8369d7dSTobias Sarnowski // disable and enable configured plugins 105f8369d7dSTobias Sarnowski foreach ($this->pluginsDisabled as $plugin) { 106f8369d7dSTobias Sarnowski if (!$plugin_controller->disable($plugin)) { 107f8369d7dSTobias Sarnowski throw new Exception('Could not disable plugin "'.$plugin.'"!'); 108f8369d7dSTobias Sarnowski } 109f8369d7dSTobias Sarnowski } 110f8369d7dSTobias Sarnowski foreach ($this->pluginsEnabled as $plugin) { 111f8369d7dSTobias Sarnowski /* enable() returns false but works... 112f8369d7dSTobias Sarnowski if (!$plugin_controller->enable($plugin)) { 113f8369d7dSTobias Sarnowski throw new Exception('Could not enable plugin "'.$plugin.'"!'); 114f8369d7dSTobias Sarnowski } 115f8369d7dSTobias Sarnowski */ 116f8369d7dSTobias Sarnowski $plugin_controller->enable($plugin); 117f8369d7dSTobias Sarnowski } 118f8369d7dSTobias Sarnowski 119f8369d7dSTobias Sarnowski // reset event handler 120f8369d7dSTobias Sarnowski global $EVENT_HANDLER; 121f8369d7dSTobias Sarnowski $EVENT_HANDLER = new Doku_Event_Handler(); 122f8369d7dSTobias Sarnowski 123f8369d7dSTobias Sarnowski // reload language 124f8369d7dSTobias Sarnowski $local = $conf['lang']; 125f8369d7dSTobias Sarnowski trigger_event('INIT_LANG_LOAD', $local, 'init_lang', true); 126c01cdb70SChristopher Smith 127c01cdb70SChristopher Smith global $INPUT; 128c01cdb70SChristopher Smith $INPUT = new Input(); 129f8369d7dSTobias Sarnowski } 130db5867f1SAndreas Gohr 131db5867f1SAndreas Gohr /** 132db5867f1SAndreas Gohr * Compatibility for older PHPUnit versions 133db5867f1SAndreas Gohr * 134db5867f1SAndreas Gohr * @param string $originalClassName 135db5867f1SAndreas Gohr * @return PHPUnit_Framework_MockObject_MockObject 136db5867f1SAndreas Gohr */ 137db5867f1SAndreas Gohr protected function createMock($originalClassName) { 138db5867f1SAndreas Gohr if(is_callable(array('parent', 'createMock'))) { 139db5867f1SAndreas Gohr return parent::createMock($originalClassName); 140db5867f1SAndreas Gohr } else { 141db5867f1SAndreas Gohr return $this->getMock($originalClassName); 142db5867f1SAndreas Gohr } 143db5867f1SAndreas Gohr } 144db5867f1SAndreas Gohr 145db5867f1SAndreas Gohr /** 146db5867f1SAndreas Gohr * Compatibility for older PHPUnit versions 147db5867f1SAndreas Gohr * 148db5867f1SAndreas Gohr * @param string $originalClassName 149db5867f1SAndreas Gohr * @param array $methods 150db5867f1SAndreas Gohr * @return PHPUnit_Framework_MockObject_MockObject 151db5867f1SAndreas Gohr */ 152db5867f1SAndreas Gohr protected function createPartialMock($originalClassName, array $methods) { 153db5867f1SAndreas Gohr if(is_callable(array('parent', 'createPartialMock'))) { 154db5867f1SAndreas Gohr return parent::createPartialMock($originalClassName, $methods); 155db5867f1SAndreas Gohr } else { 156db5867f1SAndreas Gohr return $this->getMock($originalClassName, $methods); 157db5867f1SAndreas Gohr } 158db5867f1SAndreas Gohr } 159*d732617bSAndreas Gohr 160*d732617bSAndreas Gohr /** 161*d732617bSAndreas Gohr * Waits until a new second has passed 162*d732617bSAndreas Gohr * 163*d732617bSAndreas Gohr * The very first call will return immeadiately, proceeding calls will return 164*d732617bSAndreas Gohr * only after at least 1 second after the last call has passed. 165*d732617bSAndreas Gohr * 166*d732617bSAndreas Gohr * When passing $init=true it will not return immeadiately but use the current 167*d732617bSAndreas Gohr * second as initialization. It might still return faster than a second. 168*d732617bSAndreas Gohr * 169*d732617bSAndreas Gohr * @param bool $init wait from now on, not from last time 170*d732617bSAndreas Gohr * @return int new timestamp 171*d732617bSAndreas Gohr */ 172*d732617bSAndreas Gohr protected function waitForTick($init = false) { 173*d732617bSAndreas Gohr static $last = 0; 174*d732617bSAndreas Gohr if($init) $last = time(); 175*d732617bSAndreas Gohr while($last === $now = time()) { 176*d732617bSAndreas Gohr usleep(100000); //recheck in a 10th of a second 177*d732617bSAndreas Gohr } 178*d732617bSAndreas Gohr $last = $now; 179*d732617bSAndreas Gohr return $now; 180*d732617bSAndreas Gohr } 181f8369d7dSTobias Sarnowski} 182