Changeset 1032
- Timestamp:
- 11/12/07 16:28:39 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/ipo/request/filters/stubPasswordFilter.php
r961 r1032 31 31 { 32 32 /** 33 * default error id if passwords are not equal 34 */ 35 const PASSWORDS_NOT_EQUAL = 'PASSWORDS_NOT_EQUAL'; 36 /** 37 * default error id if password is too short 38 */ 39 const STRING_TOO_SHORT = 'STRING_TOO_SHORT'; 40 /** 41 * default error id if password is invalid 42 */ 43 const PASSWORD_INVALID = 'PASSWORD_INVALID'; 44 /** 45 * default error id if password has too less different characters 46 */ 47 const PASSWORD_TOO_LESS_DIFF_CHARS = 'PASSWORD_TOO_LESS_DIFF_CHARS'; 48 /** 33 49 * validator to use for checking the minimum length of the password 34 50 * … … 54 70 */ 55 71 protected $encoder; 72 /** 73 * list of error ids to be used by this filter 74 * 75 * @var array<string,string> 76 */ 77 protected $errorIds = array(self::PASSWORDS_NOT_EQUAL => self::PASSWORDS_NOT_EQUAL, 78 self::STRING_TOO_SHORT => self::STRING_TOO_SHORT, 79 self::PASSWORD_INVALID => self::PASSWORD_INVALID, 80 self::PASSWORD_TOO_LESS_DIFF_CHARS => self::PASSWORD_TOO_LESS_DIFF_CHARS 81 ); 56 82 57 83 /** … … 130 156 131 157 /** 158 * sets the error id to be used in case the filter finds an error in the value 159 * 160 * @param string $type type of error, one of the stubHTTPURLFilter::URL_* constants 161 * @param string $errorId the id of the error object to use 162 */ 163 public function setErrorId($type, $errorId) 164 { 165 if (isset($this->errorIds[$type]) === false) { 166 throw new stubIllegalArgumentException('Illegal type ' . $type . ' given. Allowed types are ' . join(', ', array_keys($this->errorIds)) . '.'); 167 } 168 169 $this->errorIds[$type] = $errorId; 170 } 171 172 /** 132 173 * check if entered passwords fulfill password conditions 133 174 * … … 140 181 if (is_array($value) === true) { 141 182 if ($value[0] !== $value[1]) { 142 throw new stubFilterException($this->rveFactory->create( 'PASSWORDS_NOT_EQUAL'));183 throw new stubFilterException($this->rveFactory->create($this->errorIds[self::PASSWORDS_NOT_EQUAL])); 143 184 } 144 185 … … 151 192 152 193 if ($this->minLength->validate($value) === false) { 153 throw new stubFilterException($this->rveFactory->create( 'STRING_TOO_SHORT')->setValues($this->minLength->getCriteria()));194 throw new stubFilterException($this->rveFactory->create($this->errorIds[self::STRING_TOO_SHORT])->setValues($this->minLength->getCriteria())); 154 195 } 155 196 156 197 if (in_array($value, $this->nonAllowedValues) === true) { 157 throw new stubFilterException($this->rveFactory->create( 'PASSWORD_INVALID'));198 throw new stubFilterException($this->rveFactory->create($this->errorIds[self::PASSWORD_INVALID])); 158 199 } 159 200 160 201 if (null !== $this->minDiffChars) { 161 202 if (count(count_chars($value, 1)) < $this->minDiffChars) { 162 throw new stubFilterException($this->rveFactory->create( 'PASSWORD_TOO_LESS_DIFF_CHARS'));203 throw new stubFilterException($this->rveFactory->create($this->errorIds[self::PASSWORD_TOO_LESS_DIFF_CHARS])); 163 204 } 164 205 } trunk/src/test/php/net/stubbles/ipo/request/filters/stubPasswordFilterTestCase.php
r961 r1032 56 56 public function testValue() 57 57 { 58 $this->mockStubRequestValueErrorFactory->expect('create', array('STRING_TOO_SHORT')); 58 59 $this->mockMinLengthValidator->setReturnValueAt(0, 'validate', true); 59 60 $this->mockMinLengthValidator->setReturnValueAt(1, 'validate', true); … … 62 63 $this->assertEqual($this->passwordFilter->execute('foo'), md5('foo')); 63 64 $this->assertEqual($this->passwordFilter->execute('425%$%"§$%t 32'), md5('425%$%"§$%t 32')); 65 $this->expectException('stubFilterException'); 66 $this->passwordFilter->execute('anything'); 67 } 68 69 /** 70 * assure that values are returned the expected way 71 */ 72 public function testValueWithDifferentErrorId() 73 { 74 $this->mockStubRequestValueErrorFactory->expect('create', array('STRING_TOO_SHORT_TEST')); 75 $this->mockMinLengthValidator->setReturnValue('validate', false); 76 $this->mockMinLengthValidator->setReturnValue('getCriteria', array('minLength' => 'foo')); 77 $this->passwordFilter->setErrorId(stubPasswordFilter::STRING_TOO_SHORT, 'STRING_TOO_SHORT_TEST'); 64 78 $this->expectException('stubFilterException'); 65 79 $this->passwordFilter->execute('anything'); … … 83 97 public function testArrayValue() 84 98 { 99 $this->mockStubRequestValueErrorFactory->expect('create', array('PASSWORDS_NOT_EQUAL')); 85 100 $this->mockMinLengthValidator->setReturnValue('validate', true); 86 101 $this->assertEqual($this->passwordFilter->execute(array('foo', 'foo')), md5('foo')); 102 $this->expectException('stubFilterException'); 103 $this->passwordFilter->execute(array('foo', 'bar')); 104 } 105 106 /** 107 * assure that array values are returned the expected way 108 */ 109 public function testArrayValueWithDifferentErrorId() 110 { 111 $this->mockStubRequestValueErrorFactory->expect('create', array('PASSWORDS_NOT_EQUAL_TEST')); 112 $this->passwordFilter->setErrorId(stubPasswordFilter::PASSWORDS_NOT_EQUAL, 'PASSWORDS_NOT_EQUAL_TEST'); 87 113 $this->expectException('stubFilterException'); 88 114 $this->passwordFilter->execute(array('foo', 'bar')); … … 94 120 public function testUnExpectedValue() 95 121 { 122 $this->mockStubRequestValueErrorFactory->expect('create', array('PASSWORD_INVALID')); 96 123 $this->passwordFilter->setNonAllowedValues(array('bar')); 124 $this->mockMinLengthValidator->setReturnValue('getCriteria', array('minLength' => 'foo')); 125 $this->expectException('stubFilterException'); 126 $this->passwordFilter->execute('bar'); 127 } 128 129 /** 130 * assure that an unexpected value throws a stubFilterException 131 */ 132 public function testUnExpectedValueWithDifferentErrorId() 133 { 134 $this->mockStubRequestValueErrorFactory->expect('create', array('PASSWORD_INVALID_TEST')); 135 $this->passwordFilter->setNonAllowedValues(array('bar')); 136 $this->passwordFilter->setErrorId(stubPasswordFilter::PASSWORD_INVALID, 'PASSWORD_INVALID_TEST'); 97 137 $this->mockMinLengthValidator->setReturnValue('getCriteria', array('minLength' => 'foo')); 98 138 $this->expectException('stubFilterException'); … … 105 145 public function testMinDiffChars() 106 146 { 147 $this->mockStubRequestValueErrorFactory->expect('create', array('PASSWORD_TOO_LESS_DIFF_CHARS')); 107 148 $this->passwordFilter->setMinDiffChars(5); 149 $this->mockMinLengthValidator->setReturnValue('validate', true); 150 $this->assertEqual($this->passwordFilter->execute(array('abcde', 'abcde')), md5('abcde')); 151 $this->expectException('stubFilterException'); 152 $this->passwordFilter->execute(array('abcdd', 'abcdd')); 153 } 154 155 /** 156 * test checking for minimum amount of different characters works 157 */ 158 public function testMinDiffCharsWithDifferentErrorId() 159 { 160 $this->mockStubRequestValueErrorFactory->expect('create', array('PASSWORD_TOO_LESS_DIFF_CHARS_TEST')); 161 $this->passwordFilter->setMinDiffChars(5); 162 $this->passwordFilter->setErrorId(stubPasswordFilter::PASSWORD_TOO_LESS_DIFF_CHARS, 'PASSWORD_TOO_LESS_DIFF_CHARS_TEST'); 108 163 $this->mockMinLengthValidator->setReturnValue('validate', true); 109 164 $this->assertEqual($this->passwordFilter->execute(array('abcde', 'abcde')), md5('abcde'));
