TemplateParserTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * @group Templates
  4. * @covers TemplateParser
  5. */
  6. class TemplateParserTest extends MediaWikiTestCase {
  7. protected $templateDir;
  8. protected function setUp() {
  9. parent::setUp();
  10. $this->setMwGlobals( [
  11. 'wgSecretKey' => 'foo',
  12. ] );
  13. $this->templateDir = dirname( __DIR__ ) . '/data/templates/';
  14. }
  15. /**
  16. * @dataProvider provideProcessTemplate
  17. */
  18. public function testProcessTemplate( $name, $args, $result, $exception = false ) {
  19. if ( $exception ) {
  20. $this->setExpectedException( $exception );
  21. }
  22. $tp = new TemplateParser( $this->templateDir );
  23. $this->assertEquals( $result, $tp->processTemplate( $name, $args ) );
  24. }
  25. public static function provideProcessTemplate() {
  26. return [
  27. [
  28. 'foobar',
  29. [],
  30. "hello world!\n"
  31. ],
  32. [
  33. 'foobar_args',
  34. [
  35. 'planet' => 'world',
  36. ],
  37. "hello world!\n",
  38. ],
  39. [
  40. '../foobar',
  41. [],
  42. false,
  43. 'UnexpectedValueException'
  44. ],
  45. [
  46. "\000../foobar",
  47. [],
  48. false,
  49. 'UnexpectedValueException'
  50. ],
  51. [
  52. '/',
  53. [],
  54. false,
  55. 'UnexpectedValueException'
  56. ],
  57. [
  58. // Allegedly this can strip ext in windows.
  59. 'baz<',
  60. [],
  61. false,
  62. 'UnexpectedValueException'
  63. ],
  64. [
  65. '\\foo',
  66. [],
  67. false,
  68. 'UnexpectedValueException'
  69. ],
  70. [
  71. 'C:\bar',
  72. [],
  73. false,
  74. 'UnexpectedValueException'
  75. ],
  76. [
  77. "foo\000bar",
  78. [],
  79. false,
  80. 'UnexpectedValueException'
  81. ],
  82. [
  83. 'nonexistenttemplate',
  84. [],
  85. false,
  86. 'RuntimeException',
  87. ],
  88. [
  89. 'has_partial',
  90. [
  91. 'planet' => 'world',
  92. ],
  93. "Partial hello world!\n in here\n",
  94. ],
  95. [
  96. 'bad_partial',
  97. [],
  98. false,
  99. 'Exception',
  100. ],
  101. [
  102. 'parentvars',
  103. [
  104. 'foo' => 'f',
  105. 'bar' => [
  106. [ 'baz' => 'x' ],
  107. [ 'baz' => 'y' ]
  108. ]
  109. ],
  110. "f\n\n\tf x\n\n\tf y\n\n"
  111. ]
  112. ];
  113. }
  114. public function testEnableRecursivePartials() {
  115. $tp = new TemplateParser( $this->templateDir );
  116. $data = [ 'r' => [ 'r' => [ 'r' => [] ] ] ];
  117. $tp->enableRecursivePartials( true );
  118. $this->assertEquals( 'rrr', $tp->processTemplate( 'recurse', $data ) );
  119. $tp->enableRecursivePartials( false );
  120. $this->setExpectedException( Exception::class );
  121. $tp->processTemplate( 'recurse', $data );
  122. }
  123. }