| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Tests\Unit\Parser\Parsoid\Config; |
| 23 |
|
| 24 |
use DummyContentForTesting; |
| 25 |
use InvalidArgumentException; |
| 26 |
use MediaWiki\DAO\WikiAwareEntity; |
| 27 |
use MediaWiki\Page\PageIdentityValue; |
| 28 |
use MediaWiki\Parser\Parsoid\Config\PageContent; |
| 29 |
use MediaWiki\Revision\MutableRevisionRecord; |
| 30 |
use MediaWiki\Revision\RevisionRecord; |
| 31 |
use MediaWiki\Revision\SlotRecord; |
| 32 |
use MediaWikiUnitTestCase; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class PageContentTest extends MediaWikiUnitTestCase { |
| 38 |
|
| 39 |
protected RevisionRecord $rev; |
| 40 |
|
| 41 |
protected function setUp(): void { |
| 42 |
parent::setUp(); |
| 43 |
$this->rev = $this->createMock( RevisionRecord::class ); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public function testConstruct() { |
| 50 |
$pageContent = new PageContent( $this->rev ); |
| 51 |
$this->assertInstanceOf( PageContent::class, $pageContent ); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function testGetRoles() { |
| 58 |
$record = new MutableRevisionRecord( |
| 59 |
|
| 60 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', WikiAwareEntity::LOCAL ) |
| 61 |
); |
| 62 |
$mainSlot = new SlotRecord( |
| 63 |
|
| 64 |
(object)[ |
| 65 |
'slot_id' => 1, |
| 66 |
'slot_revision_id' => null, |
| 67 |
'slot_content_id' => 1, |
| 68 |
'content_address' => null, |
| 69 |
'model_name' => 'x', |
| 70 |
'role_name' => SlotRecord::MAIN, |
| 71 |
'slot_origin' => null |
| 72 |
], |
| 73 |
|
| 74 |
new DummyContentForTesting( SlotRecord::MAIN ) |
| 75 |
); |
| 76 |
|
| 77 |
$record->setSlot( $mainSlot ); |
| 78 |
|
| 79 |
$this->rev->method( 'getSlotRoles' )->willReturn( [ SlotRecord::MAIN ] ); |
| 80 |
|
| 81 |
$roles = [ SlotRecord::MAIN ]; |
| 82 |
$pageContent = new PageContent( $this->rev ); |
| 83 |
|
| 84 |
$this->assertEquals( $roles, $pageContent->getRoles() ); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function testHasRole() { |
| 91 |
$record = new MutableRevisionRecord( |
| 92 |
|
| 93 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', WikiAwareEntity::LOCAL ) |
| 94 |
); |
| 95 |
|
| 96 |
$mainSlot = new SlotRecord( |
| 97 |
(object)[ |
| 98 |
'slot_id' => 1, |
| 99 |
'slot_revision_id' => null, |
| 100 |
'slot_content_id' => 1, |
| 101 |
'content_address' => null, |
| 102 |
'model_name' => 'x', |
| 103 |
'role_name' => SlotRecord::MAIN, |
| 104 |
'slot_origin' => null |
| 105 |
], |
| 106 |
|
| 107 |
new DummyContentForTesting( SlotRecord::MAIN ) |
| 108 |
); |
| 109 |
|
| 110 |
$record->setSlot( $mainSlot ); |
| 111 |
|
| 112 |
$this->rev->method( 'hasSlot' )->willReturn( true ); |
| 113 |
|
| 114 |
$pageContent = new PageContent( $this->rev ); |
| 115 |
|
| 116 |
$this->assertTrue( $pageContent->hasRole( SlotRecord::MAIN ) ); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public function testGetModel() { |
| 123 |
|
| 124 |
$record = new MutableRevisionRecord( |
| 125 |
new PageIdentityValue( 1, NS_MAIN, 'Foo', WikiAwareEntity::LOCAL ) |
| 126 |
); |
| 127 |
|
| 128 |
$mainSlot = new SlotRecord( |
| 129 |
(object)[ |
| 130 |
'slot_id' => 1, |
| 131 |
'slot_revision_id' => null, |
| 132 |
'slot_content_id' => 1, |
| 133 |
'content_address' => null, |
| 134 |
'model_name' => 'testing', |
| 135 |
'role_name' => SlotRecord::MAIN, |
| 136 |
'slot_origin' => null |
| 137 |
], |
| 138 |
|
| 139 |
new DummyContentForTesting( SlotRecord::MAIN ) |
| 140 |
); |
| 141 |
|
| 142 |
$record->setSlot( $mainSlot ); |
| 143 |
|
| 144 |
$this->rev->method( 'hasSlot' )->willReturn( true ); |
| 145 |
$this->rev->method( 'getContent' )->willReturn( $mainSlot->getContent() ); |
| 146 |
|
| 147 |
$pageContent = new PageContent( $this->rev ); |
| 148 |
|
| 149 |
$model = $pageContent->getModel( SlotRecord::MAIN ); |
| 150 |
|
| 151 |
$this->assertEquals( 'testing', $model ); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public function testGetModelThrowsExceptionForMissingRole() { |
| 158 |
|
| 159 |
$nonExistentRole = 'nonexistent'; |
| 160 |
|
| 161 |
$this->rev->method( 'hasSlot' )->with( $nonExistentRole )->willReturn( false ); |
| 162 |
|
| 163 |
$pageContent = new PageContent( $this->rev ); |
| 164 |
|
| 165 |
$this->expectException( InvalidArgumentException::class ); |
| 166 |
$this->expectExceptionMessage( "PageContent does not have role '$nonExistentRole'" ); |
| 167 |
|
| 168 |
|
| 169 |
$pageContent->getModel( $nonExistentRole ); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public function testGetFormat() { |
| 176 |
|
| 177 |
$pageContent = $this->createMock( PageContent::class ); |
| 178 |
|
| 179 |
$pageContent->method( 'getFormat' )->willReturn( CONTENT_FORMAT_WIKITEXT ); |
| 180 |
|
| 181 |
|
| 182 |
$this->assertEquals( CONTENT_FORMAT_WIKITEXT, $pageContent->getFormat( SlotRecord::MAIN ) ); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public function testGetContent() { |
| 189 |
|
| 190 |
$content = new DummyContentForTesting( SlotRecord::MAIN ); |
| 191 |
|
| 192 |
$pageContent = $this->createMock( PageContent::class ); |
| 193 |
|
| 194 |
$pageContent->method( 'getContent' )->willReturn( $content->serialize() ); |
| 195 |
|
| 196 |
|
| 197 |
$this->assertEquals( $content->serialize(), $pageContent->getContent( SlotRecord::MAIN ) ); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public function testGetContentThrowsExceptionForMissingRole() { |
| 204 |
$nonExistentRole = 'nonexistent'; |
| 205 |
|
| 206 |
|
| 207 |
$this->rev->method( 'hasSlot' )->with( $nonExistentRole )->willReturn( false ); |
| 208 |
|
| 209 |
|
| 210 |
$pageContent = new PageContent( $this->rev ); |
| 211 |
|
| 212 |
|
| 213 |
$this->expectException( InvalidArgumentException::class ); |
| 214 |
$this->expectExceptionMessage( "PageContent does not have role '$nonExistentRole'" ); |
| 215 |
|
| 216 |
$pageContent->getContent( $nonExistentRole ); |
| 217 |
} |
| 218 |
} |