Elgg_Ecml_ProcessorTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. class Elgg_Ecml_ProcessorTest extends UnitTestCase {
  3. /**
  4. * @var Elgg_Ecml_Processor
  5. */
  6. protected $proc;
  7. function setUp() {
  8. // @todo mock the tokenizer
  9. $this->proc = new Elgg_Ecml_Processor(new Elgg_Ecml_Tokenizer());
  10. elgg_register_plugin_hook_handler('view', 'output/text', 'ecml_process_view');
  11. parent::setUp();
  12. }
  13. function testProcess() {
  14. $text = '[footnote]I\'m a footnote.[/footnote]Hello [tag bar="bar"], this [tag do="g].';
  15. elgg_register_plugin_hook_handler('prepare:tokens', 'ecml', array($this, 'ecmlFootnote'));
  16. elgg_register_plugin_hook_handler('render:tag', 'ecml', array($this, 'ecmlTag1'));
  17. $output = $this->proc->process($text, array('foo' => 'bar'));
  18. $expected = 'Hello a:3:{s:3:"foo";s:3:"bar";s:7:"keyword";s:3:"tag";s:10:"attributes";a:1:{s:3:"bar";s:3:"bar";}}, this [tag do="g].I\'m a footnote.';
  19. $this->assertEqual($output, $expected);
  20. }
  21. function testViewSystem() {
  22. $text = '[footnote]I\'m a footnote.[/footnote]Hello [br clear=all], this [br inv="alid].';
  23. elgg_register_plugin_hook_handler('prepare:tokens', 'ecml', array($this, 'ecmlFootnote'));
  24. elgg_register_plugin_hook_handler('render:br', 'ecml', array($this, 'ecmlBr'));
  25. // @todo figure out what to do about views that mangle the ECML :/
  26. $output = elgg_view('output/text', array('value' => $text));
  27. $expected = 'Hello <br clear="all">, this <br inv="&quot;alid">.I&#039;m a footnote.';
  28. $this->assertEqual($output, $expected);
  29. }
  30. /**
  31. * @param string $hook
  32. * @param string $type
  33. * @param string $value
  34. * @param array $params
  35. * @return string
  36. */
  37. function ecmlTag1($hook, $type, $value, $params) {
  38. return serialize($params);
  39. }
  40. /**
  41. * @param string $hook
  42. * @param string $type
  43. * @param string $value
  44. * @param array $params
  45. * @return string
  46. */
  47. function ecmlBr($hook, $type, $value, $params) {
  48. return '<br ' . elgg_format_attributes($params['attributes']) . '>';
  49. }
  50. /**
  51. * Example ECML plugin that works on full token set
  52. *
  53. * @param string $hook
  54. * @param string $type
  55. * @param Elgg_Ecml_Token[] $value
  56. * @param array $params
  57. * @return array
  58. */
  59. function ecmlFootnote($hook, $type, $value, $params) {
  60. $output = array();
  61. $foot = array();
  62. $inElement = false;
  63. foreach ($value as $i => $token) {
  64. /* @var Elgg_Ecml_Token $token */
  65. if ($inElement) {
  66. if ($token->keyword === '/footnote') {
  67. $inElement = false;
  68. } else {
  69. array_push($foot, $token);
  70. }
  71. } else {
  72. if ($token->keyword === 'footnote') {
  73. $inElement = true;
  74. } else {
  75. array_push($output, $token);
  76. }
  77. }
  78. }
  79. array_splice($output, count($output), 0, $foot);
  80. return $output;
  81. }
  82. }