2022-08-29 10:44:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\VisualEditor\Tests;
|
|
|
|
|
|
|
|
use MediaWiki\Config\ServiceOptions;
|
2022-09-04 09:00:14 +00:00
|
|
|
use MediaWiki\Extension\VisualEditor\DirectParsoidClient;
|
2022-10-19 12:43:26 +00:00
|
|
|
use MediaWiki\Extension\VisualEditor\DualParsoidClient;
|
2022-08-29 10:44:06 +00:00
|
|
|
use MediaWiki\Extension\VisualEditor\VisualEditorParsoidClientFactory;
|
2022-09-04 09:00:14 +00:00
|
|
|
use MediaWiki\Extension\VisualEditor\VRSParsoidClient;
|
2022-08-29 10:44:06 +00:00
|
|
|
use MediaWiki\Http\HttpRequestFactory;
|
|
|
|
use MediaWiki\MainConfigNames;
|
2022-10-19 12:43:26 +00:00
|
|
|
use MediaWiki\Permissions\Authority;
|
2023-01-16 13:11:20 +00:00
|
|
|
use MediaWiki\Rest\Handler\Helper\PageRestHelperFactory;
|
2022-08-29 10:44:06 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
|
|
|
use MultiHttpClient;
|
|
|
|
use ParsoidVirtualRESTService;
|
2022-09-04 09:00:14 +00:00
|
|
|
use Psr\Log\NullLogger;
|
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2022-08-29 10:44:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @coversDefaultClass \MediaWiki\Extension\VisualEditor\VisualEditorParsoidClientFactory
|
|
|
|
*/
|
|
|
|
class VisualEditorParsoidClientFactoryTest extends MediaWikiIntegrationTestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::__construct
|
|
|
|
*/
|
|
|
|
public function testGetVisualEditorParsoidClientFactory() {
|
|
|
|
$veParsoidClientFactory = $this->getServiceContainer()
|
|
|
|
->get( VisualEditorParsoidClientFactory::SERVICE_NAME );
|
|
|
|
|
|
|
|
$this->assertInstanceOf( VisualEditorParsoidClientFactory::class, $veParsoidClientFactory );
|
|
|
|
}
|
|
|
|
|
|
|
|
private function newClientFactory( array $optionValues ) {
|
|
|
|
$options = new ServiceOptions( VisualEditorParsoidClientFactory::CONSTRUCTOR_OPTIONS, $optionValues );
|
|
|
|
|
|
|
|
$httpRequestFactory = $this->createNoOpMock( HttpRequestFactory::class, [ 'createMultiClient' ] );
|
|
|
|
$httpRequestFactory->method( 'createMultiClient' )->willReturn(
|
|
|
|
$this->createNoOpMock( MultiHttpClient::class )
|
|
|
|
);
|
|
|
|
|
|
|
|
return new VisualEditorParsoidClientFactory(
|
|
|
|
$options,
|
2022-09-04 09:00:14 +00:00
|
|
|
$httpRequestFactory,
|
2022-08-28 19:25:04 +00:00
|
|
|
new NullLogger(),
|
2022-11-18 19:46:23 +00:00
|
|
|
$this->createNoOpMock( PageRestHelperFactory::class )
|
2022-08-29 10:44:06 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-20 15:02:35 +00:00
|
|
|
public static function provideGetClient() {
|
2022-10-19 12:43:26 +00:00
|
|
|
yield 'Empty VRS modules array, DefaultParsoidClient=vrs, no hints' => [
|
2022-08-29 10:44:06 +00:00
|
|
|
[
|
|
|
|
MainConfigNames::ParsoidSettings => [],
|
|
|
|
MainConfigNames::VirtualRestConfig => [
|
2022-09-04 09:00:14 +00:00
|
|
|
'modules' => []
|
2022-08-29 10:44:06 +00:00
|
|
|
],
|
|
|
|
VisualEditorParsoidClientFactory::ENABLE_COOKIE_FORWARDING => false,
|
2022-10-19 12:43:26 +00:00
|
|
|
VisualEditorParsoidClientFactory::DEFAULT_PARSOID_CLIENT_SETTING => 'vrs',
|
2022-08-29 10:44:06 +00:00
|
|
|
],
|
2022-10-19 12:43:26 +00:00
|
|
|
[],
|
|
|
|
DirectParsoidClient::class
|
2022-09-04 09:00:14 +00:00
|
|
|
];
|
|
|
|
|
2022-10-19 12:43:26 +00:00
|
|
|
yield 'No VRS modules array, DefaultParsoidClient=vrs, no hints' => [
|
2022-09-04 09:00:14 +00:00
|
|
|
[
|
|
|
|
MainConfigNames::ParsoidSettings => [],
|
|
|
|
MainConfigNames::VirtualRestConfig => [],
|
|
|
|
VisualEditorParsoidClientFactory::ENABLE_COOKIE_FORWARDING => false,
|
2022-10-19 12:43:26 +00:00
|
|
|
VisualEditorParsoidClientFactory::DEFAULT_PARSOID_CLIENT_SETTING => 'vrs',
|
2022-09-04 09:00:14 +00:00
|
|
|
],
|
2022-10-19 12:43:26 +00:00
|
|
|
[],
|
|
|
|
DirectParsoidClient::class
|
2022-08-29 10:44:06 +00:00
|
|
|
];
|
|
|
|
|
2022-10-19 12:43:26 +00:00
|
|
|
yield 'restbase module defined, DefaultParsoidClient=vrs, no hints' => [
|
2022-08-29 10:44:06 +00:00
|
|
|
[
|
|
|
|
MainConfigNames::ParsoidSettings => [],
|
|
|
|
MainConfigNames::VirtualRestConfig => [
|
2022-09-04 09:00:14 +00:00
|
|
|
'modules' => [ 'restbase' => [] ]
|
2022-08-29 10:44:06 +00:00
|
|
|
],
|
|
|
|
VisualEditorParsoidClientFactory::ENABLE_COOKIE_FORWARDING => false,
|
2022-10-19 12:43:26 +00:00
|
|
|
VisualEditorParsoidClientFactory::DEFAULT_PARSOID_CLIENT_SETTING => 'vrs',
|
2022-09-04 09:00:14 +00:00
|
|
|
],
|
2022-10-19 12:43:26 +00:00
|
|
|
[],
|
|
|
|
VRSParsoidClient::class
|
2022-08-29 10:44:06 +00:00
|
|
|
];
|
|
|
|
|
2022-10-19 12:43:26 +00:00
|
|
|
yield 'parsoid module defined, DefaultParsoidClient=vrs, no hints' => [
|
2022-08-29 10:44:06 +00:00
|
|
|
[
|
|
|
|
MainConfigNames::ParsoidSettings => [],
|
2022-09-04 09:00:14 +00:00
|
|
|
MainConfigNames::VirtualRestConfig => [
|
|
|
|
'modules' => [ 'parsoid' => [] ]
|
|
|
|
],
|
2022-08-29 10:44:06 +00:00
|
|
|
VisualEditorParsoidClientFactory::ENABLE_COOKIE_FORWARDING => false,
|
2022-10-19 12:43:26 +00:00
|
|
|
VisualEditorParsoidClientFactory::DEFAULT_PARSOID_CLIENT_SETTING => 'vrs',
|
2022-09-04 09:00:14 +00:00
|
|
|
],
|
2022-10-19 12:43:26 +00:00
|
|
|
[],
|
|
|
|
VRSParsoidClient::class
|
|
|
|
];
|
|
|
|
|
|
|
|
yield 'parsoid module defined, DefaultParsoidClient=direct, no hints' => [
|
|
|
|
[
|
|
|
|
MainConfigNames::ParsoidSettings => [],
|
|
|
|
MainConfigNames::VirtualRestConfig => [
|
|
|
|
'modules' => [ 'parsoid' => [] ]
|
|
|
|
],
|
|
|
|
VisualEditorParsoidClientFactory::ENABLE_COOKIE_FORWARDING => false,
|
|
|
|
VisualEditorParsoidClientFactory::DEFAULT_PARSOID_CLIENT_SETTING => 'direct',
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
DirectParsoidClient::class
|
|
|
|
];
|
|
|
|
|
|
|
|
yield 'parsoid module defined, DefaultParsoidClient=direct, ShouldUseVRS=true' => [
|
|
|
|
[
|
|
|
|
MainConfigNames::ParsoidSettings => [],
|
|
|
|
MainConfigNames::VirtualRestConfig => [
|
|
|
|
'modules' => [ 'parsoid' => [] ]
|
|
|
|
],
|
|
|
|
VisualEditorParsoidClientFactory::ENABLE_COOKIE_FORWARDING => false,
|
|
|
|
VisualEditorParsoidClientFactory::DEFAULT_PARSOID_CLIENT_SETTING => 'direct',
|
|
|
|
],
|
|
|
|
[ 'ShouldUseVRS' => true ],
|
|
|
|
VRSParsoidClient::class
|
|
|
|
];
|
|
|
|
|
|
|
|
yield 'parsoid module define, ShouldUseVRS = false' => [
|
|
|
|
[
|
|
|
|
MainConfigNames::ParsoidSettings => [],
|
|
|
|
MainConfigNames::VirtualRestConfig => [
|
|
|
|
'modules' => [ 'parsoid' => [] ]
|
|
|
|
],
|
|
|
|
VisualEditorParsoidClientFactory::ENABLE_COOKIE_FORWARDING => false,
|
|
|
|
VisualEditorParsoidClientFactory::DEFAULT_PARSOID_CLIENT_SETTING => 'vrs',
|
|
|
|
],
|
|
|
|
[ 'ShouldUseVRS' => false ],
|
|
|
|
DirectParsoidClient::class
|
|
|
|
];
|
|
|
|
|
|
|
|
yield 'No VRS modules array, ShouldUseVRS = true' => [
|
|
|
|
[
|
|
|
|
MainConfigNames::ParsoidSettings => [],
|
|
|
|
MainConfigNames::VirtualRestConfig => [],
|
|
|
|
VisualEditorParsoidClientFactory::ENABLE_COOKIE_FORWARDING => false,
|
|
|
|
VisualEditorParsoidClientFactory::DEFAULT_PARSOID_CLIENT_SETTING => 'vrs',
|
|
|
|
],
|
|
|
|
[ 'ShouldUseVRS' => true ],
|
|
|
|
DirectParsoidClient::class
|
2022-08-29 10:44:06 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-09-04 09:00:14 +00:00
|
|
|
* @dataProvider provideGetClient
|
2022-10-19 12:43:26 +00:00
|
|
|
* @covers ::createParsoidClientInternal
|
2022-09-04 09:00:14 +00:00
|
|
|
* @covers ::createParsoidClient
|
2022-08-29 10:44:06 +00:00
|
|
|
*/
|
2022-10-19 12:43:26 +00:00
|
|
|
public function testGetClient( $optionValues, $hints, $expectedType ) {
|
|
|
|
$authority = $this->createNoOpMock( Authority::class );
|
2022-08-29 10:44:06 +00:00
|
|
|
|
2022-10-13 10:05:13 +00:00
|
|
|
$factory = $this->newClientFactory( $optionValues );
|
|
|
|
|
2022-10-19 12:43:26 +00:00
|
|
|
$client = $factory->createParsoidClientInternal( false, $authority, $hints );
|
2022-09-04 09:00:14 +00:00
|
|
|
$this->assertInstanceOf( $expectedType, $client );
|
2022-10-19 12:43:26 +00:00
|
|
|
|
|
|
|
// This just checks that nothing explodes.
|
|
|
|
$client = $factory->createParsoidClient( false, $authority );
|
|
|
|
$this->assertInstanceOf( DualParsoidClient::class, $client );
|
2022-08-29 10:44:06 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 15:02:35 +00:00
|
|
|
public static function provideCookieToForward() {
|
2022-08-29 10:44:06 +00:00
|
|
|
yield 'When no cookie is sent' => [ false, false ];
|
|
|
|
|
|
|
|
yield 'When a cookie is sent as a string' => [ 'cookie', 'cookie' ];
|
|
|
|
|
|
|
|
yield 'When a cookie is sent as an array' => [ [ 'cookie' ], 'cookie' ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideCookieToForward
|
2022-09-04 09:00:14 +00:00
|
|
|
* @covers ::createParsoidClient
|
2022-08-29 10:44:06 +00:00
|
|
|
*/
|
|
|
|
public function testGetVRSClientForwardedCookies( $cookie, $expectedCookie ) {
|
2022-10-19 12:43:26 +00:00
|
|
|
$authority = $this->createNoOpMock( Authority::class );
|
|
|
|
|
2022-08-29 10:44:06 +00:00
|
|
|
$optionValues = [
|
|
|
|
MainConfigNames::ParsoidSettings => [],
|
|
|
|
MainConfigNames::VirtualRestConfig => [
|
|
|
|
'modules' => [
|
|
|
|
'parsoid' => [
|
|
|
|
'forwardCookies' => true,
|
|
|
|
'restbaseCompat' => false,
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
2022-10-19 12:43:26 +00:00
|
|
|
VisualEditorParsoidClientFactory::ENABLE_COOKIE_FORWARDING => true,
|
|
|
|
VisualEditorParsoidClientFactory::DEFAULT_PARSOID_CLIENT_SETTING => 'vrs',
|
2022-08-29 10:44:06 +00:00
|
|
|
];
|
|
|
|
|
2022-10-19 12:43:26 +00:00
|
|
|
$parsoidClient = $this->newClientFactory( $optionValues )->createParsoidClientInternal( $cookie, $authority );
|
2022-09-04 09:00:14 +00:00
|
|
|
$vrsClient = TestingAccessWrapper::newFromObject( $parsoidClient )->vrsClient;
|
|
|
|
|
2022-08-29 10:44:06 +00:00
|
|
|
$mountAndService = $vrsClient->getMountAndService( '/restbase/' );
|
|
|
|
|
|
|
|
// Assert that the mount and service are correct
|
|
|
|
$this->assertInstanceOf( ParsoidVirtualRESTService::class, $mountAndService[1] );
|
|
|
|
$this->assertSame( '/restbase/', $mountAndService[0] );
|
|
|
|
$this->assertSame( 'parsoid', $mountAndService[1]->getName() );
|
|
|
|
|
|
|
|
$reqs = [
|
|
|
|
[
|
|
|
|
'url' => 'local/v1/page/html/Main_Page',
|
|
|
|
'domain' => 'local',
|
|
|
|
'timeout' => null,
|
|
|
|
'forwardCookies' => true,
|
|
|
|
'HTTPProxy' => null,
|
|
|
|
'restbaseCompat' => true,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$res = $mountAndService[1]->onRequests( $reqs, static function () {
|
|
|
|
return;
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( $cookie && is_string( $cookie ) ) {
|
|
|
|
$this->assertTrue( isset( $res[0]['forwardCookies'] ) );
|
|
|
|
$this->assertSame( $expectedCookie, $res[0]['headers']['Cookie'] );
|
|
|
|
} elseif ( $cookie && is_array( $cookie ) ) {
|
|
|
|
$this->assertTrue( $res[0]['forwardCookies'] );
|
|
|
|
$this->assertSame( $expectedCookie, $res[0]['headers']['Cookie'][0] );
|
|
|
|
} else {
|
|
|
|
$this->assertTrue( $res[0]['forwardCookies'] );
|
|
|
|
$this->assertArrayNotHasKey( 'Cookie', $res[0]['headers'] );
|
|
|
|
}
|
|
|
|
$this->assertSame( 'local', $res[0]['domain'] );
|
|
|
|
$this->assertTrue( $res[0]['forwardCookies'] );
|
|
|
|
$this->assertArrayHasKey( 'headers', $res[0] );
|
|
|
|
$this->assertArrayHasKey( 'Host', $res[0]['headers'] );
|
|
|
|
}
|
|
|
|
|
2022-10-19 12:43:26 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideUseParsoidOverHTTP
|
|
|
|
* @covers ::useParsoidOverHTTP
|
|
|
|
*/
|
|
|
|
public function testUseParsoidOverHTTP( array $optionValues, bool $expected ) {
|
|
|
|
$parsoidClient = $this->newClientFactory( $optionValues );
|
|
|
|
|
|
|
|
$this->assertSame( $expected, $parsoidClient->useParsoidOverHTTP() );
|
|
|
|
}
|
|
|
|
|
2023-05-20 15:02:35 +00:00
|
|
|
public static function provideUseParsoidOverHTTP() {
|
2022-10-19 12:43:26 +00:00
|
|
|
// TODO: test a lot more config!
|
|
|
|
|
|
|
|
yield 'restbaseUrl: No VRS modules, DefaultParsoidClient=vrs' => [
|
|
|
|
[
|
|
|
|
'VirtualRestConfig' => [ 'modules' => [] ],
|
|
|
|
'VisualEditorRestbaseURL' => 'parsoid-url',
|
|
|
|
'VisualEditorFullRestbaseURL' => 'full-parsoid-url',
|
|
|
|
'VisualEditorDefaultParsoidClient' => 'vrs',
|
|
|
|
'EnableCookieForwarding' => true,
|
|
|
|
],
|
|
|
|
false
|
|
|
|
];
|
|
|
|
yield 'restbaseUrl: VRS modules available, DefaultParsoidClient=vrs' => [
|
|
|
|
[
|
|
|
|
'VirtualRestConfig' => [ 'modules' => [
|
|
|
|
'parsoid' => true,
|
|
|
|
] ],
|
|
|
|
'VisualEditorRestbaseURL' => 'parsoid-url',
|
|
|
|
'VisualEditorFullRestbaseURL' => 'full-parsoid-url',
|
|
|
|
'VisualEditorDefaultParsoidClient' => 'vrs',
|
|
|
|
'EnableCookieForwarding' => true,
|
|
|
|
],
|
|
|
|
true
|
|
|
|
];
|
|
|
|
yield 'restbaseUrl: VRS modules available, but no direct access URLs. DefaultParsoidClient=vrs' => [
|
|
|
|
[
|
|
|
|
'VirtualRestConfig' => [ 'modules' => [
|
|
|
|
'parsoid' => true,
|
|
|
|
] ],
|
|
|
|
'VisualEditorRestbaseURL' => 'parsoid-url',
|
|
|
|
'VisualEditorFullRestbaseURL' => 'full-parsoid-url',
|
|
|
|
'VisualEditorDefaultParsoidClient' => 'vrs',
|
|
|
|
'EnableCookieForwarding' => true,
|
|
|
|
],
|
|
|
|
true
|
|
|
|
];
|
|
|
|
|
|
|
|
yield 'restbaseUrl: VRS modules available, but DefaultParsoidClient=direct' => [
|
|
|
|
[
|
|
|
|
'VirtualRestConfig' => [ 'modules' => [
|
|
|
|
'parsoid' => true,
|
|
|
|
] ],
|
|
|
|
'VisualEditorRestbaseURL' => 'parsoid-url',
|
|
|
|
'VisualEditorFullRestbaseURL' => 'full-parsoid-url',
|
|
|
|
'VisualEditorDefaultParsoidClient' => 'direct',
|
|
|
|
'EnableCookieForwarding' => true,
|
|
|
|
],
|
|
|
|
false
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-08-29 10:44:06 +00:00
|
|
|
}
|