| 1 |
<?php |
| 2 |
namespace MediaWiki\Tests\Unit\Page; |
| 3 |
|
| 4 |
use File; |
| 5 |
use MediaWiki\Page\PageIdentityValue; |
| 6 |
use MediaWiki\Page\PageLookup; |
| 7 |
use MediaWiki\Page\PageStoreRecord; |
| 8 |
use MediaWiki\Page\RedirectStore; |
| 9 |
use MediaWiki\Title\TitleParser; |
| 10 |
use MediaWiki\Title\TitleValue; |
| 11 |
use MediaWikiUnitTestCase; |
| 12 |
use Psr\Log\NullLogger; |
| 13 |
use RepoGroup; |
| 14 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class RedirectStoreUnitTest extends MediaWikiUnitTestCase { |
| 20 |
|
| 21 |
private IConnectionProvider $connectionProvider; |
| 22 |
|
| 23 |
private PageLookup $pageLookup; |
| 24 |
|
| 25 |
private RepoGroup $repoGroup; |
| 26 |
|
| 27 |
private RedirectStore $redirectStore; |
| 28 |
|
| 29 |
protected function setUp(): void { |
| 30 |
parent::setUp(); |
| 31 |
|
| 32 |
$this->connectionProvider = $this->createMock( IConnectionProvider::class ); |
| 33 |
$this->pageLookup = $this->createMock( PageLookup::class ); |
| 34 |
$this->repoGroup = $this->createMock( RepoGroup::class ); |
| 35 |
|
| 36 |
$this->redirectStore = new RedirectStore( |
| 37 |
$this->connectionProvider, |
| 38 |
$this->pageLookup, |
| 39 |
$this->createMock( TitleParser::class ), |
| 40 |
$this->repoGroup, |
| 41 |
new NullLogger() |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
public function testShouldCacheLookupResultWhenPageDoesNotExist(): void { |
| 46 |
$page = PageIdentityValue::localIdentity( 1, NS_MAIN, 'Test' ); |
| 47 |
|
| 48 |
$this->pageLookup->expects( $this->once() ) |
| 49 |
->method( 'getPageByReference' ) |
| 50 |
->with( $page ) |
| 51 |
->willReturn( null ); |
| 52 |
|
| 53 |
$this->connectionProvider->expects( $this->never() ) |
| 54 |
->method( $this->anything() ); |
| 55 |
|
| 56 |
$this->redirectStore->getRedirectTarget( $page ); |
| 57 |
$target = $this->redirectStore->getRedirectTarget( $page ); |
| 58 |
|
| 59 |
$this->assertNull( $target ); |
| 60 |
} |
| 61 |
|
| 62 |
public function testShouldCacheLookupResultWhenPageIsNotARedirect(): void { |
| 63 |
$page = PageIdentityValue::localIdentity( 1, NS_MAIN, 'Test' ); |
| 64 |
|
| 65 |
$this->pageLookup->expects( $this->once() ) |
| 66 |
->method( 'getPageByReference' ) |
| 67 |
->with( $page ) |
| 68 |
->willReturn( new PageStoreRecord( (object)[ |
| 69 |
'page_id' => 1, |
| 70 |
'page_namespace' => NS_MAIN, |
| 71 |
'page_title' => 'Test', |
| 72 |
'page_is_redirect' => 0, |
| 73 |
'page_is_new' => 0, |
| 74 |
'page_latest' => 1, |
| 75 |
'page_touched' => '20210101000000', |
| 76 |
], PageStoreRecord::LOCAL ) ); |
| 77 |
|
| 78 |
$this->connectionProvider->expects( $this->never() ) |
| 79 |
->method( $this->anything() ); |
| 80 |
|
| 81 |
$this->redirectStore->getRedirectTarget( $page ); |
| 82 |
$target = $this->redirectStore->getRedirectTarget( $page ); |
| 83 |
|
| 84 |
$this->assertNull( $target ); |
| 85 |
} |
| 86 |
|
| 87 |
public function testShouldCacheLookupResultForFileWhenFileDoesNotExist(): void { |
| 88 |
$page = PageIdentityValue::localIdentity( 1, NS_FILE, 'Test.png' ); |
| 89 |
|
| 90 |
$this->repoGroup->expects( $this->once() ) |
| 91 |
->method( 'findFile' ) |
| 92 |
->with( $page ) |
| 93 |
->willReturn( false ); |
| 94 |
|
| 95 |
$this->pageLookup->expects( $this->once() ) |
| 96 |
->method( 'getPageByReference' ) |
| 97 |
->with( $page ) |
| 98 |
->willReturn( null ); |
| 99 |
|
| 100 |
$this->connectionProvider->expects( $this->never() ) |
| 101 |
->method( $this->anything() ); |
| 102 |
|
| 103 |
$this->redirectStore->getRedirectTarget( $page ); |
| 104 |
$target = $this->redirectStore->getRedirectTarget( $page ); |
| 105 |
|
| 106 |
$this->assertNull( $target ); |
| 107 |
} |
| 108 |
|
| 109 |
public function testShouldCacheLookupResultForLocalFileWhenPageDoesNotExist(): void { |
| 110 |
$page = PageIdentityValue::localIdentity( 1, NS_FILE, 'Test.png' ); |
| 111 |
|
| 112 |
$file = $this->createMock( File::class ); |
| 113 |
$file->method( 'isLocal' ) |
| 114 |
->willReturn( true ); |
| 115 |
|
| 116 |
$this->repoGroup->expects( $this->once() ) |
| 117 |
->method( 'findFile' ) |
| 118 |
->with( $page ) |
| 119 |
->willReturn( $file ); |
| 120 |
|
| 121 |
$this->pageLookup->expects( $this->once() ) |
| 122 |
->method( 'getPageByReference' ) |
| 123 |
->with( $page ) |
| 124 |
->willReturn( null ); |
| 125 |
|
| 126 |
$this->connectionProvider->expects( $this->never() ) |
| 127 |
->method( $this->anything() ); |
| 128 |
|
| 129 |
$this->redirectStore->getRedirectTarget( $page ); |
| 130 |
$target = $this->redirectStore->getRedirectTarget( $page ); |
| 131 |
|
| 132 |
$this->assertNull( $target ); |
| 133 |
} |
| 134 |
|
| 135 |
public function testShouldCacheLookupResultForForeignFileWhenItIsNotARedirect(): void { |
| 136 |
$page = PageIdentityValue::localIdentity( 1, NS_FILE, 'Test.png' ); |
| 137 |
|
| 138 |
$file = $this->createMock( File::class ); |
| 139 |
$file->method( 'isLocal' ) |
| 140 |
->willReturn( false ); |
| 141 |
$file->method( 'getRedirected' ) |
| 142 |
->willReturn( null ); |
| 143 |
$file->method( 'getName' ) |
| 144 |
->willReturn( 'Test.png' ); |
| 145 |
|
| 146 |
$this->repoGroup->expects( $this->once() ) |
| 147 |
->method( 'findFile' ) |
| 148 |
->with( $page ) |
| 149 |
->willReturn( $file ); |
| 150 |
|
| 151 |
$this->pageLookup->expects( $this->never() ) |
| 152 |
->method( $this->anything() ); |
| 153 |
|
| 154 |
$this->connectionProvider->expects( $this->never() ) |
| 155 |
->method( $this->anything() ); |
| 156 |
|
| 157 |
$this->redirectStore->getRedirectTarget( $page ); |
| 158 |
$target = $this->redirectStore->getRedirectTarget( $page ); |
| 159 |
|
| 160 |
$this->assertNull( $target ); |
| 161 |
} |
| 162 |
|
| 163 |
public function testShouldCacheLookupResultForForeignFileWithInvalidTarget(): void { |
| 164 |
$page = PageIdentityValue::localIdentity( 1, NS_FILE, 'Test.png' ); |
| 165 |
|
| 166 |
$file = $this->createMock( File::class ); |
| 167 |
$file->method( 'isLocal' ) |
| 168 |
->willReturn( false ); |
| 169 |
$file->method( 'getRedirected' ) |
| 170 |
->willReturn( 'Test.png' ); |
| 171 |
$file->method( 'getName' ) |
| 172 |
->willReturn( 'Test.png' ); |
| 173 |
|
| 174 |
$this->repoGroup->expects( $this->once() ) |
| 175 |
->method( 'findFile' ) |
| 176 |
->with( $page ) |
| 177 |
->willReturn( $file ); |
| 178 |
|
| 179 |
$this->pageLookup->expects( $this->never() ) |
| 180 |
->method( $this->anything() ); |
| 181 |
|
| 182 |
$this->connectionProvider->expects( $this->never() ) |
| 183 |
->method( $this->anything() ); |
| 184 |
|
| 185 |
$this->redirectStore->getRedirectTarget( $page ); |
| 186 |
$target = $this->redirectStore->getRedirectTarget( $page ); |
| 187 |
|
| 188 |
$this->assertNull( $target ); |
| 189 |
} |
| 190 |
|
| 191 |
public function testShouldCacheLookupResultForForeignFileWithValidTarget(): void { |
| 192 |
$page = PageIdentityValue::localIdentity( 1, NS_FILE, 'Test.png' ); |
| 193 |
|
| 194 |
$file = $this->createMock( File::class ); |
| 195 |
$file->method( 'isLocal' ) |
| 196 |
->willReturn( false ); |
| 197 |
$file->method( 'getRedirected' ) |
| 198 |
->willReturn( 'Test.png' ); |
| 199 |
$file->method( 'getName' ) |
| 200 |
->willReturn( 'Target.png' ); |
| 201 |
|
| 202 |
$this->repoGroup->expects( $this->once() ) |
| 203 |
->method( 'findFile' ) |
| 204 |
->with( $page ) |
| 205 |
->willReturn( $file ); |
| 206 |
|
| 207 |
$this->pageLookup->expects( $this->never() ) |
| 208 |
->method( $this->anything() ); |
| 209 |
|
| 210 |
$this->connectionProvider->expects( $this->never() ) |
| 211 |
->method( $this->anything() ); |
| 212 |
|
| 213 |
$this->redirectStore->getRedirectTarget( $page ); |
| 214 |
|
| 215 |
$expected = new TitleValue( NS_FILE, 'Target.png' ); |
| 216 |
$target = $this->redirectStore->getRedirectTarget( $page ); |
| 217 |
|
| 218 |
$this->assertNotNull( $target ); |
| 219 |
$this->assertTrue( $expected->isSameLinkAs( $target ) ); |
| 220 |
} |
| 221 |
|
| 222 |
public function testShouldVaryLookupResultCacheByPage(): void { |
| 223 |
$firstPage = PageIdentityValue::localIdentity( 1, NS_MAIN, 'Test' ); |
| 224 |
$secondPage = PageIdentityValue::localIdentity( 2, NS_MAIN, 'Test2' ); |
| 225 |
|
| 226 |
$this->pageLookup->expects( $this->exactly( 2 ) ) |
| 227 |
->method( 'getPageByReference' ) |
| 228 |
->willReturnMap( [ |
| 229 |
[ $firstPage, null ], |
| 230 |
[ $secondPage, null ], |
| 231 |
] ); |
| 232 |
|
| 233 |
$this->connectionProvider->expects( $this->never() ) |
| 234 |
->method( $this->anything() ); |
| 235 |
|
| 236 |
$this->redirectStore->getRedirectTarget( $firstPage ); |
| 237 |
$firstTarget = $this->redirectStore->getRedirectTarget( $firstPage ); |
| 238 |
|
| 239 |
$this->redirectStore->getRedirectTarget( $secondPage ); |
| 240 |
$secondTarget = $this->redirectStore->getRedirectTarget( $secondPage ); |
| 241 |
|
| 242 |
$this->assertNull( $firstTarget ); |
| 243 |
$this->assertNull( $secondTarget ); |
| 244 |
} |
| 245 |
|
| 246 |
public function testShouldInvalidateCachedLookupResultForPage(): void { |
| 247 |
$page = PageIdentityValue::localIdentity( 1, NS_MAIN, 'Test' ); |
| 248 |
|
| 249 |
$this->pageLookup->expects( $this->exactly( 2 ) ) |
| 250 |
->method( 'getPageByReference' ) |
| 251 |
->with( $page ) |
| 252 |
->willReturnOnConsecutiveCalls( |
| 253 |
null, |
| 254 |
new PageStoreRecord( (object)[ |
| 255 |
'page_id' => 1, |
| 256 |
'page_namespace' => NS_MAIN, |
| 257 |
'page_title' => 'Test', |
| 258 |
'page_is_redirect' => 0, |
| 259 |
'page_is_new' => 0, |
| 260 |
'page_latest' => 1, |
| 261 |
'page_touched' => '20210101000000', |
| 262 |
], PageStoreRecord::LOCAL ) |
| 263 |
); |
| 264 |
|
| 265 |
$this->connectionProvider->expects( $this->never() ) |
| 266 |
->method( $this->anything() ); |
| 267 |
|
| 268 |
$this->redirectStore->getRedirectTarget( $page ); |
| 269 |
$firstTarget = $this->redirectStore->getRedirectTarget( $page ); |
| 270 |
|
| 271 |
$this->redirectStore->clearCache( $page ); |
| 272 |
|
| 273 |
$this->redirectStore->getRedirectTarget( $page ); |
| 274 |
$secondTarget = $this->redirectStore->getRedirectTarget( $page ); |
| 275 |
|
| 276 |
$this->assertNull( $firstTarget ); |
| 277 |
$this->assertNull( $secondTarget ); |
| 278 |
} |
| 279 |
} |