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