wiki.techinc.nl/tests/phpunit/includes/content/UnknownContentHandlerTest.php
daniel 6906a7728c Add UnknownContentHandler.
UnknownContentHandler can be configued to handle models that
belong to extensions that have been undeployed:

  $wgContentHandlers['xyzzy'] = 'UnknownContentHandler';

This way, no errors will be thrown when trying to access
pages with the unsupported model. Instead, an error message is
shown, and editing is prevented.

This patch also improves handling of non-editable content in
EditPage and in DifferenceEngine.

Bug: T220608
Change-Id: Ia94521b786c0a5225a674e4dc3cb6761a723d75b
2019-08-29 10:43:11 +00:00

119 lines
3.4 KiB
PHP

<?php
use MediaWiki\Revision\SlotRecord;
use MediaWiki\Revision\SlotRenderingProvider;
/**
* @group ContentHandler
*/
class UnknownContentHandlerTest extends MediaWikiLangTestCase {
/**
* @covers UnknownContentHandler::supportsDirectEditing
*/
public function testSupportsDirectEditing() {
$handler = new UnknownContentHandler( 'horkyporky' );
$this->assertFalse( $handler->supportsDirectEditing(), 'direct editing supported' );
}
/**
* @covers UnknownContentHandler::serializeContent
*/
public function testSerializeContent() {
$handler = new UnknownContentHandler( 'horkyporky' );
$content = new UnknownContent( 'hello world', 'horkyporky' );
$this->assertEquals( 'hello world', $handler->serializeContent( $content ) );
$this->assertEquals(
'hello world',
$handler->serializeContent( $content, 'application/horkyporky' )
);
}
/**
* @covers UnknownContentHandler::unserializeContent
*/
public function testUnserializeContent() {
$handler = new UnknownContentHandler( 'horkyporky' );
$content = $handler->unserializeContent( 'hello world' );
$this->assertEquals( 'hello world', $content->getData() );
$content = $handler->unserializeContent( 'hello world', 'application/horkyporky' );
$this->assertEquals( 'hello world', $content->getData() );
}
/**
* @covers UnknownContentHandler::makeEmptyContent
*/
public function testMakeEmptyContent() {
$handler = new UnknownContentHandler( 'horkyporky' );
$content = $handler->makeEmptyContent();
$this->assertTrue( $content->isEmpty() );
$this->assertEquals( '', $content->getData() );
}
public static function dataIsSupportedFormat() {
return [
[ null, true ],
[ 'application/octet-stream', true ],
[ 'unknown/unknown', true ],
[ 'text/plain', false ],
[ 99887766, false ],
];
}
/**
* @dataProvider dataIsSupportedFormat
* @covers UnknownContentHandler::isSupportedFormat
*/
public function testIsSupportedFormat( $format, $supported ) {
$handler = new UnknownContentHandler( 'horkyporky' );
$this->assertEquals( $supported, $handler->isSupportedFormat( $format ) );
}
/**
* @covers ContentHandler::getSecondaryDataUpdates
*/
public function testGetSecondaryDataUpdates() {
$title = Title::newFromText( 'Somefile.jpg', NS_FILE );
$content = new UnknownContent( '', 'horkyporky' );
/** @var SlotRenderingProvider $srp */
$srp = $this->getMock( SlotRenderingProvider::class );
$handler = new UnknownContentHandler( 'horkyporky' );
$updates = $handler->getSecondaryDataUpdates( $title, $content, SlotRecord::MAIN, $srp );
$this->assertEquals( [], $updates );
}
/**
* @covers ContentHandler::getDeletionUpdates
*/
public function testGetDeletionUpdates() {
$title = Title::newFromText( 'Somefile.jpg', NS_FILE );
$handler = new UnknownContentHandler( 'horkyporky' );
$updates = $handler->getDeletionUpdates( $title, SlotRecord::MAIN );
$this->assertEquals( [], $updates );
}
/**
* @covers ContentHandler::getDeletionUpdates
*/
public function testGetSlotDiffRenderer() {
$context = new RequestContext();
$context->setRequest( new FauxRequest() );
$handler = new UnknownContentHandler( 'horkyporky' );
$slotDiffRenderer = $handler->getSlotDiffRenderer( $context );
$oldContent = $handler->unserializeContent( 'Foo' );
$newContent = $handler->unserializeContent( 'Foo bar' );
$diff = $slotDiffRenderer->getDiff( $oldContent, $newContent );
$this->assertNotEmpty( $diff );
}
}