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 * @covers RevisionSlots::getSlot
16 public function testGetSlot() {
17 $mainSlot = SlotRecord
::newUnsaved( 'main', new WikitextContent( 'A' ) );
18 $auxSlot = SlotRecord
::newUnsaved( 'aux', new WikitextContent( 'B' ) );
19 $slots = new RevisionSlots( [ $mainSlot, $auxSlot ] );
21 $this->assertSame( $mainSlot, $slots->getSlot( 'main' ) );
22 $this->assertSame( $auxSlot, $slots->getSlot( 'aux' ) );
23 $this->setExpectedException( RevisionAccessException
::class );
24 $slots->getSlot( 'nothere' );
28 * @covers RevisionSlots::getContent
30 public function testGetContent() {
31 $mainContent = new WikitextContent( 'A' );
32 $auxContent = new WikitextContent( 'B' );
33 $mainSlot = SlotRecord
::newUnsaved( 'main', $mainContent );
34 $auxSlot = SlotRecord
::newUnsaved( 'aux', $auxContent );
35 $slots = new RevisionSlots( [ $mainSlot, $auxSlot ] );
37 $this->assertSame( $mainContent, $slots->getContent( 'main' ) );
38 $this->assertSame( $auxContent, $slots->getContent( 'aux' ) );
39 $this->setExpectedException( RevisionAccessException
::class );
40 $slots->getContent( 'nothere' );
44 * @covers RevisionSlots::getSlotRoles
46 public function testGetSlotRoles_someSlots() {
47 $mainSlot = SlotRecord
::newUnsaved( 'main', new WikitextContent( 'A' ) );
48 $auxSlot = SlotRecord
::newUnsaved( 'aux', new WikitextContent( 'B' ) );
49 $slots = new RevisionSlots( [ $mainSlot, $auxSlot ] );
51 $this->assertSame( [ 'main', 'aux' ], $slots->getSlotRoles() );
55 * @covers RevisionSlots::getSlotRoles
57 public function testGetSlotRoles_noSlots() {
58 $slots = new RevisionSlots( [] );
60 $this->assertSame( [], $slots->getSlotRoles() );
64 * @covers RevisionSlots::getSlots
66 public function testGetSlots() {
67 $mainSlot = SlotRecord
::newUnsaved( 'main', new WikitextContent( 'A' ) );
68 $auxSlot = SlotRecord
::newUnsaved( 'aux', new WikitextContent( 'B' ) );
69 $slotsArray = [ $mainSlot, $auxSlot ];
70 $slots = new RevisionSlots( $slotsArray );
72 $this->assertEquals( [ 'main' => $mainSlot, 'aux' => $auxSlot ], $slots->getSlots() );
75 public function provideComputeSize() {
77 yield
[ 2, [ 'AA' ] ];
78 yield
[ 4, [ 'AA', 'X', 'H' ] ];
82 * @dataProvider provideComputeSize
83 * @covers RevisionSlots::computeSize
85 public function testComputeSize( $expected, $contentStrings ) {
87 foreach ( $contentStrings as $key => $contentString ) {
88 $slotsArray[] = SlotRecord
::newUnsaved( strval( $key ), new WikitextContent( $contentString ) );
90 $slots = new RevisionSlots( $slotsArray );
92 $this->assertSame( $expected, $slots->computeSize() );
95 public function provideComputeSha1() {
96 yield
[ 'ctqm7794fr2dp1taki8a88ovwnvmnmj', [ 'A' ] ];
97 yield
[ 'eyq8wiwlcofnaiy4eid97gyfy60uw51', [ 'AA' ] ];
98 yield
[ 'lavctqfpxartyjr31f853drgfl4kj1g', [ 'AA', 'X', 'H' ] ];
102 * @dataProvider provideComputeSha1
103 * @covers RevisionSlots::computeSha1
104 * @note this test is a bit brittle as the hashes are hardcoded, perhaps just check that strings
105 * are returned and different Slots objects return different strings?
107 public function testComputeSha1( $expected, $contentStrings ) {
109 foreach ( $contentStrings as $key => $contentString ) {
110 $slotsArray[] = SlotRecord
::newUnsaved( strval( $key ), new WikitextContent( $contentString ) );
112 $slots = new RevisionSlots( $slotsArray );
114 $this->assertSame( $expected, $slots->computeSha1() );