Development

Changeset 7109

You must first sign up to be able to contribute.

Changeset 7109

Show
Ignore:
Timestamp:
01/20/08 08:57:12 (9 months ago)
Author:
fabien
Message:

added more unit tests to sfOutputEscaper

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/view/escaper/sfOutputEscaper.class.php

    r4597 r7109  
    2828  /** 
    2929   * The escaping method that is going to be applied to the value and its 
    30    * children. This is actually the name of a PHP function
     30   * children. This is actually the name of a PHP callable
    3131   * 
    3232   * @var string 
     
    7171   * (EscapingHelper.php). 
    7272   * 
    73    * @param string $escapingMethod the escaping method (a PHP function) to apply to the value 
     73   * @param string $escapingMethod the escaping method (a PHP callable) to apply to the value 
    7474   * @param mixed $value the value to escape 
    7575   * @param mixed the escaped value 
     
    7777   * @return mixed Escaping value 
    7878   * 
    79    * @throws <b>sfException</b> If the escaping fails 
     79   * @throws InvalidArgumentException If the escaping fails 
    8080   */ 
    8181  public static function escape($escapingMethod, $value) 
    8282  { 
    83     if (is_null($value) || ($value === false) || ($escapingMethod === 'esc_raw')
     83    if (is_null($value) || 'esc_raw' == $escapingMethod
    8484    { 
    8585      return $value; 
     
    119119 
    120120    // it must be a resource; cannot escape that. 
    121     throw new sfException(sprintf('Unable to escape value "%s".', print_r($value, true))); 
     121    throw new InvalidArgumentException(sprintf('Unable to escape value "%s".', var_export($value, true))); 
    122122  } 
    123123 
  • branches/1.1/test/unit/view/escaper/sfOutputEscaperTest.php

    r6864 r7109  
    2121sfConfig::set('sf_charset', 'UTF-8'); 
    2222 
    23 $t = new lime_test(10, new lime_output_color()); 
     23$t = new lime_test(18, new lime_output_color()); 
     24 
     25class OutputEscaperTestClass 
     26
     27  public $title = '<strong>escaped!</strong>'; 
     28 
     29  public function getTitle() 
     30  { 
     31    return $this->title; 
     32  } 
     33 
     34  public function getTitleTitle() 
     35  { 
     36    $o = new self; 
     37 
     38    return $o->getTitle(); 
     39  } 
     40
    2441 
    2542// ::escape() 
    2643$t->diag('::escape()'); 
    27 $t->is(sfOutputEscaper::escape('esc_entities', null), null, '::escape() returns null if the value to escape is null'); 
    28 $t->is(sfOutputEscaper::escape('esc_entities', false), false, '::escape() returns false if the value to escape is false'); 
    29 $t->is(sfOutputEscaper::escape('esc_entities', true), true, '::escape() returns true if the value to escape is true'); 
     44$t->diag('::escape() does not escape special values'); 
     45$t->ok(sfOutputEscaper::escape('esc_entities', null) === null, '::escape() returns null if the value to escape is null'); 
     46$t->ok(sfOutputEscaper::escape('esc_entities', false) === false, '::escape() returns false if the value to escape is false'); 
     47$t->ok(sfOutputEscaper::escape('esc_entities', true) === true, '::escape() returns true if the value to escape is true'); 
    3048 
     49$t->diag('::escape() does not escape a value when escaping method is ESC_RAW'); 
    3150$t->is(sfOutputEscaper::escape('esc_raw', '<strong>escaped!</strong>'), '<strong>escaped!</strong>', '::escape() takes an escaping strategy function name as its first argument'); 
    3251 
     52$t->diag('::escape() escapes strings'); 
    3353$t->is(sfOutputEscaper::escape('esc_entities', '<strong>escaped!</strong>'), '&lt;strong&gt;escaped!&lt;/strong&gt;', '::escape() returns an escaped string if the value to escape is a string'); 
    3454$t->is(sfOutputEscaper::escape('esc_entities', '<strong>échappé</strong>'), '&lt;strong&gt;&eacute;chapp&eacute;&lt;/strong&gt;', '::escape() returns an escaped string if the value to escape is a string'); 
    3555 
    36 $t->isa_ok(sfOutputEscaper::escape('esc_entities', array(1, 2)), 'sfOutputEscaperArrayDecorator', '::escape() returns a sfOutputEscaperArrayDecorator object if the value to escape is an array'); 
     56$t->diag('::escape() escapes arrays'); 
     57$input = array( 
     58  'foo' => '<strong>escaped!</strong>', 
     59  'bar' => array('foo' => '<strong>escaped!</strong>'), 
     60); 
     61$output = sfOutputEscaper::escape('esc_entities', $input); 
     62$t->isa_ok($output, 'sfOutputEscaperArrayDecorator', '::escape() returns a sfOutputEscaperArrayDecorator object if the value to escape is an array'); 
     63$t->is($output['foo'], '&lt;strong&gt;escaped!&lt;/strong&gt;', '::escape() escapes all elements of the original array'); 
     64$t->is($output['bar']['foo'], '&lt;strong&gt;escaped!&lt;/strong&gt;', '::escape() is recursive'); 
     65$t->is($output->getRawValue(), $input, '->getRawValue() returns the unescaped value'); 
    3766 
    38 $t->isa_ok(sfOutputEscaper::escape('esc_entities', new stdClass()), 'sfOutputEscaperObjectDecorator', '::escape() returns a sfOutputEscaperObjectDecorator object if the value to escape is an object'); 
     67$t->diag('::escape() escapes objects'); 
     68$input = new OutputEscaperTestClass(); 
     69$output = sfOutputEscaper::escape('esc_entities', $input); 
     70$t->isa_ok($output, 'sfOutputEscaperObjectDecorator', '::escape() returns a sfOutputEscaperObjectDecorator object if the value to escape is an object'); 
     71$t->is($output->getTitle(), '&lt;strong&gt;escaped!&lt;/strong&gt;', '::escape() escapes all methods of the original object'); 
     72$t->is($output->title, '&lt;strong&gt;escaped!&lt;/strong&gt;', '::escape() escapes all properties of the original object'); 
     73$t->is($output->getTitleTitle(), '&lt;strong&gt;escaped!&lt;/strong&gt;', '::escape() is recursive'); 
     74$t->is($output->getRawValue(), $input, '->getRawValue() returns the unescaped value'); 
    3975 
    40 class OutputEscaperTestClass 
     76$t->is(sfOutputEscaper::escape('esc_entities', $output)->getTitle(), '&lt;strong&gt;escaped!&lt;/strong&gt;', '::escape() does not double escape an object'); 
     77$t->isa_ok(sfOutputEscaper::escape('esc_entities', new DirectoryIterator('.')), 'sfOutputEscaperIteratorDecorator', '::escape() returns a sfOutputEscaperIteratorDecorator object if the value to escape is an object that implements the ArrayAccess interface'); 
     78 
     79$t->diag('::escape() cannot escape resources'); 
     80$fh = fopen(__FILE__, 'r'); 
     81try 
    4182{ 
    42   public function getTitle() 
    43   { 
    44     return '<strong>escaped!</strong>'; 
    45   } 
     83  sfOutputEscaper::escape('esc_entities', $fh); 
     84  $t->fail('::escape() throws an InvalidArgumentException if the value cannot be escaped'); 
    4685} 
    47  
    48 $object = new OutputEscaperTestClass(); 
    49 $escaped_object = sfOutputEscaper::escape('esc_entities', $object); 
    50 $t->is(sfOutputEscaper::escape('esc_entities', $escaped_object)->getTitle(), '&lt;strong&gt;escaped!&lt;/strong&gt;', '::escape() does not double escape an object'); 
    51  
    52 $t->isa_ok(sfOutputEscaper::escape('esc_entities', new DirectoryIterator('.')), 'sfOutputEscaperIteratorDecorator', '::escape() returns a sfOutputEscaperIteratorDecorator object if the value to escape is an object that implements the ArrayAccess interface'); 
     86catch (InvalidArgumentException $e) 
     87
     88  $t->pass('::escape() throws an InvalidArgumentException if the value cannot be escaped'); 
     89