Debian Codegraph @

tests/phpunit/unit/includes/parser/Parsoid/Config/PageContentTest.php

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