Merge "Add tags for undo edits"
[lhc/web/wiklou.git] / tests / phpunit / includes / Storage / RevisionSlotsTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Storage;
4
5 use MediaWiki\Storage\RevisionAccessException;
6 use MediaWiki\Storage\RevisionSlots;
7 use MediaWiki\Storage\SlotRecord;
8 use MediaWikiTestCase;
9 use WikitextContent;
10
11 class RevisionSlotsTest extends MediaWikiTestCase {
12
13 /**
14 * @covers \MediaWiki\Storage\RevisionSlots::getSlot
15 */
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 ] );
20
21 $this->assertSame( $mainSlot, $slots->getSlot( 'main' ) );
22 $this->assertSame( $auxSlot, $slots->getSlot( 'aux' ) );
23 $this->setExpectedException( RevisionAccessException::class );
24 $slots->getSlot( 'nothere' );
25 }
26
27 /**
28 * @covers \MediaWiki\Storage\RevisionSlots::getContent
29 */
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 ] );
36
37 $this->assertSame( $mainContent, $slots->getContent( 'main' ) );
38 $this->assertSame( $auxContent, $slots->getContent( 'aux' ) );
39 $this->setExpectedException( RevisionAccessException::class );
40 $slots->getContent( 'nothere' );
41 }
42
43 /**
44 * @covers \MediaWiki\Storage\RevisionSlots::getSlotRoles
45 */
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 ] );
50
51 $this->assertSame( [ 'main', 'aux' ], $slots->getSlotRoles() );
52 }
53
54 /**
55 * @covers \MediaWiki\Storage\RevisionSlots::getSlotRoles
56 */
57 public function testGetSlotRoles_noSlots() {
58 $slots = new RevisionSlots( [] );
59
60 $this->assertSame( [], $slots->getSlotRoles() );
61 }
62
63 /**
64 * @covers \MediaWiki\Storage\RevisionSlots::getSlots
65 */
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 );
71
72 $this->assertEquals( [ 'main' => $mainSlot, 'aux' => $auxSlot ], $slots->getSlots() );
73 }
74
75 public function provideComputeSize() {
76 yield [ 1, [ 'A' ] ];
77 yield [ 2, [ 'AA' ] ];
78 yield [ 4, [ 'AA', 'X', 'H' ] ];
79 }
80
81 /**
82 * @dataProvider provideComputeSize
83 * @covers \MediaWiki\Storage\RevisionSlots::computeSize
84 */
85 public function testComputeSize( $expected, $contentStrings ) {
86 $slotsArray = [];
87 foreach ( $contentStrings as $key => $contentString ) {
88 $slotsArray[] = SlotRecord::newUnsaved( strval( $key ), new WikitextContent( $contentString ) );
89 }
90 $slots = new RevisionSlots( $slotsArray );
91
92 $this->assertSame( $expected, $slots->computeSize() );
93 }
94
95 public function provideComputeSha1() {
96 yield [ 'ctqm7794fr2dp1taki8a88ovwnvmnmj', [ 'A' ] ];
97 yield [ 'eyq8wiwlcofnaiy4eid97gyfy60uw51', [ 'AA' ] ];
98 yield [ 'lavctqfpxartyjr31f853drgfl4kj1g', [ 'AA', 'X', 'H' ] ];
99 }
100
101 /**
102 * @dataProvider provideComputeSha1
103 * @covers \MediaWiki\Storage\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?
106 */
107 public function testComputeSha1( $expected, $contentStrings ) {
108 $slotsArray = [];
109 foreach ( $contentStrings as $key => $contentString ) {
110 $slotsArray[] = SlotRecord::newUnsaved( strval( $key ), new WikitextContent( $contentString ) );
111 }
112 $slots = new RevisionSlots( $slotsArray );
113
114 $this->assertSame( $expected, $slots->computeSha1() );
115 }
116
117 }