1<?php 2 3namespace Wikimedia\ParamValidator\TypeDef; 4 5use Wikimedia\Message\DataMessageValue; 6use Wikimedia\ParamValidator\ParamValidator; 7use Wikimedia\ParamValidator\SimpleCallbacks; 8use Wikimedia\ParamValidator\ValidationException; 9 10/** 11 * @covers Wikimedia\ParamValidator\TypeDef\StringDef 12 */ 13class StringDefTest extends TypeDefTestCase { 14 15 protected static $testClass = StringDef::class; 16 17 protected function getInstance( SimpleCallbacks $callbacks, array $options ) { 18 if ( static::$testClass === null ) { 19 throw new \LogicException( 'Either assign static::$testClass or override ' . __METHOD__ ); 20 } 21 22 return new static::$testClass( $callbacks, $options ); 23 } 24 25 public function provideValidate() { 26 $req = [ 27 ParamValidator::PARAM_REQUIRED => true, 28 ]; 29 $maxBytes = [ 30 StringDef::PARAM_MAX_BYTES => 4, 31 ]; 32 $maxChars = [ 33 StringDef::PARAM_MAX_CHARS => 2, 34 ]; 35 36 return [ 37 'Basic' => [ '123', '123' ], 38 'Empty' => [ '', '' ], 39 'Empty, required' => [ 40 '', 41 new ValidationException( 42 DataMessageValue::new( 'paramvalidator-missingparam', [], 'missingparam' ), 43 'test', '', [] 44 ), 45 $req, 46 ], 47 'Empty, required, allowed' => [ '', '', $req, [ 'allowEmptyWhenRequired' => true ] ], 48 'Max bytes, ok' => [ 'abcd', 'abcd', $maxBytes ], 49 'Max bytes, exceeded' => [ 50 'abcde', 51 new ValidationException( 52 DataMessageValue::new( 'paramvalidator-maxbytes', [], 'maxbytes', [ 53 'maxbytes' => 4, 'maxchars' => null, 54 ] ), 55 'test', '', [] 56 ), 57 $maxBytes, 58 ], 59 'Max bytes, ok (2)' => [ '', '', $maxBytes ], 60 'Max bytes, exceeded (2)' => [ 61 '?', 62 new ValidationException( 63 DataMessageValue::new( 'paramvalidator-maxbytes', [], 'maxbytes', [ 64 'maxbytes' => 4, 'maxchars' => null, 65 ] ), 66 'test', '', [] 67 ), 68 $maxBytes, 69 ], 70 'Max chars, ok' => [ 'ab', 'ab', $maxChars ], 71 'Max chars, exceeded' => [ 72 'abc', 73 new ValidationException( 74 DataMessageValue::new( 'paramvalidator-maxchars', [], 'maxchars', [ 75 'maxbytes' => null, 'maxchars' => 2, 76 ] ), 77 'test', '', [] 78 ), 79 $maxChars, 80 ], 81 'Max chars, ok (2)' => [ '', '', $maxChars ], 82 'Max chars, exceeded (2)' => [ 83 '??', 84 new ValidationException( 85 DataMessageValue::new( 'paramvalidator-maxchars', [], 'maxchars', [ 86 'maxbytes' => null, 'maxchars' => 2, 87 ] ), 88 'test', '', [] 89 ), 90 $maxChars, 91 ], 92 ]; 93 } 94 95 public function provideCheckSettings() { 96 $keys = [ 'Y', StringDef::PARAM_MAX_BYTES, StringDef::PARAM_MAX_CHARS ]; 97 98 return [ 99 'Basic test' => [ 100 [], 101 self::STDRET, 102 [ 103 'issues' => [ 'X' ], 104 'allowedKeys' => $keys, 105 'messages' => [], 106 ], 107 ], 108 'Test with everything' => [ 109 [ 110 StringDef::PARAM_MAX_BYTES => 255, 111 StringDef::PARAM_MAX_CHARS => 100, 112 ], 113 self::STDRET, 114 [ 115 'issues' => [ 'X' ], 116 'allowedKeys' => $keys, 117 'messages' => [], 118 ], 119 ], 120 'Bad types' => [ 121 [ 122 StringDef::PARAM_MAX_BYTES => '255', 123 StringDef::PARAM_MAX_CHARS => 100.0, 124 ], 125 self::STDRET, 126 [ 127 'issues' => [ 128 'X', 129 StringDef::PARAM_MAX_BYTES => 'PARAM_MAX_BYTES must be an integer, got string', 130 StringDef::PARAM_MAX_CHARS => 'PARAM_MAX_CHARS must be an integer, got double', 131 ], 132 'allowedKeys' => $keys, 133 'messages' => [], 134 ], 135 ], 136 'Out of range' => [ 137 [ 138 StringDef::PARAM_MAX_BYTES => -1, 139 StringDef::PARAM_MAX_CHARS => -1, 140 ], 141 self::STDRET, 142 [ 143 'issues' => [ 144 'X', 145 StringDef::PARAM_MAX_BYTES => 'PARAM_MAX_BYTES must be greater than or equal to 0', 146 StringDef::PARAM_MAX_CHARS => 'PARAM_MAX_CHARS must be greater than or equal to 0', 147 ], 148 'allowedKeys' => $keys, 149 'messages' => [], 150 ], 151 ], 152 'Zero not allowed when required and !allowEmptyWhenRequired' => [ 153 [ 154 ParamValidator::PARAM_REQUIRED => true, 155 StringDef::PARAM_MAX_BYTES => 0, 156 StringDef::PARAM_MAX_CHARS => 0, 157 ], 158 self::STDRET, 159 [ 160 'issues' => [ 161 'X', 162 // phpcs:ignore Generic.Files.LineLength 163 'PARAM_REQUIRED is set, allowEmptyWhenRequired is not set, and PARAM_MAX_BYTES is 0. That\'s impossible to satisfy.', 164 // phpcs:ignore Generic.Files.LineLength 165 'PARAM_REQUIRED is set, allowEmptyWhenRequired is not set, and PARAM_MAX_CHARS is 0. That\'s impossible to satisfy.', 166 ], 167 'allowedKeys' => $keys, 168 'messages' => [], 169 ], 170 [ 'allowEmptyWhenRequired' => false ], 171 ], 172 'Zero allowed when not required' => [ 173 [ 174 StringDef::PARAM_MAX_BYTES => 0, 175 StringDef::PARAM_MAX_CHARS => 0, 176 ], 177 self::STDRET, 178 [ 179 'issues' => [ 'X' ], 180 'allowedKeys' => $keys, 181 'messages' => [], 182 ], 183 [ 'allowEmptyWhenRequired' => false ], 184 ], 185 'Zero allowed when allowEmptyWhenRequired' => [ 186 [ 187 ParamValidator::PARAM_REQUIRED => true, 188 StringDef::PARAM_MAX_BYTES => 0, 189 StringDef::PARAM_MAX_CHARS => 0, 190 ], 191 self::STDRET, 192 [ 193 'issues' => [ 'X' ], 194 'allowedKeys' => $keys, 195 'messages' => [], 196 ], 197 [ 'allowEmptyWhenRequired' => true ], 198 ], 199 ]; 200 } 201 202 public function provideGetInfo() { 203 return [ 204 'Basic test' => [ 205 [], 206 [ 'maxbytes' => null, 'maxchars' => null ], 207 [], 208 ], 209 'With settings' => [ 210 [ 211 StringDef::PARAM_MAX_BYTES => 4, 212 StringDef::PARAM_MAX_CHARS => 2, 213 ParamValidator::PARAM_ISMULTI => true, 214 ], 215 [ 'maxbytes' => 4, 'maxchars' => 2 ], 216 [ 217 // phpcs:ignore Generic.Files.LineLength.TooLong 218 StringDef::PARAM_MAX_BYTES => '<message key="paramvalidator-help-type-string-maxbytes"><num>4</num></message>', 219 // phpcs:ignore Generic.Files.LineLength.TooLong 220 StringDef::PARAM_MAX_CHARS => '<message key="paramvalidator-help-type-string-maxchars"><num>2</num></message>', 221 ], 222 ], 223 ]; 224 } 225 226} 227