| 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 |
|
| 16 |
|
| 17 |
class MainSlotRoleHandlerTest extends MediaWikiUnitTestCase { |
| 18 |
use DummyServicesTrait; |
| 19 |
use MockTitleTrait; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
private function getRoleHandler( array $namespaceContentModels ) { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 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 |
|
| 44 |
|
| 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 |
|
| 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 |
|
| 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 |
} |