MediaWiki REL1_33
SlotRoleRegistryTest.php
Go to the documentation of this file.
1<?php
2
4
5use InvalidArgumentException;
6use LogicException;
13use Wikimedia\Assert\PostconditionException;
14
19
20 private function makeBlankTitleObject() {
22 $title = $this->getMockBuilder( Title::class )
23 ->disableOriginalConstructor()
24 ->getMock();
25
26 return $title;
27 }
28
29 private function makeNameTableStore( array $names = [] ) {
30 $mock = $this->getMockBuilder( NameTableStore::class )
31 ->disableOriginalConstructor()
32 ->getMock();
33
34 $mock->method( 'getMap' )
35 ->willReturn( $names );
36
37 return $mock;
38 }
39
40 private function newSlotRoleRegistry( NameTableStore $roleNameStore = null ) {
41 if ( !$roleNameStore ) {
42 $roleNameStore = $this->makeNameTableStore();
43 }
44
45 return new SlotRoleRegistry( $roleNameStore );
46 }
47
54 public function testDefineRole() {
55 $registry = $this->newSlotRoleRegistry();
56 $registry->defineRole( 'foo', function ( $role ) {
57 return new SlotRoleHandler( $role, 'FooModel' );
58 } );
59
60 $this->assertTrue( $registry->isDefinedRole( 'foo' ) );
61 $this->assertContains( 'foo', $registry->getDefinedRoles() );
62 $this->assertContains( 'foo', $registry->getKnownRoles() );
63
64 $handler = $registry->getRoleHandler( 'foo' );
65 $this->assertSame( 'foo', $handler->getRole() );
66
68 $this->assertSame( 'FooModel', $handler->getDefaultModel( $title ) );
69 }
70
74 public function testDefineRoleFailsForDupe() {
75 $registry = $this->newSlotRoleRegistry();
76 $registry->defineRole( 'foo', function ( $role ) {
77 return new SlotRoleHandler( $role, 'FooModel' );
78 } );
79
80 $this->setExpectedException( LogicException::class );
81 $registry->defineRole( 'foo', function ( $role ) {
82 return new SlotRoleHandler( $role, 'FooModel' );
83 } );
84 }
85
93 $registry = $this->newSlotRoleRegistry();
94 $registry->defineRoleWithModel( 'foo', 'FooModel' );
95
96 $this->assertTrue( $registry->isDefinedRole( 'foo' ) );
97 $this->assertContains( 'foo', $registry->getDefinedRoles() );
98 $this->assertContains( 'foo', $registry->getKnownRoles() );
99
100 $handler = $registry->getRoleHandler( 'foo' );
101 $this->assertSame( 'foo', $handler->getRole() );
102
104 $title = $this->getMockBuilder( Title::class )
105 ->disableOriginalConstructor()
106 ->getMock();
107 $this->assertSame( 'FooModel', $handler->getDefaultModel( $title ) );
108 }
109
114 $registry = $this->newSlotRoleRegistry();
115
116 $this->setExpectedException( InvalidArgumentException::class );
117
118 $registry->getRoleHandler( 'foo' );
119 }
120
125 $registry = $this->newSlotRoleRegistry(
126 $this->makeNameTableStore( [ 1 => 'foo' ] )
127 );
128
129 \Wikimedia\suppressWarnings();
130 $handler = $registry->getRoleHandler( 'foo' );
131 $this->assertSame( 'foo', $handler->getRole() );
132
133 \Wikimedia\restoreWarnings();
134 }
135
140 $registry = $this->newSlotRoleRegistry();
141 $registry->defineRole( 'foo', function ( $role ) {
142 return 'Not a SlotRoleHandler instance';
143 } );
144
145 $this->setExpectedException( PostconditionException::class );
146 $registry->getRoleHandler( 'foo' );
147 }
148
152 public function testGetRequiredRoles() {
153 $registry = $this->newSlotRoleRegistry();
154 $registry->defineRole( 'main', function ( $role ) {
155 return new MainSlotRoleHandler( [] );
156 } );
157
158 $title = $this->makeBlankTitleObject();
159 $this->assertEquals( [ 'main' ], $registry->getRequiredRoles( $title ) );
160 }
161
165 public function testGetAllowedRoles() {
166 $registry = $this->newSlotRoleRegistry();
167 $registry->defineRole( 'main', function ( $role ) {
168 return new MainSlotRoleHandler( [] );
169 } );
170 $registry->defineRoleWithModel( 'foo', CONTENT_MODEL_TEXT );
171
172 $title = $this->makeBlankTitleObject();
173 $this->assertEquals( [ 'main', 'foo' ], $registry->getAllowedRoles( $title ) );
174 }
175
180 public function testGetKnownRoles() {
181 $registry = $this->newSlotRoleRegistry(
182 $this->makeNameTableStore( [ 1 => 'foo' ] )
183 );
184 $registry->defineRoleWithModel( 'bar', CONTENT_MODEL_TEXT );
185
186 $this->assertTrue( $registry->isKnownRole( 'foo' ) );
187 $this->assertTrue( $registry->isKnownRole( 'bar' ) );
188 $this->assertFalse( $registry->isKnownRole( 'xyzzy' ) );
189
190 $title = $this->makeBlankTitleObject();
191 $this->assertArrayEquals( [ 'foo', 'bar' ], $registry->getKnownRoles( $title ) );
192 }
193
194}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
A SlotRoleHandler for the main slot.
SlotRoleHandler instances are used to declare the existence and behavior of slot roles.
A registry service for SlotRoleHandlers, used to define which slot roles are available on which page.
testGetAllowedRoles()
\MediaWiki\Revision\SlotRoleRegistry::getAllowedRoles()
newSlotRoleRegistry(NameTableStore $roleNameStore=null)
testGetKnownRoles()
\MediaWiki\Revision\SlotRoleRegistry::getKnownRoles() \MediaWiki\Revision\SlotRoleRegistry::isKnownRo...
testGetRequiredRoles()
\MediaWiki\Revision\SlotRoleRegistry::getRequiredRoles()
testGetRoleHandlerFallbackHandler()
\MediaWiki\Revision\SlotRoleRegistry::getRoleHandler()
testDefineRoleWithContentModel()
\MediaWiki\Revision\SlotRoleRegistry::defineRoleWithModel() \MediaWiki\Revision\SlotRoleRegistry::get...
testDefineRole()
\MediaWiki\Revision\SlotRoleRegistry::defineRole() \MediaWiki\Revision\SlotRoleRegistry::getDefinedRo...
testDefineRoleFailsForDupe()
\MediaWiki\Revision\SlotRoleRegistry::defineRole()
testGetRoleHandlerWithBadInstantiator()
\MediaWiki\Revision\SlotRoleRegistry::getRoleHandler()
testGetRoleHandlerForUnknownModel()
\MediaWiki\Revision\SlotRoleRegistry::getRoleHandler()
Represents a title within MediaWiki.
Definition Title.php:40
const CONTENT_MODEL_TEXT
Definition Defines.php:247
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Definition hooks.txt:894
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:955
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))