| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
} |