Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Unit\Revision;
4
5 use InvalidArgumentException;
6 use LogicException;
7 use MediaWiki\Content\IContentHandlerFactory;
8 use MediaWiki\HookContainer\HookContainer;
9 use MediaWiki\Revision\MainSlotRoleHandler;
10 use MediaWiki\Revision\SlotRecord;
11 use MediaWiki\Revision\SlotRoleHandler;
12 use MediaWiki\Revision\SlotRoleRegistry;
13 use MediaWiki\Storage\NameTableStore;
14 use MediaWiki\Title\Title;
15 use MediaWiki\Title\TitleFactory;
16 use MediaWikiUnitTestCase;
17 use Wikimedia\Assert\PostconditionException;
18
19 /**
20 * @covers \MediaWiki\Revision\SlotRoleRegistry
21 */
22 class SlotRoleRegistryTest extends MediaWikiUnitTestCase {
23
24 private function makeNameTableStore( array $names = [] ) {
25 $mock = $this->createMock( NameTableStore::class );
26
27 $mock->method( 'getMap' )
28 ->willReturn( $names );
29
30 return $mock;
31 }
32
33 private function newSlotRoleRegistry( ?NameTableStore $roleNameStore = null ) {
34 if ( !$roleNameStore ) {
35 $roleNameStore = $this->makeNameTableStore();
36 }
37
38 return new SlotRoleRegistry( $roleNameStore );
39 }
40
41 public function testDefineRole() {
42 $registry = $this->newSlotRoleRegistry();
43 $registry->defineRole( 'FOO', static function ( $role ) {
44 return new SlotRoleHandler( $role, 'FooModel' );
45 } );
46
47 $this->assertTrue( $registry->isDefinedRole( 'foo' ) );
48 $this->assertTrue( $registry->isDefinedRole( 'Foo' ) );
49 $this->assertContains( 'foo', $registry->getDefinedRoles() );
50 $this->assertContains( 'foo', $registry->getKnownRoles() );
51 $this->assertNotContains( 'FOO', $registry->getDefinedRoles() );
52 $this->assertNotContains( 'FOO', $registry->getKnownRoles() );
53
54 $handler = $registry->getRoleHandler( 'foo' );
55 $this->assertSame( 'foo', $handler->getRole() );
56
57 $handler = $registry->getRoleHandler( 'Foo' );
58 $this->assertSame( 'foo', $handler->getRole() );
59
60 $title = $this->createMock( Title::class );
61 $this->assertSame( 'FooModel', $handler->getDefaultModel( $title ) );
62 }
63
64 public function testDefineRoleFailsForDupe() {
65 $registry = $this->newSlotRoleRegistry();
66 $registry->defineRole( 'foo', static function ( $role ) {
67 return new SlotRoleHandler( $role, 'FooModel' );
68 } );
69
70 $this->expectException( LogicException::class );
71 $registry->defineRole( 'FOO', static function ( $role ) {
72 return new SlotRoleHandler( $role, 'FooModel' );
73 } );
74 }
75
76 public function testDefineRoleWithContentModel() {
77 $registry = $this->newSlotRoleRegistry();
78 $registry->defineRoleWithModel( 'FOO', 'FooModel' );
79
80 $this->assertTrue( $registry->isDefinedRole( 'foo' ) );
81 $this->assertContains( 'foo', $registry->getDefinedRoles() );
82 $this->assertContains( 'foo', $registry->getKnownRoles() );
83
84 $handler = $registry->getRoleHandler( 'foo' );
85 $this->assertSame( 'foo', $handler->getRole() );
86
87 /** @var Title $title */
88 $title = $this->createMock( Title::class );
89 $this->assertSame( 'FooModel', $handler->getDefaultModel( $title ) );
90 }
91
92 public function testGetRoleHandlerForUnknownModel() {
93 $registry = $this->newSlotRoleRegistry();
94
95 $this->expectException( InvalidArgumentException::class );
96
97 $registry->getRoleHandler( 'foo' );
98 }
99
100 public function testGetRoleHandlerFallbackHandler() {
101 $registry = $this->newSlotRoleRegistry(
102 $this->makeNameTableStore( [ 1 => 'foo' ] )
103 );
104
105 $handler = @$registry->getRoleHandler( 'foo' );
106 $this->assertSame( 'foo', $handler->getRole() );
107 }
108
109 public function testGetRoleHandlerWithBadInstantiator() {
110 $registry = $this->newSlotRoleRegistry();
111 $registry->defineRole( 'foo', static function ( $role ) {
112 return 'Not a SlotRoleHandler instance';
113 } );
114
115 $this->expectException( PostconditionException::class );
116 $registry->getRoleHandler( 'foo' );
117 }
118
119 public function testGetRequiredRoles() {
120 $registry = $this->newSlotRoleRegistry();
121 $registry->defineRole( SlotRecord::MAIN, function ( $role ) {
122 return new MainSlotRoleHandler(
123 [],
124 $this->createMock( IContentHandlerFactory::class ),
125 $this->createMock( HookContainer::class ),
126 $this->createMock( TitleFactory::class )
127 );
128 } );
129
130 $title = $this->createMock( Title::class );
131 $this->assertEquals( [ SlotRecord::MAIN ], $registry->getRequiredRoles( $title ) );
132 }
133
134 public function testGetAllowedRoles() {
135 $registry = $this->newSlotRoleRegistry();
136 $registry->defineRole( SlotRecord::MAIN, function ( $role ) {
137 return new MainSlotRoleHandler(
138 [],
139 $this->createMock( IContentHandlerFactory::class ),
140 $this->createMock( HookContainer::class ),
141 $this->createMock( TitleFactory::class )
142 );
143 } );
144 $registry->defineRoleWithModel( 'FOO', CONTENT_MODEL_TEXT );
145
146 $title = $this->createMock( Title::class );
147 $this->assertEquals( [ SlotRecord::MAIN, 'foo' ], $registry->getAllowedRoles( $title ) );
148 }
149
150 public function testGetKnownRoles() {
151 $registry = $this->newSlotRoleRegistry(
152 $this->makeNameTableStore( [ 1 => 'foo' ] )
153 );
154 $registry->defineRoleWithModel( 'BAR', CONTENT_MODEL_TEXT );
155
156 $this->assertTrue( $registry->isKnownRole( 'foo' ) );
157 $this->assertTrue( $registry->isKnownRole( 'bar' ) );
158 $this->assertTrue( $registry->isKnownRole( 'Bar' ) );
159 $this->assertFalse( $registry->isKnownRole( 'xyzzy' ) );
160
161 $this->assertArrayEquals( [ 'foo', 'bar' ], $registry->getKnownRoles() );
162 }
163
164 }