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