| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Integration\Permissions; |
| 4 |
|
| 5 |
use MediaWiki\Cache\CacheKeyHelper; |
| 6 |
use MediaWiki\Cache\LinkCache; |
| 7 |
use MediaWiki\CommentStore\CommentStore; |
| 8 |
use MediaWiki\Config\ServiceOptions; |
| 9 |
use MediaWiki\HookContainer\HookContainer; |
| 10 |
use MediaWiki\Linker\LinksMigration; |
| 11 |
use MediaWiki\MainConfigNames; |
| 12 |
use MediaWiki\Page\PageIdentityValue; |
| 13 |
use MediaWiki\Page\PageStore; |
| 14 |
use MediaWiki\Permissions\RestrictionStore; |
| 15 |
use MediaWiki\SpecialPage\SpecialPage; |
| 16 |
use MediaWiki\Title\Title; |
| 17 |
use MediaWikiIntegrationTestCase; |
| 18 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 19 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 20 |
use Wikimedia\Rdbms\ILoadBalancer; |
| 21 |
use Wikimedia\TestingAccessWrapper; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class RestrictionStoreTest extends MediaWikiIntegrationTestCase { |
| 31 |
private const DEFAULT_RESTRICTION_TYPES = [ 'create', 'edit', 'move', 'upload' ]; |
| 32 |
|
| 33 |
private WANObjectCache $wanCache; |
| 34 |
private ILoadBalancer $loadBalancer; |
| 35 |
private LinkCache $linkCache; |
| 36 |
private LinksMigration $linksMigration; |
| 37 |
private HookContainer $hookContainer; |
| 38 |
private CommentStore $commentStore; |
| 39 |
private PageStore $pageStore; |
| 40 |
|
| 41 |
|
| 42 |
private static $testPageRestrictionSource; |
| 43 |
|
| 44 |
private static $testPageRestrictionCascade; |
| 45 |
|
| 46 |
private static $testFileRestrictionSource; |
| 47 |
|
| 48 |
private static $testFileTarget; |
| 49 |
|
| 50 |
protected function setUp(): void { |
| 51 |
parent::setUp(); |
| 52 |
|
| 53 |
$services = $this->getServiceContainer(); |
| 54 |
$this->wanCache = $services->getMainWANObjectCache(); |
| 55 |
$this->loadBalancer = $services->getDBLoadBalancer(); |
| 56 |
$this->linkCache = $services->getLinkCache(); |
| 57 |
$this->linksMigration = $services->getLinksMigration(); |
| 58 |
$this->hookContainer = $services->getHookContainer(); |
| 59 |
$this->commentStore = $services->getCommentStore(); |
| 60 |
$this->pageStore = $services->getPageStore(); |
| 61 |
} |
| 62 |
|
| 63 |
public function addDBDataOnce() { |
| 64 |
self::$testPageRestrictionCascade = |
| 65 |
$this->insertPage( 'Template:RestrictionStoreTestA', 'wooooooo' ); |
| 66 |
$this->insertPage( 'Template:RestrictionStoreTestB', '{{RestrictionStoreTestA}}' ); |
| 67 |
|
| 68 |
self::$testPageRestrictionSource = |
| 69 |
$this->insertPage( 'RestrictionStoreTest_1', '{{RestrictionStoreTestB}}' ); |
| 70 |
|
| 71 |
$this->updateRestrictions( self::$testPageRestrictionSource['title'], [ 'edit' => 'sysop' ] ); |
| 72 |
|
| 73 |
self::$testFileTarget = $this->insertPage( 'File:RestrictionStoreTest.jpg', 'test file' ); |
| 74 |
self::$testFileRestrictionSource = |
| 75 |
$this->insertPage( 'RestrictionStoreTest_File', '[[File:RestrictionStoreTest.jpg]]' ); |
| 76 |
|
| 77 |
$this->updateRestrictions( self::$testFileRestrictionSource['title'], [ 'edit' => 'sysop' ], 1 ); |
| 78 |
} |
| 79 |
|
| 80 |
private function newRestrictionStore( array $options = [] ) { |
| 81 |
return new RestrictionStore( |
| 82 |
new ServiceOptions( RestrictionStore::CONSTRUCTOR_OPTIONS, $options + [ |
| 83 |
MainConfigNames::NamespaceProtection => [], |
| 84 |
MainConfigNames::RestrictionLevels => [ '', 'autoconfirmed', 'sysop' ], |
| 85 |
MainConfigNames::RestrictionTypes => self::DEFAULT_RESTRICTION_TYPES, |
| 86 |
MainConfigNames::SemiprotectedRestrictionLevels => [ 'autoconfirmed' ], |
| 87 |
] ), |
| 88 |
$this->wanCache, |
| 89 |
$this->loadBalancer, |
| 90 |
$this->linkCache, |
| 91 |
$this->linksMigration, |
| 92 |
$this->commentStore, |
| 93 |
$this->hookContainer, |
| 94 |
$this->pageStore |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
private function updateRestrictions( $page, array $limit, int $cascade = 1 ) { |
| 99 |
$this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $page ) |
| 100 |
->doUpdateRestrictions( |
| 101 |
$limit, |
| 102 |
[], |
| 103 |
$cascade, |
| 104 |
'test', |
| 105 |
$this->getTestSysop()->getUser() |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
public function testGetCascadeProtectionSources() { |
| 110 |
$page = self::$testPageRestrictionCascade['title']; |
| 111 |
$pageSource = self::$testPageRestrictionSource['title']; |
| 112 |
|
| 113 |
[ $sources, $restrictions, $tlSources, $ilSources ] = $this->newRestrictionStore() |
| 114 |
->getCascadeProtectionSources( $page ); |
| 115 |
$this->assertCount( 1, $sources ); |
| 116 |
$this->assertCount( 1, $tlSources ); |
| 117 |
$this->assertCount( 0, $ilSources ); |
| 118 |
$this->assertTrue( $pageSource->isSamePageAs( $sources[$pageSource->getId()] ) ); |
| 119 |
$this->assertArrayEquals( [ 'edit' => [ 'sysop' ] ], $restrictions ); |
| 120 |
|
| 121 |
[ $sources, $restrictions, $tlSources, $ilSources ] = $this->newRestrictionStore() |
| 122 |
->getCascadeProtectionSources( $pageSource ); |
| 123 |
$this->assertCount( 0, $sources ); |
| 124 |
$this->assertCount( 0, $tlSources ); |
| 125 |
$this->assertCount( 0, $ilSources ); |
| 126 |
$this->assertCount( 0, $restrictions ); |
| 127 |
} |
| 128 |
|
| 129 |
public function testGetCascadeProtectionSourcesSpecialPage() { |
| 130 |
[ $sources, $restrictions, $tlSources, $ilSources ] = $this->newRestrictionStore() |
| 131 |
->getCascadeProtectionSources( SpecialPage::getTitleFor( 'Whatlinkshere' ) ); |
| 132 |
$this->assertCount( 0, $sources ); |
| 133 |
$this->assertCount( 0, $tlSources ); |
| 134 |
$this->assertCount( 0, $ilSources ); |
| 135 |
$this->assertCount( 0, $restrictions ); |
| 136 |
} |
| 137 |
|
| 138 |
public function testGetCascadeProtectionSourcesFile() { |
| 139 |
$page = self::$testFileTarget['title']; |
| 140 |
$pageSource = self::$testFileRestrictionSource['title']; |
| 141 |
|
| 142 |
[ $sources, $restrictions, $tlSources, $ilSources ] = $this->newRestrictionStore() |
| 143 |
->getCascadeProtectionSources( $page ); |
| 144 |
|
| 145 |
$this->assertCount( 1, $sources ); |
| 146 |
$this->assertTrue( $pageSource->isSamePageAs( $sources[$pageSource->getId()] ) ); |
| 147 |
$this->assertArrayEquals( [ 'edit' => [ 'sysop' ] ], $restrictions ); |
| 148 |
$this->assertCount( 1, $ilSources ); |
| 149 |
$this->assertCount( 0, $tlSources ); |
| 150 |
|
| 151 |
[ $sources, $restrictions, $tlSources, $ilSources ] = $this->newRestrictionStore() |
| 152 |
->getCascadeProtectionSources( $pageSource ); |
| 153 |
$this->assertCount( 0, $sources ); |
| 154 |
$this->assertCount( 0, $tlSources ); |
| 155 |
$this->assertCount( 0, $ilSources ); |
| 156 |
$this->assertCount( 0, $restrictions ); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public function testLoadRestrictions( $page, $expectedCacheSubmap, ?array $restrictions = null ) { |
| 163 |
$cacheKey = CacheKeyHelper::getKeyForPage( $page ); |
| 164 |
|
| 165 |
if ( $restrictions ) { |
| 166 |
$this->updateRestrictions( $page, $restrictions ); |
| 167 |
} |
| 168 |
|
| 169 |
$restrictionStore = $this->newRestrictionStore(); |
| 170 |
$restrictionStore->loadRestrictions( $page ); |
| 171 |
$wrapper = TestingAccessWrapper::newFromObject( $restrictionStore ); |
| 172 |
$this->assertArraySubmapSame( |
| 173 |
$expectedCacheSubmap, |
| 174 |
$wrapper->cache[$cacheKey] |
| 175 |
); |
| 176 |
} |
| 177 |
|
| 178 |
public static function provideLoadRestrictions(): array { |
| 179 |
return [ |
| 180 |
'Regular page with restrictions' => [ |
| 181 |
Title::makeTitle( NS_MAIN, 'RestrictionStoreTest_1' ), |
| 182 |
[ 'restrictions' => [ 'edit' => [ 'sysop' ] ] ] |
| 183 |
], |
| 184 |
'Nonexistent page' => [ |
| 185 |
PageIdentityValue::localIdentity( 0, NS_MAIN, 'X' ), |
| 186 |
[ 'create_protection' => null ] |
| 187 |
], |
| 188 |
'Nonexistent page with restrictions' => [ |
| 189 |
PageIdentityValue::localIdentity( 0, NS_MAIN, 'X' ), |
| 190 |
[ 'create_protection' => [ 'expiry' => 'infinity' ] ], |
| 191 |
[ 'create' => 'sysop' ] |
| 192 |
], |
| 193 |
]; |
| 194 |
} |
| 195 |
|
| 196 |
public function testLoadRestrictions_latest() { |
| 197 |
$pageSource = self::$testPageRestrictionSource['title']; |
| 198 |
$cacheKey = CacheKeyHelper::getKeyForPage( $pageSource ); |
| 199 |
|
| 200 |
$restrictionStore = $this->newRestrictionStore(); |
| 201 |
$restrictionStore->loadRestrictions( $pageSource ); |
| 202 |
$wrapper = TestingAccessWrapper::newFromObject( $restrictionStore ); |
| 203 |
$this->assertArraySubmapSame( |
| 204 |
[ 'restrictions' => [ 'edit' => [ 'sysop' ] ] ], |
| 205 |
$wrapper->cache[$cacheKey] |
| 206 |
); |
| 207 |
|
| 208 |
$this->updateRestrictions( $pageSource, [ 'move' => 'sysop' ] ); |
| 209 |
$restrictionStore->loadRestrictions( $pageSource, IDBAccessObject::READ_LATEST ); |
| 210 |
$this->assertArraySubmapSame( |
| 211 |
[ 'restrictions' => [ 'move' => [ 'sysop' ] ] ], |
| 212 |
$wrapper->cache[$cacheKey] |
| 213 |
); |
| 214 |
} |
| 215 |
} |