aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/unit/includes/Revision/MutableRevisionSlotsTest.php
blob: 9f504bfd12023e383b81d61c524320084dfc64ad (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
<?php

namespace MediaWiki\Tests\Unit\Revision;

use InvalidArgumentException;
use MediaWiki\Content\Content;
use MediaWiki\Content\WikitextContent;
use MediaWiki\Revision\MutableRevisionSlots;
use MediaWiki\Revision\RevisionAccessException;
use MediaWiki\Revision\RevisionSlots;
use MediaWiki\Revision\SlotRecord;

/**
 * @covers \MediaWiki\Revision\MutableRevisionSlots
 * @covers \MediaWiki\Revision\RevisionSlots
 */
class MutableRevisionSlotsTest extends RevisionSlotsTest {

	/**
	 * @param SlotRecord[] $slots
	 * @return RevisionSlots
	 */
	protected function newRevisionSlots( $slots = [] ) {
		return new MutableRevisionSlots( $slots );
	}

	public static function provideConstructorFailue() {
		yield 'array or the wrong thing' => [
			[ 1, 2, 3 ]
		];
	}

	/**
	 * @dataProvider provideConstructorFailue
	 * @param array $slots
	 */
	public function testConstructorFailue( $slots ) {
		$this->expectException( InvalidArgumentException::class );

		new MutableRevisionSlots( $slots );
	}

	public function testSetMultipleSlots() {
		$slots = new MutableRevisionSlots();

		$this->assertSame( [], $slots->getSlots() );

		$slotA = SlotRecord::newUnsaved( 'some', new WikitextContent( 'A' ) );
		$slots->setSlot( $slotA );
		$this->assertTrue( $slots->hasSlot( 'some' ) );
		$this->assertSame( $slotA, $slots->getSlot( 'some' ) );
		$this->assertSame( [ 'some' => $slotA ], $slots->getSlots() );

		$slotB = SlotRecord::newUnsaved( 'other', new WikitextContent( 'B' ) );
		$slots->setSlot( $slotB );
		$this->assertTrue( $slots->hasSlot( 'other' ) );
		$this->assertSame( $slotB, $slots->getSlot( 'other' ) );
		$this->assertSame( [ 'some' => $slotA, 'other' => $slotB ], $slots->getSlots() );
	}

	public function testSetExistingSlotOverwritesSlot() {
		$slots = new MutableRevisionSlots();

		$this->assertSame( [], $slots->getSlots() );

		$slotA = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
		$slots->setSlot( $slotA );
		$this->assertSame( $slotA, $slots->getSlot( SlotRecord::MAIN ) );
		$this->assertSame( [ SlotRecord::MAIN => $slotA ], $slots->getSlots() );

		$slotB = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'B' ) );
		$slots->setSlot( $slotB );
		$this->assertSame( $slotB, $slots->getSlot( SlotRecord::MAIN ) );
		$this->assertSame( [ SlotRecord::MAIN => $slotB ], $slots->getSlots() );
	}

	/**
	 * @param string $role
	 * @param Content $content
	 * @return SlotRecord
	 */
	private function newSavedSlot( $role, Content $content ) {
		return SlotRecord::newSaved( 7, 7, 'xyz', SlotRecord::newUnsaved( $role, $content ) );
	}

	public function testInheritSlotOverwritesSlot() {
		$slots = new MutableRevisionSlots();
		$slotA = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
		$slots->setSlot( $slotA );
		$slotB = $this->newSavedSlot( SlotRecord::MAIN, new WikitextContent( 'B' ) );
		$slotC = $this->newSavedSlot( 'foo', new WikitextContent( 'C' ) );
		$slots->inheritSlot( $slotB );
		$slots->inheritSlot( $slotC );
		$this->assertSame( [ SlotRecord::MAIN, 'foo' ], $slots->getSlotRoles() );
		$this->assertNotSame( $slotB, $slots->getSlot( SlotRecord::MAIN ) );
		$this->assertNotSame( $slotC, $slots->getSlot( 'foo' ) );
		$this->assertTrue( $slots->getSlot( SlotRecord::MAIN )->isInherited() );
		$this->assertTrue( $slots->getSlot( 'foo' )->isInherited() );
		$this->assertSame( $slotB->getContent(), $slots->getSlot( SlotRecord::MAIN )->getContent() );
		$this->assertSame( $slotC->getContent(), $slots->getSlot( 'foo' )->getContent() );
	}

	public function testSetContentOfExistingSlotOverwritesContent() {
		$slots = new MutableRevisionSlots();

		$this->assertSame( [], $slots->getSlots() );

		$slotA = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
		$slots->setSlot( $slotA );
		$this->assertSame( $slotA, $slots->getSlot( SlotRecord::MAIN ) );
		$this->assertSame( [ SlotRecord::MAIN => $slotA ], $slots->getSlots() );

		$newContent = new WikitextContent( 'B' );
		$slots->setContent( SlotRecord::MAIN, $newContent );
		$this->assertSame( $newContent, $slots->getContent( SlotRecord::MAIN ) );
	}

	public function testRemoveExistingSlot() {
		$slotA = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
		$slots = new MutableRevisionSlots( [ $slotA ] );

		$this->assertSame( [ SlotRecord::MAIN => $slotA ], $slots->getSlots() );

		$slots->removeSlot( SlotRecord::MAIN );
		$this->assertSame( [], $slots->getSlots() );
		$this->expectException( RevisionAccessException::class );
		$slots->getSlot( SlotRecord::MAIN );
	}

	public function testNewFromParentRevisionSlots() {
		/** @var SlotRecord[] $parentSlots */
		$parentSlots = [
			'some' => $this->newSavedSlot( 'some', new WikitextContent( 'X' ) ),
			'other' => $this->newSavedSlot( 'other', new WikitextContent( 'Y' ) ),
		];
		$slots = MutableRevisionSlots::newFromParentRevisionSlots( $parentSlots );
		$this->assertSame( [ 'some', 'other' ], $slots->getSlotRoles() );
		$this->assertNotSame( $parentSlots['some'], $slots->getSlot( 'some' ) );
		$this->assertNotSame( $parentSlots['other'], $slots->getSlot( 'other' ) );
		$this->assertTrue( $slots->getSlot( 'some' )->isInherited() );
		$this->assertTrue( $slots->getSlot( 'other' )->isInherited() );
		$this->assertSame( $parentSlots['some']->getContent(), $slots->getContent( 'some' ) );
		$this->assertSame( $parentSlots['other']->getContent(), $slots->getContent( 'other' ) );
	}

	public function testResetCallback() {
		$counter = 0;
		$callback = static function () use ( &$counter ) {
			$counter++;
		};

		$slots = new MutableRevisionSlots( [], $callback );

		$slot = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
		$slots->setSlot( $slot );
		$this->assertSame( 1, $counter, 'setSlot triggers callback' );

		$slots->setContent( 'other', new WikitextContent( 'Y' ) );
		$this->assertSame( 2, $counter, 'setContent triggers callback' );

		$slot = $this->newSavedSlot( 'some', new WikitextContent( 'X' ) );
		$slots->inheritSlot( $slot );
		$this->assertSame( 3, $counter, 'inheritSlot triggers callback' );

		$slots->removeSlot( SlotRecord::MAIN );
		$this->assertSame( 4, $counter, 'inheritSlot triggers callback' );
	}

}