1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @since 1.42
*/
namespace MediaWiki\Tests\Unit\Parser\Parsoid\Config;
use DummyContentForTesting;
use InvalidArgumentException;
use MediaWiki\DAO\WikiAwareEntity;
use MediaWiki\Page\PageIdentityValue;
use MediaWiki\Parser\Parsoid\Config\PageContent;
use MediaWiki\Revision\MutableRevisionRecord;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\SlotRecord;
use MediaWikiUnitTestCase;
/**
* @covers \MediaWiki\Parser\Parsoid\Config\PageContent
*/
class PageContentTest extends MediaWikiUnitTestCase {
protected RevisionRecord $rev;
protected function setUp(): void {
parent::setUp();
$this->rev = $this->createMock( RevisionRecord::class );
}
/**
* Tests the constructor of the PageContent class.
*/
public function testConstruct() {
$pageContent = new PageContent( $this->rev );
$this->assertInstanceOf( PageContent::class, $pageContent );
}
/**
* Tests retrieving the roles assigned to the page content.
*/
public function testGetRoles() {
$record = new MutableRevisionRecord(
// Create a new mock revision record for a page.
new PageIdentityValue( 1, NS_MAIN, 'Foo', WikiAwareEntity::LOCAL )
);
$mainSlot = new SlotRecord(
// Creating a dummy slot record.
(object)[
'slot_id' => 1,
'slot_revision_id' => null,
'slot_content_id' => 1,
'content_address' => null,
'model_name' => 'x',
'role_name' => SlotRecord::MAIN,
'slot_origin' => null
],
// Assigning dummy content for testing.
new DummyContentForTesting( SlotRecord::MAIN )
);
// Setting the slot in the mock revision record.
$record->setSlot( $mainSlot );
// Mocking the 'getSlotRoles' method to return the MAIN slot role.
$this->rev->method( 'getSlotRoles' )->willReturn( [ SlotRecord::MAIN ] );
// Creating a new PageContent object with the mock revision record.
$roles = [ SlotRecord::MAIN ];
$pageContent = new PageContent( $this->rev );
// Asserts that the correct roles are returned.
$this->assertEquals( $roles, $pageContent->getRoles() );
}
/**
* Tests the hasRole method of the PageContent class.
*/
public function testHasRole() {
$record = new MutableRevisionRecord(
// Create a new mock revision record for a page.
new PageIdentityValue( 1, NS_MAIN, 'Foo', WikiAwareEntity::LOCAL )
);
// Creating a dummy slot record.
$mainSlot = new SlotRecord(
(object)[
'slot_id' => 1,
'slot_revision_id' => null,
'slot_content_id' => 1,
'content_address' => null,
'model_name' => 'x',
'role_name' => SlotRecord::MAIN,
'slot_origin' => null
],
// Assigning dummy content for testing.
new DummyContentForTesting( SlotRecord::MAIN )
);
// Setting the slot in the mock revision record.
$record->setSlot( $mainSlot );
// Mocking the 'hasSlot' method to return true.
$this->rev->method( 'hasSlot' )->willReturn( true );
// Creating a new PageContent object with the mock revision record.
$pageContent = new PageContent( $this->rev );
// Asserts that the correct roles are returned.
$this->assertTrue( $pageContent->hasRole( SlotRecord::MAIN ) );
}
/**
* Tests the hasRole method of the PageContent class.
*/
public function testGetModel() {
// Create a new mock revision record for a page.
$record = new MutableRevisionRecord(
new PageIdentityValue( 1, NS_MAIN, 'Foo', WikiAwareEntity::LOCAL )
);
// Creating a dummy slot record.
$mainSlot = new SlotRecord(
(object)[
'slot_id' => 1,
'slot_revision_id' => null,
'slot_content_id' => 1,
'content_address' => null,
'model_name' => 'testing',
'role_name' => SlotRecord::MAIN,
'slot_origin' => null
],
// Assigning dummy content for testing.
new DummyContentForTesting( SlotRecord::MAIN )
);
// Setting the slot in the mock revision record.
$record->setSlot( $mainSlot );
// Mocking the 'hasSlot' method to return true.
$this->rev->method( 'hasSlot' )->willReturn( true );
$this->rev->method( 'getContent' )->willReturn( $mainSlot->getContent() );
// Creating a new PageContent object with the mock revision record.
$pageContent = new PageContent( $this->rev );
// Getting the model of the MAIN slot.
$model = $pageContent->getModel( SlotRecord::MAIN );
// Asserts that the correct model is returned.
$this->assertEquals( 'testing', $model );
}
/**
* Tests the getModel method of the PageContent class.
*/
public function testGetModelThrowsExceptionForMissingRole() {
// Create a new mock revision record for a page.
$nonExistentRole = 'nonexistent';
// No need to set up any slots since we're testing for a missing role
$this->rev->method( 'hasSlot' )->with( $nonExistentRole )->willReturn( false );
// Creating a new PageContent object with the mock revision record.
$pageContent = new PageContent( $this->rev );
// Expect InvalidArgumentException when trying to get the model of a non-existent role
$this->expectException( InvalidArgumentException::class );
$this->expectExceptionMessage( "PageContent does not have role '$nonExistentRole'" );
// Getting the model of the non-existent role
// Attempts to get the model of a non-existent role.
$pageContent->getModel( $nonExistentRole );
}
/**
* Tests the getFormat method of the PageContent class.
*/
public function testGetFormat() {
// Create a new mock revision record for a page.
$pageContent = $this->createMock( PageContent::class );
// Mocking the 'getFormat' method to return CONTENT_FORMAT_WIKITEXT.
$pageContent->method( 'getFormat' )->willReturn( CONTENT_FORMAT_WIKITEXT );
// Getting the format of the MAIN slot.
// Asserts that the correct format is returned.
$this->assertEquals( CONTENT_FORMAT_WIKITEXT, $pageContent->getFormat( SlotRecord::MAIN ) );
}
/**
* Tests the getFormat method of the PageContent class.
*/
public function testGetContent() {
// Create dummy content for testing.
$content = new DummyContentForTesting( SlotRecord::MAIN );
// Create a new mock page content object.
$pageContent = $this->createMock( PageContent::class );
// Mocking the 'getContent' method to return the serialized content.
$pageContent->method( 'getContent' )->willReturn( $content->serialize() );
// Getting the content of the MAIN slot.
// Asserts that the correct content is returned.
$this->assertEquals( $content->serialize(), $pageContent->getContent( SlotRecord::MAIN ) );
}
/**
* Tests the getContent method of the PageContent class.
*/
public function testGetContentThrowsExceptionForMissingRole() {
$nonExistentRole = 'nonexistent';
// No need to set up any slots since we're testing for a missing role
$this->rev->method( 'hasSlot' )->with( $nonExistentRole )->willReturn( false );
// Creating a new PageContent object with the mock revision record.
$pageContent = new PageContent( $this->rev );
// Expect InvalidArgumentException when trying to get the content of a non-existent role
$this->expectException( InvalidArgumentException::class );
$this->expectExceptionMessage( "PageContent does not have role '$nonExistentRole'" );
$pageContent->getContent( $nonExistentRole );
}
}
|