| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Content\Content; |
| 22 |
use MediaWiki\Content\Renderer\ContentParseParams; |
| 23 |
use MediaWiki\Content\TextContentHandler; |
| 24 |
use MediaWiki\Content\Transform\PreloadTransformParamsValue; |
| 25 |
use MediaWiki\Content\Transform\PreSaveTransformParamsValue; |
| 26 |
use MediaWiki\MediaWikiServices; |
| 27 |
use MediaWiki\Parser\ParserOptions; |
| 28 |
use MediaWiki\Parser\ParserOutput; |
| 29 |
use Wikimedia\TestingAccessWrapper; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class ContentHandlerFunctionalTest extends MediaWikiIntegrationTestCase { |
| 36 |
|
| 37 |
public function testMakeEmptyContent() { |
| 38 |
$contentHandlerFactory = MediaWikiServices::getInstance()->getContentHandlerFactory(); |
| 39 |
foreach ( $contentHandlerFactory->getContentModels() as $model ) { |
| 40 |
$handler = $this->getServiceContainer()->getContentHandlerFactory() |
| 41 |
->getContentHandler( $model ); |
| 42 |
|
| 43 |
$content = $handler->makeEmptyContent(); |
| 44 |
$this->assertInstanceOf( Content::class, $content ); |
| 45 |
if ( $handler instanceof TextContentHandler ) { |
| 46 |
|
| 47 |
|
| 48 |
$testingWrapper = TestingAccessWrapper::newFromObject( $handler ); |
| 49 |
$this->assertInstanceOf( $testingWrapper->getContentClass(), $content ); |
| 50 |
} |
| 51 |
|
| 52 |
$handlerClass = get_class( $handler ); |
| 53 |
$contentClass = get_class( $content ); |
| 54 |
|
| 55 |
if ( $handler->supportsDirectEditing() ) { |
| 56 |
$this->assertTrue( |
| 57 |
$content->isValid(), |
| 58 |
"$handlerClass::makeEmptyContent() did not return a valid content ($contentClass::isValid())" |
| 59 |
); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function testGetParserOutput() { |
| 68 |
$contentHandlerFactory = MediaWikiServices::getInstance()->getContentHandlerFactory(); |
| 69 |
foreach ( $contentHandlerFactory->getContentModels() as $model ) { |
| 70 |
$this->filterDeprecated( '/Use of AbstractContent::getParserOutput was deprecated/' ); |
| 71 |
|
| 72 |
$handler = $this->getServiceContainer()->getContentHandlerFactory() |
| 73 |
->getContentHandler( $model ); |
| 74 |
|
| 75 |
$title = $this->getExistingTestPage()->getTitle(); |
| 76 |
$content = $handler->makeEmptyContent(); |
| 77 |
|
| 78 |
$gpoParams = new ContentParseParams( $title ); |
| 79 |
$this->assertInstanceOf( |
| 80 |
ParserOutput::class, |
| 81 |
$handler->getParserOutput( $content, $gpoParams ) |
| 82 |
); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function testPreSaveTransform() { |
| 90 |
$contentHandlerFactory = MediaWikiServices::getInstance()->getContentHandlerFactory(); |
| 91 |
foreach ( $contentHandlerFactory->getContentModels() as $model ) { |
| 92 |
$this->filterDeprecated( '/Use of AbstractContent::preSaveTransform was deprecated/' ); |
| 93 |
|
| 94 |
$handler = $this->getServiceContainer()->getContentHandlerFactory() |
| 95 |
->getContentHandler( $model ); |
| 96 |
|
| 97 |
$title = $this->getExistingTestPage()->getTitle(); |
| 98 |
$user = $this->getTestUser()->getUser(); |
| 99 |
$popts = ParserOptions::newFromAnon(); |
| 100 |
$content = $handler->makeEmptyContent(); |
| 101 |
|
| 102 |
$pstParams = new PreSaveTransformParamsValue( $title, $user, $popts ); |
| 103 |
$this->assertInstanceOf( |
| 104 |
Content::class, |
| 105 |
$handler->preSaveTransform( $content, $pstParams ) |
| 106 |
); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function testPreloadTransform() { |
| 114 |
$contentHandlerFactory = MediaWikiServices::getInstance()->getContentHandlerFactory(); |
| 115 |
foreach ( $contentHandlerFactory->getContentModels() as $model ) { |
| 116 |
$this->filterDeprecated( '/Use of AbstractContent::preloadTransform was deprecated/' ); |
| 117 |
|
| 118 |
$handler = $this->getServiceContainer()->getContentHandlerFactory() |
| 119 |
->getContentHandler( $model ); |
| 120 |
|
| 121 |
$title = $this->getExistingTestPage()->getTitle(); |
| 122 |
$popts = ParserOptions::newFromAnon(); |
| 123 |
$content = $handler->makeEmptyContent(); |
| 124 |
|
| 125 |
$pltParams = new PreloadTransformParamsValue( $title, $popts, [] ); |
| 126 |
$this->assertInstanceOf( |
| 127 |
Content::class, |
| 128 |
$handler->preloadTransform( $content, $pltParams ) |
| 129 |
); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public function testSerializationRoundTrips() { |
| 137 |
$contentHandlerFactory = MediaWikiServices::getInstance()->getContentHandlerFactory(); |
| 138 |
foreach ( $contentHandlerFactory->getContentModels() as $model ) { |
| 139 |
if ( preg_match( '/^wikibase-/', $model ) ) { |
| 140 |
|
| 141 |
|
| 142 |
$this->markTestSkipped( 'Wikibase doesn\'t support serializing empty content' ); |
| 143 |
} |
| 144 |
|
| 145 |
$handler = $this->getServiceContainer()->getContentHandlerFactory() |
| 146 |
->getContentHandler( $model ); |
| 147 |
|
| 148 |
$content = $handler->makeEmptyContent(); |
| 149 |
$data = $content->serialize(); |
| 150 |
$content2 = $handler->unserializeContent( $data ); |
| 151 |
$this->assertTrue( $content->equals( $content2 ) ); |
| 152 |
} |
| 153 |
} |
| 154 |
} |