r92206 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92205‎ | r92206 | r92207 >
Date:22:23, 14 July 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
added test, added support for open bounds to item count criterion and swapped false and null usage for the length and item count criteria
Modified paths:
  • /trunk/extensions/Validator/includes/criteria/CriterionHasLength.php (modified) (history)
  • /trunk/extensions/Validator/includes/criteria/CriterionItemCount.php (modified) (history)
  • /trunk/extensions/Validator/test/ValidatorCriteriaTests.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Validator/test/ValidatorCriteriaTests.php
@@ -18,13 +18,13 @@
1919 $tests = array(
2020 array( true, 0, 5, 'foo' ),
2121 array( false, 0, 5, 'foobar' ),
22 - array( false, 3, null, 'a' ),
23 - array( true, 3, null, 'aw<dfxdfwdxgtdfgdfhfdgsfdxgtffds' ),
24 - array( true, null, null, 'aw<dfxdfwdxgtdfgdfhfdgsfdxgtffds' ),
25 - array( true, null, null, '' ),
 22+ array( false, 3, false, 'a' ),
 23+ array( true, 3, false, 'aw<dfxdfwdxgtdfgdfhfdgsfdxgtffds' ),
 24+ array( true, false, false, 'aw<dfxdfwdxgtdfgdfhfdgsfdxgtffds' ),
 25+ array( true, false, false, '' ),
2626 array( false, 2, 3, '' ),
27 - array( true, 3, false, 'foo' ),
28 - array( false, 3, false, 'foobar' ),
 27+ array( true, 3, null, 'foo' ),
 28+ array( false, 3, null, 'foobar' ),
2929 );
3030
3131 foreach ( $tests as $test ) {
@@ -185,4 +185,34 @@
186186 }
187187 }
188188
 189+ /**
 190+ * Tests CriterionItemCount.
 191+ */
 192+ public function testCriterionItemCount() {
 193+ $tests = array(
 194+ array( true, array( 'foo', 'bar', 'baz' ), 0, 5 ),
 195+ array( false, array( 'foo', 'bar', 'baz' ), 0, 2 ),
 196+ array( true, array( 'foo', 'bar', 'baz' ), 0, false ),
 197+ array( true, array( 'foo', 'bar', 'baz' ), false, 99 ),
 198+ array( true, array( 'foo', 'bar', 'baz' ), 3, 3 ),
 199+ array( false, array(), 1, 1 ),
 200+ array( true, array( 'foo', 'bar', 'baz' ), false, false ),
 201+ array( true, array( 'foo', 'bar', 'baz' ), 3, null ),
 202+ array( false, array( 'foo', 'bar', 'baz' ), 2, null ),
 203+ );
 204+
 205+ foreach ( $tests as $test ) {
 206+ $c = new CriterionItemCount( $test[2], $test[3] );
 207+ $p = new ListParameter( 'test' );
 208+ $p->setUserValue( 'test', '' );
 209+ $p->setValue( $test[1] );
 210+
 211+ $this->assertEquals(
 212+ $test[0],
 213+ $c->validate( $p, array() ),
 214+ 'List "'. $GLOBALS['wgLang']->listToText( $test[1] ) . '" should ' . ( $test[0] ? '' : 'not ' ) . " have between and $test[2], $test[3] items."
 215+ );
 216+ }
 217+ }
 218+
189219 }
Index: trunk/extensions/Validator/includes/criteria/CriterionHasLength.php
@@ -19,16 +19,16 @@
2020 /**
2121 * Constructor.
2222 *
23 - * @param integer $lowerBound Null for no lower bound (since 0.4.8).
24 - * @param mixed $upperBound False for same value as lower bound. Null for no upper bound (since 0.4.8).
 23+ * @param integer $lowerBound False for no lower bound (since 0.4.8).
 24+ * @param mixed $upperBound False for same value as lower bound. False for no upper bound (since 0.4.8).
2525 *
2626 * @since 0.4
2727 */
28 - public function __construct( $lowerBound, $upperBound = false ) {
 28+ public function __construct( $lowerBound, $upperBound = null ) {
2929 parent::__construct();
3030
3131 $this->lowerBound = $lowerBound;
32 - $this->upperBound = $upperBound === false ? $lowerBound : $upperBound;
 32+ $this->upperBound = is_null( $upperBound ) ? $lowerBound : $upperBound;
3333 }
3434
3535 /**
@@ -36,8 +36,8 @@
3737 */
3838 protected function doValidation( $value, Parameter $parameter, array $parameters ) {
3939 $strlen = strlen( $value );
40 - return ( is_null( $this->upperBound ) || $strlen <= $this->upperBound )
41 - && ( is_null( $this->lowerBound ) || $strlen >= $this->lowerBound );
 40+ return ( $this->upperBound === false || $strlen <= $this->upperBound )
 41+ && ( $this->lowerBound === false || $strlen >= $this->lowerBound );
4242 }
4343
4444 /**
Index: trunk/extensions/Validator/includes/criteria/CriterionItemCount.php
@@ -19,16 +19,16 @@
2020 /**
2121 * Constructor.
2222 *
23 - * @param integer $lowerBound
24 - * @param mixed $upperBound
 23+ * @param integer $lowerBound False for no lower bound (since 0.4.8).
 24+ * @param mixed $upperBound False for no lower bound (since 0.4.8).
2525 *
2626 * @since 0.4
2727 */
28 - public function __construct( $lowerBound, $upperBound = false ) {
 28+ public function __construct( $lowerBound, $upperBound = null ) {
2929 parent::__construct();
3030
3131 $this->lowerBound = $lowerBound;
32 - $this->upperBound = $upperBound === false ? $lowerBound : $upperBound;
 32+ $this->upperBound = is_null( $upperBound ) ? $lowerBound : $upperBound;
3333 }
3434
3535 /**
@@ -36,7 +36,8 @@
3737 */
3838 public function validate( Parameter $parameter, array $parameters) {
3939 $count = count( $parameter->getValue() );
40 - return $count <= $this->upperBound && $count >= $this->lowerBound;
 40+ return ( $this->upperBound === false || $count <= $this->upperBound )
 41+ && ( $this->lowerBound === false || $count >= $this->lowerBound );
4142 }
4243
4344 }
\ No newline at end of file