getServiceContainer(); $directClient = new DirectParsoidClient( $services->getPageRestHelperFactory(), $services->getUserFactory()->newAnonymous() ); return $directClient; } /** * @return DirectParsoidClient */ private function createDirectClientWithHttpExceptionFromFactory(): DirectParsoidClient { $factory = $this->createNoOpMock( PageRestHelperFactory::class, [ 'newHtmlOutputRendererHelper', 'newHtmlInputTransformHelper', ] ); $e = new HttpException( 'testing', 400 ); $factory->method( 'newHtmlOutputRendererHelper' )->willThrowException( $e ); $factory->method( 'newHtmlInputTransformHelper' )->willThrowException( $e ); $services = $this->getServiceContainer(); $directClient = new DirectParsoidClient( $factory, $services->getUserFactory()->newAnonymous() ); return $directClient; } /** @return Generator */ public function provideLanguageCodes() { yield 'German language code' => [ 'de' ]; yield 'English language code' => [ 'en' ]; yield 'French language code' => [ 'fr' ]; yield 'No language code, fallback to en' => [ null ]; } private function createLanguage( $langCode, $allowNull = false ) { if ( $langCode === null ) { $language = $this->getServiceContainer()->getContentLanguage(); $langCode = $language->getCode(); if ( $allowNull ) { $language = null; } } else { $language = $this->createNoOpMock( Language::class, [ 'getCode', 'toBcp47Code', 'getDir' ] ); $language->method( 'getCode' )->willReturn( $langCode ); $language->method( 'toBcp47Code' )->willReturn( LanguageCode::bcp47( $langCode ) ); $language->method( 'getDir' )->willReturn( 'ltr' ); } return [ $language, $langCode ]; } /** * @covers ::getPageHtml * @dataProvider provideLanguageCodes */ public function testGetPageHtml( $langCode ) { $directClient = $this->createDirectClient(); $revision = $this->getExistingTestPage( 'DirectParsoidClient' ) ->getRevisionRecord(); [ $language, $langCode ] = $this->createLanguage( $langCode, true ); $response = $directClient->getPageHtml( $revision, $language ); $pageHtml = $response['body']; $headers = $response['headers']; $this->assertIsArray( $response ); $this->assertArrayHasKey( 'body', $response ); $this->assertStringContainsString( 'DirectParsoidClient', $pageHtml ); $this->assertArrayHasKey( 'headers', $response ); $this->assertSame( $langCode, $headers['content-language'] ); $this->assertStringContainsString( 'lang="' . $langCode . '"', $pageHtml ); $this->assertArrayHasKey( 'etag', $headers ); $this->assertStringContainsString( (string)$revision->getId(), $headers['etag'] ); } /** * @covers ::transformHTML * @dataProvider provideLanguageCodes */ public function testTransformHtml( $langCode ) { $directClient = $this->createDirectClient(); $pageIdentity = PageIdentityValue::localIdentity( 1, NS_MAIN, 'DirectParsoidClient' ); [ $language, ] = $this->createLanguage( $langCode ); $html = '