ShellTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. use MediaWiki\Shell\Command;
  3. use MediaWiki\Shell\Shell;
  4. use Wikimedia\TestingAccessWrapper;
  5. /**
  6. * @covers \MediaWiki\Shell\Shell
  7. * @group Shell
  8. */
  9. class ShellTest extends MediaWikiTestCase {
  10. use MediaWikiCoversValidator;
  11. public function testIsDisabled() {
  12. $this->assertInternalType( 'bool', Shell::isDisabled() ); // sanity
  13. }
  14. /**
  15. * @dataProvider provideEscape
  16. */
  17. public function testEscape( $args, $expected ) {
  18. if ( wfIsWindows() ) {
  19. $this->markTestSkipped( 'This test requires a POSIX environment.' );
  20. }
  21. $this->assertSame( $expected, call_user_func_array( [ Shell::class, 'escape' ], $args ) );
  22. }
  23. public function provideEscape() {
  24. return [
  25. 'simple' => [ [ 'true' ], "'true'" ],
  26. 'with args' => [ [ 'convert', '-font', 'font name' ], "'convert' '-font' 'font name'" ],
  27. 'array' => [ [ [ 'convert', '-font', 'font name' ] ], "'convert' '-font' 'font name'" ],
  28. 'skip nulls' => [ [ 'ls', null ], "'ls'" ],
  29. ];
  30. }
  31. /**
  32. * @covers \MediaWiki\Shell\Shell::makeScriptCommand
  33. * @dataProvider provideMakeScriptCommand
  34. *
  35. * @param string $expected
  36. * @param string $script
  37. * @param string[] $parameters
  38. * @param string[] $options
  39. * @param callable|null $hook
  40. */
  41. public function testMakeScriptCommand( $expected,
  42. $script,
  43. $parameters,
  44. $options = [],
  45. $hook = null
  46. ) {
  47. // Running tests under Vagrant involves MWMultiVersion that uses the below hook
  48. $this->setMwGlobals( 'wgHooks', [] );
  49. if ( $hook ) {
  50. $this->setTemporaryHook( 'wfShellWikiCmd', $hook );
  51. }
  52. $command = Shell::makeScriptCommand( $script, $parameters, $options );
  53. $command->params( 'safe' )
  54. ->unsafeParams( 'unsafe' );
  55. $this->assertType( Command::class, $command );
  56. $wrapper = TestingAccessWrapper::newFromObject( $command );
  57. $this->assertEquals( $expected, $wrapper->command );
  58. $this->assertEquals( 0, $wrapper->restrictions & Shell::NO_LOCALSETTINGS );
  59. }
  60. public function provideMakeScriptCommand() {
  61. global $wgPhpCli;
  62. return [
  63. [
  64. "'$wgPhpCli' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe",
  65. 'maintenance/foobar.php',
  66. [ 'bar\'"baz' ],
  67. ],
  68. [
  69. "'$wgPhpCli' 'changed.php' '--wiki=somewiki' 'bar'\\''\"baz' 'safe' unsafe",
  70. 'maintenance/foobar.php',
  71. [ 'bar\'"baz' ],
  72. [],
  73. function ( &$script, array &$parameters ) {
  74. $script = 'changed.php';
  75. array_unshift( $parameters, '--wiki=somewiki' );
  76. }
  77. ],
  78. [
  79. "'/bin/perl' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe",
  80. 'maintenance/foobar.php',
  81. [ 'bar\'"baz' ],
  82. [ 'php' => '/bin/perl' ],
  83. ],
  84. [
  85. "'$wgPhpCli' 'foobinize' 'maintenance/foobar.php' 'bar'\\''\"baz' 'safe' unsafe",
  86. 'maintenance/foobar.php',
  87. [ 'bar\'"baz' ],
  88. [ 'wrapper' => 'foobinize' ],
  89. ],
  90. ];
  91. }
  92. }