xref: /dokuwiki/_test/core/DokuWikiTest.php (revision cbb44eabe033d70affb048ec0daf4e579e09dd20)
1f8369d7dSTobias Sarnowski<?php
2e1d9dcc8SAndreas Gohr
3*cbb44eabSAndreas Gohruse dokuwiki\Extension\Event;
4e1d9dcc8SAndreas Gohruse dokuwiki\Extension\EventHandler;
5e1d9dcc8SAndreas Gohr
601ef6ea2SAndreas Gohrif(!class_exists('PHPUnit_Framework_TestCase')) {
701ef6ea2SAndreas Gohr    /**
801ef6ea2SAndreas Gohr     * phpunit 5/6 compatibility
901ef6ea2SAndreas Gohr     */
1001ef6ea2SAndreas Gohr    class PHPUnit_Framework_TestCase extends PHPUnit\Framework\TestCase {
1101ef6ea2SAndreas Gohr        /**
123c1490b3SPhy         * setExpectedException is deprecated in PHPUnit 6
133c1490b3SPhy         *
1401ef6ea2SAndreas Gohr         * @param string $class
1501ef6ea2SAndreas Gohr         * @param null|string $message
1601ef6ea2SAndreas Gohr         */
1701ef6ea2SAndreas Gohr        public function setExpectedException($class, $message=null) {
1801ef6ea2SAndreas Gohr            $this->expectException($class);
1901ef6ea2SAndreas Gohr            if(!is_null($message)) {
2001ef6ea2SAndreas Gohr                $this->expectExceptionMessage($message);
2101ef6ea2SAndreas Gohr            }
2201ef6ea2SAndreas Gohr        }
2301ef6ea2SAndreas Gohr    }
2401ef6ea2SAndreas Gohr}
2501ef6ea2SAndreas Gohr
26f8369d7dSTobias Sarnowski/**
27f8369d7dSTobias Sarnowski * Helper class to provide basic functionality for tests
283c1490b3SPhy *
293c1490b3SPhy * @uses PHPUnit_Framework_TestCase and thus PHPUnit 5.7+ is required
30f8369d7dSTobias Sarnowski */
3101ef6ea2SAndreas Gohrabstract class DokuWikiTest extends PHPUnit_Framework_TestCase {
32f8369d7dSTobias Sarnowski
33f8369d7dSTobias Sarnowski    /**
34f8369d7dSTobias Sarnowski     * tests can override this
35f8369d7dSTobias Sarnowski     *
36f8369d7dSTobias Sarnowski     * @var array plugins to enable for test class
37f8369d7dSTobias Sarnowski     */
38f8369d7dSTobias Sarnowski    protected $pluginsEnabled = array();
39f8369d7dSTobias Sarnowski
40f8369d7dSTobias Sarnowski    /**
41f8369d7dSTobias Sarnowski     * tests can override this
42f8369d7dSTobias Sarnowski     *
43f8369d7dSTobias Sarnowski     * @var array plugins to disable for test class
44f8369d7dSTobias Sarnowski     */
45f8369d7dSTobias Sarnowski    protected $pluginsDisabled = array();
46f8369d7dSTobias Sarnowski
47f8369d7dSTobias Sarnowski    /**
480644090aSAndreas Gohr     * Setup the data directory
490644090aSAndreas Gohr     *
500644090aSAndreas Gohr     * This is ran before each test class
510644090aSAndreas Gohr     */
520644090aSAndreas Gohr    public static function setUpBeforeClass() {
530644090aSAndreas Gohr        // just to be safe not to delete something undefined later
540644090aSAndreas Gohr        if(!defined('TMP_DIR')) die('no temporary directory');
550644090aSAndreas Gohr        if(!defined('DOKU_TMP_DATA')) die('no temporary data directory');
560644090aSAndreas Gohr
571c0be3ebSAndreas Gohr        self::setupDataDir();
581c0be3ebSAndreas Gohr        self::setupConfDir();
590644090aSAndreas Gohr    }
600644090aSAndreas Gohr
610644090aSAndreas Gohr    /**
62f8369d7dSTobias Sarnowski     * Reset the DokuWiki environment before each test run. Makes sure loaded config,
63f8369d7dSTobias Sarnowski     * language and plugins are correct.
64f8369d7dSTobias Sarnowski     *
65f8369d7dSTobias Sarnowski     * @throws Exception if plugin actions fail
66f8369d7dSTobias Sarnowski     * @return void
67f8369d7dSTobias Sarnowski     */
68f8369d7dSTobias Sarnowski    public function setUp() {
690644090aSAndreas Gohr
70f8369d7dSTobias Sarnowski        // reload config
71f8369d7dSTobias Sarnowski        global $conf, $config_cascade;
72f8369d7dSTobias Sarnowski        $conf = array();
73f8369d7dSTobias Sarnowski        foreach (array('default','local','protected') as $config_group) {
74f8369d7dSTobias Sarnowski            if (empty($config_cascade['main'][$config_group])) continue;
75f8369d7dSTobias Sarnowski            foreach ($config_cascade['main'][$config_group] as $config_file) {
7679e79377SAndreas Gohr                if (file_exists($config_file)) {
77f8369d7dSTobias Sarnowski                    include($config_file);
78f8369d7dSTobias Sarnowski                }
79f8369d7dSTobias Sarnowski            }
80f8369d7dSTobias Sarnowski        }
81f8369d7dSTobias Sarnowski
82f8369d7dSTobias Sarnowski        // reload license config
83f8369d7dSTobias Sarnowski        global $license;
84f8369d7dSTobias Sarnowski        $license = array();
85f8369d7dSTobias Sarnowski
86f8369d7dSTobias Sarnowski        // load the license file(s)
87f8369d7dSTobias Sarnowski        foreach (array('default','local') as $config_group) {
88f8369d7dSTobias Sarnowski            if (empty($config_cascade['license'][$config_group])) continue;
89f8369d7dSTobias Sarnowski            foreach ($config_cascade['license'][$config_group] as $config_file) {
9079e79377SAndreas Gohr                if(file_exists($config_file)){
91f8369d7dSTobias Sarnowski                    include($config_file);
92f8369d7dSTobias Sarnowski                }
93f8369d7dSTobias Sarnowski            }
94f8369d7dSTobias Sarnowski        }
95f48e16abSGerrit Uitslag        // reload some settings
96f48e16abSGerrit Uitslag        $conf['gzip_output'] &= (strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false);
97f8369d7dSTobias Sarnowski
98f48e16abSGerrit Uitslag        if($conf['compression'] == 'bz2' && !DOKU_HAS_BZIP) {
99f48e16abSGerrit Uitslag            $conf['compression'] = 'gz';
100f48e16abSGerrit Uitslag        }
101f48e16abSGerrit Uitslag        if($conf['compression'] == 'gz' && !DOKU_HAS_GZIP) {
102f48e16abSGerrit Uitslag            $conf['compression'] = 0;
103f48e16abSGerrit Uitslag        }
104f8369d7dSTobias Sarnowski        // make real paths and check them
105f8369d7dSTobias Sarnowski        init_paths();
106f8369d7dSTobias Sarnowski        init_files();
107f8369d7dSTobias Sarnowski
108f8369d7dSTobias Sarnowski        // reset loaded plugins
109f8369d7dSTobias Sarnowski        global $plugin_controller_class, $plugin_controller;
110e3ab6fc5SMichael Hamann        /** @var Doku_Plugin_Controller $plugin_controller */
111f8369d7dSTobias Sarnowski        $plugin_controller = new $plugin_controller_class();
112f8369d7dSTobias Sarnowski
113f8369d7dSTobias Sarnowski        // disable all non-default plugins
114f8369d7dSTobias Sarnowski        global $default_plugins;
115f8369d7dSTobias Sarnowski        foreach ($plugin_controller->getList() as $plugin) {
116f8369d7dSTobias Sarnowski            if (!in_array($plugin, $default_plugins)) {
117f8369d7dSTobias Sarnowski                if (!$plugin_controller->disable($plugin)) {
118f8369d7dSTobias Sarnowski                    throw new Exception('Could not disable plugin "'.$plugin.'"!');
119f8369d7dSTobias Sarnowski                }
120f8369d7dSTobias Sarnowski            }
121f8369d7dSTobias Sarnowski        }
122f8369d7dSTobias Sarnowski
123f8369d7dSTobias Sarnowski        // disable and enable configured plugins
124f8369d7dSTobias Sarnowski        foreach ($this->pluginsDisabled as $plugin) {
125f8369d7dSTobias Sarnowski            if (!$plugin_controller->disable($plugin)) {
126f8369d7dSTobias Sarnowski                throw new Exception('Could not disable plugin "'.$plugin.'"!');
127f8369d7dSTobias Sarnowski            }
128f8369d7dSTobias Sarnowski        }
129f8369d7dSTobias Sarnowski        foreach ($this->pluginsEnabled as $plugin) {
130f8369d7dSTobias Sarnowski            /*  enable() returns false but works...
131f8369d7dSTobias Sarnowski            if (!$plugin_controller->enable($plugin)) {
132f8369d7dSTobias Sarnowski                throw new Exception('Could not enable plugin "'.$plugin.'"!');
133f8369d7dSTobias Sarnowski            }
134f8369d7dSTobias Sarnowski            */
135f8369d7dSTobias Sarnowski            $plugin_controller->enable($plugin);
136f8369d7dSTobias Sarnowski        }
137f8369d7dSTobias Sarnowski
138f8369d7dSTobias Sarnowski        // reset event handler
139f8369d7dSTobias Sarnowski        global $EVENT_HANDLER;
140e1d9dcc8SAndreas Gohr        $EVENT_HANDLER = new EventHandler();
141f8369d7dSTobias Sarnowski
142f8369d7dSTobias Sarnowski        // reload language
143f8369d7dSTobias Sarnowski        $local = $conf['lang'];
144*cbb44eabSAndreas Gohr        Event::createAndTrigger('INIT_LANG_LOAD', $local, 'init_lang', true);
145c01cdb70SChristopher Smith
146c01cdb70SChristopher Smith        global $INPUT;
147ccc4c71cSAndreas Gohr        $INPUT = new \dokuwiki\Input\Input();
148f8369d7dSTobias Sarnowski    }
149db5867f1SAndreas Gohr
150db5867f1SAndreas Gohr    /**
1511c0be3ebSAndreas Gohr     * Reinitialize the data directory for this class run
1521c0be3ebSAndreas Gohr     */
1531c0be3ebSAndreas Gohr    public static function setupDataDir() {
1541c0be3ebSAndreas Gohr        // remove any leftovers from the last run
1551c0be3ebSAndreas Gohr        if(is_dir(DOKU_TMP_DATA)) {
1561c0be3ebSAndreas Gohr            // clear indexer data and cache
1571c0be3ebSAndreas Gohr            idx_get_indexer()->clear();
1581c0be3ebSAndreas Gohr            TestUtils::rdelete(DOKU_TMP_DATA);
1591c0be3ebSAndreas Gohr        }
1601c0be3ebSAndreas Gohr
1611c0be3ebSAndreas Gohr        // populate default dirs
1621c0be3ebSAndreas Gohr        TestUtils::rcopy(TMP_DIR, __DIR__ . '/../data/');
1631c0be3ebSAndreas Gohr    }
1641c0be3ebSAndreas Gohr
1651c0be3ebSAndreas Gohr    /**
1661c0be3ebSAndreas Gohr     * Reinitialize the conf directory for this class run
1671c0be3ebSAndreas Gohr     */
1681c0be3ebSAndreas Gohr    public static function setupConfDir() {
1691c0be3ebSAndreas Gohr        $defaults = [
1701c0be3ebSAndreas Gohr            'acronyms.conf',
1711c0be3ebSAndreas Gohr            'dokuwiki.php',
1721c0be3ebSAndreas Gohr            'entities.conf',
1731c0be3ebSAndreas Gohr            'interwiki.conf',
1741c0be3ebSAndreas Gohr            'license.php',
1751c0be3ebSAndreas Gohr            'manifest.json',
1761c0be3ebSAndreas Gohr            'mediameta.php',
1771c0be3ebSAndreas Gohr            'mime.conf',
1781c0be3ebSAndreas Gohr            'plugins.php',
1791c0be3ebSAndreas Gohr            'plugins.required.php',
1801c0be3ebSAndreas Gohr            'scheme.conf',
1811c0be3ebSAndreas Gohr            'smileys.conf',
1821c0be3ebSAndreas Gohr            'wordblock.conf'
1831c0be3ebSAndreas Gohr        ];
1841c0be3ebSAndreas Gohr
1851c0be3ebSAndreas Gohr        // clear any leftovers
1861c0be3ebSAndreas Gohr        if(is_dir(DOKU_CONF)) {
1871c0be3ebSAndreas Gohr            TestUtils::rdelete(DOKU_CONF);
1881c0be3ebSAndreas Gohr        }
1891c0be3ebSAndreas Gohr        mkdir(DOKU_CONF);
1901c0be3ebSAndreas Gohr
1911c0be3ebSAndreas Gohr        // copy defaults
1921c0be3ebSAndreas Gohr        foreach($defaults as $file) {
1931c0be3ebSAndreas Gohr            copy(DOKU_INC . '/conf/' . $file, DOKU_CONF . $file);
1941c0be3ebSAndreas Gohr        }
1951c0be3ebSAndreas Gohr
1961c0be3ebSAndreas Gohr        // copy test files
1971c0be3ebSAndreas Gohr        TestUtils::rcopy(TMP_DIR, __DIR__ . '/../conf');
1981c0be3ebSAndreas Gohr    }
1991c0be3ebSAndreas Gohr
2001c0be3ebSAndreas Gohr    /**
201d732617bSAndreas Gohr     * Waits until a new second has passed
202d732617bSAndreas Gohr     *
203d732617bSAndreas Gohr     * The very first call will return immeadiately, proceeding calls will return
204d732617bSAndreas Gohr     * only after at least 1 second after the last call has passed.
205d732617bSAndreas Gohr     *
206d732617bSAndreas Gohr     * When passing $init=true it will not return immeadiately but use the current
207d732617bSAndreas Gohr     * second as initialization. It might still return faster than a second.
208d732617bSAndreas Gohr     *
209d732617bSAndreas Gohr     * @param bool $init wait from now on, not from last time
210d732617bSAndreas Gohr     * @return int new timestamp
211d732617bSAndreas Gohr     */
212d732617bSAndreas Gohr    protected function waitForTick($init = false) {
213d732617bSAndreas Gohr        static $last = 0;
214d732617bSAndreas Gohr        if($init) $last = time();
215d732617bSAndreas Gohr        while($last === $now = time()) {
216d732617bSAndreas Gohr            usleep(100000); //recheck in a 10th of a second
217d732617bSAndreas Gohr        }
218d732617bSAndreas Gohr        $last = $now;
219d732617bSAndreas Gohr        return $now;
220d732617bSAndreas Gohr    }
221210ff133SAndreas Gohr
222210ff133SAndreas Gohr    /**
223210ff133SAndreas Gohr     * Allow for testing inaccessible methods (private or protected)
224210ff133SAndreas Gohr     *
225210ff133SAndreas Gohr     * This makes it easier to test protected methods without needing to create intermediate
226210ff133SAndreas Gohr     * classes inheriting and changing the access.
227210ff133SAndreas Gohr     *
228210ff133SAndreas Gohr     * @link https://stackoverflow.com/a/8702347/172068
229210ff133SAndreas Gohr     * @param object $obj Object in which to call the method
230210ff133SAndreas Gohr     * @param string $func The method to call
231210ff133SAndreas Gohr     * @param array $args The arguments to call the method with
232210ff133SAndreas Gohr     * @return mixed
233210ff133SAndreas Gohr     * @throws ReflectionException when the given obj/func does not exist
234210ff133SAndreas Gohr     */
235210ff133SAndreas Gohr    protected static function callInaccessibleMethod($obj, $func, array $args) {
236210ff133SAndreas Gohr        $class = new \ReflectionClass($obj);
237210ff133SAndreas Gohr        $method = $class->getMethod($func);
238210ff133SAndreas Gohr        $method->setAccessible(true);
239210ff133SAndreas Gohr        return $method->invokeArgs($obj, $args);
240210ff133SAndreas Gohr    }
241836f6efbSAndreas Gohr
242836f6efbSAndreas Gohr    /**
243836f6efbSAndreas Gohr     * Allow for reading inaccessible properties (private or protected)
244836f6efbSAndreas Gohr     *
245836f6efbSAndreas Gohr     * This makes it easier to check internals of tested objects. This should generally
246836f6efbSAndreas Gohr     * be avoided.
247836f6efbSAndreas Gohr     *
248836f6efbSAndreas Gohr     * @param object $obj Object on which to access the property
249836f6efbSAndreas Gohr     * @param string $prop name of the property to access
250836f6efbSAndreas Gohr     * @return mixed
251836f6efbSAndreas Gohr     * @throws ReflectionException  when the given obj/prop does not exist
252836f6efbSAndreas Gohr     */
253836f6efbSAndreas Gohr    protected static function getInaccessibleProperty($obj, $prop) {
254836f6efbSAndreas Gohr        $class = new \ReflectionClass($obj);
255836f6efbSAndreas Gohr        $property = $class->getProperty($prop);
256836f6efbSAndreas Gohr        $property->setAccessible(true);
257836f6efbSAndreas Gohr        return $property->getValue($obj);
258836f6efbSAndreas Gohr    }
259836f6efbSAndreas Gohr
260836f6efbSAndreas Gohr    /**
261836f6efbSAndreas Gohr     * Allow for reading inaccessible properties (private or protected)
262836f6efbSAndreas Gohr     *
263836f6efbSAndreas Gohr     * This makes it easier to set internals of tested objects. This should generally
264836f6efbSAndreas Gohr     * be avoided.
265836f6efbSAndreas Gohr     *
266836f6efbSAndreas Gohr     * @param object $obj Object on which to access the property
267836f6efbSAndreas Gohr     * @param string $prop name of the property to access
268836f6efbSAndreas Gohr     * @param mixed $value new value to set the property to
269836f6efbSAndreas Gohr     * @return void
270836f6efbSAndreas Gohr     * @throws ReflectionException when the given obj/prop does not exist
271836f6efbSAndreas Gohr     */
272836f6efbSAndreas Gohr    protected static function setInaccessibleProperty($obj, $prop, $value) {
273836f6efbSAndreas Gohr        $class = new \ReflectionClass($obj);
274836f6efbSAndreas Gohr        $property = $class->getProperty($prop);
275836f6efbSAndreas Gohr        $property->setAccessible(true);
276836f6efbSAndreas Gohr        $property->setValue($obj, $value);
277836f6efbSAndreas Gohr    }
278f8369d7dSTobias Sarnowski}
279