| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\MainConfigNames; |
| 4 |
use MediaWiki\Shell\CommandFactory; |
| 5 |
use MediaWiki\SyntaxHighlight\Pygmentize; |
| 6 |
use MediaWiki\SyntaxHighlight\SyntaxHighlight; |
| 7 |
use Shellbox\Command\BoxedCommand; |
| 8 |
use Shellbox\Command\BoxedResult; |
| 9 |
use Shellbox\ShellboxError; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class PygmentizeTest extends MediaWikiIntegrationTestCase { |
| 16 |
protected function setUp(): void { |
| 17 |
parent::setUp(); |
| 18 |
$this->overrideConfigValues( [ |
| 19 |
|
| 20 |
'PygmentizePath' => false, |
| 21 |
|
| 22 |
MainConfigNames::DevelopmentWarnings => false, |
| 23 |
] ); |
| 24 |
} |
| 25 |
|
| 26 |
private function stubShellbox( ?BoxedResult $result, ?Exception $e ) { |
| 27 |
$factory = $this->createStub( CommandFactory::class ); |
| 28 |
$command = new class ( $result, $e ) extends BoxedCommand { |
| 29 |
private $result; |
| 30 |
private $e; |
| 31 |
|
| 32 |
public function __construct( $result, $e ) { |
| 33 |
$this->result = $result; |
| 34 |
$this->e = $e; |
| 35 |
} |
| 36 |
|
| 37 |
public function execute(): BoxedResult { |
| 38 |
if ( $this->e ) { |
| 39 |
throw $this->e; |
| 40 |
} |
| 41 |
return $this->result; |
| 42 |
} |
| 43 |
}; |
| 44 |
$factory->method( 'createBoxed' )->willReturn( $command ); |
| 45 |
$this->setService( 'ShellCommandFactory', $factory ); |
| 46 |
} |
| 47 |
|
| 48 |
public static function provideHighlight() { |
| 49 |
yield 'basic' => [ |
| 50 |
( new BoxedResult ) |
| 51 |
->stdout( '<div class="mw-highlight><code>x</code></div>' ) |
| 52 |
->exitCode( 0 ), |
| 53 |
null, |
| 54 |
'<div class="mw-highlight mw-highlight-lang-json mw-content-ltr" dir="ltr"><code>x</code></div>' |
| 55 |
]; |
| 56 |
yield 'pre-fallback for non-zero exit' => [ |
| 57 |
( new BoxedResult ) |
| 58 |
->stdout( 'Boo' ) |
| 59 |
->exitCode( 42 ), |
| 60 |
null, |
| 61 |
'<div class="mw-highlight mw-highlight-lang-json mw-content-ltr" dir="ltr"><pre>"example"</pre></div>' |
| 62 |
]; |
| 63 |
yield 'pre-fallback for network error (T292663)' => [ |
| 64 |
null, |
| 65 |
new ShellboxError( 'Wazaaaa', 0 ), |
| 66 |
'<div class="mw-highlight mw-highlight-lang-json mw-content-ltr" dir="ltr"><pre>"example"</pre></div>' |
| 67 |
]; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function testHighlightBasic( ?BoxedResult $result, ?Exception $e, string $expect ) { |
| 74 |
$this->stubShellbox( $result, $e ); |
| 75 |
|
| 76 |
$status = SyntaxHighlight::highlight( '"example"', 'json' ); |
| 77 |
$this->assertSame( $expect, $status->getValue() ); |
| 78 |
} |
| 79 |
|
| 80 |
public function testFetchGeneratedCss() { |
| 81 |
|
| 82 |
$file = __DIR__ . '/../../modules/pygments.generated.css'; |
| 83 |
$fileContent = file_get_contents( $file ); |
| 84 |
|
| 85 |
$generatedCss = "/* Stylesheet generated by updateCSS.php */\n" . Pygmentize::fetchGeneratedCSS(); |
| 86 |
$this->assertEquals( $generatedCss, $fileContent ); |
| 87 |
} |
| 88 |
} |