MediaWiki  1.33.0
SlotRoleRegistryTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use InvalidArgumentException;
6 use LogicException;
12 use Title;
13 use 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 
67  $title = $this->makeBlankTitleObject();
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 
92  public function testDefineRoleWithContentModel() {
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 }
MediaWiki\Tests\Revision\SlotRoleRegistryTest\testGetKnownRoles
testGetKnownRoles()
\MediaWiki\Revision\SlotRoleRegistry::getKnownRoles() \MediaWiki\Revision\SlotRoleRegistry::isKnownRo...
Definition: SlotRoleRegistryTest.php:180
MediaWikiTestCase\assertArrayEquals
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
Definition: MediaWikiTestCase.php:2070
Revision\MainSlotRoleHandler
A SlotRoleHandler for the main slot.
Definition: MainSlotRoleHandler.php:41
MediaWiki\Tests\Revision\SlotRoleRegistryTest\makeNameTableStore
makeNameTableStore(array $names=[])
Definition: SlotRoleRegistryTest.php:29
MediaWiki\Tests\Revision\SlotRoleRegistryTest
\MediaWiki\Revision\SlotRoleRegistry
Definition: SlotRoleRegistryTest.php:18
MediaWiki\Tests\Revision
Definition: FallbackSlotRoleHandlerTest.php:3
MediaWiki\Tests\Revision\SlotRoleRegistryTest\testDefineRoleWithContentModel
testDefineRoleWithContentModel()
\MediaWiki\Revision\SlotRoleRegistry::defineRoleWithModel() \MediaWiki\Revision\SlotRoleRegistry::get...
Definition: SlotRoleRegistryTest.php:92
MediaWiki\Tests\Revision\SlotRoleRegistryTest\testDefineRoleFailsForDupe
testDefineRoleFailsForDupe()
\MediaWiki\Revision\SlotRoleRegistry::defineRole()
Definition: SlotRoleRegistryTest.php:74
Revision\SlotRoleHandler
SlotRoleHandler instances are used to declare the existence and behavior of slot roles.
Definition: SlotRoleHandler.php:34
MediaWiki\Tests\Revision\SlotRoleRegistryTest\testGetRoleHandlerWithBadInstantiator
testGetRoleHandlerWithBadInstantiator()
\MediaWiki\Revision\SlotRoleRegistry::getRoleHandler()
Definition: SlotRoleRegistryTest.php:139
php
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:35
MediaWiki\Tests\Revision\SlotRoleRegistryTest\makeBlankTitleObject
makeBlankTitleObject()
Definition: SlotRoleRegistryTest.php:20
MediaWiki\Tests\Revision\SlotRoleRegistryTest\testGetAllowedRoles
testGetAllowedRoles()
\MediaWiki\Revision\SlotRoleRegistry::getAllowedRoles()
Definition: SlotRoleRegistryTest.php:165
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
$handler
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:780
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MediaWiki\Tests\Revision\SlotRoleRegistryTest\testGetRoleHandlerFallbackHandler
testGetRoleHandlerFallbackHandler()
\MediaWiki\Revision\SlotRoleRegistry::getRoleHandler()
Definition: SlotRoleRegistryTest.php:124
array
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))
MediaWiki\Tests\Revision\SlotRoleRegistryTest\testGetRoleHandlerForUnknownModel
testGetRoleHandlerForUnknownModel()
\MediaWiki\Revision\SlotRoleRegistry::getRoleHandler()
Definition: SlotRoleRegistryTest.php:113
MediaWiki\Storage\NameTableStore
Definition: NameTableStore.php:35
Title
Represents a title within MediaWiki.
Definition: Title.php:40
MediaWiki\Tests\Revision\SlotRoleRegistryTest\testDefineRole
testDefineRole()
\MediaWiki\Revision\SlotRoleRegistry::defineRole() \MediaWiki\Revision\SlotRoleRegistry::getDefinedRo...
Definition: SlotRoleRegistryTest.php:54
MediaWiki\Tests\Revision\SlotRoleRegistryTest\testGetRequiredRoles
testGetRequiredRoles()
\MediaWiki\Revision\SlotRoleRegistry::getRequiredRoles()
Definition: SlotRoleRegistryTest.php:152
Revision\SlotRoleRegistry
A registry service for SlotRoleHandlers, used to define which slot roles are available on which page.
Definition: SlotRoleRegistry.php:48
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
CONTENT_MODEL_TEXT
const CONTENT_MODEL_TEXT
Definition: Defines.php:238
MediaWiki\Tests\Revision\SlotRoleRegistryTest\newSlotRoleRegistry
newSlotRoleRegistry(NameTableStore $roleNameStore=null)
Definition: SlotRoleRegistryTest.php:40