Fix 'Tags' padding to keep it farther from the edge and document the source of the...
[lhc/web/wiklou.git] / tests / phpunit / includes / Storage / RevisionSlotsUpdateTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Storage;
4
5 use MediaWiki\Storage\RevisionSlots;
6 use MediaWiki\Storage\RevisionSlotsUpdate;
7 use MediaWiki\Storage\RevisionAccessException;
8 use MediaWiki\Storage\SlotRecord;
9 use MediaWikiTestCase;
10 use WikitextContent;
11
12 /**
13 * @covers \MediaWiki\Storage\RevisionSlotsUpdate
14 */
15 class RevisionSlotsUpdateTest extends MediaWikiTestCase {
16
17 public function provideNewFromRevisionSlots() {
18 $slotA = SlotRecord::newUnsaved( 'A', new WikitextContent( 'A' ) );
19 $slotB = SlotRecord::newUnsaved( 'B', new WikitextContent( 'B' ) );
20 $slotC = SlotRecord::newUnsaved( 'C', new WikitextContent( 'C' ) );
21
22 $slotB2 = SlotRecord::newUnsaved( 'B', new WikitextContent( 'B2' ) );
23
24 $parentSlots = new RevisionSlots( [
25 'A' => $slotA,
26 'B' => $slotB,
27 'C' => $slotC,
28 ] );
29
30 $newSlots = new RevisionSlots( [
31 'A' => $slotA,
32 'B' => $slotB2,
33 ] );
34
35 yield [ $newSlots, null, [ 'A', 'B' ], [] ];
36 yield [ $newSlots, $parentSlots, [ 'B' ], [ 'C' ] ];
37 }
38
39 /**
40 * @dataProvider provideNewFromRevisionSlots
41 *
42 * @param RevisionSlots $newSlots
43 * @param RevisionSlots $parentSlots
44 * @param $modified
45 * @param $removed
46 */
47 public function testNewFromRevisionSlots(
48 RevisionSlots $newSlots,
49 RevisionSlots $parentSlots = null,
50 array $modified = [],
51 array $removed = []
52 ) {
53 $update = RevisionSlotsUpdate::newFromRevisionSlots( $newSlots, $parentSlots );
54
55 $this->assertEquals( $modified, $update->getModifiedRoles() );
56 $this->assertEquals( $removed, $update->getRemovedRoles() );
57
58 foreach ( $modified as $role ) {
59 $this->assertSame( $newSlots->getSlot( $role ), $update->getModifiedSlot( $role ) );
60 }
61 }
62
63 public function testConstructor() {
64 $update = new RevisionSlotsUpdate();
65
66 $this->assertEmpty( $update->getModifiedRoles() );
67 $this->assertEmpty( $update->getRemovedRoles() );
68
69 $slotA = SlotRecord::newUnsaved( 'A', new WikitextContent( 'A' ) );
70 $update = new RevisionSlotsUpdate( [ 'A' => $slotA ] );
71
72 $this->assertEquals( [ 'A' ], $update->getModifiedRoles() );
73 $this->assertEmpty( $update->getRemovedRoles() );
74
75 $update = new RevisionSlotsUpdate( [ 'A' => $slotA ], [ 'X' ] );
76
77 $this->assertEquals( [ 'A' ], $update->getModifiedRoles() );
78 $this->assertEquals( [ 'X' ], $update->getRemovedRoles() );
79 }
80
81 public function testModifySlot() {
82 $slots = new RevisionSlotsUpdate();
83
84 $this->assertSame( [], $slots->getModifiedRoles() );
85 $this->assertSame( [], $slots->getRemovedRoles() );
86
87 $slotA = SlotRecord::newUnsaved( 'some', new WikitextContent( 'A' ) );
88 $slots->modifySlot( $slotA );
89 $this->assertTrue( $slots->isModifiedSlot( 'some' ) );
90 $this->assertFalse( $slots->isRemovedSlot( 'some' ) );
91 $this->assertSame( $slotA, $slots->getModifiedSlot( 'some' ) );
92 $this->assertSame( [ 'some' ], $slots->getModifiedRoles() );
93 $this->assertSame( [], $slots->getRemovedRoles() );
94
95 $slotB = SlotRecord::newUnsaved( 'other', new WikitextContent( 'B' ) );
96 $slots->modifySlot( $slotB );
97 $this->assertTrue( $slots->isModifiedSlot( 'other' ) );
98 $this->assertFalse( $slots->isRemovedSlot( 'other' ) );
99 $this->assertSame( $slotB, $slots->getModifiedSlot( 'other' ) );
100 $this->assertSame( [ 'some', 'other' ], $slots->getModifiedRoles() );
101 $this->assertSame( [], $slots->getRemovedRoles() );
102
103 // modify slot A again
104 $slots->modifySlot( $slotA );
105 $this->assertArrayEquals( [ 'some', 'other' ], $slots->getModifiedRoles() );
106
107 // remove modified slot
108 $slots->removeSlot( 'some' );
109 $this->assertSame( [ 'other' ], $slots->getModifiedRoles() );
110 $this->assertSame( [ 'some' ], $slots->getRemovedRoles() );
111
112 // modify removed slot
113 $slots->modifySlot( $slotA );
114 $this->assertArrayEquals( [ 'some', 'other' ], $slots->getModifiedRoles() );
115 $this->assertSame( [], $slots->getRemovedRoles() );
116 }
117
118 public function testRemoveSlot() {
119 $slots = new RevisionSlotsUpdate();
120
121 $slotA = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
122 $slots->modifySlot( $slotA );
123
124 $this->assertSame( [ 'main' ], $slots->getModifiedRoles() );
125
126 $slots->removeSlot( 'main' );
127 $slots->removeSlot( 'other' );
128 $this->assertSame( [], $slots->getModifiedRoles() );
129 $this->assertSame( [ 'main', 'other' ], $slots->getRemovedRoles() );
130 $this->assertTrue( $slots->isRemovedSlot( 'main' ) );
131 $this->assertTrue( $slots->isRemovedSlot( 'other' ) );
132 $this->assertFalse( $slots->isModifiedSlot( 'main' ) );
133
134 // removing the same slot again should not trigger an error
135 $slots->removeSlot( 'main' );
136
137 // getting a slot after removing it should fail
138 $this->setExpectedException( RevisionAccessException::class );
139 $slots->getModifiedSlot( 'main' );
140 }
141
142 public function testGetModifiedRoles() {
143 $slots = new RevisionSlotsUpdate( [], [ 'xyz' ] );
144
145 $this->assertSame( [], $slots->getModifiedRoles() );
146
147 $slots->modifyContent( 'main', new WikitextContent( 'A' ) );
148 $slots->modifyContent( 'foo', new WikitextContent( 'Foo' ) );
149 $this->assertSame( [ 'main', 'foo' ], $slots->getModifiedRoles() );
150
151 $slots->removeSlot( 'main' );
152 $this->assertSame( [ 'foo' ], $slots->getModifiedRoles() );
153 }
154
155 public function testGetRemovedRoles() {
156 $slotA = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
157 $slots = new RevisionSlotsUpdate( [ $slotA ] );
158
159 $this->assertSame( [], $slots->getRemovedRoles() );
160
161 $slots->removeSlot( 'main', new WikitextContent( 'A' ) );
162 $slots->removeSlot( 'foo', new WikitextContent( 'Foo' ) );
163
164 $this->assertSame( [ 'main', 'foo' ], $slots->getRemovedRoles() );
165
166 $slots->modifyContent( 'main', new WikitextContent( 'A' ) );
167 $this->assertSame( [ 'foo' ], $slots->getRemovedRoles() );
168 }
169
170 public function provideHasSameUpdates() {
171 $fooX = SlotRecord::newUnsaved( 'x', new WikitextContent( 'Foo' ) );
172 $barZ = SlotRecord::newUnsaved( 'z', new WikitextContent( 'Bar' ) );
173
174 $a = new RevisionSlotsUpdate();
175 $a->modifySlot( $fooX );
176 $a->modifySlot( $barZ );
177 $a->removeSlot( 'Q' );
178
179 $a2 = new RevisionSlotsUpdate();
180 $a2->modifySlot( $fooX );
181 $a2->modifySlot( $barZ );
182 $a2->removeSlot( 'Q' );
183
184 $b = new RevisionSlotsUpdate();
185 $b->modifySlot( $barZ );
186 $b->removeSlot( 'Q' );
187
188 $c = new RevisionSlotsUpdate();
189 $c->modifySlot( $fooX );
190 $c->modifySlot( $barZ );
191
192 yield 'same instance' => [ $a, $a, true ];
193 yield 'same udpates' => [ $a, $a2, true ];
194
195 yield 'different modified' => [ $a, $b, false ];
196 yield 'different removed' => [ $a, $c, false ];
197 }
198
199 /**
200 * @dataProvider provideHasSameUpdates
201 */
202 public function testHasSameUpdates( RevisionSlotsUpdate $a, RevisionSlotsUpdate $b, $same ) {
203 $this->assertSame( $same, $a->hasSameUpdates( $b ) );
204 $this->assertSame( $same, $b->hasSameUpdates( $a ) );
205 }
206
207 }