@@ -39,40 +39,108 @@ public function testRun() {
3939 }
4040
4141 public function testDeletedPageInvalidatesCache () {
42- $ registeredCallbacks = [];
42+ $ callbacks = [];
43+ $ spy = $ this ->newSpyCacheInvalidator ();
4344
44- $ hookContainer = $ this ->createMock ( HookContainer::class );
45- $ hookContainer ->method ( 'register ' )
46- ->willReturnCallback ( function ( $ name , $ callback ) use ( &$ registeredCallbacks ) {
47- $ registeredCallbacks [$ name ] = $ callback ;
48- } );
45+ $ this ->runSetupWithAutoRefresh ( $ spy , $ callbacks );
4946
50- $ cacheInvalidator = new class implements CacheInvalidator {
51- public ?Title $ invalidatedTitle = null ;
47+ $ page = $ this ->createMock ( ProperPageIdentity::class );
48+ $ page ->method ( 'getNamespace ' )->willReturn ( NS_MAIN );
49+ $ page ->method ( 'getDBkey ' )->willReturn ( 'DeletedPage ' );
5250
53- public function invalidateCaches ( Title $ title ): void {
54- $ this ->invalidatedTitle = $ title ;
55- }
56- };
51+ $ callbacks ['PageDeleteComplete ' ]( $ page );
52+
53+ $ this ->assertCount ( 1 , $ spy ->invalidatedTitles );
54+ $ this ->assertSame ( 'DeletedPage ' , $ spy ->invalidatedTitles [0 ]->getDBkey () );
55+ $ this ->assertSame ( NS_MAIN , $ spy ->invalidatedTitles [0 ]->getNamespace () );
56+ }
57+
58+ public function testSavedPageInvalidatesCache () {
59+ $ callbacks = [];
60+ $ spy = $ this ->newSpyCacheInvalidator ();
61+
62+ $ this ->runSetupWithAutoRefresh ( $ spy , $ callbacks );
63+
64+ $ wikiPage = $ this ->createMock ( \WikiPage::class );
65+ $ wikiPage ->method ( 'getTitle ' )->willReturn ( Title::makeTitle ( NS_TALK , 'SavedPage ' ) );
66+
67+ $ callbacks ['PageSaveComplete ' ]( $ wikiPage );
68+
69+ $ this ->assertCount ( 1 , $ spy ->invalidatedTitles );
70+ $ this ->assertSame ( 'SavedPage ' , $ spy ->invalidatedTitles [0 ]->getDBkey () );
71+ $ this ->assertSame ( NS_TALK , $ spy ->invalidatedTitles [0 ]->getNamespace () );
72+ }
73+
74+ public function testMovedPageInvalidatesCacheForBothTitles () {
75+ $ callbacks = [];
76+ $ spy = $ this ->newSpyCacheInvalidator ();
77+
78+ $ this ->runSetupWithAutoRefresh ( $ spy , $ callbacks );
79+
80+ $ callbacks ['PageMoveComplete ' ](
81+ Title::makeTitle ( NS_MAIN , 'OldPage ' ),
82+ Title::makeTitle ( NS_MAIN , 'NewPage ' )
83+ );
84+
85+ $ this ->assertCount ( 2 , $ spy ->invalidatedTitles );
86+ $ this ->assertSame ( 'OldPage ' , $ spy ->invalidatedTitles [0 ]->getDBkey () );
87+ $ this ->assertSame ( 'NewPage ' , $ spy ->invalidatedTitles [1 ]->getDBkey () );
88+ }
89+
90+ public function testAutoRefreshDisabledDoesNotInvalidateCache () {
91+ $ callbacks = [];
92+ $ hookContainer = $ this ->newCapturingHookContainer ( $ callbacks );
93+ $ spy = $ this ->newSpyCacheInvalidator ();
5794
5895 $ extension = $ this ->createMock ( Extension::class );
5996 $ extension ->method ( 'getSettings ' )
60- ->willReturn ( new Settings ( [ Settings::AUTO_REFRESH => true ] ) );
97+ ->willReturn ( new Settings ( [ Settings::AUTO_REFRESH => false ] ) );
6198 $ extension ->method ( 'getCacheInvalidator ' )
62- ->willReturn ( $ cacheInvalidator );
99+ ->willReturn ( $ spy );
63100
64101 $ setup = new Setup ( $ extension , $ hookContainer , __DIR__ . '/.. ' );
65102 $ setup ->run ();
66103
67104 $ page = $ this ->createMock ( ProperPageIdentity::class );
68105 $ page ->method ( 'getNamespace ' )->willReturn ( NS_MAIN );
69- $ page ->method ( 'getDBkey ' )->willReturn ( 'TestPage ' );
106+ $ page ->method ( 'getDBkey ' )->willReturn ( 'SomePage ' );
107+
108+ $ callbacks ['PageDeleteComplete ' ]( $ page );
109+
110+ $ this ->assertEmpty ( $ spy ->invalidatedTitles );
111+ }
112+
113+ private function runSetupWithAutoRefresh ( CacheInvalidator $ cacheInvalidator , array &$ callbacks ): void {
114+ $ hookContainer = $ this ->newCapturingHookContainer ( $ callbacks );
70115
71- $ this ->assertArrayHasKey ( 'PageDeleteComplete ' , $ registeredCallbacks );
72- $ registeredCallbacks ['PageDeleteComplete ' ]( $ page );
116+ $ extension = $ this ->createMock ( Extension::class );
117+ $ extension ->method ( 'getSettings ' )
118+ ->willReturn ( new Settings ( [ Settings::AUTO_REFRESH => true ] ) );
119+ $ extension ->method ( 'getCacheInvalidator ' )
120+ ->willReturn ( $ cacheInvalidator );
73121
74- $ this ->assertSame ( 'TestPage ' , $ cacheInvalidator ->invalidatedTitle ->getDBkey () );
75- $ this ->assertSame ( NS_MAIN , $ cacheInvalidator ->invalidatedTitle ->getNamespace () );
122+ $ setup = new Setup ( $ extension , $ hookContainer , __DIR__ . '/.. ' );
123+ $ setup ->run ();
124+ }
125+
126+ private function newCapturingHookContainer ( array &$ callbacks ): HookContainer {
127+ $ hookContainer = $ this ->createMock ( HookContainer::class );
128+ $ hookContainer ->method ( 'register ' )
129+ ->willReturnCallback ( function ( $ name , $ callback ) use ( &$ callbacks ) {
130+ $ callbacks [$ name ] = $ callback ;
131+ } );
132+ return $ hookContainer ;
133+ }
134+
135+ private function newSpyCacheInvalidator () {
136+ return new class implements CacheInvalidator {
137+ /** @var Title[] */
138+ public array $ invalidatedTitles = [];
139+
140+ public function invalidateCaches ( Title $ title ): void {
141+ $ this ->invalidatedTitles [] = $ title ;
142+ }
143+ };
76144 }
77145
78146 private function newExtension () {
0 commit comments