2021-09-26 19:33:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\VisualEditor\Tests;
|
|
|
|
|
2022-01-21 17:55:03 +00:00
|
|
|
use ApiTestCase;
|
2021-09-26 19:33:26 +00:00
|
|
|
use ApiVisualEditor;
|
|
|
|
use ExtensionRegistry;
|
|
|
|
use HashConfig;
|
|
|
|
use Wikimedia\ScopedCallback;
|
|
|
|
|
|
|
|
/**
|
2022-01-21 17:55:03 +00:00
|
|
|
* @group medium
|
|
|
|
*
|
2021-09-26 19:33:26 +00:00
|
|
|
* @covers \ApiVisualEditor
|
|
|
|
*/
|
2022-01-21 17:55:03 +00:00
|
|
|
class ApiVisualEditorTest extends ApiTestCase {
|
2021-09-26 19:33:26 +00:00
|
|
|
|
|
|
|
/** @var ScopedCallback|null */
|
|
|
|
private $scopedCallback;
|
|
|
|
|
|
|
|
protected function setUp(): void {
|
|
|
|
parent::setUp();
|
|
|
|
$this->scopedCallback = ExtensionRegistry::getInstance()->setAttributeForTest(
|
|
|
|
'VisualEditorAvailableNamespaces',
|
2021-10-01 12:00:56 +00:00
|
|
|
[ 'User' => true, 'Template_Talk' => true ]
|
2021-09-26 19:33:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown(): void {
|
|
|
|
$this->scopedCallback = null;
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2022-01-21 09:23:08 +00:00
|
|
|
private function loadEditor( $overrideParams = [] ) {
|
|
|
|
$params = array_merge( [
|
2022-01-21 17:55:03 +00:00
|
|
|
'action' => 'visualeditor',
|
2022-02-25 12:15:37 +00:00
|
|
|
'paction' => 'metadata',
|
2022-01-21 17:55:03 +00:00
|
|
|
'page' => 'SomeTestPage',
|
2022-01-21 09:23:08 +00:00
|
|
|
], $overrideParams );
|
2022-01-21 17:55:03 +00:00
|
|
|
return $this->doApiRequestWithToken( $params );
|
|
|
|
}
|
|
|
|
|
2022-01-21 09:23:08 +00:00
|
|
|
public function testLoadEditorBasic() {
|
2022-02-28 22:15:55 +00:00
|
|
|
$data = $this->loadEditor()[0]['visualeditor'];
|
|
|
|
|
|
|
|
$this->assertSame( 'success', $data['result'] );
|
|
|
|
|
|
|
|
$properties = [
|
|
|
|
'notices',
|
|
|
|
'copyrightWarning',
|
|
|
|
'checkboxesDef',
|
|
|
|
'checkboxesMessages',
|
|
|
|
'protectedClasses',
|
|
|
|
'basetimestamp',
|
|
|
|
'starttimestamp',
|
|
|
|
'oldid',
|
|
|
|
'blockinfo',
|
|
|
|
'canEdit',
|
|
|
|
'content',
|
|
|
|
];
|
|
|
|
foreach ( $properties as $prop ) {
|
|
|
|
$this->assertArrayHasKey( $prop, $data, "Result has key '$prop'" );
|
|
|
|
}
|
2022-01-21 17:55:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-21 09:23:08 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideLoadEditorPreload
|
|
|
|
*/
|
|
|
|
public function testLoadEditorPreload( $params, $expected ) {
|
|
|
|
$this->assertSame(
|
|
|
|
$expected,
|
|
|
|
$this->loadEditor( $params )[0]['visualeditor']['content']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideLoadEditorPreload() {
|
|
|
|
return [
|
|
|
|
'load with preload content' => [
|
|
|
|
[
|
|
|
|
'preload' => 'UTPage',
|
|
|
|
'paction' => 'wikitext',
|
|
|
|
],
|
|
|
|
'UTContent',
|
|
|
|
],
|
|
|
|
'load with preload via Special:MyLanguage' => [
|
|
|
|
// NB UTPage isn't actually translated, so we get the same content back.
|
|
|
|
[
|
|
|
|
'preload' => 'Special:MyLanguage/UTPage',
|
|
|
|
'paction' => 'wikitext',
|
|
|
|
],
|
|
|
|
'UTContent',
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-09-26 19:33:26 +00:00
|
|
|
public function testIsAllowedNamespace() {
|
|
|
|
$config = new HashConfig( [ 'VisualEditorAvailableNamespaces' => [
|
|
|
|
0 => true,
|
|
|
|
1 => false,
|
|
|
|
] ] );
|
|
|
|
$this->assertTrue( ApiVisualEditor::isAllowedNamespace( $config, 0 ) );
|
|
|
|
$this->assertFalse( ApiVisualEditor::isAllowedNamespace( $config, 1 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAvailableNamespaceIds() {
|
|
|
|
$config = new HashConfig( [ 'VisualEditorAvailableNamespaces' => [
|
|
|
|
0 => true,
|
|
|
|
1 => false,
|
|
|
|
-1 => true,
|
|
|
|
999999 => true,
|
2021-10-01 12:00:56 +00:00
|
|
|
2 => false,
|
2021-09-26 19:33:26 +00:00
|
|
|
'Template' => true,
|
|
|
|
'Foobar' => true,
|
|
|
|
] ] );
|
|
|
|
$this->assertSame(
|
2021-10-01 12:00:56 +00:00
|
|
|
[ -1, 0, 10, 11 ],
|
2021-09-26 19:33:26 +00:00
|
|
|
ApiVisualEditor::getAvailableNamespaceIds( $config )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsAllowedContentType() {
|
|
|
|
$config = new HashConfig( [ 'VisualEditorAvailableContentModels' => [
|
|
|
|
'on' => true,
|
|
|
|
'off' => false,
|
|
|
|
] ] );
|
|
|
|
$this->assertTrue( ApiVisualEditor::isAllowedContentType( $config, 'on' ) );
|
|
|
|
$this->assertFalse( ApiVisualEditor::isAllowedContentType( $config, 'off' ) );
|
|
|
|
$this->assertFalse( ApiVisualEditor::isAllowedContentType( $config, 'unknown' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|