MediaWiki  1.33.0
RevisionSlotsUpdateTest.php
Go to the documentation of this file.
1 <?php
2 
4 
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 
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() {
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 
242  public function testHasSameUpdates( RevisionSlotsUpdate $a, RevisionSlotsUpdate $b, $same ) {
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 );
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 }
Revision\RevisionAccessException
Exception representing a failure to look up a revision.
Definition: RevisionAccessException.php:33
MediaWikiTestCase\assertArrayEquals
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
Definition: MediaWikiTestCase.php:2070
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
MediaWiki\Tests\Storage\RevisionSlotsUpdateTest\testApplyUpdate
testApplyUpdate()
Definition: RevisionSlotsUpdateTest.php:256
MediaWiki\Tests\Storage\RevisionSlotsUpdateTest\testGetModifiedRoles
testGetModifiedRoles()
Definition: RevisionSlotsUpdateTest.php:182
MediaWiki\Tests\Storage\RevisionSlotsUpdateTest\testModifySlot
testModifySlot()
Definition: RevisionSlotsUpdateTest.php:121
Revision\MutableRevisionSlots
Mutable version of RevisionSlots, for constructing a new revision.
Definition: MutableRevisionSlots.php:33
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWiki\Tests\Storage\RevisionSlotsUpdateTest\testHasSameUpdates
testHasSameUpdates(RevisionSlotsUpdate $a, RevisionSlotsUpdate $b, $same)
provideHasSameUpdates
Definition: RevisionSlotsUpdateTest.php:242
MediaWiki\Tests\Storage
Definition: BlobStoreFactoryTest.php:3
MediaWiki\Tests\Storage\RevisionSlotsUpdateTest\testGetRemovedRoles
testGetRemovedRoles()
Definition: RevisionSlotsUpdateTest.php:195
MediaWiki\Tests\Storage\RevisionSlotsUpdateTest\provideHasSameUpdates
provideHasSameUpdates()
Definition: RevisionSlotsUpdateTest.php:210
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
MediaWiki\Storage\RevisionSlotsUpdate\hasSameUpdates
hasSameUpdates(RevisionSlotsUpdate $other)
Returns true if $other represents the same update - that is, if all methods defined by RevisionSlotsU...
Definition: RevisionSlotsUpdate.php:261
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MediaWiki\Tests\Storage\RevisionSlotsUpdateTest\testRemoveSlot
testRemoveSlot()
Definition: RevisionSlotsUpdateTest.php:158
WikitextContent
Content object for wiki text pages.
Definition: WikitextContent.php:36
array
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))
Revision\SlotRecord\newUnsaved
static newUnsaved( $role, Content $content)
Constructs a new Slot from a Content object for a new revision.
Definition: SlotRecord.php:129
MediaWiki\Storage\RevisionSlotsUpdate
Value object representing a modification of revision slots.
Definition: RevisionSlotsUpdate.php:36
MediaWiki\Tests\Storage\RevisionSlotsUpdateTest\testNewFromRevisionSlots
testNewFromRevisionSlots(RevisionSlots $newSlots, RevisionSlots $parentSlots=null, array $modified=[], array $removed=[])
provideNewFromRevisionSlots
Definition: RevisionSlotsUpdateTest.php:49
Revision\SlotRecord\MAIN
const MAIN
Definition: SlotRecord.php:41
Content
Base interface for content objects.
Definition: Content.php:34
Revision\MutableRevisionSlots\newFromParentRevisionSlots
static newFromParentRevisionSlots(array $slots)
Constructs a MutableRevisionSlots that inherits from the given list of slots.
Definition: MutableRevisionSlots.php:43
Revision\RevisionSlots\getSlot
getSlot( $role)
Returns the SlotRecord of the given slot.
Definition: RevisionSlots.php:106
MediaWiki\Storage\RevisionSlotsUpdate\newFromRevisionSlots
static newFromRevisionSlots(RevisionSlots $newSlots, RevisionSlots $parentSlots=null)
Constructs a RevisionSlotsUpdate representing the update that turned $parentSlots into $newSlots.
Definition: RevisionSlotsUpdate.php:58
Revision\SlotRecord\newSaved
static newSaved( $revisionId, $contentId, $contentAddress, SlotRecord $protoSlot)
Constructs a complete SlotRecord for a newly saved revision, based on the incomplete proto-slot.
Definition: SlotRecord.php:164
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
MediaWiki\Tests\Storage\RevisionSlotsUpdateTest\newSavedSlot
newSavedSlot( $role, Content $content)
Definition: RevisionSlotsUpdateTest.php:252
Revision\RevisionSlots
Value object representing the set of slots belonging to a revision.
Definition: RevisionSlots.php:35
MediaWiki\Tests\Storage\RevisionSlotsUpdateTest\testConstructor
testConstructor()
Definition: RevisionSlotsUpdateTest.php:103
true
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:1985
$content
$content
Definition: pageupdater.txt:72
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
MediaWiki\Tests\Storage\RevisionSlotsUpdateTest\provideNewFromContent
provideNewFromContent()
Definition: RevisionSlotsUpdateTest.php:65
MediaWiki\Tests\Storage\RevisionSlotsUpdateTest
\MediaWiki\Storage\RevisionSlotsUpdate
Definition: RevisionSlotsUpdateTest.php:17
MediaWiki\Tests\Storage\RevisionSlotsUpdateTest\provideNewFromRevisionSlots
provideNewFromRevisionSlots()
Definition: RevisionSlotsUpdateTest.php:19
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39
MediaWiki\Storage\RevisionSlotsUpdate\newFromContent
static newFromContent(array $newContent, RevisionSlots $parentSlots=null)
Constructs a RevisionSlotsUpdate representing the update of $parentSlots when changing $newContent.
Definition: RevisionSlotsUpdate.php:92
MediaWiki\Tests\Storage\RevisionSlotsUpdateTest\testNewFromContent
testNewFromContent(array $newContent, RevisionSlots $parentSlots=null, array $modified=[])
provideNewFromContent
Definition: RevisionSlotsUpdateTest.php:92