| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Unit\Revision; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Content\Content; |
| 7 |
use MediaWiki\Content\WikitextContent; |
| 8 |
use MediaWiki\Revision\MutableRevisionSlots; |
| 9 |
use MediaWiki\Revision\RevisionAccessException; |
| 10 |
use MediaWiki\Revision\RevisionSlots; |
| 11 |
use MediaWiki\Revision\SlotRecord; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class MutableRevisionSlotsTest extends RevisionSlotsTest { |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
protected function newRevisionSlots( $slots = [] ) { |
| 24 |
return new MutableRevisionSlots( $slots ); |
| 25 |
} |
| 26 |
|
| 27 |
public static function provideConstructorFailue() { |
| 28 |
yield 'array or the wrong thing' => [ |
| 29 |
[ 1, 2, 3 ] |
| 30 |
]; |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public function testConstructorFailue( $slots ) { |
| 38 |
$this->expectException( InvalidArgumentException::class ); |
| 39 |
|
| 40 |
new MutableRevisionSlots( $slots ); |
| 41 |
} |
| 42 |
|
| 43 |
public function testSetMultipleSlots() { |
| 44 |
$slots = new MutableRevisionSlots(); |
| 45 |
|
| 46 |
$this->assertSame( [], $slots->getSlots() ); |
| 47 |
|
| 48 |
$slotA = SlotRecord::newUnsaved( 'some', new WikitextContent( 'A' ) ); |
| 49 |
$slots->setSlot( $slotA ); |
| 50 |
$this->assertTrue( $slots->hasSlot( 'some' ) ); |
| 51 |
$this->assertSame( $slotA, $slots->getSlot( 'some' ) ); |
| 52 |
$this->assertSame( [ 'some' => $slotA ], $slots->getSlots() ); |
| 53 |
|
| 54 |
$slotB = SlotRecord::newUnsaved( 'other', new WikitextContent( 'B' ) ); |
| 55 |
$slots->setSlot( $slotB ); |
| 56 |
$this->assertTrue( $slots->hasSlot( 'other' ) ); |
| 57 |
$this->assertSame( $slotB, $slots->getSlot( 'other' ) ); |
| 58 |
$this->assertSame( [ 'some' => $slotA, 'other' => $slotB ], $slots->getSlots() ); |
| 59 |
} |
| 60 |
|
| 61 |
public function testSetExistingSlotOverwritesSlot() { |
| 62 |
$slots = new MutableRevisionSlots(); |
| 63 |
|
| 64 |
$this->assertSame( [], $slots->getSlots() ); |
| 65 |
|
| 66 |
$slotA = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) ); |
| 67 |
$slots->setSlot( $slotA ); |
| 68 |
$this->assertSame( $slotA, $slots->getSlot( SlotRecord::MAIN ) ); |
| 69 |
$this->assertSame( [ SlotRecord::MAIN => $slotA ], $slots->getSlots() ); |
| 70 |
|
| 71 |
$slotB = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'B' ) ); |
| 72 |
$slots->setSlot( $slotB ); |
| 73 |
$this->assertSame( $slotB, $slots->getSlot( SlotRecord::MAIN ) ); |
| 74 |
$this->assertSame( [ SlotRecord::MAIN => $slotB ], $slots->getSlots() ); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
private function newSavedSlot( $role, Content $content ) { |
| 83 |
return SlotRecord::newSaved( 7, 7, 'xyz', SlotRecord::newUnsaved( $role, $content ) ); |
| 84 |
} |
| 85 |
|
| 86 |
public function testInheritSlotOverwritesSlot() { |
| 87 |
$slots = new MutableRevisionSlots(); |
| 88 |
$slotA = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) ); |
| 89 |
$slots->setSlot( $slotA ); |
| 90 |
$slotB = $this->newSavedSlot( SlotRecord::MAIN, new WikitextContent( 'B' ) ); |
| 91 |
$slotC = $this->newSavedSlot( 'foo', new WikitextContent( 'C' ) ); |
| 92 |
$slots->inheritSlot( $slotB ); |
| 93 |
$slots->inheritSlot( $slotC ); |
| 94 |
$this->assertSame( [ SlotRecord::MAIN, 'foo' ], $slots->getSlotRoles() ); |
| 95 |
$this->assertNotSame( $slotB, $slots->getSlot( SlotRecord::MAIN ) ); |
| 96 |
$this->assertNotSame( $slotC, $slots->getSlot( 'foo' ) ); |
| 97 |
$this->assertTrue( $slots->getSlot( SlotRecord::MAIN )->isInherited() ); |
| 98 |
$this->assertTrue( $slots->getSlot( 'foo' )->isInherited() ); |
| 99 |
$this->assertSame( $slotB->getContent(), $slots->getSlot( SlotRecord::MAIN )->getContent() ); |
| 100 |
$this->assertSame( $slotC->getContent(), $slots->getSlot( 'foo' )->getContent() ); |
| 101 |
} |
| 102 |
|
| 103 |
public function testSetContentOfExistingSlotOverwritesContent() { |
| 104 |
$slots = new MutableRevisionSlots(); |
| 105 |
|
| 106 |
$this->assertSame( [], $slots->getSlots() ); |
| 107 |
|
| 108 |
$slotA = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) ); |
| 109 |
$slots->setSlot( $slotA ); |
| 110 |
$this->assertSame( $slotA, $slots->getSlot( SlotRecord::MAIN ) ); |
| 111 |
$this->assertSame( [ SlotRecord::MAIN => $slotA ], $slots->getSlots() ); |
| 112 |
|
| 113 |
$newContent = new WikitextContent( 'B' ); |
| 114 |
$slots->setContent( SlotRecord::MAIN, $newContent ); |
| 115 |
$this->assertSame( $newContent, $slots->getContent( SlotRecord::MAIN ) ); |
| 116 |
} |
| 117 |
|
| 118 |
public function testRemoveExistingSlot() { |
| 119 |
$slotA = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) ); |
| 120 |
$slots = new MutableRevisionSlots( [ $slotA ] ); |
| 121 |
|
| 122 |
$this->assertSame( [ SlotRecord::MAIN => $slotA ], $slots->getSlots() ); |
| 123 |
|
| 124 |
$slots->removeSlot( SlotRecord::MAIN ); |
| 125 |
$this->assertSame( [], $slots->getSlots() ); |
| 126 |
$this->expectException( RevisionAccessException::class ); |
| 127 |
$slots->getSlot( SlotRecord::MAIN ); |
| 128 |
} |
| 129 |
|
| 130 |
public function testNewFromParentRevisionSlots() { |
| 131 |
|
| 132 |
$parentSlots = [ |
| 133 |
'some' => $this->newSavedSlot( 'some', new WikitextContent( 'X' ) ), |
| 134 |
'other' => $this->newSavedSlot( 'other', new WikitextContent( 'Y' ) ), |
| 135 |
]; |
| 136 |
$slots = MutableRevisionSlots::newFromParentRevisionSlots( $parentSlots ); |
| 137 |
$this->assertSame( [ 'some', 'other' ], $slots->getSlotRoles() ); |
| 138 |
$this->assertNotSame( $parentSlots['some'], $slots->getSlot( 'some' ) ); |
| 139 |
$this->assertNotSame( $parentSlots['other'], $slots->getSlot( 'other' ) ); |
| 140 |
$this->assertTrue( $slots->getSlot( 'some' )->isInherited() ); |
| 141 |
$this->assertTrue( $slots->getSlot( 'other' )->isInherited() ); |
| 142 |
$this->assertSame( $parentSlots['some']->getContent(), $slots->getContent( 'some' ) ); |
| 143 |
$this->assertSame( $parentSlots['other']->getContent(), $slots->getContent( 'other' ) ); |
| 144 |
} |
| 145 |
|
| 146 |
public function testResetCallback() { |
| 147 |
$counter = 0; |
| 148 |
$callback = static function () use ( &$counter ) { |
| 149 |
$counter++; |
| 150 |
}; |
| 151 |
|
| 152 |
$slots = new MutableRevisionSlots( [], $callback ); |
| 153 |
|
| 154 |
$slot = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) ); |
| 155 |
$slots->setSlot( $slot ); |
| 156 |
$this->assertSame( 1, $counter, 'setSlot triggers callback' ); |
| 157 |
|
| 158 |
$slots->setContent( 'other', new WikitextContent( 'Y' ) ); |
| 159 |
$this->assertSame( 2, $counter, 'setContent triggers callback' ); |
| 160 |
|
| 161 |
$slot = $this->newSavedSlot( 'some', new WikitextContent( 'X' ) ); |
| 162 |
$slots->inheritSlot( $slot ); |
| 163 |
$this->assertSame( 3, $counter, 'inheritSlot triggers callback' ); |
| 164 |
|
| 165 |
$slots->removeSlot( SlotRecord::MAIN ); |
| 166 |
$this->assertSame( 4, $counter, 'inheritSlot triggers callback' ); |
| 167 |
} |
| 168 |
|
| 169 |
} |