Debian Codegraph @

tests/phpunit/unit/includes/editpage/Constraint/ContentModelChangeConstraintTest.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 */
20
21 use MediaWiki\EditPage\Constraint\ContentModelChangeConstraint;
22 use MediaWiki\EditPage\Constraint\IEditConstraint;
23 use MediaWiki\Page\PageIdentity;
24 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
25 use MediaWiki\Title\Title;
26
27 /**
28 * Tests the ContentModelChangeConstraint
29 *
30 * @author DannyS712
31 *
32 * @covers \MediaWiki\EditPage\Constraint\ContentModelChangeConstraint
33 */
34 class ContentModelChangeConstraintTest extends MediaWikiUnitTestCase {
35 use EditConstraintTestTrait;
36 use MockAuthorityTrait;
37 use MockTitleTrait;
38
39 public function testPass() {
40 $newContentModel = 'FooBarBaz';
41
42 $title = $this->getMockBuilder( Title::class )
43 ->disableOriginalConstructor()
44 ->onlyMethods( [ 'getContentModel', 'setContentModel' ] )
45 ->getMock();
46 $title->expects( $this->once() )
47 ->method( 'getContentModel' )
48 ->willReturn( 'differentStartingContentModel' );
49 $title->expects( $this->once() )
50 ->method( 'setContentModel' )
51 ->with( $newContentModel );
52
53 $performer = $this->mockRegisteredAuthorityWithPermissions( [ 'edit', 'editcontentmodel' ] );
54 $constraint = new ContentModelChangeConstraint(
55 $performer,
56 $title,
57 $newContentModel
58 );
59 $this->assertConstraintPassed( $constraint );
60 }
61
62 public function testNoChange() {
63 $unchangingContentModel = 'FooBarBaz';
64 $title = $this->makeMockTitle( __METHOD__, [
65 'contentModel' => $unchangingContentModel,
66 ] );
67 $constraint = new ContentModelChangeConstraint(
68 $this->mockRegisteredUltimateAuthority(),
69 $title,
70 $unchangingContentModel
71 );
72 $this->assertConstraintPassed( $constraint );
73 }
74
75 public function testFailure() {
76 $newContentModel = 'FooBarBaz';
77
78 $title = $this->getMockBuilder( Title::class )
79 ->disableOriginalConstructor()
80 ->onlyMethods( [ 'getContentModel', 'setContentModel' ] )
81 ->getMock();
82 $title->expects( $this->once() )
83 ->method( 'getContentModel' )
84 ->willReturn( 'differentStartingContentModel' );
85 $title->expects( $this->once() )
86 ->method( 'setContentModel' )
87 ->with( $newContentModel );
88
89 $performer = $this->mockRegisteredAuthority( function (
90 string $permission,
91 ?PageIdentity $page = null
92 ) use ( $title ) {
93 if ( $permission === 'editcontentmodel' ) {
94 if ( $page ) {
95 $this->assertEquals( $title, $page );
96 }
97 return true;
98 }
99 if ( $permission === 'edit' ) {
100 $this->assertEquals( $title, $page );
101 return false;
102 }
103 $this->fail( "Unexpected permission check $permission" );
104 return false;
105 } );
106
107 $constraint = new ContentModelChangeConstraint(
108 $performer,
109 $title,
110 $newContentModel
111 );
112 $this->assertConstraintFailed(
113 $constraint,
114 IEditConstraint::AS_NO_CHANGE_CONTENT_MODEL
115 );
116 }
117
118 public function testFailure_quick() {
119 $title = $this->makeMockTitle( __METHOD__, [
120 'contentModel' => 'differentStartingContentModel',
121 ] );
122
123 $constraint = new ContentModelChangeConstraint(
124 $this->mockRegisteredAuthorityWithoutPermissions( [ 'editcontentmodel' ] ),
125 $title,
126 'FooBarBaz'
127 );
128 $this->assertConstraintFailed(
129 $constraint,
130 IEditConstraint::AS_NO_CHANGE_CONTENT_MODEL
131 );
132 }
133 }