Merge "Force usage of MCR aware database schema"
[lhc/web/wiklou.git] / tests / phpunit / includes / Revision / RevisionStoreFactoryTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Revision;
4
5 use ActorMigration;
6 use CommentStore;
7 use MediaWiki\Revision\RevisionStore;
8 use MediaWiki\Revision\RevisionStoreFactory;
9 use MediaWiki\Revision\SlotRoleRegistry;
10 use MediaWiki\Storage\BlobStore;
11 use MediaWiki\Storage\BlobStoreFactory;
12 use MediaWiki\Storage\NameTableStore;
13 use MediaWiki\Storage\NameTableStoreFactory;
14 use MediaWiki\Storage\SqlBlobStore;
15 use Psr\Log\LoggerInterface;
16 use Psr\Log\NullLogger;
17 use WANObjectCache;
18 use Wikimedia\Rdbms\ILBFactory;
19 use Wikimedia\Rdbms\ILoadBalancer;
20 use Wikimedia\TestingAccessWrapper;
21
22 class RevisionStoreFactoryTest extends \MediaWikiIntegrationTestCase {
23
24 /**
25 * @covers \MediaWiki\Revision\RevisionStoreFactory::__construct
26 */
27 public function testValidConstruction_doesntCauseErrors() {
28 new RevisionStoreFactory(
29 $this->getMockLoadBalancerFactory(),
30 $this->getMockBlobStoreFactory(),
31 $this->getNameTableStoreFactory(),
32 $this->getMockSlotRoleRegistry(),
33 $this->getHashWANObjectCache(),
34 $this->getMockCommentStore(),
35 ActorMigration::newMigration(),
36 MIGRATION_NEW,
37 new NullLogger(),
38 true
39 );
40 $this->assertTrue( true );
41 }
42
43 public function provideWikiIds() {
44 yield [ true ];
45 yield [ false ];
46 yield [ 'somewiki' ];
47 }
48
49 /**
50 * @dataProvider provideWikiIds
51 * @covers \MediaWiki\Revision\RevisionStoreFactory::getRevisionStore
52 */
53 public function testGetRevisionStore(
54 $dbDomain,
55 $mcrMigrationStage = MIGRATION_NEW,
56 $contentHandlerUseDb = true
57 ) {
58 $lbFactory = $this->getMockLoadBalancerFactory();
59 $blobStoreFactory = $this->getMockBlobStoreFactory();
60 $nameTableStoreFactory = $this->getNameTableStoreFactory();
61 $slotRoleRegistry = $this->getMockSlotRoleRegistry();
62 $cache = $this->getHashWANObjectCache();
63 $commentStore = $this->getMockCommentStore();
64 $actorMigration = ActorMigration::newMigration();
65 $logger = new NullLogger();
66
67 $factory = new RevisionStoreFactory(
68 $lbFactory,
69 $blobStoreFactory,
70 $nameTableStoreFactory,
71 $slotRoleRegistry,
72 $cache,
73 $commentStore,
74 $actorMigration,
75 $mcrMigrationStage,
76 $logger,
77 $contentHandlerUseDb
78 );
79
80 $store = $factory->getRevisionStore( $dbDomain );
81 $wrapper = TestingAccessWrapper::newFromObject( $store );
82
83 // ensure the correct object type is returned
84 $this->assertInstanceOf( RevisionStore::class, $store );
85
86 // ensure the RevisionStore is for the given wikiId
87 $this->assertSame( $dbDomain, $wrapper->dbDomain );
88
89 // ensure all other required services are correctly set
90 $this->assertSame( $cache, $wrapper->cache );
91 $this->assertSame( $commentStore, $wrapper->commentStore );
92 $this->assertSame( $mcrMigrationStage, $wrapper->mcrMigrationStage );
93 $this->assertSame( $actorMigration, $wrapper->actorMigration );
94 $this->assertSame( $contentHandlerUseDb, $store->getContentHandlerUseDB() );
95
96 $this->assertInstanceOf( ILoadBalancer::class, $wrapper->loadBalancer );
97 $this->assertInstanceOf( BlobStore::class, $wrapper->blobStore );
98 $this->assertInstanceOf( NameTableStore::class, $wrapper->contentModelStore );
99 $this->assertInstanceOf( NameTableStore::class, $wrapper->slotRoleStore );
100 $this->assertInstanceOf( LoggerInterface::class, $wrapper->logger );
101 }
102
103 /**
104 * @return \PHPUnit_Framework_MockObject_MockObject|ILoadBalancer
105 */
106 private function getMockLoadBalancer() {
107 return $this->getMockBuilder( ILoadBalancer::class )
108 ->disableOriginalConstructor()->getMock();
109 }
110
111 /**
112 * @return \PHPUnit_Framework_MockObject_MockObject|ILBFactory
113 */
114 private function getMockLoadBalancerFactory() {
115 $mock = $this->getMockBuilder( ILBFactory::class )
116 ->disableOriginalConstructor()->getMock();
117
118 $mock->method( 'getMainLB' )
119 ->willReturnCallback( function () {
120 return $this->getMockLoadBalancer();
121 } );
122
123 return $mock;
124 }
125
126 /**
127 * @return \PHPUnit_Framework_MockObject_MockObject|SqlBlobStore
128 */
129 private function getMockSqlBlobStore() {
130 return $this->getMockBuilder( SqlBlobStore::class )
131 ->disableOriginalConstructor()->getMock();
132 }
133
134 /**
135 * @return \PHPUnit_Framework_MockObject_MockObject|BlobStoreFactory
136 */
137 private function getMockBlobStoreFactory() {
138 $mock = $this->getMockBuilder( BlobStoreFactory::class )
139 ->disableOriginalConstructor()->getMock();
140
141 $mock->method( 'newSqlBlobStore' )
142 ->willReturnCallback( function () {
143 return $this->getMockSqlBlobStore();
144 } );
145
146 return $mock;
147 }
148
149 /**
150 * @return SlotRoleRegistry
151 */
152 private function getMockSlotRoleRegistry() {
153 return $this->createMock( SlotRoleRegistry::class );
154 }
155
156 /**
157 * @return NameTableStoreFactory
158 */
159 private function getNameTableStoreFactory() {
160 return new NameTableStoreFactory(
161 $this->getMockLoadBalancerFactory(),
162 $this->getHashWANObjectCache(),
163 new NullLogger() );
164 }
165
166 /**
167 * @return \PHPUnit_Framework_MockObject_MockObject|CommentStore
168 */
169 private function getMockCommentStore() {
170 return $this->getMockBuilder( CommentStore::class )
171 ->disableOriginalConstructor()->getMock();
172 }
173
174 private function getHashWANObjectCache() {
175 return new WANObjectCache( [ 'cache' => new \HashBagOStuff() ] );
176 }
177
178 }