BxDolServiceMacroTest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. use PHPUnit\Framework\Attributes\DataProvider;
  3. /**
  4. * Macro parse errors and service-request path/class guards (no real module dispatch).
  5. */
  6. class BxDolServiceMacroTest extends \PHPUnit\Framework\TestCase
  7. {
  8. #[DataProvider('providerForMalformedMacro')]
  9. public function testCallMacroMalformed($s)
  10. {
  11. bx_import('BxDolLanguages');
  12. $this->assertSame(_t('_sys_macros_malformed'), BxDolService::callMacro($s));
  13. }
  14. static public function providerForMalformedMacro()
  15. {
  16. return [
  17. [''],
  18. ['not-a-macro'],
  19. ['system'],
  20. ['system:test{'],
  21. ['system:test{not json}'],
  22. ];
  23. }
  24. #[DataProvider('providerForMacrosInContent')]
  25. public function testIsMacrosInContent($s, $bOut)
  26. {
  27. $this->assertSame($bOut, bx_is_macros_in_content($s));
  28. }
  29. static public function providerForMacrosInContent()
  30. {
  31. return [
  32. ['plain text', false],
  33. ['before {{~system:method}} after', true],
  34. ['{{~', true],
  35. ['{~not a macro', false],
  36. ];
  37. }
  38. public function testCheckCallRejectsTraversalPath()
  39. {
  40. $aModule = [
  41. 'name' => 'evil',
  42. 'path' => '../evil/',
  43. 'class_prefix' => 'Bx',
  44. 'uri' => 'evil',
  45. ];
  46. $this->assertSame(1, BxDolRequest::checkCall($aModule, 'foo', [], 'Module'));
  47. }
  48. public function testCheckCallRejectsInvalidClassName()
  49. {
  50. $aModule = [
  51. 'name' => 'test',
  52. 'path' => 'boonex/test/',
  53. 'class_prefix' => '',
  54. 'uri' => 'test',
  55. ];
  56. $this->assertSame(1, BxDolRequest::checkCall($aModule, 'foo', [], 'Foo-Bar'));
  57. }
  58. }