3 namespace MediaWiki\Tests\Storage
;
5 use MediaWiki\Storage\RevisionAccessException
;
6 use MediaWiki\Storage\RevisionSlots
;
7 use MediaWiki\Storage\SlotRecord
;
11 class RevisionSlotsTest
extends MediaWikiTestCase
{
14 * @param SlotRecord[] $slots
15 * @return RevisionSlots
17 protected function newRevisionSlots( $slots = [] ) {
18 return new RevisionSlots( $slots );
22 * @covers \MediaWiki\Storage\RevisionSlots::getSlot
24 public function testGetSlot() {
25 $mainSlot = SlotRecord
::newUnsaved( 'main', new WikitextContent( 'A' ) );
26 $auxSlot = SlotRecord
::newUnsaved( 'aux', new WikitextContent( 'B' ) );
27 $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] );
29 $this->assertSame( $mainSlot, $slots->getSlot( 'main' ) );
30 $this->assertSame( $auxSlot, $slots->getSlot( 'aux' ) );
31 $this->setExpectedException( RevisionAccessException
::class );
32 $slots->getSlot( 'nothere' );
36 * @covers \MediaWiki\Storage\RevisionSlots::hasSlot
38 public function testHasSlot() {
39 $mainSlot = SlotRecord
::newUnsaved( 'main', new WikitextContent( 'A' ) );
40 $auxSlot = SlotRecord
::newUnsaved( 'aux', new WikitextContent( 'B' ) );
41 $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] );
43 $this->assertTrue( $slots->hasSlot( 'main' ) );
44 $this->assertTrue( $slots->hasSlot( 'aux' ) );
45 $this->assertFalse( $slots->hasSlot( 'AUX' ) );
46 $this->assertFalse( $slots->hasSlot( 'xyz' ) );
50 * @covers \MediaWiki\Storage\RevisionSlots::getContent
52 public function testGetContent() {
53 $mainContent = new WikitextContent( 'A' );
54 $auxContent = new WikitextContent( 'B' );
55 $mainSlot = SlotRecord
::newUnsaved( 'main', $mainContent );
56 $auxSlot = SlotRecord
::newUnsaved( 'aux', $auxContent );
57 $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] );
59 $this->assertSame( $mainContent, $slots->getContent( 'main' ) );
60 $this->assertSame( $auxContent, $slots->getContent( 'aux' ) );
61 $this->setExpectedException( RevisionAccessException
::class );
62 $slots->getContent( 'nothere' );
66 * @covers \MediaWiki\Storage\RevisionSlots::getSlotRoles
68 public function testGetSlotRoles_someSlots() {
69 $mainSlot = SlotRecord
::newUnsaved( 'main', new WikitextContent( 'A' ) );
70 $auxSlot = SlotRecord
::newUnsaved( 'aux', new WikitextContent( 'B' ) );
71 $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] );
73 $this->assertSame( [ 'main', 'aux' ], $slots->getSlotRoles() );
77 * @covers \MediaWiki\Storage\RevisionSlots::getSlotRoles
79 public function testGetSlotRoles_noSlots() {
80 $slots = $this->newRevisionSlots( [] );
82 $this->assertSame( [], $slots->getSlotRoles() );
86 * @covers \MediaWiki\Storage\RevisionSlots::getSlots
88 public function testGetSlots() {
89 $mainSlot = SlotRecord
::newUnsaved( 'main', new WikitextContent( 'A' ) );
90 $auxSlot = SlotRecord
::newUnsaved( 'aux', new WikitextContent( 'B' ) );
91 $slotsArray = [ $mainSlot, $auxSlot ];
92 $slots = $this->newRevisionSlots( $slotsArray );
94 $this->assertEquals( [ 'main' => $mainSlot, 'aux' => $auxSlot ], $slots->getSlots() );
97 public function provideComputeSize() {
99 yield
[ 2, [ 'AA' ] ];
100 yield
[ 4, [ 'AA', 'X', 'H' ] ];
104 * @dataProvider provideComputeSize
105 * @covers \MediaWiki\Storage\RevisionSlots::computeSize
107 public function testComputeSize( $expected, $contentStrings ) {
109 foreach ( $contentStrings as $key => $contentString ) {
110 $slotsArray[] = SlotRecord
::newUnsaved( strval( $key ), new WikitextContent( $contentString ) );
112 $slots = $this->newRevisionSlots( $slotsArray );
114 $this->assertSame( $expected, $slots->computeSize() );
117 public function provideComputeSha1() {
118 yield
[ 'ctqm7794fr2dp1taki8a88ovwnvmnmj', [ 'A' ] ];
119 yield
[ 'eyq8wiwlcofnaiy4eid97gyfy60uw51', [ 'AA' ] ];
120 yield
[ 'lavctqfpxartyjr31f853drgfl4kj1g', [ 'AA', 'X', 'H' ] ];
124 * @dataProvider provideComputeSha1
125 * @covers \MediaWiki\Storage\RevisionSlots::computeSha1
126 * @note this test is a bit brittle as the hashes are hardcoded, perhaps just check that strings
127 * are returned and different Slots objects return different strings?
129 public function testComputeSha1( $expected, $contentStrings ) {
131 foreach ( $contentStrings as $key => $contentString ) {
132 $slotsArray[] = SlotRecord
::newUnsaved( strval( $key ), new WikitextContent( $contentString ) );
134 $slots = $this->newRevisionSlots( $slotsArray );
136 $this->assertSame( $expected, $slots->computeSha1() );