1<?php
2
3namespace MediaWiki\Tests\Unit;
4
5use MediaWiki\Revision\SlotRecord;
6use MediaWiki\Revision\SlotRenderingProvider;
7use MediaWikiUnitTestCase;
8use MWException;
9use Title;
10use WikitextContent;
11use WikitextContentHandler;
12
13/**
14 * Split from \WikitextContentHandlerTest integration tests
15 *
16 * @group ContentHandler
17 * @coversDefaultClass \WikitextContentHandler
18 */
19class WikitextContentHandlerTest extends MediaWikiUnitTestCase {
20
21	/**
22	 * @covers ::serializeContent
23	 */
24	public function testSerializeContent() {
25		$content = new WikitextContent( 'hello world' );
26		$handler = new WikitextContentHandler();
27
28		$this->assertEquals( 'hello world', $handler->serializeContent( $content ) );
29		$this->assertEquals(
30			'hello world',
31			$handler->serializeContent( $content, CONTENT_FORMAT_WIKITEXT )
32		);
33
34		$this->expectException( MWException::class );
35		$handler->serializeContent( $content, 'dummy/foo' );
36	}
37
38	/**
39	 * @covers ::unserializeContent
40	 */
41	public function testUnserializeContent() {
42		$handler = new WikitextContentHandler();
43
44		$content = $handler->unserializeContent( 'hello world' );
45		$this->assertEquals( 'hello world', $content->getText() );
46
47		$content = $handler->unserializeContent( 'hello world', CONTENT_FORMAT_WIKITEXT );
48		$this->assertEquals( 'hello world', $content->getText() );
49
50		$this->expectException( MWException::class );
51		$handler->unserializeContent( 'hello world', 'dummy/foo' );
52	}
53
54	/**
55	 * @covers WikitextContentHandler::makeEmptyContent
56	 */
57	public function testMakeEmptyContent() {
58		$handler = new WikitextContentHandler();
59		$content = $handler->makeEmptyContent();
60
61		$this->assertTrue( $content->isEmpty() );
62		$this->assertSame( '', $content->getText() );
63	}
64
65	public function dataIsSupportedFormat() {
66		return [
67			[ null, true ],
68			[ CONTENT_FORMAT_WIKITEXT, true ],
69			[ 99887766, false ],
70		];
71	}
72
73	/**
74	 * @dataProvider dataIsSupportedFormat
75	 * @covers ::isSupportedFormat
76	 */
77	public function testIsSupportedFormat( $format, $supported ) {
78		$handler = new WikitextContentHandler();
79		$this->assertEquals( $supported, $handler->isSupportedFormat( $format ) );
80	}
81
82	/**
83	 * @covers ::supportsDirectEditing
84	 */
85	public function testSupportsDirectEditing() {
86		$handler = new WikiTextContentHandler();
87		$this->assertTrue( $handler->supportsDirectEditing(), 'direct editing is supported' );
88	}
89
90	/**
91	 * @covers ::getSecondaryDataUpdates
92	 */
93	public function testGetSecondaryDataUpdates() {
94		$title = $this->createMock( Title::class );
95		$content = new WikitextContent( '' );
96		$srp = $this->createMock( SlotRenderingProvider::class );
97		$handler = new WikitextContentHandler();
98
99		$updates = $handler->getSecondaryDataUpdates( $title, $content, SlotRecord::MAIN, $srp );
100
101		$this->assertEquals( [], $updates );
102	}
103
104	/**
105	 * @covers ::getDeletionUpdates
106	 */
107	public function testGetDeletionUpdates() {
108		$title = $this->createMock( Title::class );
109		$handler = new WikitextContentHandler();
110
111		$updates = $handler->getDeletionUpdates( $title, SlotRecord::MAIN );
112		$this->assertEquals( [], $updates );
113	}
114
115}
116