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 $key = array_pop(array_keys($meta)); 54 $this->assertTrue(is_string($meta[$key])); // ensure we really have a key 55 // ensure that the metadata property hasn't been set previously 56 $this->assertNotEquals($meta[$key], p_get_metadata($this->id, $key)); 57 } 58 } 59 60 // helper for injecting an instruction for this test case 61 function helper_inject_test_instruction($event) { 62 if ($this->active) 63 $event->data->calls[] = array('plugin', array('parserutils_test', array())); 64 } 65 66 // fake syntax plugin rendering method that tries calling p_set_metadata during the actual rendering process 67 function render($format, &$renderer, $data) { 68 if ($this->active) { 69 $key = 'test_during_rendering'; 70 p_set_metadata($this->id, array($key => 'test'), false, true); 71 // ensure that the metadata property hasn't been set previously 72 $this->assertNotEquals($key, p_get_metadata($this->id, $key)); 73 } 74 } 75 76 // wrapper function for the fake plugin controller 77 function getList($type='',$all=false){ 78 return $this->plugin_controller->getList(); 79 } 80 81 // wrapper function for the fake plugin controller, return $this for the fake syntax of this test 82 function &load($type,$name,$new=false,$disabled=false){ 83 if ($name == 'parserutils_test') { 84 return $this; 85 } else { 86 return $this->plugin_controller->load($type, $name, $new, $disabled); 87 } 88 } 89} 90 91// vim:ts=4:sw=4:et: 92