Fix 'Tags' padding to keep it farther from the edge and document the source of the...
[lhc/web/wiklou.git] / tests / phpunit / includes / Storage / MutableRevisionSlotsTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Storage;
4
5 use InvalidArgumentException;
6 use MediaWiki\Storage\MutableRevisionSlots;
7 use MediaWiki\Storage\RevisionAccessException;
8 use MediaWiki\Storage\RevisionSlots;
9 use MediaWiki\Storage\SlotRecord;
10 use WikitextContent;
11
12 /**
13 * @covers \MediaWiki\Storage\MutableRevisionSlots
14 */
15 class MutableRevisionSlotsTest extends RevisionSlotsTest {
16
17 /**
18 * @param SlotRecord[] $slots
19 * @return RevisionSlots
20 */
21 protected function newRevisionSlots( $slots = [] ) {
22 return new MutableRevisionSlots( $slots );
23 }
24
25 public function provideConstructorFailue() {
26 yield 'array or the wrong thing' => [
27 [ 1, 2, 3 ]
28 ];
29 }
30
31 /**
32 * @dataProvider provideConstructorFailue
33 * @param $slots
34 *
35 * @covers \MediaWiki\Storage\RevisionSlots::__construct
36 * @covers \MediaWiki\Storage\RevisionSlots::setSlotsInternal
37 */
38 public function testConstructorFailue( $slots ) {
39 $this->setExpectedException( InvalidArgumentException::class );
40
41 new MutableRevisionSlots( $slots );
42 }
43
44 public function testSetMultipleSlots() {
45 $slots = new MutableRevisionSlots();
46
47 $this->assertSame( [], $slots->getSlots() );
48
49 $slotA = SlotRecord::newUnsaved( 'some', new WikitextContent( 'A' ) );
50 $slots->setSlot( $slotA );
51 $this->assertTrue( $slots->hasSlot( 'some' ) );
52 $this->assertSame( $slotA, $slots->getSlot( 'some' ) );
53 $this->assertSame( [ 'some' => $slotA ], $slots->getSlots() );
54
55 $slotB = SlotRecord::newUnsaved( 'other', new WikitextContent( 'B' ) );
56 $slots->setSlot( $slotB );
57 $this->assertTrue( $slots->hasSlot( 'other' ) );
58 $this->assertSame( $slotB, $slots->getSlot( 'other' ) );
59 $this->assertSame( [ 'some' => $slotA, 'other' => $slotB ], $slots->getSlots() );
60 }
61
62 public function testSetExistingSlotOverwritesSlot() {
63 $slots = new MutableRevisionSlots();
64
65 $this->assertSame( [], $slots->getSlots() );
66
67 $slotA = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
68 $slots->setSlot( $slotA );
69 $this->assertSame( $slotA, $slots->getSlot( 'main' ) );
70 $this->assertSame( [ 'main' => $slotA ], $slots->getSlots() );
71
72 $slotB = SlotRecord::newUnsaved( 'main', new WikitextContent( 'B' ) );
73 $slots->setSlot( $slotB );
74 $this->assertSame( $slotB, $slots->getSlot( 'main' ) );
75 $this->assertSame( [ 'main' => $slotB ], $slots->getSlots() );
76 }
77
78 public function testSetContentOfExistingSlotOverwritesContent() {
79 $slots = new MutableRevisionSlots();
80
81 $this->assertSame( [], $slots->getSlots() );
82
83 $slotA = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
84 $slots->setSlot( $slotA );
85 $this->assertSame( $slotA, $slots->getSlot( 'main' ) );
86 $this->assertSame( [ 'main' => $slotA ], $slots->getSlots() );
87
88 $newContent = new WikitextContent( 'B' );
89 $slots->setContent( 'main', $newContent );
90 $this->assertSame( $newContent, $slots->getContent( 'main' ) );
91 }
92
93 public function testRemoveExistingSlot() {
94 $slotA = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
95 $slots = new MutableRevisionSlots( [ $slotA ] );
96
97 $this->assertSame( [ 'main' => $slotA ], $slots->getSlots() );
98
99 $slots->removeSlot( 'main' );
100 $this->assertSame( [], $slots->getSlots() );
101 $this->setExpectedException( RevisionAccessException::class );
102 $slots->getSlot( 'main' );
103 }
104
105 }