wiki.techinc.nl/tests/phpunit/unit/includes/content/FileContentHandlerTest.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

77 lines
2 KiB
PHP
Raw Normal View History

<?php
namespace MediaWiki\Tests\Unit;
use MediaWiki\Content\FileContentHandler;
use MediaWiki\Languages\LanguageNameUtils;
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Parser\MagicWordFactory;
Allow setting a ParserOption to generate Parsoid HTML This is an initial quick-and-dirty implementation. The ParsoidParser class will eventually inherit from \Parser, but this is an initial placeholder to unblock other Parsoid read views work. Currently Parsoid does not fully implement all the ParserOutput metadata set by the legacy parser, but we're working on it. This patch also addresses T300325 by ensuring the the Page HTML APIs use ParserOutput::getRawText(), which will return the entire Parsoid HTML document without post-processing. This is what the Parsoid team refers to as "edit mode" HTML. The ParserOutput::getText() method returns only the <body> contents of the HTML, and applies several transformations, including inserting Table of Contents and style deduplication; this is the "read views" flavor of the Parsoid HTML. We need to be careful of the interaction of the `useParsoid` flag with the ParserCacheMetadata. Effectively `useParsoid` should *always* be marked as "used" or else the ParserCache will assume its value doesn't matter and will serve legacy content for parsoid requests and vice-versa. T330677 is a follow up to address this more thoroughly by splitting the parser cache in ParserOutputAccess; the stop gap in this patch is fragile and, because it doesn't fork the ParserCacheMetadata cache, may corrupt the ParserCacheMetadata in the case when Parsoid and the legacy parser consult different sets of options to render a page. Bug: T300191 Bug: T330677 Bug: T300325 Change-Id: Ica09a4284c00d7917f8b6249e946232b2fb38011
2022-05-27 16:38:32 +00:00
use MediaWiki\Parser\Parsoid\ParsoidParserFactory;
use MediaWiki\Title\TitleFactory;
use MediaWikiUnitTestCase;
use ParserFactory;
use SearchEngine;
use SearchIndexField;
use SearchIndexFieldDefinition;
use Wikimedia\UUID\GlobalIdGenerator;
/**
* @group ContentHandler
*
* @covers \MediaWiki\Content\FileContentHandler
*/
class FileContentHandlerTest extends MediaWikiUnitTestCase {
/**
* @var FileContentHandler
*/
private $handler;
protected function setUp(): void {
parent::setUp();
$this->handler = new FileContentHandler(
CONTENT_MODEL_WIKITEXT,
$this->createMock( TitleFactory::class ),
$this->createMock( ParserFactory::class ),
$this->createMock( GlobalIdGenerator::class ),
$this->createMock( LanguageNameUtils::class ),
$this->createMock( LinkRenderer::class ),
Allow setting a ParserOption to generate Parsoid HTML This is an initial quick-and-dirty implementation. The ParsoidParser class will eventually inherit from \Parser, but this is an initial placeholder to unblock other Parsoid read views work. Currently Parsoid does not fully implement all the ParserOutput metadata set by the legacy parser, but we're working on it. This patch also addresses T300325 by ensuring the the Page HTML APIs use ParserOutput::getRawText(), which will return the entire Parsoid HTML document without post-processing. This is what the Parsoid team refers to as "edit mode" HTML. The ParserOutput::getText() method returns only the <body> contents of the HTML, and applies several transformations, including inserting Table of Contents and style deduplication; this is the "read views" flavor of the Parsoid HTML. We need to be careful of the interaction of the `useParsoid` flag with the ParserCacheMetadata. Effectively `useParsoid` should *always* be marked as "used" or else the ParserCache will assume its value doesn't matter and will serve legacy content for parsoid requests and vice-versa. T330677 is a follow up to address this more thoroughly by splitting the parser cache in ParserOutputAccess; the stop gap in this patch is fragile and, because it doesn't fork the ParserCacheMetadata cache, may corrupt the ParserCacheMetadata in the case when Parsoid and the legacy parser consult different sets of options to render a page. Bug: T300191 Bug: T330677 Bug: T300325 Change-Id: Ica09a4284c00d7917f8b6249e946232b2fb38011
2022-05-27 16:38:32 +00:00
$this->createMock( MagicWordFactory::class ),
$this->createMock( ParsoidParserFactory::class )
);
}
public function testIndexMapping() {
$mockEngine = $this->createMock( SearchEngine::class );
$mockEngine->expects( $this->atLeastOnce() )
->method( 'makeSearchFieldMapping' )
->willReturnCallback( function ( $name, $type ) {
$mockField =
$this->getMockBuilder( SearchIndexFieldDefinition::class )
->onlyMethods( [ 'getMapping' ] )
->setConstructorArgs( [ $name, $type ] )
->getMock();
return $mockField;
} );
$map = $this->handler->getFieldsForSearchIndex( $mockEngine );
$expect = [
'file_media_type' => 1,
'file_mime' => 1,
'file_size' => 1,
'file_width' => 1,
'file_height' => 1,
'file_bits' => 1,
'file_resolution' => 1,
'file_text' => 1,
];
foreach ( $map as $name => $field ) {
$this->assertInstanceOf( SearchIndexField::class, $field );
$this->assertEquals( $name, $field->getName() );
unset( $expect[$name] );
}
$this->assertSame( [], $expect );
}
}