aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/unit/includes/installer/PingbackTest.php
blob: 404df12634ac7af3cb3a014c8b8c41a1587d0230 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php

use MediaWiki\Config\HashConfig;
use MediaWiki\Http\HttpRequestFactory;
use MediaWiki\Installer\Pingback;
use MediaWiki\MainConfigNames;
use MediaWiki\Status\Status;
use Psr\Log\NullLogger;
use Wikimedia\Rdbms\IConnectionProvider;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\InsertQueryBuilder;
use Wikimedia\Rdbms\SelectQueryBuilder;
use Wikimedia\Timestamp\ConvertibleTimestamp;

/**
 * @covers \MediaWiki\Installer\Pingback
 */
class PingbackTest extends MediaWikiUnitTestCase {

	protected function setUp(): void {
		parent::setUp();
		ConvertibleTimestamp::setFakeTime( '20110401090000' );
	}

	/**
	 * @param IDatabase $database
	 * @param HttpRequestFactory $httpRequestFactory
	 * @param bool $enablePingback
	 * @param BagOStuff|null $cache
	 * @return Pingback
	 */
	private function makePingback(
		IDatabase $database,
		$httpRequestFactory,
		bool $enablePingback = true,
		$cache = null
	) {
		$dbProvider = $this->createNoOpMock( IConnectionProvider::class, [ 'getPrimaryDatabase', 'getReplicaDatabase' ] );
		$dbProvider->method( 'getPrimaryDatabase' )->willReturn( $database );
		$dbProvider->method( 'getReplicaDatabase' )->willReturn( $database );

		return new MockPingback(
			new HashConfig( [ MainConfigNames::Pingback => $enablePingback ] ),
			$dbProvider,
			$cache ?? new HashBagOStuff(),
			$httpRequestFactory,
			new NullLogger()
		);
	}

	public function testDisabled() {
		// Scenario: Disabled
		$pingback = $this->makePingback(
			$this->createNoOpMock( IDatabase::class ),
			$this->createNoOpMock( HttpRequestFactory::class ),
			false /* $enablePingback */
		);
		// Expect:
		// - no db select, lock, or upsert (asserted via no-op mock)
		// - no HTTP request (asserted via no-op mock)
		$pingback->run();
	}

	public function testCacheBusy() {
		// Scenario:
		// - enabled
		// - table is empty
		// - cache lock is unavailable
		$database = $this->createNoOpMock( IDatabase::class, [ 'selectField', 'newSelectQueryBuilder' ] );
		$database->expects( $this->once() )->method( 'selectField' )->willReturn( false );
		$database->method( 'newSelectQueryBuilder' )->willReturnCallback( static fn () => new SelectQueryBuilder( $database ) );

		$cache = $this->createMock( BagOStuff::class );
		$cache->method( 'add' )->willReturn( false );

		$pingback = $this->makePingback(
			$database,
			$this->createNoOpMock( HttpRequestFactory::class ),
			true, /* $enablePingback */
			$cache
		);

		// Expect:
		// - no db lock
		// - no HTTP request
		// - no db upsert for timestamp
		$pingback->run();
	}

	public function testDbBusy() {
		// Scenario:
		// - enabled
		// - table is empty
		// - cache lock is available
		// - db lock is unavailable
		$database = $this->createNoOpMock( IDatabase::class, [ 'selectField', 'lock', 'newSelectQueryBuilder' ] );
		$database->expects( $this->once() )->method( 'selectField' )->willReturn( false );
		$database->expects( $this->once() )->method( 'lock' )->willReturn( false );
		$database->method( 'newSelectQueryBuilder' )->willReturnCallback( static fn () => new SelectQueryBuilder( $database ) );

		$pingback = $this->makePingback(
			$database,
			$this->createNoOpMock( HttpRequestFactory::class )
		);
		// Expect:
		// - no HTTP request
		// - no db upsert for timestamp
		$pingback->run();
	}

	/**
	 * @dataProvider provideMakePing
	 */
	public function testMakePing( $priorPing ) {
		// Scenario:
		// - enabled
		// - table is either
		//     - empty ($priorPing is false)
		//     - has a ping from over a month ago ($piorPing)
		// - cache lock and db lock are available
		$database = $this->createNoOpMock(
			IDatabase::class,
			[ 'selectField', 'lock', 'upsert', 'newSelectQueryBuilder', 'newInsertQueryBuilder' ]
		);
		$httpRequestFactory = $this->createNoOpMock( HttpRequestFactory::class, [ 'create' ] );
		$httpRequest = $this->createNoOpMock( MWHttpRequest::class, [ 'setHeader', 'execute' ] );

		$database->expects( $this->once() )->method( 'selectField' )->willReturn( $priorPing );
		$database->expects( $this->once() )->method( 'lock' )->willReturn( true );

		$httpRequestFactory->expects( $this->once() )
			->method( 'create' )
			->with(
				'https://intake-analytics.wikimedia.org/v1/events?hasty=true',
				[
					'method' => 'POST',
					'postData' => '{"some":"stuff"}',
				]
			)
			->willReturn( $httpRequest );

		$httpRequest->expects( $this->once() )
			->method( 'execute' )
			->willReturn( Status::newGood() );

		$database->expects( $this->once() )->method( 'upsert' );
		$database->method( 'newSelectQueryBuilder' )->willReturnCallback( static fn () => new SelectQueryBuilder( $database ) );
		$database->method( 'newInsertQueryBuilder' )->willReturnCallback( static fn () => new InsertQueryBuilder( $database ) );

		$pingback = $this->makePingback(
			$database,
			$httpRequestFactory
		);
		// Expect:
		// - db lock acquired
		// - HTTP POST request
		// - db upsert for timestamp
		$pingback->run();
	}

	public static function provideMakePing() {
		yield 'No prior ping' => [ false ];
		yield 'Prior ping from over a month ago' => [
			ConvertibleTimestamp::convert( TS_UNIX, '20110301080000' )
		];
	}

	public function testAfterRecentPing() {
		// Scenario:
		// - enabled
		// - table contains a ping from one hour ago
		// - cache lock and db lock are available
		$database = $this->createNoOpMock( IDatabase::class, [ 'selectField', 'newSelectQueryBuilder' ] );
		$database->expects( $this->once() )->method( 'selectField' )->willReturn(
			ConvertibleTimestamp::convert( TS_UNIX, '20110401080000' )
		);
		$database->method( 'newSelectQueryBuilder' )->willReturnCallback( static fn () => new SelectQueryBuilder( $database ) );

		$pingback = $this->makePingback(
			$database,
			$this->createNoOpMock( HttpRequestFactory::class )
		);
		// Expect:
		// - no db lock
		// - no HTTP request
		// - no db upsert for timestamp
		$pingback->run();
	}
}

class MockPingback extends Pingback {
	protected function getData(): array {
		return [ 'some' => 'stuff' ];
	}
}