Debian Codegraph @

tests/phpunit/unit/includes/Revision/MainSlotRoleHandlerTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Unit\Revision;
4
5 use MediaWiki\Content\ContentHandler;
6 use MediaWiki\Revision\MainSlotRoleHandler;
7 use MediaWiki\Revision\SlotRecord;
8 use MediaWiki\Tests\Unit\DummyServicesTrait;
9 use MediaWiki\Title\Title;
10 use MediaWiki\Title\TitleFactory;
11 use MediaWikiUnitTestCase;
12 use MockTitleTrait;
13
14 /**
15 * @covers \MediaWiki\Revision\MainSlotRoleHandler
16 */
17 class MainSlotRoleHandlerTest extends MediaWikiUnitTestCase {
18 use DummyServicesTrait;
19 use MockTitleTrait;
20
21 /**
22 * @param string[] $namespaceContentModels
23 * @return MainSlotRoleHandler
24 */
25 private function getRoleHandler( array $namespaceContentModels ) {
26 // The ContentHandlerFactory is only used for retrieving the content handler
27 // for the model in ::isAllowedModel(), so that that content handler can have
28 // ::canBeUsedOn() called. We only have tests for ::isAllowedModel() with
29 // CONTENT_MODEL_WIKITEXT and CONTENT_MODEL_TEXT, and neither TextContentHandler
30 // or WikitextContentHandler overrides the method ::canBeUsedOn(), instead using
31 // the default in ContentHandler::canBeUsedOn of returning true unless a hook
32 // says otherwise. That hook is called via ProtectedHookAccessorTrait and
33 // thus uses MediaWikiServices, which we can't have in this unit test. But, since
34 // we aren't testing anything with the hooks, we can just return a mock content
35 // handler that returns true directly
36 $contentHandler = $this->createMock( ContentHandler::class );
37 $contentHandler->method( 'canBeUsedOn' )->willReturn( true );
38 $contentHandlerFactory = $this->getDummyContentHandlerFactory( [
39 CONTENT_MODEL_WIKITEXT => $contentHandler,
40 CONTENT_MODEL_TEXT => $contentHandler,
41 ] );
42
43 // TitleFactory that for these tests is only called with Title objects, so just
44 // return them
45 $titleFactory = $this->createMock( TitleFactory::class );
46 $titleFactory->method( 'newFromLinkTarget' )
47 ->with( $this->isInstanceOf( Title::class ) )
48 ->willReturnArgument( 0 );
49 $titleFactory->method( 'newFromPageIdentity' )
50 ->with( $this->isInstanceOf( Title::class ) )
51 ->willReturnArgument( 0 );
52
53 return new MainSlotRoleHandler(
54 $namespaceContentModels,
55 $contentHandlerFactory,
56 $this->createHookContainer(),
57 $titleFactory
58 );
59 }
60
61 public function testConstruction() {
62 $handler = $this->getRoleHandler( [] );
63 $this->assertSame( SlotRecord::MAIN, $handler->getRole() );
64 $this->assertSame( 'slot-name-main', $handler->getNameMessageKey() );
65
66 $hints = $handler->getOutputLayoutHints();
67 $this->assertArrayHasKey( 'display', $hints );
68 $this->assertArrayHasKey( 'region', $hints );
69 $this->assertArrayHasKey( 'placement', $hints );
70 }
71
72 public function testGetDefaultModel() {
73 $handler = $this->getRoleHandler(
74 [ 100 => CONTENT_MODEL_TEXT ]
75 );
76
77 // For the main handler, the namespace determins the default model
78 $titleMain = $this->makeMockTitle(
79 'Article',
80 [ 'namespace' => NS_MAIN ]
81 );
82 $this->assertSame( CONTENT_MODEL_WIKITEXT, $handler->getDefaultModel( $titleMain ) );
83
84 $title100 = $this->makeMockTitle(
85 'Other page',
86 [ 'namespace' => 100 ]
87 );
88 $this->assertSame( CONTENT_MODEL_TEXT, $handler->getDefaultModel( $title100 ) );
89 }
90
91 public function testIsAllowedModel() {
92 $handler = $this->getRoleHandler( [] );
93
94 // For the main handler, (nearly) all models are allowed
95 $title = $this->makeMockTitle(
96 'Article',
97 [ 'namespace' => NS_MAIN ]
98 );
99 $this->assertTrue( $handler->isAllowedModel( CONTENT_MODEL_WIKITEXT, $title ) );
100 $this->assertTrue( $handler->isAllowedModel( CONTENT_MODEL_TEXT, $title ) );
101 }
102
103 public function testSupportsArticleCount() {
104 $handler = $this->getRoleHandler( [] );
105
106 $this->assertTrue( $handler->supportsArticleCount() );
107 }
108
109 }