Merge "Fix 'Tags' padding to keep it farther from the edge and document the source...
[lhc/web/wiklou.git] / tests / phpunit / includes / Storage / RevisionSlotsTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Storage;
4
5 use InvalidArgumentException;
6 use MediaWiki\Storage\RevisionAccessException;
7 use MediaWiki\Storage\RevisionSlots;
8 use MediaWiki\Storage\SlotRecord;
9 use MediaWikiTestCase;
10 use TextContent;
11 use WikitextContent;
12
13 class RevisionSlotsTest extends MediaWikiTestCase {
14
15 /**
16 * @param SlotRecord[] $slots
17 * @return RevisionSlots
18 */
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
32 /**
33 * @dataProvider provideConstructorFailue
34 * @param $slots
35 *
36 * @covers \MediaWiki\Storage\RevisionSlots::__construct
37 * @covers \MediaWiki\Storage\RevisionSlots::setSlotsInternal
38 */
39 public function testConstructorFailue( $slots ) {
40 $this->setExpectedException( InvalidArgumentException::class );
41
42 new RevisionSlots( $slots );
43 }
44
45 /**
46 * @covers \MediaWiki\Storage\RevisionSlots::getSlot
47 */
48 public function testGetSlot() {
49 $mainSlot = SlotRecord::newUnsaved( '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( 'main' ) );
54 $this->assertSame( $auxSlot, $slots->getSlot( 'aux' ) );
55 $this->setExpectedException( RevisionAccessException::class );
56 $slots->getSlot( 'nothere' );
57 }
58
59 /**
60 * @covers \MediaWiki\Storage\RevisionSlots::hasSlot
61 */
62 public function testHasSlot() {
63 $mainSlot = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
64 $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) );
65 $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] );
66
67 $this->assertTrue( $slots->hasSlot( 'main' ) );
68 $this->assertTrue( $slots->hasSlot( 'aux' ) );
69 $this->assertFalse( $slots->hasSlot( 'AUX' ) );
70 $this->assertFalse( $slots->hasSlot( 'xyz' ) );
71 }
72
73 /**
74 * @covers \MediaWiki\Storage\RevisionSlots::getContent
75 */
76 public function testGetContent() {
77 $mainContent = new WikitextContent( 'A' );
78 $auxContent = new WikitextContent( 'B' );
79 $mainSlot = SlotRecord::newUnsaved( 'main', $mainContent );
80 $auxSlot = SlotRecord::newUnsaved( 'aux', $auxContent );
81 $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] );
82
83 $this->assertSame( $mainContent, $slots->getContent( 'main' ) );
84 $this->assertSame( $auxContent, $slots->getContent( 'aux' ) );
85 $this->setExpectedException( RevisionAccessException::class );
86 $slots->getContent( 'nothere' );
87 }
88
89 /**
90 * @covers \MediaWiki\Storage\RevisionSlots::getSlotRoles
91 */
92 public function testGetSlotRoles_someSlots() {
93 $mainSlot = SlotRecord::newUnsaved( '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
100 /**
101 * @covers \MediaWiki\Storage\RevisionSlots::getSlotRoles
102 */
103 public function testGetSlotRoles_noSlots() {
104 $slots = $this->newRevisionSlots( [] );
105
106 $this->assertSame( [], $slots->getSlotRoles() );
107 }
108
109 /**
110 * @covers \MediaWiki\Storage\RevisionSlots::getSlots
111 */
112 public function testGetSlots() {
113 $mainSlot = SlotRecord::newUnsaved( '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
121 /**
122 * @covers \MediaWiki\Storage\RevisionSlots::getInheritedSlots
123 */
124 public function testGetInheritedSlots() {
125 $mainSlot = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
126 $auxSlot = SlotRecord::newInherited(
127 SlotRecord::newSaved(
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
138 /**
139 * @covers \MediaWiki\Storage\RevisionSlots::getOriginalSlots
140 */
141 public function testGetOriginalSlots() {
142 $mainSlot = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
143 $auxSlot = SlotRecord::newInherited(
144 SlotRecord::newSaved(
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
161 /**
162 * @dataProvider provideComputeSize
163 * @covers \MediaWiki\Storage\RevisionSlots::computeSize
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 function provideComputeSha1() {
176 yield [ 'ctqm7794fr2dp1taki8a88ovwnvmnmj', [ 'A' ] ];
177 yield [ 'eyq8wiwlcofnaiy4eid97gyfy60uw51', [ 'AA' ] ];
178 yield [ 'lavctqfpxartyjr31f853drgfl4kj1g', [ 'AA', 'X', 'H' ] ];
179 }
180
181 /**
182 * @dataProvider provideComputeSha1
183 * @covers \MediaWiki\Storage\RevisionSlots::computeSha1
184 * @note this test is a bit brittle as the hashes are hardcoded, perhaps just check that strings
185 * are returned and different Slots objects return different strings?
186 */
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
218 /**
219 * @dataProvider provideHasSameContent
220 * @covers \MediaWiki\Storage\RevisionSlots::hasSameContent
221 */
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
227 public function provideGetRolesWithDifferentContent() {
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
248 /**
249 * @dataProvider provideGetRolesWithDifferentContent
250 * @covers \MediaWiki\Storage\RevisionSlots::getRolesWithDifferentContent
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 }