Index: tests/t3lib/t3lib_div_testcase.php =================================================================== --- tests/t3lib/t3lib_div_testcase.php (revision 7181) +++ tests/t3lib/t3lib_div_testcase.php (working copy) @@ -34,6 +34,28 @@ */ class t3lib_div_testcase extends tx_phpunit_testcase { /** + * backup of the global variables _GET, _POST, _SERVER + * + * @var array + */ + private $backupGlobalVariables; + + public function setUp() { + $this->backupGlobalVariables = array( + '_GET' => $_GET, + '_POST' => $_POST, + '_SERVER' => $_SERVER, + ); + } + + public function tearDown() { + foreach ($this->backupGlobalVariables as $key => $data) { + $GLOBALS[$key] = $data; + } + } + + + /** * @test */ public function checkIntExplodeConvertsStringsToInteger() { @@ -838,7 +860,7 @@ */ public function readLLfileLocallangXMLOverride() { $unique = uniqid('locallangXMLOverrideTest'); - + $xml = ' @@ -866,6 +888,155 @@ unlink($file); } -} + + + /////////////////////////////// + // Tests concerning _GETset() + /////////////////////////////// + + /** + * @test + */ + public function getSetCanSetWholeArray() { + $_GET = array(); + $GLOBALS['HTTP_GET_VARS'] = array(); + t3lib_div::_GETset(array('oneKey' => 'oneValue')); + + $this->assertEquals( + array('oneKey' => 'oneValue'), + $_GET + ); + $this->assertEquals( + array('oneKey' => 'oneValue'), + $GLOBALS['HTTP_GET_VARS'] + ); + } + /** + * @test + */ + public function getSetForArrayDropsExistingValues() { + $_GET = array(); + $GLOBALS['HTTP_GET_VARS'] = array(); + + t3lib_div::_GETset(array('foo' => 'bar')); + t3lib_div::_GETset(array('oneKey' => 'oneValue')); + + $this->assertEquals( + array('oneKey' => 'oneValue'), + $_GET + ); + $this->assertEquals( + array('oneKey' => 'oneValue'), + $GLOBALS['HTTP_GET_VARS'] + ); + } + + /** + * @test + */ + public function getSetCanAssignOneValueToOneKey() { + $_GET = array(); + $GLOBALS['HTTP_GET_VARS'] = array(); + + t3lib_div::_GETset('oneValue', 'oneKey'); + + $this->assertEquals( + 'oneValue', + $_GET['oneKey'] + ); + $this->assertEquals( + 'oneValue', + $GLOBALS['HTTP_GET_VARS']['oneKey'] + ); + } + + /** + * @test + */ + public function getSetForOneValueNotDropsExistingValues() { + $_GET = array(); + $GLOBALS['HTTP_GET_VARS'] = array(); + + t3lib_div::_GETset(array('foo' => 'bar')); + t3lib_div::_GETset('oneValue', 'oneKey'); + + $this->assertEquals( + array('foo' => 'bar', 'oneKey' => 'oneValue'), + $_GET + ); + $this->assertEquals( + array('foo' => 'bar', 'oneKey' => 'oneValue'), + $GLOBALS['HTTP_GET_VARS'] + ); + } + + /** + * @test + */ + public function getCanAssignAnArrayToSpecificArrayElement() { + $_GET = array(); + $GLOBALS['HTTP_GET_VARS'] = array(); + + t3lib_div::_GETset(array('childKey' => 'oneValue'), 'parentKey'); + + $this->assertEquals( + array('parentKey' => array('childKey' => 'oneValue')), + $_GET + ); + $this->assertEquals( + array('parentKey' => array('childKey' => 'oneValue')), + $GLOBALS['HTTP_GET_VARS'] + ); + } + + /** + * @test + */ + public function getSetCanAssignAValueToSpecificArrayChildElement() { + $_GET = array(); + $GLOBALS['HTTP_GET_VARS'] = array(); + + t3lib_div::_GETset('oneValue', 'parentKey|childKey'); + + $this->assertEquals( + array('parentKey' => array('childKey' => 'oneValue')), + $_GET + ); + $this->assertEquals( + array('parentKey' => array('childKey' => 'oneValue')), + $GLOBALS['HTTP_GET_VARS'] + ); + } + + /** + * @test + */ + public function getSetCanAssignAnArrayToSpecificArrayChildElement() { + $_GET = array(); + $GLOBALS['HTTP_GET_VARS'] = array(); + + t3lib_div::_GETset( + array('key1' => 'value1', 'key2' => 'value2'), + 'parentKey|childKey' + ); + + $this->assertEquals( + array( + 'parentKey' => array( + 'childKey' => array('key1' => 'value1', 'key2' => 'value2') + ) + ), + $_GET + ); + $this->assertEquals( + array( + 'parentKey' => array( + 'childKey' => array('key1' => 'value1', 'key2' => 'value2') + ) + ), + $GLOBALS['HTTP_GET_VARS'] + ); + } +} ?> \ No newline at end of file Index: t3lib/class.t3lib_div.php =================================================================== --- t3lib/class.t3lib_div.php (revision 7181) +++ t3lib/class.t3lib_div.php (working copy) @@ -322,25 +322,54 @@ } /** - * Writes input value to $_GET + * Writes input value to $_GET. * Usage: 2 * - * @param mixed Array to write to $_GET. Values should NOT be escaped at input time (but will be escaped before writing according to TYPO3 standards). - * @param string Alternative key; If set, this will not set the WHOLE GET array, but only the key in it specified by this value! + * @param mixed $inputGet + * array or single value to write to $_GET. Values should NOT be + * escaped at input time (but will be escaped before writing + * according to TYPO3 standards). + * @param string $key + * alternative key; If set, this will not set the WHOLE GET array, + * but only the key in it specified by this value! + * You can specify to replace keys on deeper array levels by + * separating the keys with a pipe. + * Example: 'parentKey|childKey' will result in + * array('parentKey' => array('childKey' => $inputGet)) + * * @return void */ - public static function _GETset($inputGet,$key='') { - // ADDS slashes since TYPO3 standard currently is that slashes MUST be applied (regardless of magic_quotes setting). - if (strcmp($key,'')) { - if (is_array($inputGet)) { - self::addSlashesOnArray($inputGet); + public static function _GETset($inputGet, $key = '') { + // adds slashes since TYPO3 standard currently is that slashes + // must be applied (regardless of magic_quotes setting) + if (is_array($inputGet)) { + self::addSlashesOnArray($inputGet); + } else { + $inputGet = addslashes($inputGet); + } + + if ($key != '') { + if (strpos($key, '|') !== FALSE) { + $pieces = explode('|', $key); + $newGet = array(); + $pointer =& $newGet; + foreach ($pieces as $piece) { + $pointer =& $pointer[$piece]; + } + $pointer = $inputGet; + $mergedGet = self::array_merge_recursive_overrule( + $_GET, $newGet + ); + + $_GET = $mergedGet; + $GLOBALS['HTTP_GET_VARS'] = $mergedGet; } else { - $inputGet = addslashes($inputGet); + $_GET[$key] = $inputGet; + $GLOBALS['HTTP_GET_VARS'][$key] = $inputGet; } - $GLOBALS['HTTP_GET_VARS'][$key] = $_GET[$key] = $inputGet; } elseif (is_array($inputGet)) { - self::addSlashesOnArray($inputGet); - $GLOBALS['HTTP_GET_VARS'] = $_GET = $inputGet; + $_GET = $inputGet; + $GLOBALS['HTTP_GET_VARS'] = $inputGet; } }