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 ExtensionRegistry;
|
|
|
|
use HashConfig;
|
2022-03-13 01:38:23 +00:00
|
|
|
use MediaWiki\Extension\VisualEditor\ApiVisualEditor;
|
2021-09-26 19:33:26 +00:00
|
|
|
use Wikimedia\ScopedCallback;
|
|
|
|
|
|
|
|
/**
|
2022-01-21 17:55:03 +00:00
|
|
|
* @group medium
|
2022-06-10 09:20:42 +00:00
|
|
|
* @group Database
|
2022-01-21 17:55:03 +00:00
|
|
|
*
|
2022-03-13 01:38:23 +00:00
|
|
|
* @covers \MediaWiki\Extension\VisualEditor\ApiVisualEditor
|
2021-09-26 19:33:26 +00:00
|
|
|
*/
|
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;
|
|
|
|
|
2022-06-10 09:20:42 +00:00
|
|
|
/** @var @inheritDoc */
|
|
|
|
protected $tablesUsed = [ 'page', 'revision' ];
|
|
|
|
|
2021-09-26 19:33:26 +00:00
|
|
|
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 = [
|
2022-04-04 22:53:04 +00:00
|
|
|
// When updating this, also update the sample response in
|
|
|
|
// ve.init.mw.DesktopArticleTarget.test.js
|
|
|
|
'result',
|
2022-02-28 22:15:55 +00:00
|
|
|
'notices',
|
|
|
|
'copyrightWarning',
|
|
|
|
'checkboxesDef',
|
|
|
|
'checkboxesMessages',
|
|
|
|
'protectedClasses',
|
|
|
|
'basetimestamp',
|
|
|
|
'starttimestamp',
|
|
|
|
'oldid',
|
|
|
|
'blockinfo',
|
2023-04-27 22:23:16 +00:00
|
|
|
'wouldautocreate',
|
2022-02-28 22:15:55 +00:00
|
|
|
'canEdit',
|
|
|
|
'content',
|
2022-04-04 22:53:04 +00:00
|
|
|
// When updating this, also update the sample response in
|
|
|
|
// ve.init.mw.DesktopArticleTarget.test.js
|
2022-02-28 22:15:55 +00:00
|
|
|
];
|
|
|
|
foreach ( $properties as $prop ) {
|
|
|
|
$this->assertArrayHasKey( $prop, $data, "Result has key '$prop'" );
|
|
|
|
}
|
2022-04-04 22:53:04 +00:00
|
|
|
|
|
|
|
$this->assertSameSize( $properties, $data, "No other properties are expected" );
|
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' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|