File: MutableRevisionRecordTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (461 lines) | stat: -rw-r--r-- 14,687 bytes parent folder | download
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<?php

namespace MediaWiki\Tests\Unit\Revision;

use DummyContentForTesting;
use InvalidArgumentException;
use MediaWiki\CommentStore\CommentStoreComment;
use MediaWiki\Page\PageIdentity;
use MediaWiki\Page\PageIdentityValue;
use MediaWiki\Revision\BadRevisionException;
use MediaWiki\Revision\MutableRevisionRecord;
use MediaWiki\Revision\MutableRevisionSlots;
use MediaWiki\Revision\RevisionAccessException;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\Storage\RevisionSlotsUpdate;
use MediaWiki\User\UserIdentityValue;
use MediaWikiUnitTestCase;
use Wikimedia\Assert\PreconditionException;

/**
 * @covers \MediaWiki\Revision\MutableRevisionRecord
 * @covers \MediaWiki\Revision\RevisionRecord
 */
class MutableRevisionRecordTest extends MediaWikiUnitTestCase {
	use RevisionRecordTests;

	protected function expectedDefaultFieldVisibility( $field ): bool {
		return true;
	}

	/**
	 * @param array $rowOverrides
	 * @return MutableRevisionRecord
	 */
	protected function newRevision( array $rowOverrides = [] ) {
		$user = new UserIdentityValue( 11, 'Tester' );
		$comment = CommentStoreComment::newUnsavedComment( 'Hello World' );
		$wikiId = $rowOverrides['wikiId'] ?? RevisionRecord::LOCAL;

		$record = new MutableRevisionRecord(
			new PageIdentityValue( 17, NS_MAIN, 'Dummy', $wikiId ),
			$wikiId
		);

		if ( isset( $rowOverrides['rev_deleted'] ) ) {
			$record->setVisibility( $rowOverrides['rev_deleted'] );
		}

		if ( isset( $rowOverrides['rev_id'] ) ) {
			$record->setId( $rowOverrides['rev_id'] );
		}

		if ( isset( $rowOverrides['rev_page_id'] ) ) {
			$record->setPageId( $rowOverrides['rev_page_id'] );
		}

		if ( isset( $rowOverrides['rev_parent_id'] ) ) {
			$record->setParentId( $rowOverrides['rev_parent_id'] );
		}

		$record->setContent( SlotRecord::MAIN, new DummyContentForTesting( 'Lorem Ipsum' ) );
		$record->setComment( $comment );
		$record->setUser( $user );
		$record->setTimestamp( '20101010000000' );

		return $record;
	}

	public static function provideConstructorFailure() {
		yield 'not a wiki id' => [
			new PageIdentityValue( 17, NS_MAIN, 'Dummy', PageIdentity::LOCAL ),
			InvalidArgumentException::class,
			null,
		];
		yield 'wiki id mismatch' => [
			new PageIdentityValue( 17, NS_MAIN, 'Dummy', 'acme' ),
			PreconditionException::class,
			'blawiki',
		];
	}

	/**
	 * @dataProvider provideConstructorFailure
	 */
	public function testConstructorFailure(
		PageIdentity $page,
		string $expectedException,
		$wikiId = false
	) {
		$this->expectException( $expectedException );
		new MutableRevisionRecord( $page, $wikiId );
	}

