1f8369d7dSTobias Sarnowski<?php 2f8369d7dSTobias Sarnowski 35805f15aSMichael Großeclass parserutils_set_metadata_during_rendering_test extends DokuWikiTest 45805f15aSMichael Große{ 5f8369d7dSTobias Sarnowski // the id used for this test case 6f8369d7dSTobias Sarnowski private $id; 7f8369d7dSTobias Sarnowski // if the test case is currently running 8f8369d7dSTobias Sarnowski private $active = false; 9f8369d7dSTobias Sarnowski // the original plugin controller 10f8369d7dSTobias Sarnowski private $plugin_controller; 11f8369d7dSTobias Sarnowski 12*9dcfcc15SMichael Große /** 13*9dcfcc15SMichael Große * the actual test 14*9dcfcc15SMichael Große * 15*9dcfcc15SMichael Große * @runInSeparateProcess 16*9dcfcc15SMichael Große * @preserveGlobalState disabled 17*9dcfcc15SMichael Große */ 185805f15aSMichael Große public function test_p_set_metadata_during_rendering() 195805f15aSMichael Große { 20f8369d7dSTobias Sarnowski global $EVENT_HANDLER; 21f8369d7dSTobias Sarnowski $this->id = 'test:p_set_metadata_during_rendering'; 22f8369d7dSTobias Sarnowski $this->active = true; 23f8369d7dSTobias Sarnowski 24f8369d7dSTobias Sarnowski // write the wiki page so it exists and needs to be rendered 25f8369d7dSTobias Sarnowski saveWikiText($this->id, 'Test ' . time(), 'Test data setup'); 26f8369d7dSTobias Sarnowski 275805f15aSMichael Große $EVENT_HANDLER->register_hook( 285805f15aSMichael Große 'PARSER_METADATA_RENDER', 295805f15aSMichael Große 'BEFORE', 305805f15aSMichael Große $this, 315805f15aSMichael Große 'helper_set_metadata', 325805f15aSMichael Große ['test_before_set' => 'test'] 335805f15aSMichael Große ); 345805f15aSMichael Große $EVENT_HANDLER->register_hook( 355805f15aSMichael Große 'PARSER_METADATA_RENDER', 365805f15aSMichael Große 'AFTER', 375805f15aSMichael Große $this, 385805f15aSMichael Große 'helper_set_metadata', 395805f15aSMichael Große ['test_after_set' => 'test'] 405805f15aSMichael Große ); 41f8369d7dSTobias Sarnowski $EVENT_HANDLER->register_hook('PARSER_HANDLER_DONE', 'BEFORE', $this, 'helper_inject_test_instruction'); 42f8369d7dSTobias Sarnowski 43f8369d7dSTobias Sarnowski // Change the global plugin controller so this test can be a fake syntax plugin 44f8369d7dSTobias Sarnowski global $plugin_controller; 45f8369d7dSTobias Sarnowski $this->plugin_controller = $plugin_controller; 46f8369d7dSTobias Sarnowski $plugin_controller = $this; 47f8369d7dSTobias Sarnowski 48f8369d7dSTobias Sarnowski // the actual rendering, all hooks should be executed here 49f8369d7dSTobias Sarnowski $newMeta = p_get_metadata($this->id); 50f8369d7dSTobias Sarnowski 51f8369d7dSTobias Sarnowski // restore the plugin controller 52f8369d7dSTobias Sarnowski $plugin_controller = $this->plugin_controller; 53f8369d7dSTobias Sarnowski 54f8369d7dSTobias Sarnowski // assert that all three calls to p_set_metadata have been successful 55f8369d7dSTobias Sarnowski $this->assertEquals($newMeta[ 'test_before_set' ], 'test'); 56f8369d7dSTobias Sarnowski $this->assertEquals($newMeta[ 'test_after_set' ], 'test'); 57f8369d7dSTobias Sarnowski $this->assertEquals($newMeta[ 'test_during_rendering' ], 'test'); 58f8369d7dSTobias Sarnowski 59f8369d7dSTobias Sarnowski // clean up 60f8369d7dSTobias Sarnowski $this->active = false; 61f8369d7dSTobias Sarnowski 62f8369d7dSTobias Sarnowski // make sure the saved metadata is the one that has been rendered 63f8369d7dSTobias Sarnowski $this->assertEquals($newMeta, p_get_metadata($this->id)); 64f8369d7dSTobias Sarnowski 65f8369d7dSTobias Sarnowski saveWikiText($this->id, '', 'Test data remove'); 66f8369d7dSTobias Sarnowski } 67f8369d7dSTobias Sarnowski 68f8369d7dSTobias Sarnowski // helper for the action plugin part of the test, tries executing p_set_metadata during rendering 695805f15aSMichael Große public function helper_set_metadata($event, $meta) 705805f15aSMichael Große { 71f8369d7dSTobias Sarnowski if ($this->active) { 72f8369d7dSTobias Sarnowski p_set_metadata($this->id, $meta, false, true); 73e8b5a4f9SAndreas Gohr $keys = array_keys($meta); 74e8b5a4f9SAndreas Gohr $key = array_pop($keys); 75f8369d7dSTobias Sarnowski $this->assertTrue(is_string($meta[ $key ])); // ensure we really have a key 76f8369d7dSTobias Sarnowski // ensure that the metadata property hasn't been set previously 77f8369d7dSTobias Sarnowski $this->assertNotEquals($meta[ $key ], p_get_metadata($this->id, $key)); 78f8369d7dSTobias Sarnowski } 79f8369d7dSTobias Sarnowski } 80f8369d7dSTobias Sarnowski 81f8369d7dSTobias Sarnowski // helper for injecting an instruction for this test case 825805f15aSMichael Große public function helper_inject_test_instruction($event) 835805f15aSMichael Große { 845805f15aSMichael Große if ($this->active) { 85e855b7edSMichael Große $event->data->calls[] = ['plugin', ['parserutils_test', []], -9000]; 865805f15aSMichael Große } 87f8369d7dSTobias Sarnowski } 88f8369d7dSTobias Sarnowski 89f8369d7dSTobias Sarnowski // fake syntax plugin rendering method that tries calling p_set_metadata during the actual rendering process 905805f15aSMichael Große public function render($format, &$renderer, $data) 915805f15aSMichael Große { 92f8369d7dSTobias Sarnowski if ($this->active) { 93f8369d7dSTobias Sarnowski $key = 'test_during_rendering'; 945805f15aSMichael Große p_set_metadata($this->id, [$key => 'test'], false, true); 95f8369d7dSTobias Sarnowski // ensure that the metadata property hasn't been set previously 96f8369d7dSTobias Sarnowski $this->assertNotEquals($key, p_get_metadata($this->id, $key)); 97f8369d7dSTobias Sarnowski } 98f8369d7dSTobias Sarnowski } 99f8369d7dSTobias Sarnowski 100f8369d7dSTobias Sarnowski // wrapper function for the fake plugin controller 1015805f15aSMichael Große public function getList($type = '', $all = false) 1025805f15aSMichael Große { 103f8369d7dSTobias Sarnowski return $this->plugin_controller->getList(); 104f8369d7dSTobias Sarnowski } 105f8369d7dSTobias Sarnowski 106f8369d7dSTobias Sarnowski // wrapper function for the fake plugin controller, return $this for the fake syntax of this test 1075805f15aSMichael Große public function load($type, $name, $new = false, $disabled = false) 1085805f15aSMichael Große { 109f8369d7dSTobias Sarnowski if ($name == 'parserutils_test') { 110f8369d7dSTobias Sarnowski return $this; 111f8369d7dSTobias Sarnowski } else { 112f8369d7dSTobias Sarnowski return $this->plugin_controller->load($type, $name, $new, $disabled); 113f8369d7dSTobias Sarnowski } 114f8369d7dSTobias Sarnowski } 115f8369d7dSTobias Sarnowski} 116f8369d7dSTobias Sarnowski 117f8369d7dSTobias Sarnowski// vim:ts=4:sw=4:et: 118