Debian Codegraph @

tests/phpunit/unit/includes/Revision/RevisionSlotsTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Unit\Revision;
4
5 use InvalidArgumentException;
6 use MediaWiki\Content\TextContent;
7 use MediaWiki\Content\TextContentHandler;
8 use MediaWiki\Content\WikitextContent;
9 use MediaWiki\Revision\RevisionAccessException;
10 use MediaWiki\Revision\RevisionSlots;
11 use MediaWiki\Revision\SlotRecord;
12 use MediaWikiUnitTestCase;
13
14 /**
15 * @covers \MediaWiki\Revision\RevisionSlots
16 */
17 class RevisionSlotsTest extends MediaWikiUnitTestCase {
18
19 /**
20 * Creates a subclass that overrides AbstractContent::getContentHandler() and returns a
21 * ContentHandler without the need to go through MediaWikiServices.
22 *
23 * @param string $text
24 * @return TextContent
25 */
26 protected function getTextContent( $text ) {
27 return new class( $text ) extends TextContent {
28 public function getContentHandler() {
29 return new TextContentHandler();
30 }
31 };
32 }
33
34 /**
35 * @param SlotRecord[] $slots
36 * @return RevisionSlots
37 */
38 protected function newRevisionSlots( $slots = [] ) {
39 return new RevisionSlots( $slots );
40 }
41
42 public static function provideConstructorFailue() {
43 yield 'not an array or callable' => [
44 'foo'
45 ];
46 yield 'array of the wrong thing' => [
47 [ 1, 2, 3 ]
48 ];
49 }
50
51 /**
52 * @dataProvider provideConstructorFailue
53 * @param array $slots
54 */
55 public function testConstructorFailue( $slots ) {
56 $this->expectException( InvalidArgumentException::class );
57
58 new RevisionSlots( $slots );
59 }
60
61 public function testGetSlot() {
62 $mainSlot = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
63 $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) );
64 $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] );
65
66 $this->assertSame( $mainSlot, $slots->getSlot( SlotRecord::MAIN ) );
67 $this->assertSame( $auxSlot, $slots->getSlot( 'aux' ) );
68 $this->expectException( RevisionAccessException::class );
69 $slots->getSlot( 'nothere' );
70 }
71
72 public function testHasSlot() {
73 $mainSlot = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
74 $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) );
75 $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] );
76
77 $this->assertTrue( $slots->hasSlot( SlotRecord::MAIN ) );
78 $this->assertTrue( $slots->hasSlot( 'aux' ) );
79 $this->assertFalse( $slots->hasSlot( 'AUX' ) );
80 $this->assertFalse( $slots->hasSlot( 'xyz' ) );
81 }
82
83 public function testGetContent() {
84 $mainContent = new WikitextContent( 'A' );
85 $auxContent = new WikitextContent( 'B' );
86 $mainSlot = SlotRecord::newUnsaved( SlotRecord::MAIN, $mainContent );
87 $auxSlot = SlotRecord::newUnsaved( 'aux', $auxContent );
88 $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] );
89
90 $this->assertSame( $mainContent, $slots->getContent( SlotRecord::MAIN ) );
91 $this->assertSame( $auxContent, $slots->getContent( 'aux' ) );
92 $this->expectException( RevisionAccessException::class );
93 $slots->getContent( 'nothere' );
94 }
95
96 public function testGetSlotRoles_someSlots() {
97 $mainSlot = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
98 $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) );
99 $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] );
100
101 $this->assertSame( [ SlotRecord::MAIN, 'aux' ], $slots->getSlotRoles() );
102 }
103
104 public function testGetSlotRoles_noSlots() {
105 $slots = $this->newRevisionSlots( [] );
106
107 $this->assertSame( [], $slots->getSlotRoles() );
108 }
109
110 public function testGetSlots() {
111 $mainSlot = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
112 $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) );
113 $slotsArray = [ $mainSlot, $auxSlot ];
114 $slots = $this->newRevisionSlots( $slotsArray );
115
116 $this->assertEquals( [ SlotRecord::MAIN => $mainSlot, 'aux' => $auxSlot ], $slots->getSlots() );
117 }
118
119 public function testGetNonDerivedSlots() {
120 $mainSlot = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
121 $auxSlot = SlotRecord::newDerived( 'aux', new WikitextContent( 'B' ) );
122 $slotsArray = [ $mainSlot, $auxSlot ];
123 $slots = $this->newRevisionSlots( $slotsArray );
124
125 $this->assertEquals( [ SlotRecord::MAIN => $mainSlot ], $slots->getPrimarySlots() );
126 }
127
128 public function testGetInheritedSlots() {
129 $mainSlot = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
130 $auxSlot = SlotRecord::newInherited(
131 SlotRecord::newSaved(
132 7, 7, 'foo',
133 SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) )
134 )
135 );
136 $slotsArray = [ $mainSlot, $auxSlot ];
137 $slots = $this->newRevisionSlots( $slotsArray );
138
139 $this->assertEquals( [ 'aux' => $auxSlot ], $slots->getInheritedSlots() );
140 }
141
142 public function testGetOriginalSlots() {
143 $mainSlot = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
144 $auxSlot = SlotRecord::newInherited(
145 SlotRecord::newSaved(
146 7, 7, 'foo',
147 SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) )
148 )
149 );
150 $slotsArray = [ $mainSlot, $auxSlot ];
151 $slots = $this->newRevisionSlots( $slotsArray );
152
153 $this->assertEquals( [ SlotRecord::MAIN => $mainSlot ], $slots->getOriginalSlots() );
154 }
155
156 public static function provideComputeSize() {
157 yield [ 1, [ 'A' ] ];
158 yield [ 2, [ 'AA' ] ];
159 yield [ 4, [ 'AA', 'X', 'H' ] ];
160 }
161
162 /**
163 * @dataProvider provideComputeSize
164 */
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 static function provideComputeSha1() {
176 yield [ 'ctqm7794fr2dp1taki8a88ovwnvmnmj', [ 'A' ] ];
177 yield [ 'eyq8wiwlcofnaiy4eid97gyfy60uw51', [ 'AA' ] ];
178 yield [ 'lavctqfpxartyjr31f853drgfl4kj1g', [ 'AA', 'X', 'H' ] ];
179 }
180
181 /**
182 * @dataProvider provideComputeSha1
183 * @note this test is a bit brittle as the hashes are hardcoded, perhaps just check that strings
184 * are returned and different Slots objects return different strings?
185 */
186 public function testComputeSha1( $expected, $contentStrings ) {
187 $slotsArray = [];
188 foreach ( $contentStrings as $key => $contentString ) {
189 $slotsArray[] = SlotRecord::newUnsaved(
190 strval( $key ),
191 $this->getTextContent( $contentString )
192 );
193 }
194 $slots = $this->newRevisionSlots( $slotsArray );
195
196 $this->assertSame( $expected, $slots->computeSha1() );
197 }
198
199 public function provideHasSameContent() {
200 $fooX = SlotRecord::newUnsaved( 'x', $this->getTextContent( 'Foo' ) );
201 $barZ = SlotRecord::newUnsaved( 'z', $this->getTextContent( 'Bar' ) );
202 $fooY = SlotRecord::newUnsaved( 'y', $this->getTextContent( 'Foo' ) );
203 $barZS = SlotRecord::newSaved( 7, 7, 'xyz', $barZ );
204 $barZ2 = SlotRecord::newUnsaved( 'z', $this->getTextContent( 'Baz' ) );
205
206 $a = $this->newRevisionSlots( [ 'x' => $fooX, 'z' => $barZ ] );
207 $a2 = $this->newRevisionSlots( [ 'x' => $fooX, 'z' => $barZ ] );
208 $a3 = $this->newRevisionSlots( [ 'x' => $fooX, 'z' => $barZS ] );
209 $b = $this->newRevisionSlots( [ 'y' => $fooY, 'z' => $barZ ] );
210 $c = $this->newRevisionSlots( [ 'x' => $fooX, 'z' => $barZ2 ] );
211
212 yield 'same instance' => [ $a, $a, true ];
213 yield 'same slots' => [ $a, $a2, true ];
214 yield 'same content' => [ $a, $a3, true ];
215
216 yield 'different roles' => [ $a, $b, false ];
217 yield 'different content' => [ $a, $c, false ];
218 }
219
220 /**
221 * @dataProvider provideHasSameContent
222 */
223 public function testHasSameContent( RevisionSlots $a, RevisionSlots $b, $same ) {
224 $this->assertSame( $same, $a->hasSameContent( $b ) );
225 $this->assertSame( $same, $b->hasSameContent( $a ) );
226 }
227
228 public function provideGetRolesWithDifferentContent() {
229 $fooX = SlotRecord::newUnsaved( 'x', $this->getTextContent( 'Foo' ) );
230 $barZ = SlotRecord::newUnsaved( 'z', $this->getTextContent( 'Bar' ) );
231 $fooY = SlotRecord::newUnsaved( 'y', $this->getTextContent( 'Foo' ) );
232 $barZS = SlotRecord::newSaved( 7, 7, 'xyz', $barZ );
233 $barZ2 = SlotRecord::newUnsaved( 'z', $this->getTextContent( 'Baz' ) );
234
235 $a = $this->newRevisionSlots( [ 'x' => $fooX, 'z' => $barZ ] );
236 $a2 = $this->newRevisionSlots( [ 'x' => $fooX, 'z' => $barZ ] );
237 $a3 = $this->newRevisionSlots( [ 'x' => $fooX, 'z' => $barZS ] );
238 $b = $this->newRevisionSlots( [ 'y' => $fooY, 'z' => $barZ ] );
239 $c = $this->newRevisionSlots( [ 'x' => $fooX, 'z' => $barZ2 ] );
240
241 yield 'same instance' => [ $a, $a, [] ];
242 yield 'same slots' => [ $a, $a2, [] ];
243 yield 'same content' => [ $a, $a3, [] ];
244
245 yield 'different roles' => [ $a, $b, [ 'x', 'y' ] ];
246 yield 'different content' => [ $a, $c, [ 'z' ] ];
247 }
248
249 /**
250 * @dataProvider provideGetRolesWithDifferentContent
251 */
252 public function testGetRolesWithDifferentContent( RevisionSlots $a, RevisionSlots $b, $roles ) {
253 $this->assertArrayEquals( $roles, $a->getRolesWithDifferentContent( $b ) );
254 $this->assertArrayEquals( $roles, $b->getRolesWithDifferentContent( $a ) );
255 }
256
257 }