xref: /dokuwiki/_test/core/DokuWikiTest.php (revision 25a7457ca09e648a58f9a5db5aa595253ce8a885)
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            // clear indexer data and cache
34            idx_get_indexer()->clear();
35            TestUtils::rdelete(DOKU_TMP_DATA);
36        }
37
38        // populate default dirs
39        TestUtils::rcopy(TMP_DIR, dirname(__FILE__).'/../data/');
40    }
41
42    /**
43     * Reset the DokuWiki environment before each test run. Makes sure loaded config,
44     * language and plugins are correct.
45     *
46     * @throws Exception if plugin actions fail
47     * @return void
48     */
49    public function setUp() {
50
51        // reload config
52        global $conf, $config_cascade;
53        $conf = array();
54        foreach (array('default','local','protected') as $config_group) {
55            if (empty($config_cascade['main'][$config_group])) continue;
56            foreach ($config_cascade['main'][$config_group] as $config_file) {
57                if (@file_exists($config_file)) {
58                    include($config_file);
59                }
60            }
61        }
62
63        // reload license config
64        global $license;
65        $license = array();
66
67        // load the license file(s)
68        foreach (array('default','local') as $config_group) {
69            if (empty($config_cascade['license'][$config_group])) continue;
70            foreach ($config_cascade['license'][$config_group] as $config_file) {
71                if(@file_exists($config_file)){
72                    include($config_file);
73                }
74            }
75        }
76
77        // make real paths and check them
78        init_paths();
79        init_files();
80
81        // reset loaded plugins
82        global $plugin_controller_class, $plugin_controller;
83        /** @var Doku_Plugin_Controller $plugin_controller */
84        $plugin_controller = new $plugin_controller_class();
85
86        // disable all non-default plugins
87        global $default_plugins;
88        foreach ($plugin_controller->getList() as $plugin) {
89            if (!in_array($plugin, $default_plugins)) {
90                if (!$plugin_controller->disable($plugin)) {
91                    throw new Exception('Could not disable plugin "'.$plugin.'"!');
92                }
93            }
94        }
95
96        // disable and enable configured plugins
97        foreach ($this->pluginsDisabled as $plugin) {
98            if (!$plugin_controller->disable($plugin)) {
99                throw new Exception('Could not disable plugin "'.$plugin.'"!');
100            }
101        }
102        foreach ($this->pluginsEnabled as $plugin) {
103            /*  enable() returns false but works...
104            if (!$plugin_controller->enable($plugin)) {
105                throw new Exception('Could not enable plugin "'.$plugin.'"!');
106            }
107            */
108            $plugin_controller->enable($plugin);
109        }
110
111        // reset event handler
112        global $EVENT_HANDLER;
113        $EVENT_HANDLER = new Doku_Event_Handler();
114
115        // reload language
116        $local = $conf['lang'];
117        trigger_event('INIT_LANG_LOAD', $local, 'init_lang', true);
118    }
119}
120