aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/deferred/CdnCacheUpdateTest.php
blob: 6365eca5c3c01e18bbfb16cbc573c634b7d65c96 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php

class CdnCacheUpdateTest extends MediaWikiIntegrationTestCase {

	/**
	 * @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 ] )
			->onlyMethods( [ 'doUpdate' ] )
			->getMock();
	}
}