Skip to content
This repository has been archived by the owner on Nov 26, 2017. It is now read-only.

Commit

Permalink
Add getAll method to jinput + doc comment for properties
Browse files Browse the repository at this point in the history
  • Loading branch information
florianv committed Oct 10, 2012
1 parent aed408f commit c29a795
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 0 deletions.
34 changes: 34 additions & 0 deletions libraries/joomla/filter/input.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,40 @@ public function clean($source, $type = 'string')
return $result;
}

/**
* Clean an array of data in a recursive manner.
*
* @param array $data An n-dimensional array of data to clean.
* @param array $filters An associative n-dimensional array of filters having keys matching the data keys
* and value corresponding to the filter names to apply.
* If it is empty or missing values, the 'cmd' filter will be applied.
*
* @return array The filtered data.
*
* @since 12.3
*/
public function cleanRecursive(array $data, array $filters = array())
{
$result = array();

foreach ($data as $key => $value)
{
if (is_array($value))
{
$filter = isset($filters[$key]) ? $filters[$key] : array();
$result[$key] = $this->cleanRecursive($value, $filter);
}

else
{
$filter = isset($filters[$key]) ? $filters[$key] : 'cmd';
$result[$key] = $this->clean($value, $filter);
}
}

return $result;
}

/**
* Function to determine if contents of an attribute are safe
*
Expand Down
22 changes: 22 additions & 0 deletions libraries/joomla/input/input.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
* @subpackage Input
* @since 11.1
*
* @property JInput $get
* @property JInput $post
* @property JInput $server
* @property JInputFiles $files
* @property JInputCookie $cookie
*
* @method integer getInt() getInt($name, $default = null) Get a signed integer.
* @method integer getUint() getUint($name, $default = null) Get an unsigned integer.
* @method float getFloat() getFloat($name, $default = null) Get a floating-point number.
Expand Down Expand Up @@ -164,6 +170,22 @@ public function get($name, $default = null, $filter = 'cmd')
return $default;
}

/**
* Get all values filtered from the input data.
*
* @param array $filters An n-dimensional associative array of input key names as keys
* and filter names as value.
* If it is empty or missing values, the 'cmd' filter will be applied.
*
* @return array The input data.
*
* @since 12.3
*/
public function getAll(array $filters = array())
{
return $this->filter->cleanRecursive($this->data, $filters);
}

/**
* Gets an array of values from the request.
*
Expand Down
103 changes: 103 additions & 0 deletions tests/suites/unit/joomla/filter/JFilterInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1333,4 +1333,107 @@ function testCleanWithClassBlackList($type, $data, $expect, $message )
$message
);
}

/**
* Test the clean recursive method.
* We just test that it went to level 3 and cleaned 'bar' with the cmd filter.
*
* @return void
*
* @covers JFilterInput::cleanRecursive
*
* @since 12.3
*/
public function testCleanRecursiveRecursion()
{
$data = array(
'level1' => array (
'level2' => array(
'bar' => 'bar',
'level3' => array(
'bar' => '.bar'
)
)
)
);

$expected = array(
'level1' => array (
'level2' => array(
'bar' => 'bar',
'level3' => array(
'bar' => 'bar'
)
)
)
);

$filter = new JFilterInput;
$result = $filter->cleanRecursive($data);

$this->assertEquals($expected, $result);
}

/**
* Test the clean recursive method.
* We test that the specified filters are correctly applied.
*
* @return void
*
* @covers JFilterInput::cleanRecursive
*
* @since 12.3
*/
public function testCleanRecursiveWithFilters()
{
$data = array(
'level1' => array (
'level2' => array(
'bar' => '123bar',
'level3' => array(
'bar' => '.bar'
)
)
),

'level11' => array(
'foo' => '.foo',
'bar' => '1.23xx'
)
);

// We ommit some command filters.
$filters = array(
'level1' => array (
'level2' => array(
'bar' => 'int',
)
),

'level11' => array(
'bar' => 'float'
)
);

$expected = array(
'level1' => array (
'level2' => array(
'bar' => '123',
'level3' => array(
'bar' => 'bar'
)
)
),

'level11' => array(
'foo' => 'foo',
'bar' => '1.23'
)
);

$filter = new JFilterInput;
$result = $filter->cleanRecursive($data, $filters);

$this->assertEquals($expected, $result);
}
}
30 changes: 30 additions & 0 deletions tests/suites/unit/joomla/input/JInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,36 @@ public function testGet()

}

/**
* Test the JInput::getAll.
*
* @return void
*
* @since 12.3
*/
public function testGetAll()
{
$filters = array('foo');
$data = array(
'foo' => array('bar'),
'bar' => 'bar'
);

$input = new JInput;
$input->set('foo', array('bar'));
$input->set('bar', 'bar');

$mock = $this->getMock('JFilterInput', array('cleanRecursive'));
$mock->expects($this->once())
->method('cleanRecursive')
->with($data, $filters);

// Inject the mock.
TestReflection::setValue($input, 'filter', $mock);

$input->getAll($filters);
}

/**
* Test the JInput::def method.
*
Expand Down

0 comments on commit c29a795

Please sign in to comment.