wiki.techinc.nl/tests/phpunit/includes/deferred/CdnCacheUpdateTest.php
Aaron Schulz 3c7f29a6b9 Add small HtmlCacheUpdater service class to normalize purging code (2)
This is a re-submit of 35da1bbd7c, which was accidentally merged before
CR (and reverted with aa4da3c2e8).

The purge() method handles purging of both file cache and CDN, using
a PRESEND deferred update. This avoids code duplication and missing
file cache purge calls.

Also:
* Migrate HTMLCacheUpdate callers to just directly using HTMLCacheUpdateJob
* Add HtmlFileCacheUpdate class and defer such updates just like with CDN
* Simplify HTMLCacheUpdate constructor parameters
* Remove BacklinkCache::clear() calls which do nothing since the backlink
  query does not actually happen until the job runs

Bug: T230025
Change-Id: Ic1005e70e2c22d5bd1ca36dcdb618108ebe290f3
2020-04-14 03:19:07 +00:00

68 lines
1.9 KiB
PHP

<?php
class CdnCacheUpdateTest extends MediaWikiTestCase {
/**
* @covers CdnCacheUpdate::merge
*/
public function testPurgeMergeWeb() {
$this->setMwGlobals( 'wgCommandLineMode', false );
$title = Title::newMainPage();
$urls1 = [];
$urls1[] = $title->getCanonicalURL( '?x=1' );
$urls1[] = $title->getCanonicalURL( '?x=2' );
$urls1[] = $title->getCanonicalURL( '?x=3' );
$update1 = $this->newCdnCacheUpdate( $urls1 );
$urls2 = [];
$urls2[] = $title->getCanonicalURL( '?x=2' );
$urls2[] = $title->getCanonicalURL( '?x=3' );
$urls2[] = $title->getCanonicalURL( '?x=4' );
$urls2[] = $title;
$update2 = $this->newCdnCacheUpdate( $urls2 );
$expected = [
$title->getInternalURL(),
$title->getInternalURL( 'action=history' ),
$title->getCanonicalURL( '?x=1' ),
$title->getCanonicalURL( '?x=2' ),
$title->getCanonicalURL( '?x=3' ),
$title->getCanonicalURL( '?x=4' ),
];
DeferredUpdates::addUpdate( $update1 );
DeferredUpdates::addUpdate( $update2 );
$this->assertEquals( $expected, $update1->getUrls() );
/** @var CdnCacheUpdate $update */
$update = null;
DeferredUpdates::clearPendingUpdates();
DeferredUpdates::addCallableUpdate( function () use ( $urls1, $urls2, &$update ) {
$update = $this->newCdnCacheUpdate( $urls1 );
DeferredUpdates::addUpdate( $update );
DeferredUpdates::addUpdate( $this->newCdnCacheUpdate( $urls2 ) );
DeferredUpdates::addUpdate(
$this->newCdnCacheUpdate( $urls2 ),
DeferredUpdates::PRESEND
);
} );
DeferredUpdates::doUpdates();
$this->assertEquals( $expected, $update->getUrls() );
$this->assertEquals( DeferredUpdates::pendingUpdatesCount(), 0, 'PRESEND update run' );
}
/**
* @param array $urls
* @return CdnCacheUpdate
*/
private function newCdnCacheUpdate( array $urls ) {
return $this->getMockBuilder( CdnCacheUpdate::class )
->setConstructorArgs( [ $urls ] )
->setMethods( [ 'doUpdate' ] )
->getMock();
}
}