3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
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
;
35 class ContentHandlerFunctionalTest
extends MediaWikiIntegrationTestCase
{
37 public function testMakeEmptyContent() {
38 $contentHandlerFactory = MediaWikiServices
::getInstance()->getContentHandlerFactory();
39 foreach ( $contentHandlerFactory->getContentModels() as $model ) {
40 $handler = $this->getServiceContainer()->getContentHandlerFactory()
41 ->getContentHandler( $model );
43 $content = $handler->makeEmptyContent();
44 $this->assertInstanceOf( Content
::class, $content );
45 if ( $handler instanceof TextContentHandler
) {
46 // TextContentHandler::getContentClass() is protected, so bypass
48 $testingWrapper = TestingAccessWrapper
::newFromObject( $handler );
49 $this->assertInstanceOf( $testingWrapper->getContentClass(), $content );
52 $handlerClass = get_class( $handler );
53 $contentClass = get_class( $content );
55 if ( $handler->supportsDirectEditing() ) {
58 "$handlerClass::makeEmptyContent() did not return a valid content ($contentClass::isValid())"
65 * Test that getParserOutput works on all content models
67 public function testGetParserOutput() {
68 $contentHandlerFactory = MediaWikiServices
::getInstance()->getContentHandlerFactory();
69 foreach ( $contentHandlerFactory->getContentModels() as $model ) {
70 $this->filterDeprecated( '/Use of AbstractContent::getParserOutput was deprecated/' );
72 $handler = $this->getServiceContainer()->getContentHandlerFactory()
73 ->getContentHandler( $model );
75 $title = $this->getExistingTestPage()->getTitle();
76 $content = $handler->makeEmptyContent();
78 $gpoParams = new ContentParseParams( $title );
79 $this->assertInstanceOf(
81 $handler->getParserOutput( $content, $gpoParams )
87 * Test that preSaveTransform works on all content models
89 public function testPreSaveTransform() {
90 $contentHandlerFactory = MediaWikiServices
::getInstance()->getContentHandlerFactory();
91 foreach ( $contentHandlerFactory->getContentModels() as $model ) {
92 $this->filterDeprecated( '/Use of AbstractContent::preSaveTransform was deprecated/' );
94 $handler = $this->getServiceContainer()->getContentHandlerFactory()
95 ->getContentHandler( $model );
97 $title = $this->getExistingTestPage()->getTitle();
98 $user = $this->getTestUser()->getUser();
99 $popts = ParserOptions
::newFromAnon();
100 $content = $handler->makeEmptyContent();
102 $pstParams = new PreSaveTransformParamsValue( $title, $user, $popts );
103 $this->assertInstanceOf(
105 $handler->preSaveTransform( $content, $pstParams )
111 * Test that preloadTransform works on all content models
113 public function testPreloadTransform() {
114 $contentHandlerFactory = MediaWikiServices
::getInstance()->getContentHandlerFactory();
115 foreach ( $contentHandlerFactory->getContentModels() as $model ) {
116 $this->filterDeprecated( '/Use of AbstractContent::preloadTransform was deprecated/' );
118 $handler = $this->getServiceContainer()->getContentHandlerFactory()
119 ->getContentHandler( $model );
121 $title = $this->getExistingTestPage()->getTitle();
122 $popts = ParserOptions
::newFromAnon();
123 $content = $handler->makeEmptyContent();
125 $pltParams = new PreloadTransformParamsValue( $title, $popts, [] );
126 $this->assertInstanceOf(
128 $handler->preloadTransform( $content, $pltParams )
134 * Test that serialization and unserialization works on all content models
136 public function testSerializationRoundTrips() {
137 $contentHandlerFactory = MediaWikiServices
::getInstance()->getContentHandlerFactory();
138 foreach ( $contentHandlerFactory->getContentModels() as $model ) {
139 if ( preg_match( '/^wikibase-/', $model ) ) {
140 // TODO: Make Wikibase support serialization of empty content, just so
141 // we can test it here.
142 $this->markTestSkipped( 'Wikibase doesn\'t support serializing empty content' );
145 $handler = $this->getServiceContainer()->getContentHandlerFactory()
146 ->getContentHandler( $model );
148 $content = $handler->makeEmptyContent();
149 $data = $content->serialize();
150 $content2 = $handler->unserializeContent( $data );
151 $this->assertTrue( $content->equals( $content2 ) );