[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/_test/tests/inc/ -> parserutils_set_metadata_during_rendering.test.php (source)

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