assertEquals( $html, $actualHtml ); $actualWt = WTUtils::decodeComment( $html ); $this->assertEquals( $wikitext, $actualWt ); $doc = DOMCompat::newDocument( true ); $doc->loadHTML( "" ); $body = $doc->getElementsByTagName( "body" )->item( 0 ); $node = $body->childNodes->item( 0 ); $actualLen = WTUtils::decodedCommentLength( $node ); $this->assertEquals( $length, $actualLen ); } public function provideCommentEncoding(): array { // length includes the length of the delimiters return [ [ 'abc', 'abc', 10 ], [ '& - >', '& - >', 12 ], [ 'Use > here', 'Use > here', 20 ], [ '-->', '-->', 13 ], [ '-->', '-->', 17 ], [ '-->', '-->', 21 ], ]; } /** * @covers ::createPageContentI18nFragment */ public function testCreatePageContentI18nFragment() { $doc = ContentUtils::createAndLoadDocument( "" ); $fragment = WTUtils::createPageContentI18nFragment( $doc, 'key.of.message' ); DOMDataUtils::visitAndStoreDataAttribs( $fragment, [ 'discardDataParsoid' => true ] ); $actualHtml = DOMUtils::getFragmentInnerHTML( $fragment ); $expectedHtml = ''; self::assertEquals( $expectedHtml, $actualHtml ); } /** * @covers ::createInterfaceI18nFragment */ public function testCreateInterfaceI18nFragment() { $doc = ContentUtils::createAndLoadDocument( "" ); $fragment = WTUtils::createInterfaceI18nFragment( $doc, 'key.of.message', [ 'Foo' ] ); DOMDataUtils::visitAndStoreDataAttribs( $fragment, [ 'discardDataParsoid' => true ] ); $actualHtml = DOMUtils::getFragmentInnerHTML( $fragment ); $expectedHtml = ''; self::assertEquals( $expectedHtml, $actualHtml ); } /** * @covers ::createLangI18nFragment */ public function testCreateLangI18nFragment() { $doc = ContentUtils::createAndLoadDocument( "" ); $lang = new Bcp47CodeValue( 'fr' ); $fragment = WTUtils::createLangI18nFragment( $doc, $lang, 'key.of.message' ); DOMDataUtils::visitAndStoreDataAttribs( $fragment, [ 'discardDataParsoid' => true ] ); $actualHtml = DOMUtils::getFragmentInnerHTML( $fragment ); $expectedHtml = ''; self::assertEquals( $expectedHtml, $actualHtml ); } /** * @covers ::addInterfaceI18nAttribute * @covers ::addPageContentI18nAttribute * @covers ::addLangI18nAttribute */ public function testAddI18nAttributes() { $doc = ContentUtils::createAndLoadDocument( "hello" ); $span = DOMCompat::getBody( $doc )->firstChild; WTUtils::addPageContentI18nAttribute( $span, 'param1', 'key1' ); WTUtils::addInterfaceI18nAttribute( $span, 'param2', 'key2', [ 'Foo' ] ); $lang = new Bcp47CodeValue( 'fr' ); WTUtils::addLangI18nAttribute( $span, $lang, 'param3', 'key3' ); DOMDataUtils::visitAndStoreDataAttribs( $doc, [ 'discardDataParsoid' => true ] ); $actualHtml = DOMCompat::getInnerHTML( DOMCompat::getBody( $doc ) ); $expectedHtml = 'hello'; self::assertEquals( $expectedHtml, $actualHtml ); } /** * This also tests the storage/loading of mw-data-i18n attributes in the databag. * @covers ::addPageContentI18nAttribute * @covers ::createInterfaceI18nFragment */ public function testCombinedI18n() { $doc = ContentUtils::createAndLoadDocument( "" ); $fragment = WTUtils::createInterfaceI18nFragment( $doc, 'key.of.message', [ 'Foo' ] ); WTUtils::addPageContentI18nAttribute( $fragment->firstChild, 'attr1', 'key1' ); DOMDataUtils::visitAndStoreDataAttribs( $fragment, [ 'discardDataParsoid' => true ] ); $newDoc = ContentUtils::createAndLoadDocument( '' . DOMUtils::getFragmentInnerHTML( $fragment ) . '' ); $span = DOMCompat::getBody( $newDoc )->firstChild; $typeof = DOMUtils::attributes( $span )['typeof']; self::assertEquals( 'mw:I18n mw:LocalizedAttrs', $typeof ); $spanI18n = DOMDataUtils::getDataNodeI18n( $span ); self::assertNotNull( $spanI18n ); $attrI18n = DOMDataUtils::getDataAttrI18n( $span, 'attr1' ); self::assertNotNull( $attrI18n ); self::assertEquals( I18nInfo::USER_LANG, $spanI18n->lang ); self::assertEquals( [ 'Foo' ], $spanI18n->params ); self::assertEquals( 'key.of.message', $spanI18n->key ); self::assertEquals( I18nInfo::PAGE_LANG, $attrI18n->lang ); self::assertNull( $attrI18n->params ); self::assertEquals( 'key1', $attrI18n->key ); } /** * @covers ::decodedCommentLength */ public function testDecodedCommentLength() { $doc = DOMCompat::newDocument( true ); $doc->loadHTML( "
" . "

" . "a " . "" ); $body = DOMCompat::getBody( $doc ); $body->setAttribute( 'hasUnclosedComment', "1" ); $div = $body->firstChild; $p = $div->firstChild; self::assertEquals( 7, WTUtils::decodedCommentLength( $div->lastChild ) ); self::assertEquals( 9, WTUtils::decodedCommentLength( $p->lastChild ) ); } /** * @covers ::fromExtensionContent */ public function testFromExtensionContent() { $html = "bar"; $doc = ContentUtils::createAndLoadDocument( $html ); $body = DOMCompat::getBody( $doc ); $node = $body->firstChild; // span wrapper self::assertTrue( WTUtils::fromExtensionContent( $node ) ); self::assertTrue( WTUtils::fromExtensionContent( $node, "foo" ) ); self::assertFalse( WTUtils::fromExtensionContent( $node, "poem" ) ); $node = $node->firstChild; // "bar" text node self::assertTrue( WTUtils::fromExtensionContent( $node ) ); self::assertTrue( WTUtils::fromExtensionContent( $node, "foo" ) ); self::assertFalse( WTUtils::fromExtensionContent( $node, "poem" ) ); } }