Merge "Revert "Log the reason why revision->getContent() returns null""
[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 * @param SlotRecord[] $slots
15 * @return RevisionSlots
16 */
17 protected function newRevisionSlots( $slots = [] ) {
18 return new RevisionSlots( $slots );
19 }
20
21 /**
22 * @covers \MediaWiki\Storage\RevisionSlots::getSlot
23 */
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 ] );
28
29 $this->assertSame( $mainSlot, $slots->getSlot( 'main' ) );
30 $this->assertSame( $auxSlot, $slots->getSlot( 'aux' ) );
31 $this->setExpectedException( RevisionAccessException::class );
32 $slots->getSlot( 'nothere' );
33 }
34
35 /**
36 * @covers \MediaWiki\Storage\RevisionSlots::hasSlot
37 */
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 ] );
42
43 $this->assertTrue( $slots->hasSlot( 'main' ) );
44 $this->assertTrue( $slots->hasSlot( 'aux' ) );
45 $this->assertFalse( $slots->hasSlot( 'AUX' ) );
46 $this->assertFalse( $slots->hasSlot( 'xyz' ) );
47 }
48
49 /**
50 * @covers \MediaWiki\Storage\RevisionSlots::getContent
51 */
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 ] );
58
59 $this->assertSame( $mainContent, $slots->getContent( 'main' ) );
60 $this->assertSame( $auxContent, $slots->getContent( 'aux' ) );
61 $this->setExpectedException( RevisionAccessException::class );
62 $slots->getContent( 'nothere' );
63 }
64
65 /**
66 * @covers \MediaWiki\Storage\RevisionSlots::getSlotRoles
67 */
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 ] );
72
73 $this->assertSame( [ 'main', 'aux' ], $slots->getSlotRoles() );
74 }
75
76 /**
77 * @covers \MediaWiki\Storage\RevisionSlots::getSlotRoles
78 */
79 public function testGetSlotRoles_noSlots() {
80 $slots = $this->newRevisionSlots( [] );
81
82 $this->assertSame( [], $slots->getSlotRoles() );
83 }
84
85 /**
86 * @covers \MediaWiki\Storage\RevisionSlots::getSlots
87 */
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 );
93
94 $this->assertEquals( [ 'main' => $mainSlot, 'aux' => $auxSlot ], $slots->getSlots() );
95 }
96
97 public function provideComputeSize() {
98 yield [ 1, [ 'A' ] ];
99 yield [ 2, [ 'AA' ] ];
100 yield [ 4, [ 'AA', 'X', 'H' ] ];
101 }
102
103 /**
104 * @dataProvider provideComputeSize
105 * @covers \MediaWiki\Storage\RevisionSlots::computeSize
106 */
107 public function testComputeSize( $expected, $contentStrings ) {
108 $slotsArray = [];
109 foreach ( $contentStrings as $key => $contentString ) {
110 $slotsArray[] = SlotRecord::newUnsaved( strval( $key ), new WikitextContent( $contentString ) );
111 }
112 $slots = $this->newRevisionSlots( $slotsArray );
113
114 $this->assertSame( $expected, $slots->computeSize() );
115 }
116
117 public function provideComputeSha1() {
118 yield [ 'ctqm7794fr2dp1taki8a88ovwnvmnmj', [ 'A' ] ];
119 yield [ 'eyq8wiwlcofnaiy4eid97gyfy60uw51', [ 'AA' ] ];
120 yield [ 'lavctqfpxartyjr31f853drgfl4kj1g', [ 'AA', 'X', 'H' ] ];
121 }
122
123 /**
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?
128 */
129 public function testComputeSha1( $expected, $contentStrings ) {
130 $slotsArray = [];
131 foreach ( $contentStrings as $key => $contentString ) {
132 $slotsArray[] = SlotRecord::newUnsaved( strval( $key ), new WikitextContent( $contentString ) );
133 }
134 $slots = $this->newRevisionSlots( $slotsArray );
135
136 $this->assertSame( $expected, $slots->computeSha1() );
137 }
138
139 }