MediaWiki  1.33.0
RevisionSlotsTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use InvalidArgumentException;
12 
14 
19  protected function newRevisionSlots( $slots = [] ) {
20  return new RevisionSlots( $slots );
21  }
22 
23  public function provideConstructorFailue() {
24  yield 'not an array or callable' => [
25  'foo'
26  ];
27  yield 'array of the wrong thing' => [
28  [ 1, 2, 3 ]
29  ];
30  }
31 
39  public function testConstructorFailue( $slots ) {
40  $this->setExpectedException( InvalidArgumentException::class );
41 
42  new RevisionSlots( $slots );
43  }
44 
48  public function testGetSlot() {
49  $mainSlot = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
50  $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) );
51  $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] );
52 
53  $this->assertSame( $mainSlot, $slots->getSlot( SlotRecord::MAIN ) );
54  $this->assertSame( $auxSlot, $slots->getSlot( 'aux' ) );
55  $this->setExpectedException( RevisionAccessException::class );
56  $slots->getSlot( 'nothere' );
57  }
58 
62  public function testHasSlot() {
63  $mainSlot = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
64  $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) );
65  $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] );
66 
67  $this->assertTrue( $slots->hasSlot( SlotRecord::MAIN ) );
68  $this->assertTrue( $slots->hasSlot( 'aux' ) );
69  $this->assertFalse( $slots->hasSlot( 'AUX' ) );
70  $this->assertFalse( $slots->hasSlot( 'xyz' ) );
71  }
72 
76  public function testGetContent() {
77  $mainContent = new WikitextContent( 'A' );
78  $auxContent = new WikitextContent( 'B' );
79  $mainSlot = SlotRecord::newUnsaved( SlotRecord::MAIN, $mainContent );
80  $auxSlot = SlotRecord::newUnsaved( 'aux', $auxContent );
81  $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] );
82 
83  $this->assertSame( $mainContent, $slots->getContent( SlotRecord::MAIN ) );
84  $this->assertSame( $auxContent, $slots->getContent( 'aux' ) );
85  $this->setExpectedException( RevisionAccessException::class );
86  $slots->getContent( 'nothere' );
87  }
88 
92  public function testGetSlotRoles_someSlots() {
93  $mainSlot = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
94  $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) );
95  $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] );
96 
97  $this->assertSame( [ 'main', 'aux' ], $slots->getSlotRoles() );
98  }
99 
103  public function testGetSlotRoles_noSlots() {
104  $slots = $this->newRevisionSlots( [] );
105 
106  $this->assertSame( [], $slots->getSlotRoles() );
107  }
108 
112  public function testGetSlots() {
113  $mainSlot = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
114  $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) );
115  $slotsArray = [ $mainSlot, $auxSlot ];
116  $slots = $this->newRevisionSlots( $slotsArray );
117 
118  $this->assertEquals( [ 'main' => $mainSlot, 'aux' => $auxSlot ], $slots->getSlots() );
119  }
120 
124  public function testGetInheritedSlots() {
125  $mainSlot = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
126  $auxSlot = SlotRecord::newInherited(
128  7, 7, 'foo',
129  SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) )
130  )
131  );
132  $slotsArray = [ $mainSlot, $auxSlot ];
133  $slots = $this->newRevisionSlots( $slotsArray );
134 
135  $this->assertEquals( [ 'aux' => $auxSlot ], $slots->getInheritedSlots() );
136  }
137 
141  public function testGetOriginalSlots() {
142  $mainSlot = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
143  $auxSlot = SlotRecord::newInherited(
145  7, 7, 'foo',
146  SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) )
147  )
148  );
149  $slotsArray = [ $mainSlot, $auxSlot ];
150  $slots = $this->newRevisionSlots( $slotsArray );
151 
152  $this->assertEquals( [ 'main' => $mainSlot ], $slots->getOriginalSlots() );
153  }
154 
155  public function provideComputeSize() {
156  yield [ 1, [ 'A' ] ];
157  yield [ 2, [ 'AA' ] ];
158  yield [ 4, [ 'AA', 'X', 'H' ] ];
159  }
160 
165  public function testComputeSize( $expected, $contentStrings ) {
166  $slotsArray = [];
167  foreach ( $contentStrings as $key => $contentString ) {
168  $slotsArray[] = SlotRecord::newUnsaved( strval( $key ), new WikitextContent( $contentString ) );
169  }
170  $slots = $this->newRevisionSlots( $slotsArray );
171 
172  $this->assertSame( $expected, $slots->computeSize() );
173  }
174 
175  public function provideComputeSha1() {
176  yield [ 'ctqm7794fr2dp1taki8a88ovwnvmnmj', [ 'A' ] ];
177  yield [ 'eyq8wiwlcofnaiy4eid97gyfy60uw51', [ 'AA' ] ];
178  yield [ 'lavctqfpxartyjr31f853drgfl4kj1g', [ 'AA', 'X', 'H' ] ];
179  }
180 
187  public function testComputeSha1( $expected, $contentStrings ) {
188  $slotsArray = [];
189  foreach ( $contentStrings as $key => $contentString ) {
190  $slotsArray[] = SlotRecord::newUnsaved( strval( $key ), new WikitextContent( $contentString ) );
191  }
192  $slots = $this->newRevisionSlots( $slotsArray );
193 
194  $this->assertSame( $expected, $slots->computeSha1() );
195  }
196 
197  public function provideHasSameContent() {
198  $fooX = SlotRecord::newUnsaved( 'x', new TextContent( 'Foo' ) );
199  $barZ = SlotRecord::newUnsaved( 'z', new TextContent( 'Bar' ) );
200  $fooY = SlotRecord::newUnsaved( 'y', new TextContent( 'Foo' ) );
201  $barZS = SlotRecord::newSaved( 7, 7, 'xyz', $barZ );
202  $barZ2 = SlotRecord::newUnsaved( 'z', new TextContent( 'Baz' ) );
203 
204  $a = $this->newRevisionSlots( [ 'x' => $fooX, 'z' => $barZ ] );
205  $a2 = $this->newRevisionSlots( [ 'x' => $fooX, 'z' => $barZ ] );
206  $a3 = $this->newRevisionSlots( [ 'x' => $fooX, 'z' => $barZS ] );
207  $b = $this->newRevisionSlots( [ 'y' => $fooY, 'z' => $barZ ] );
208  $c = $this->newRevisionSlots( [ 'x' => $fooX, 'z' => $barZ2 ] );
209 
210  yield 'same instance' => [ $a, $a, true ];
211  yield 'same slots' => [ $a, $a2, true ];
212  yield 'same content' => [ $a, $a3, true ];
213 
214  yield 'different roles' => [ $a, $b, false ];
215  yield 'different content' => [ $a, $c, false ];
216  }
217 
222  public function testHasSameContent( RevisionSlots $a, RevisionSlots $b, $same ) {
223  $this->assertSame( $same, $a->hasSameContent( $b ) );
224  $this->assertSame( $same, $b->hasSameContent( $a ) );
225  }
226 
228  $fooX = SlotRecord::newUnsaved( 'x', new TextContent( 'Foo' ) );
229  $barZ = SlotRecord::newUnsaved( 'z', new TextContent( 'Bar' ) );
230  $fooY = SlotRecord::newUnsaved( 'y', new TextContent( 'Foo' ) );
231  $barZS = SlotRecord::newSaved( 7, 7, 'xyz', $barZ );
232  $barZ2 = SlotRecord::newUnsaved( 'z', new TextContent( 'Baz' ) );
233 
234  $a = $this->newRevisionSlots( [ 'x' => $fooX, 'z' => $barZ ] );
235  $a2 = $this->newRevisionSlots( [ 'x' => $fooX, 'z' => $barZ ] );
236  $a3 = $this->newRevisionSlots( [ 'x' => $fooX, 'z' => $barZS ] );
237  $b = $this->newRevisionSlots( [ 'y' => $fooY, 'z' => $barZ ] );
238  $c = $this->newRevisionSlots( [ 'x' => $fooX, 'z' => $barZ2 ] );
239 
240  yield 'same instance' => [ $a, $a, [] ];
241  yield 'same slots' => [ $a, $a2, [] ];
242  yield 'same content' => [ $a, $a3, [] ];
243 
244  yield 'different roles' => [ $a, $b, [ 'x', 'y' ] ];
245  yield 'different content' => [ $a, $c, [ 'z' ] ];
246  }
247 
253  $this->assertArrayEquals( $roles, $a->getRolesWithDifferentContent( $b ) );
254  $this->assertArrayEquals( $roles, $b->getRolesWithDifferentContent( $a ) );
255  }
256 
257 }
MediaWiki\Tests\Revision\RevisionSlotsTest\provideComputeSha1
provideComputeSha1()
Definition: RevisionSlotsTest.php:175
Revision\RevisionAccessException
Exception representing a failure to look up a revision.
Definition: RevisionAccessException.php:33
MediaWiki\Tests\Revision\RevisionSlotsTest\newRevisionSlots
newRevisionSlots( $slots=[])
Definition: RevisionSlotsTest.php:19
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
Revision\RevisionSlots\getRolesWithDifferentContent
getRolesWithDifferentContent(RevisionSlots $other)
Find roles for which the $other RevisionSlots object has different content as this RevisionSlots obje...
Definition: RevisionSlots.php:283
Revision\SlotRecord\newInherited
static newInherited(SlotRecord $slot)
Constructs a new SlotRecord for a new revision, inheriting the content of the given SlotRecord of a p...
Definition: SlotRecord.php:103
MediaWiki\Tests\Revision\RevisionSlotsTest\provideConstructorFailue
provideConstructorFailue()
Definition: RevisionSlotsTest.php:23
MediaWiki\Tests\Revision\RevisionSlotsTest\testGetSlotRoles_someSlots
testGetSlotRoles_someSlots()
\MediaWiki\Revision\RevisionSlots::getSlotRoles
Definition: RevisionSlotsTest.php:92
MediaWiki\Tests\Revision
Definition: FallbackSlotRoleHandlerTest.php:3
MediaWiki\Tests\Revision\RevisionSlotsTest\testComputeSha1
testComputeSha1( $expected, $contentStrings)
provideComputeSha1 \MediaWiki\Revision\RevisionSlots::computeSha1
Definition: RevisionSlotsTest.php:187
MediaWiki\Tests\Revision\RevisionSlotsTest\testHasSameContent
testHasSameContent(RevisionSlots $a, RevisionSlots $b, $same)
provideHasSameContent \MediaWiki\Revision\RevisionSlots::hasSameContent
Definition: RevisionSlotsTest.php:222
Revision\RevisionSlots\hasSameContent
hasSameContent(RevisionSlots $other)
Checks whether the other RevisionSlots instance has the same content as this instance.
Definition: RevisionSlots.php:248
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\Revision\RevisionSlotsTest\testComputeSize
testComputeSize( $expected, $contentStrings)
provideComputeSize \MediaWiki\Revision\RevisionSlots::computeSize
Definition: RevisionSlotsTest.php:165
MediaWiki\Tests\Revision\RevisionSlotsTest\testHasSlot
testHasSlot()
\MediaWiki\Revision\RevisionSlots::hasSlot
Definition: RevisionSlotsTest.php:62
MediaWiki\Tests\Revision\RevisionSlotsTest\testGetContent
testGetContent()
\MediaWiki\Revision\RevisionSlots::getContent
Definition: RevisionSlotsTest.php:76
MediaWiki\Tests\Revision\RevisionSlotsTest\provideComputeSize
provideComputeSize()
Definition: RevisionSlotsTest.php:155
MediaWiki\Tests\Revision\RevisionSlotsTest\testConstructorFailue
testConstructorFailue( $slots)
provideConstructorFailue
Definition: RevisionSlotsTest.php:39
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
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\Revision\RevisionSlotsTest\testGetSlot
testGetSlot()
\MediaWiki\Revision\RevisionSlots::getSlot
Definition: RevisionSlotsTest.php:48
MediaWiki\Tests\Revision\RevisionSlotsTest\testGetRolesWithDifferentContent
testGetRolesWithDifferentContent(RevisionSlots $a, RevisionSlots $b, $roles)
provideGetRolesWithDifferentContent \MediaWiki\Revision\RevisionSlots::getRolesWithDifferentContent
Definition: RevisionSlotsTest.php:252
WikitextContent
Content object for wiki text pages.
Definition: WikitextContent.php:36
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\Tests\Revision\RevisionSlotsTest\testGetInheritedSlots
testGetInheritedSlots()
\MediaWiki\Revision\RevisionSlots::getInheritedSlots
Definition: RevisionSlotsTest.php:124
MediaWiki\Tests\Revision\RevisionSlotsTest\provideHasSameContent
provideHasSameContent()
Definition: RevisionSlotsTest.php:197
MediaWiki\Tests\Revision\RevisionSlotsTest\testGetOriginalSlots
testGetOriginalSlots()
\MediaWiki\Revision\RevisionSlots::getOriginalSlots
Definition: RevisionSlotsTest.php:141
MediaWiki\Tests\Revision\RevisionSlotsTest
Definition: RevisionSlotsTest.php:13
MediaWiki\Tests\Revision\RevisionSlotsTest\testGetSlotRoles_noSlots
testGetSlotRoles_noSlots()
\MediaWiki\Revision\RevisionSlots::getSlotRoles
Definition: RevisionSlotsTest.php:103
Revision\SlotRecord\MAIN
const MAIN
Definition: SlotRecord.php:41
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:37
MediaWiki\Tests\Revision\RevisionSlotsTest\testGetSlots
testGetSlots()
\MediaWiki\Revision\RevisionSlots::getSlots
Definition: RevisionSlotsTest.php:112
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
MediaWiki\Tests\Revision\RevisionSlotsTest\provideGetRolesWithDifferentContent
provideGetRolesWithDifferentContent()
Definition: RevisionSlotsTest.php:227
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
Revision\RevisionSlots
Value object representing the set of slots belonging to a revision.
Definition: RevisionSlots.php:35
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
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
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39