	public function testSetGetId() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$this->assertNull( $record->getId() );
		$record->setId( 888 );
		$this->assertSame( 888, $record->getId() );
	}

	public function testSetGetUser() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$user = new UserIdentityValue( 0, 'Bla' );
		$this->assertNull( $record->getUser() );
		$record->setUser( $user );
		$this->assertSame( $user, $record->getUser() );
	}

	public function testSetGetPageId() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 0, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$this->assertSame( 0, $record->getPageId() );
		$record->setPageId( 999 );
		$this->assertSame( 999, $record->getPageId() );
	}

	public function testSetGetParentId() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$this->assertNull( $record->getParentId() );
		$record->setParentId( 100 );
		$this->assertSame( 100, $record->getParentId() );
	}

	public function testGetMainContentWhenEmpty() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$this->expectException( RevisionAccessException::class );
		$this->assertNull( $record->getContent( SlotRecord::MAIN ) );
	}

	public function testSetGetMainContent() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$content = new DummyContentForTesting( 'Badger' );
		$record->setContent( SlotRecord::MAIN, $content );
		$this->assertSame( $content, $record->getContent( SlotRecord::MAIN ) );
	}

	public function testGetSlotWhenEmpty() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$this->assertFalse( $record->hasSlot( SlotRecord::MAIN ) );

		$this->expectException( RevisionAccessException::class );
		$record->getSlot( SlotRecord::MAIN );
	}

	public function testSetGetSlot() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$slot = SlotRecord::newUnsaved(
			SlotRecord::MAIN,
			new DummyContentForTesting( 'x' )
		);
		$record->setSlot( $slot );
		$this->assertTrue( $record->hasSlot( SlotRecord::MAIN ) );
		$this->assertSame( $slot, $record->getSlot( SlotRecord::MAIN ) );
	}

	public function testSetGetMinor() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$this->assertFalse( $record->isMinor() );
		$record->setMinorEdit( true );
		$this->assertSame( true, $record->isMinor() );
	}

	public function testSetGetTimestamp() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$this->assertNull( $record->getTimestamp() );
		$record->setTimestamp( '20180101010101' );
		$this->assertSame( '20180101010101', $record->getTimestamp() );
	}

	public function testSetGetVisibility() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$this->assertSame( 0, $record->getVisibility() );
		$record->setVisibility( RevisionRecord::DELETED_USER );
		$this->assertSame( RevisionRecord::DELETED_USER, $record->getVisibility() );
	}

	public function testSetGetSha1() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$this->assertSame( 'phoiac9h4m842xq45sp7s6u21eteeq1', $record->getSha1() );
		$record->setSha1( 'someHash' );
		$this->assertSame( 'someHash', $record->getSha1() );
	}

	public function testResetSha1() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);

		$record->setContent( SlotRecord::MAIN, new DummyContentForTesting( 'foo' ) );
		$fooHash = $record->getSha1();

		// setting the content directly updates the hash
		$record->setContent( SlotRecord::MAIN, new DummyContentForTesting( 'barx' ) );
		$barxHash = $record->getSha1();
		$this->assertNotSame( $fooHash, $barxHash );

		// setting the content indirectly also updates the hash
		$record->getSlots()->setContent( 'aux', new DummyContentForTesting( 'frump' ) );
		$frumpHash = $record->getSha1();
		$this->assertNotSame( $barxHash, $frumpHash );
	}

	public function testGetSlots() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$this->assertInstanceOf( MutableRevisionSlots::class, $record->getSlots() );
	}

	public function testSetGetSize() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$this->assertSame( 0, $record->getSize() );
		$record->setSize( 775 );
		$this->assertSame( 775, $record->getSize() );
	}

	public function testResetSize() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);

		$record->setContent( SlotRecord::MAIN, new DummyContentForTesting( 'foo' ) );
		$fooSize = $record->getSize();

		// setting the content directly updates the hash
		$record->setContent( SlotRecord::MAIN, new DummyContentForTesting( 'barx' ) );
		$barxSize = $record->getSize();
		$this->assertNotSame( $fooSize, $barxSize );

		// setting the content indirectly also updates the hash
		$record->getSlots()->setContent( 'aux', new DummyContentForTesting( 'frump' ) );
		$frumpSize = $record->getSize();
		$this->assertNotSame( $barxSize, $frumpSize );
	}

	public function testSetGetComment() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$comment = new CommentStoreComment( 1, 'foo' );
		$this->assertNull( $record->getComment() );
		$record->setComment( $comment );
		$this->assertSame( $comment, $record->getComment() );
	}

	public function testSimpleGetOriginalAndInheritedSlots() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$mainSlot = new SlotRecord(
			(object)[
				'slot_id' => 1,
				'slot_revision_id' => null, // unsaved
				'slot_content_id' => 1,
				'content_address' => null, // touched
				'model_name' => 'x',
				'role_name' => SlotRecord::MAIN,
				'slot_origin' => null // touched
			],
			new DummyContentForTesting( SlotRecord::MAIN )
		);
		$auxSlot = new SlotRecord(
			(object)[
				'slot_id' => 2,
				'slot_revision_id' => null, // unsaved
				'slot_content_id' => 1,
				'content_address' => 'foo', // inherited
				'model_name' => 'x',
				'role_name' => 'aux',
				'slot_origin' => 1 // inherited
			],
			new DummyContentForTesting( 'aux' )
		);

		$record->setSlot( $mainSlot );
		$record->setSlot( $auxSlot );

		$this->assertSame( [ SlotRecord::MAIN ], $record->getOriginalSlots()->getSlotRoles() );
		$this->assertSame( $mainSlot, $record->getOriginalSlots()->getSlot( SlotRecord::MAIN ) );

		$this->assertSame( [ 'aux' ], $record->getInheritedSlots()->getSlotRoles() );
		$this->assertSame( $auxSlot, $record->getInheritedSlots()->getSlot( 'aux' ) );
	}

	public function testSimpleremoveSlot() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);

		$a = new DummyContentForTesting( 'a' );
		$b = new DummyContentForTesting( 'b' );

		$record->inheritSlot( SlotRecord::newSaved( 7, 3, 'a', SlotRecord::newUnsaved( 'a', $a ) ) );
		$record->inheritSlot( SlotRecord::newSaved( 7, 4, 'b', SlotRecord::newUnsaved( 'b', $b ) ) );

		$record->removeSlot( 'b' );

		$this->assertTrue( $record->hasSlot( 'a' ) );
		$this->assertFalse( $record->hasSlot( 'b' ) );
	}

	public function testApplyUpdate() {
		$update = new RevisionSlotsUpdate();

		$a = new DummyContentForTesting( 'a' );
		$b = new DummyContentForTesting( 'b' );
		$c = new DummyContentForTesting( 'c' );
		$x = new DummyContentForTesting( 'x' );

		$update->modifyContent( 'b', $x );
		$update->modifyContent( 'c', $x );
		$update->removeSlot( 'c' );
		$update->removeSlot( 'd' );

		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$record->inheritSlot( SlotRecord::newSaved( 7, 3, 'a', SlotRecord::newUnsaved( 'a', $a ) ) );
		$record->inheritSlot( SlotRecord::newSaved( 7, 4, 'b', SlotRecord::newUnsaved( 'b', $b ) ) );
		$record->inheritSlot( SlotRecord::newSaved( 7, 5, 'c', SlotRecord::newUnsaved( 'c', $c ) ) );

		$record->applyUpdate( $update );

		$this->assertEquals( [ 'b' ], array_keys( $record->getOriginalSlots()->getSlots() ) );
		$this->assertEquals( $a, $record->getSlot( 'a' )->getContent() );
		$this->assertEquals( $x, $record->getSlot( 'b' )->getContent() );
		$this->assertFalse( $record->hasSlot( 'c' ) );
	}

	public function provideNotReadyForInsertion() {
		$title = $this->makeMockTitle( 'Dummy' );
		$user = new UserIdentityValue( 42, 'Test' );

		/** @var CommentStoreComment $comment */
		$comment = $this->createMock( CommentStoreComment::class );

		$content = new DummyContentForTesting( 'Test' );

		$rev = new MutableRevisionRecord( $title );
		yield 'empty' => [ $rev ];

		$rev = new MutableRevisionRecord( $title );
		$rev->setContent( SlotRecord::MAIN, $content );
		$rev->setUser( $user );
		$rev->setComment( $comment );
		yield 'no timestamp' => [ $rev ];

		$rev = new MutableRevisionRecord( $title );
		$rev->setUser( $user );
		$rev->setComment( $comment );
		$rev->setTimestamp( '20101010000000' );
		yield 'no content' => [ $rev ];

		$rev = new MutableRevisionRecord( $title );
		$rev->setContent( SlotRecord::MAIN, $content );
		$rev->setComment( $comment );
		$rev->setTimestamp( '20101010000000' );
		yield 'no user' => [ $rev ];

		$rev = new MutableRevisionRecord( $title );
		$rev->setUser( $user );
		$rev->setContent( SlotRecord::MAIN, $content );
		$rev->setTimestamp( '20101010000000' );
		yield 'no comment' => [ $rev ];
	}

	/**
	 * @dataProvider provideNotReadyForInsertion
	 */
	public function testNotReadyForInsertion( $rev ) {
		$this->assertFalse( $rev->isReadyForInsertion() );
	}

	public function testIsCurrent() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$this->assertFalse( $record->isCurrent(),
			MutableRevisionRecord::class . ' cannot be stored current revision' );
	}

	public function testHasSameContent() {
		$rev1 = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$this->assertTrue( $rev1->hasSameContent( $rev1 ) );

		$rev2 = new MutableRevisionRecord(
			new PageIdentityValue( 2, NS_MAIN, 'Bar', PageIdentity::LOCAL )
		);
		$rev1->setSize( 1 );
		$rev2->setSize( 2 );
		$this->assertFalse( $rev1->hasSameContent( $rev2 ) );
	}

	public function testAudienceCan() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$this->expectException( InvalidArgumentException::class );
		$this->expectExceptionMessage(
			'An Authority object must be given when checking FOR_THIS_USER audience.'
		);
		$record->audienceCan( RevisionRecord::DELETED_TEXT, RevisionRecord::FOR_THIS_USER );
	}

	public function testGetContent_bad() {
		$record = new MutableRevisionRecord(
			new PageIdentityValue( 1, NS_MAIN, 'Foo', PageIdentity::LOCAL )
		);
		$slot = new SlotRecord(
			(object)[
				'slot_id' => 1,
				'slot_revision_id' => null,
				'slot_content_id' => 1,
				'content_address' => null,
				'model_name' => 'x',
				'role_name' => SlotRecord::MAIN,
				'slot_origin' => null
			],
			static function () {
				throw new BadRevisionException( 'bad' );
			}
		);
		$record->setSlot( $slot );

		$exception = null;
		try {
			$record->getContentOrThrow( SlotRecord::MAIN );
		} catch ( BadRevisionException $exception ) {
		}
		$this->assertNotNull( $exception );
		$this->assertNull( $record->getContent( SlotRecord::MAIN ) );
	}
}