| 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\EditConstraintRunner; |
| 22 |
use MediaWiki\EditPage\Constraint\IEditConstraint; |
| 23 |
use MediaWiki\EditPage\Constraint\PageSizeConstraint; |
| 24 |
use Wikimedia\Assert\PreconditionException; |
| 25 |
use Wikimedia\TestingAccessWrapper; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class EditConstraintRunnerTest extends MediaWikiUnitTestCase { |
| 35 |
|
| 36 |
private function getConstraint( $result ) { |
| 37 |
$constraint = $this->getMockBuilder( IEditConstraint::class ) |
| 38 |
->onlyMethods( [ 'checkConstraint' ] ) |
| 39 |
->getMockForAbstractClass(); |
| 40 |
$constraint->expects( $this->once() ) |
| 41 |
->method( 'checkConstraint' ) |
| 42 |
->willReturn( $result ); |
| 43 |
return $constraint; |
| 44 |
} |
| 45 |
|
| 46 |
public function testCheckConstraint_pass() { |
| 47 |
$runner = new EditConstraintRunner(); |
| 48 |
$constraint = $this->getConstraint( IEditConstraint::CONSTRAINT_PASSED ); |
| 49 |
|
| 50 |
$runner->addConstraint( $constraint ); |
| 51 |
$this->assertTrue( $runner->checkConstraints() ); |
| 52 |
} |
| 53 |
|
| 54 |
public function testCheckConstraint_fail() { |
| 55 |
$runner = new EditConstraintRunner(); |
| 56 |
$constraint = $this->getConstraint( IEditConstraint::CONSTRAINT_FAILED ); |
| 57 |
|
| 58 |
$runner->addConstraint( $constraint ); |
| 59 |
$this->assertFalse( $runner->checkConstraints() ); |
| 60 |
$this->assertSame( |
| 61 |
$constraint, |
| 62 |
$runner->getFailedConstraint() |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
public function testCheckConstraint_multi() { |
| 67 |
$runner = new EditConstraintRunner(); |
| 68 |
$constraintPass = $this->getConstraint( IEditConstraint::CONSTRAINT_PASSED ); |
| 69 |
$constraintFail = $this->getConstraint( IEditConstraint::CONSTRAINT_FAILED ); |
| 70 |
|
| 71 |
$runner->addConstraint( $constraintPass ); |
| 72 |
$runner->addConstraint( $constraintFail ); |
| 73 |
$this->assertFalse( $runner->checkConstraints() ); |
| 74 |
$this->assertSame( |
| 75 |
$constraintFail, |
| 76 |
$runner->getFailedConstraint() |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
public function testGetFailedConstraint_exception() { |
| 81 |
$this->expectException( PreconditionException::class ); |
| 82 |
$this->expectExceptionMessage( 'getFailedConstraint called with no failed constraint' ); |
| 83 |
|
| 84 |
$runner = new EditConstraintRunner(); |
| 85 |
$runner->getFailedConstraint(); |
| 86 |
} |
| 87 |
|
| 88 |
public function testGetConstraintName() { |
| 89 |
$runner = TestingAccessWrapper::newFromObject( new EditConstraintRunner() ); |
| 90 |
$pageSizeConstraint = new PageSizeConstraint( |
| 91 |
1, |
| 92 |
512, |
| 93 |
PageSizeConstraint::BEFORE_MERGE |
| 94 |
); |
| 95 |
$this->assertSame( |
| 96 |
'PageSizeConstraint ' . PageSizeConstraint::BEFORE_MERGE, |
| 97 |
$runner->getConstraintName( $pageSizeConstraint ) |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
} |