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 } 130*db5867f1SAndreas Gohr 131*db5867f1SAndreas Gohr /** 132*db5867f1SAndreas Gohr * Compatibility for older PHPUnit versions 133*db5867f1SAndreas Gohr * 134*db5867f1SAndreas Gohr * @param string $originalClassName 135*db5867f1SAndreas Gohr * @return PHPUnit_Framework_MockObject_MockObject 136*db5867f1SAndreas Gohr */ 137*db5867f1SAndreas Gohr protected function createMock($originalClassName) { 138*db5867f1SAndreas Gohr if(is_callable(array('parent', 'createMock'))) { 139*db5867f1SAndreas Gohr return parent::createMock($originalClassName); 140*db5867f1SAndreas Gohr } else { 141*db5867f1SAndreas Gohr return $this->getMock($originalClassName); 142*db5867f1SAndreas Gohr } 143*db5867f1SAndreas Gohr } 144*db5867f1SAndreas Gohr 145*db5867f1SAndreas Gohr /** 146*db5867f1SAndreas Gohr * Compatibility for older PHPUnit versions 147*db5867f1SAndreas Gohr * 148*db5867f1SAndreas Gohr * @param string $originalClassName 149*db5867f1SAndreas Gohr * @param array $methods 150*db5867f1SAndreas Gohr * @return PHPUnit_Framework_MockObject_MockObject 151*db5867f1SAndreas Gohr */ 152*db5867f1SAndreas Gohr protected function createPartialMock($originalClassName, array $methods) { 153*db5867f1SAndreas Gohr if(is_callable(array('parent', 'createPartialMock'))) { 154*db5867f1SAndreas Gohr return parent::createPartialMock($originalClassName, $methods); 155*db5867f1SAndreas Gohr } else { 156*db5867f1SAndreas Gohr return $this->getMock($originalClassName, $methods); 157*db5867f1SAndreas Gohr } 158*db5867f1SAndreas Gohr } 159f8369d7dSTobias Sarnowski} 160