xref: /dokuwiki/_test/core/DokuWikiTest.php (revision e260f93b6cea05bc39bbd77b9db5bdc0c2c424bf)
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        $plugin_controller = new $plugin_controller_class();
82
83        // disable all non-default plugins
84        global $default_plugins;
85        foreach ($plugin_controller->getList() as $plugin) {
86            if (!in_array($plugin, $default_plugins)) {
87                if (!$plugin_controller->disable($plugin)) {
88                    throw new Exception('Could not disable plugin "'.$plugin.'"!');
89                }
90            }
91        }
92
93        // disable and enable configured plugins
94        foreach ($this->pluginsDisabled as $plugin) {
95            if (!$plugin_controller->disable($plugin)) {
96                throw new Exception('Could not disable plugin "'.$plugin.'"!');
97            }
98        }
99        foreach ($this->pluginsEnabled as $plugin) {
100            /*  enable() returns false but works...
101            if (!$plugin_controller->enable($plugin)) {
102                throw new Exception('Could not enable plugin "'.$plugin.'"!');
103            }
104            */
105            $plugin_controller->enable($plugin);
106        }
107
108        // reset event handler
109        global $EVENT_HANDLER;
110        $EVENT_HANDLER = new Doku_Event_Handler();
111
112        // reload language
113        $local = $conf['lang'];
114        trigger_event('INIT_LANG_LOAD', $local, 'init_lang', true);
115    }
116}
117