xref: /dokuwiki/_test/tests/inc/parserutils_set_metadata_during_rendering.test.php (revision 2af726d3d50169d07b960a42ffbb6719701c9307)
1<?php
2
3class parserutils_set_metadata_during_rendering_test extends DokuWikiTest {
4    // the id used for this test case
5    private $id;
6    // if the test case is currently running
7    private $active = false;
8    // the original plugin controller
9    private $plugin_controller;
10
11    // the actual test
12    function test_p_set_metadata_during_rendering() {
13        global $EVENT_HANDLER;
14        $this->id = 'test:p_set_metadata_during_rendering';
15        $this->active = true;
16
17        // write the wiki page so it exists and needs to be rendered
18        saveWikiText($this->id, 'Test '.time(), 'Test data setup');
19
20        $EVENT_HANDLER->register_hook('PARSER_METADATA_RENDER', 'BEFORE', $this, 'helper_set_metadata', array('test_before_set' => 'test'));
21        $EVENT_HANDLER->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, 'helper_set_metadata', array('test_after_set' => 'test'));
22        $EVENT_HANDLER->register_hook('PARSER_HANDLER_DONE', 'BEFORE', $this, 'helper_inject_test_instruction');
23
24        // Change the global plugin controller so this test can be a fake syntax plugin
25        global $plugin_controller;
26        $this->plugin_controller = $plugin_controller;
27        $plugin_controller = $this;
28
29        // the actual rendering, all hooks should be executed here
30        $newMeta = p_get_metadata($this->id);
31
32        // restore the plugin controller
33        $plugin_controller = $this->plugin_controller;
34
35        // assert that all three calls to p_set_metadata have been successful
36        $this->assertEquals($newMeta['test_before_set'], 'test');
37        $this->assertEquals($newMeta['test_after_set'], 'test');
38        $this->assertEquals($newMeta['test_during_rendering'], 'test');
39
40        // clean up
41        $this->active = false;
42
43        // make sure the saved metadata is the one that has been rendered
44        $this->assertEquals($newMeta, p_get_metadata($this->id));
45
46        saveWikiText($this->id, '', 'Test data remove');
47    }
48
49    // helper for the action plugin part of the test, tries executing p_set_metadata during rendering
50    function helper_set_metadata($event, $meta) {
51        if ($this->active) {
52            p_set_metadata($this->id, $meta, false, true);
53            $keys = array_keys($meta);
54            $key = array_pop($keys);
55            $this->assertTrue(is_string($meta[$key])); // ensure we really have a key
56            // ensure that the metadata property hasn't been set previously
57            $this->assertNotEquals($meta[$key], p_get_metadata($this->id, $key));
58        }
59    }
60
61    // helper for injecting an instruction for this test case
62    function helper_inject_test_instruction($event) {
63        if ($this->active)
64            $event->data->calls[] = array('plugin', array('parserutils_test', array()));
65    }
66
67    // fake syntax plugin rendering method that tries calling p_set_metadata during the actual rendering process
68    function render($format, &$renderer, $data) {
69        if ($this->active) {
70            $key = 'test_during_rendering';
71            p_set_metadata($this->id, array($key => 'test'), false, true);
72            // ensure that the metadata property hasn't been set previously
73            $this->assertNotEquals($key, p_get_metadata($this->id, $key));
74        }
75    }
76
77    // wrapper function for the fake plugin controller
78    function getList($type='',$all=false){
79        return $this->plugin_controller->getList();
80    }
81
82    // wrapper function for the fake plugin controller, return $this for the fake syntax of this test
83    function load($type,$name,$new=false,$disabled=false){
84        if ($name == 'parserutils_test') {
85            return $this;
86        } else {
87            return $this->plugin_controller->load($type, $name, $new, $disabled);
88        }
89    }
90}
91
92// vim:ts=4:sw=4:et:
93