MediaWiki REL1_32
RevisionSlotsUpdateTest.php
Go to the documentation of this file.
1<?php
2
4
5use Content;
13
18
19 public function provideNewFromRevisionSlots() {
20 $slotA = SlotRecord::newUnsaved( 'A', new WikitextContent( 'A' ) );
21 $slotB = SlotRecord::newUnsaved( 'B', new WikitextContent( 'B' ) );
22 $slotC = SlotRecord::newUnsaved( 'C', new WikitextContent( 'C' ) );
23
24 $slotB2 = SlotRecord::newUnsaved( 'B', new WikitextContent( 'B2' ) );
25
26 $parentSlots = new RevisionSlots( [
27 'A' => $slotA,
28 'B' => $slotB,
29 'C' => $slotC,
30 ] );
31
32 $newSlots = new RevisionSlots( [
33 'A' => $slotA,
34 'B' => $slotB2,
35 ] );
36
37 yield [ $newSlots, null, [ 'A', 'B' ], [] ];
38 yield [ $newSlots, $parentSlots, [ 'B' ], [ 'C' ] ];
39 }
40
49 public function testNewFromRevisionSlots(
50 RevisionSlots $newSlots,
51 RevisionSlots $parentSlots = null,
52 array $modified = [],
53 array $removed = []
54 ) {
55 $update = RevisionSlotsUpdate::newFromRevisionSlots( $newSlots, $parentSlots );
56
57 $this->assertEquals( $modified, $update->getModifiedRoles() );
58 $this->assertEquals( $removed, $update->getRemovedRoles() );
59
60 foreach ( $modified as $role ) {
61 $this->assertSame( $newSlots->getSlot( $role ), $update->getModifiedSlot( $role ) );
62 }
63 }
64
65 public function provideNewFromContent() {
66 $slotA = SlotRecord::newUnsaved( 'A', new WikitextContent( 'A' ) );
67 $slotB = SlotRecord::newUnsaved( 'B', new WikitextContent( 'B' ) );
68 $slotC = SlotRecord::newUnsaved( 'C', new WikitextContent( 'C' ) );
69
70 $parentSlots = new RevisionSlots( [
71 'A' => $slotA,
72 'B' => $slotB,
73 'C' => $slotC,
74 ] );
75
76 $newContent = [
77 'A' => new WikitextContent( 'A' ),
78 'B' => new WikitextContent( 'B2' ),
79 ];
80
81 yield [ $newContent, null, [ 'A', 'B' ] ];
82 yield [ $newContent, $parentSlots, [ 'B' ] ];
83 }
84
92 public function testNewFromContent(
93 array $newContent,
94 RevisionSlots $parentSlots = null,
95 array $modified = []
96 ) {
97 $update = RevisionSlotsUpdate::newFromContent( $newContent, $parentSlots );
98
99 $this->assertEquals( $modified, $update->getModifiedRoles() );
100 $this->assertEmpty( $update->getRemovedRoles() );
101 }
102
103 public function testConstructor() {
104 $update = new RevisionSlotsUpdate();
105
106 $this->assertEmpty( $update->getModifiedRoles() );
107 $this->assertEmpty( $update->getRemovedRoles() );
108
109 $slotA = SlotRecord::newUnsaved( 'A', new WikitextContent( 'A' ) );
110 $update = new RevisionSlotsUpdate( [ 'A' => $slotA ] );
111
112 $this->assertEquals( [ 'A' ], $update->getModifiedRoles() );
113 $this->assertEmpty( $update->getRemovedRoles() );
114
115 $update = new RevisionSlotsUpdate( [ 'A' => $slotA ], [ 'X' ] );
116
117 $this->assertEquals( [ 'A' ], $update->getModifiedRoles() );
118 $this->assertEquals( [ 'X' ], $update->getRemovedRoles() );
119 }
120
121 public function testModifySlot() {
122 $slots = new RevisionSlotsUpdate();
123
124 $this->assertSame( [], $slots->getModifiedRoles() );
125 $this->assertSame( [], $slots->getRemovedRoles() );
126
127 $slotA = SlotRecord::newUnsaved( 'some', new WikitextContent( 'A' ) );
128 $slots->modifySlot( $slotA );
129 $this->assertTrue( $slots->isModifiedSlot( 'some' ) );
130 $this->assertFalse( $slots->isRemovedSlot( 'some' ) );
131 $this->assertSame( $slotA, $slots->getModifiedSlot( 'some' ) );
132 $this->assertSame( [ 'some' ], $slots->getModifiedRoles() );
133 $this->assertSame( [], $slots->getRemovedRoles() );
134
135 $slotB = SlotRecord::newUnsaved( 'other', new WikitextContent( 'B' ) );
136 $slots->modifySlot( $slotB );
137 $this->assertTrue( $slots->isModifiedSlot( 'other' ) );
138 $this->assertFalse( $slots->isRemovedSlot( 'other' ) );
139 $this->assertSame( $slotB, $slots->getModifiedSlot( 'other' ) );
140 $this->assertSame( [ 'some', 'other' ], $slots->getModifiedRoles() );
141 $this->assertSame( [], $slots->getRemovedRoles() );
142
143 // modify slot A again
144 $slots->modifySlot( $slotA );
145 $this->assertArrayEquals( [ 'some', 'other' ], $slots->getModifiedRoles() );
146
147 // remove modified slot
148 $slots->removeSlot( 'some' );
149 $this->assertSame( [ 'other' ], $slots->getModifiedRoles() );
150 $this->assertSame( [ 'some' ], $slots->getRemovedRoles() );
151
152 // modify removed slot
153 $slots->modifySlot( $slotA );
154 $this->assertArrayEquals( [ 'some', 'other' ], $slots->getModifiedRoles() );
155 $this->assertSame( [], $slots->getRemovedRoles() );
156 }
157
158 public function testRemoveSlot() {
159 $slots = new RevisionSlotsUpdate();
160
161 $slotA = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
162 $slots->modifySlot( $slotA );
163
164 $this->assertSame( [ 'main' ], $slots->getModifiedRoles() );
165
166 $slots->removeSlot( SlotRecord::MAIN );
167 $slots->removeSlot( 'other' );
168 $this->assertSame( [], $slots->getModifiedRoles() );
169 $this->assertSame( [ 'main', 'other' ], $slots->getRemovedRoles() );
170 $this->assertTrue( $slots->isRemovedSlot( SlotRecord::MAIN ) );
171 $this->assertTrue( $slots->isRemovedSlot( 'other' ) );
172 $this->assertFalse( $slots->isModifiedSlot( SlotRecord::MAIN ) );
173
174 // removing the same slot again should not trigger an error
175 $slots->removeSlot( SlotRecord::MAIN );
176
177 // getting a slot after removing it should fail
178 $this->setExpectedException( RevisionAccessException::class );
179 $slots->getModifiedSlot( SlotRecord::MAIN );
180 }
181
182 public function testGetModifiedRoles() {
183 $slots = new RevisionSlotsUpdate( [], [ 'xyz' ] );
184
185 $this->assertSame( [], $slots->getModifiedRoles() );
186
187 $slots->modifyContent( SlotRecord::MAIN, new WikitextContent( 'A' ) );
188 $slots->modifyContent( 'foo', new WikitextContent( 'Foo' ) );
189 $this->assertSame( [ 'main', 'foo' ], $slots->getModifiedRoles() );
190
191 $slots->removeSlot( SlotRecord::MAIN );
192 $this->assertSame( [ 'foo' ], $slots->getModifiedRoles() );
193 }
194
195 public function testGetRemovedRoles() {
196 $slotA = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
197 $slots = new RevisionSlotsUpdate( [ $slotA ] );
198
199 $this->assertSame( [], $slots->getRemovedRoles() );
200
201 $slots->removeSlot( SlotRecord::MAIN, new WikitextContent( 'A' ) );
202 $slots->removeSlot( 'foo', new WikitextContent( 'Foo' ) );
203
204 $this->assertSame( [ 'main', 'foo' ], $slots->getRemovedRoles() );
205
206 $slots->modifyContent( SlotRecord::MAIN, new WikitextContent( 'A' ) );
207 $this->assertSame( [ 'foo' ], $slots->getRemovedRoles() );
208 }
209
210 public function provideHasSameUpdates() {
211 $fooX = SlotRecord::newUnsaved( 'x', new WikitextContent( 'Foo' ) );
212 $barZ = SlotRecord::newUnsaved( 'z', new WikitextContent( 'Bar' ) );
213
214 $a = new RevisionSlotsUpdate();
215 $a->modifySlot( $fooX );
216 $a->modifySlot( $barZ );
217 $a->removeSlot( 'Q' );
218
219 $a2 = new RevisionSlotsUpdate();
220 $a2->modifySlot( $fooX );
221 $a2->modifySlot( $barZ );
222 $a2->removeSlot( 'Q' );
223
224 $b = new RevisionSlotsUpdate();
225 $b->modifySlot( $barZ );
226 $b->removeSlot( 'Q' );
227
228 $c = new RevisionSlotsUpdate();
229 $c->modifySlot( $fooX );
230 $c->modifySlot( $barZ );
231
232 yield 'same instance' => [ $a, $a, true ];
233 yield 'same udpates' => [ $a, $a2, true ];
234
235 yield 'different modified' => [ $a, $b, false ];
236 yield 'different removed' => [ $a, $c, false ];
237 }
238
243 $this->assertSame( $same, $a->hasSameUpdates( $b ) );
244 $this->assertSame( $same, $b->hasSameUpdates( $a ) );
245 }
246
252 private function newSavedSlot( $role, Content $content ) {
253 return SlotRecord::newSaved( 7, 7, 'xyz', SlotRecord::newUnsaved( $role, $content ) );
254 }
255
256 public function testApplyUpdate() {
258 $parentSlots = [
259 'X' => $this->newSavedSlot( 'X', new WikitextContent( 'X' ) ),
260 'Y' => $this->newSavedSlot( 'Y', new WikitextContent( 'Y' ) ),
261 'Z' => $this->newSavedSlot( 'Z', new WikitextContent( 'Z' ) ),
262 ];
263 $slots = MutableRevisionSlots::newFromParentRevisionSlots( $parentSlots );
264 $update = RevisionSlotsUpdate::newFromContent( [
265 'A' => new WikitextContent( 'A' ),
266 'Y' => new WikitextContent( 'yyy' ),
267 ] );
268
269 $update->removeSlot( 'Z' );
270
271 $update->apply( $slots );
272 $this->assertSame( [ 'X', 'Y', 'A' ], $slots->getSlotRoles() );
273 $this->assertSame( $update->getModifiedSlot( 'A' ), $slots->getSlot( 'A' ) );
274 $this->assertSame( $update->getModifiedSlot( 'Y' ), $slots->getSlot( 'Y' ) );
275 }
276
277}
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
Mutable version of RevisionSlots, for constructing a new revision.
Exception representing a failure to look up a revision.
Value object representing the set of slots belonging to a revision.
Value object representing a content slot associated with a page revision.
Value object representing a modification of revision slots.
testNewFromContent(array $newContent, RevisionSlots $parentSlots=null, array $modified=[])
provideNewFromContent
testNewFromRevisionSlots(RevisionSlots $newSlots, RevisionSlots $parentSlots=null, array $modified=[], array $removed=[])
provideNewFromRevisionSlots
testHasSameUpdates(RevisionSlotsUpdate $a, RevisionSlotsUpdate $b, $same)
provideHasSameUpdates
Content object for wiki text pages.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2055
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
Base interface for content objects.
Definition Content.php:34
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$content