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 /** 22*0644090aSAndreas Gohr * Setup the data directory 23*0644090aSAndreas Gohr * 24*0644090aSAndreas Gohr * This is ran before each test class 25*0644090aSAndreas Gohr */ 26*0644090aSAndreas Gohr public static function setUpBeforeClass() { 27*0644090aSAndreas Gohr // just to be safe not to delete something undefined later 28*0644090aSAndreas Gohr if(!defined('TMP_DIR')) die('no temporary directory'); 29*0644090aSAndreas Gohr if(!defined('DOKU_TMP_DATA')) die('no temporary data directory'); 30*0644090aSAndreas Gohr 31*0644090aSAndreas Gohr // remove any leftovers from the last run 32*0644090aSAndreas Gohr if(is_dir(DOKU_TMP_DATA)){ 33*0644090aSAndreas Gohr TestUtils::rdelete(DOKU_TMP_DATA); 34*0644090aSAndreas Gohr } 35*0644090aSAndreas Gohr 36*0644090aSAndreas Gohr // populate default dirs 37*0644090aSAndreas Gohr TestUtils::rcopy(TMP_DIR, dirname(__FILE__).'/../data/'); 38*0644090aSAndreas Gohr } 39*0644090aSAndreas Gohr 40*0644090aSAndreas Gohr /** 41f8369d7dSTobias Sarnowski * Reset the DokuWiki environment before each test run. Makes sure loaded config, 42f8369d7dSTobias Sarnowski * language and plugins are correct. 43f8369d7dSTobias Sarnowski * 44f8369d7dSTobias Sarnowski * @throws Exception if plugin actions fail 45f8369d7dSTobias Sarnowski * @return void 46f8369d7dSTobias Sarnowski */ 47f8369d7dSTobias Sarnowski public function setUp() { 48*0644090aSAndreas Gohr 49f8369d7dSTobias Sarnowski // reload config 50f8369d7dSTobias Sarnowski global $conf, $config_cascade; 51f8369d7dSTobias Sarnowski $conf = array(); 52f8369d7dSTobias Sarnowski foreach (array('default','local','protected') as $config_group) { 53f8369d7dSTobias Sarnowski if (empty($config_cascade['main'][$config_group])) continue; 54f8369d7dSTobias Sarnowski foreach ($config_cascade['main'][$config_group] as $config_file) { 55f8369d7dSTobias Sarnowski if (@file_exists($config_file)) { 56f8369d7dSTobias Sarnowski include($config_file); 57f8369d7dSTobias Sarnowski } 58f8369d7dSTobias Sarnowski } 59f8369d7dSTobias Sarnowski } 60f8369d7dSTobias Sarnowski 61f8369d7dSTobias Sarnowski // reload license config 62f8369d7dSTobias Sarnowski global $license; 63f8369d7dSTobias Sarnowski $license = array(); 64f8369d7dSTobias Sarnowski 65f8369d7dSTobias Sarnowski // load the license file(s) 66f8369d7dSTobias Sarnowski foreach (array('default','local') as $config_group) { 67f8369d7dSTobias Sarnowski if (empty($config_cascade['license'][$config_group])) continue; 68f8369d7dSTobias Sarnowski foreach ($config_cascade['license'][$config_group] as $config_file) { 69f8369d7dSTobias Sarnowski if(@file_exists($config_file)){ 70f8369d7dSTobias Sarnowski include($config_file); 71f8369d7dSTobias Sarnowski } 72f8369d7dSTobias Sarnowski } 73f8369d7dSTobias Sarnowski } 74f8369d7dSTobias Sarnowski 75f8369d7dSTobias Sarnowski // make real paths and check them 76f8369d7dSTobias Sarnowski init_paths(); 77f8369d7dSTobias Sarnowski init_files(); 78f8369d7dSTobias Sarnowski 79f8369d7dSTobias Sarnowski // reset loaded plugins 80f8369d7dSTobias Sarnowski global $plugin_controller_class, $plugin_controller; 81f8369d7dSTobias Sarnowski $plugin_controller = new $plugin_controller_class(); 82f8369d7dSTobias Sarnowski 83f8369d7dSTobias Sarnowski // disable all non-default plugins 84f8369d7dSTobias Sarnowski global $default_plugins; 85f8369d7dSTobias Sarnowski foreach ($plugin_controller->getList() as $plugin) { 86f8369d7dSTobias Sarnowski if (!in_array($plugin, $default_plugins)) { 87f8369d7dSTobias Sarnowski if (!$plugin_controller->disable($plugin)) { 88f8369d7dSTobias Sarnowski throw new Exception('Could not disable plugin "'.$plugin.'"!'); 89f8369d7dSTobias Sarnowski } 90f8369d7dSTobias Sarnowski } 91f8369d7dSTobias Sarnowski } 92f8369d7dSTobias Sarnowski 93f8369d7dSTobias Sarnowski // disable and enable configured plugins 94f8369d7dSTobias Sarnowski foreach ($this->pluginsDisabled as $plugin) { 95f8369d7dSTobias Sarnowski if (!$plugin_controller->disable($plugin)) { 96f8369d7dSTobias Sarnowski throw new Exception('Could not disable plugin "'.$plugin.'"!'); 97f8369d7dSTobias Sarnowski } 98f8369d7dSTobias Sarnowski } 99f8369d7dSTobias Sarnowski foreach ($this->pluginsEnabled as $plugin) { 100f8369d7dSTobias Sarnowski /* enable() returns false but works... 101f8369d7dSTobias Sarnowski if (!$plugin_controller->enable($plugin)) { 102f8369d7dSTobias Sarnowski throw new Exception('Could not enable plugin "'.$plugin.'"!'); 103f8369d7dSTobias Sarnowski } 104f8369d7dSTobias Sarnowski */ 105f8369d7dSTobias Sarnowski $plugin_controller->enable($plugin); 106f8369d7dSTobias Sarnowski } 107f8369d7dSTobias Sarnowski 108f8369d7dSTobias Sarnowski // reset event handler 109f8369d7dSTobias Sarnowski global $EVENT_HANDLER; 110f8369d7dSTobias Sarnowski $EVENT_HANDLER = new Doku_Event_Handler(); 111f8369d7dSTobias Sarnowski 112f8369d7dSTobias Sarnowski // reload language 113f8369d7dSTobias Sarnowski $local = $conf['lang']; 114f8369d7dSTobias Sarnowski trigger_event('INIT_LANG_LOAD', $local, 'init_lang', true); 115f8369d7dSTobias Sarnowski } 116f8369d7dSTobias Sarnowski} 117