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 21use MediaWiki\EditPage\Constraint\EditConstraintRunner; 22use MediaWiki\EditPage\Constraint\IEditConstraint; 23use MediaWiki\EditPage\Constraint\PageSizeConstraint; 24use Wikimedia\Assert\PreconditionException; 25use Wikimedia\TestingAccessWrapper; 26 27/** 28 * Tests the EditConstraintRunner 29 * 30 * @author DannyS712 31 * 32 * @covers \MediaWiki\EditPage\Constraint\EditConstraintRunner 33 */ 34class EditConstraintRunnerTest extends MediaWikiUnitTestCase { 35 36 private function getConstraint( $result ) { 37 $constraint = $this->getMockBuilder( IEditConstraint::class ) 38 ->setMethods( [ '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} 102