Changeset 1032

Show
Ignore:
Timestamp:
11/12/07 16:28:39 (1 year ago)
Author:
mikey
Message:

implemented enhancement #96 for net.stubbles.ipo.request.filters.stubPasswordFilter

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/ipo/request/filters/stubPasswordFilter.php

    r961 r1032  
    3131{ 
    3232    /** 
     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    /** 
    3349     * validator to use for checking the minimum length of the password 
    3450     * 
     
    5470     */ 
    5571    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                                  ); 
    5682 
    5783    /** 
     
    130156 
    131157    /** 
     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    /** 
    132173     * check if entered passwords fulfill password conditions 
    133174     * 
     
    140181        if (is_array($value) === true) { 
    141182            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])); 
    143184            } 
    144185 
     
    151192 
    152193        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())); 
    154195        } 
    155196 
    156197        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])); 
    158199        } 
    159200         
    160201        if (null !== $this->minDiffChars) { 
    161202            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])); 
    163204            } 
    164205        } 
  • trunk/src/test/php/net/stubbles/ipo/request/filters/stubPasswordFilterTestCase.php

    r961 r1032  
    5656    public function testValue() 
    5757    { 
     58        $this->mockStubRequestValueErrorFactory->expect('create', array('STRING_TOO_SHORT')); 
    5859        $this->mockMinLengthValidator->setReturnValueAt(0, 'validate', true); 
    5960        $this->mockMinLengthValidator->setReturnValueAt(1, 'validate', true); 
     
    6263        $this->assertEqual($this->passwordFilter->execute('foo'), md5('foo')); 
    6364        $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'); 
    6478        $this->expectException('stubFilterException'); 
    6579        $this->passwordFilter->execute('anything'); 
     
    8397    public function testArrayValue() 
    8498    { 
     99        $this->mockStubRequestValueErrorFactory->expect('create', array('PASSWORDS_NOT_EQUAL')); 
    85100        $this->mockMinLengthValidator->setReturnValue('validate', true); 
    86101        $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'); 
    87113        $this->expectException('stubFilterException'); 
    88114        $this->passwordFilter->execute(array('foo', 'bar')); 
     
    94120    public function testUnExpectedValue() 
    95121    { 
     122        $this->mockStubRequestValueErrorFactory->expect('create', array('PASSWORD_INVALID')); 
    96123        $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'); 
    97137        $this->mockMinLengthValidator->setReturnValue('getCriteria', array('minLength' => 'foo')); 
    98138        $this->expectException('stubFilterException'); 
     
    105145    public function testMinDiffChars() 
    106146    { 
     147        $this->mockStubRequestValueErrorFactory->expect('create', array('PASSWORD_TOO_LESS_DIFF_CHARS')); 
    107148        $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'); 
    108163        $this->mockMinLengthValidator->setReturnValue('validate', true); 
    109164        $this->assertEqual($this->passwordFilter->execute(array('abcde', 'abcde')), md5('abcde'));