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