From 0f4b3d29cbe87e0f04b8d5883f3bae2670b13121 Mon Sep 17 00:00:00 2001 From: Cameron Date: Sun, 20 Dec 2020 19:41:16 -0800 Subject: [PATCH] Some tests added. --- e107_handlers/arraystorage_class.php | 42 ++---- e107_tests/tests/unit/ArrayDataTest.php | 79 +++++++++++ e107_tests/tests/unit/e_prefTest.php | 166 ++++++++++++++++++++++++ 3 files changed, 253 insertions(+), 34 deletions(-) create mode 100644 e107_tests/tests/unit/ArrayDataTest.php create mode 100644 e107_tests/tests/unit/e_prefTest.php diff --git a/e107_handlers/arraystorage_class.php b/e107_handlers/arraystorage_class.php index c027d14f3..dc5b841ea 100644 --- a/e107_handlers/arraystorage_class.php +++ b/e107_handlers/arraystorage_class.php @@ -11,7 +11,7 @@ if (!defined('e107_INIT')) { exit; } /** -* @DEPRECATED: Allows Storage of arrays without use of serialize functions +* @deprecated: Allows Storage of arrays without use of serialize functions * */ class ArrayData { @@ -39,52 +39,26 @@ class ArrayData { } /** * Return a string containg exported array data. - * @DEPRECATED use e107::serialize() instead. + * @deprecated use e107::serialize() instead. * @param array $ArrayData array to be stored * @param bool $AddSlashes default true, add slashes for db storage, else false * @return string */ - function WriteArray($ArrayData, $AddSlashes = true) { - if (!is_array($ArrayData)) { - return false; - } - $Array = var_export($ArrayData, true); - if ($AddSlashes == true) { - $Array = addslashes($Array); - } - return $Array; + function WriteArray($ArrayData, $AddSlashes = true) + { + return e107::serialize($ArrayData, $AddSlashes); + } /** * Returns an array from stored array data. - * @DEPRECATED use e107::unserialize() instead. + * @deprecated use e107::unserialize() instead. * @param string $ArrayData * @return bool|array stored data */ function ReadArray($ArrayData) { - if (empty($ArrayData)) - { - return false; - } - - // Saftety mechanism for 0.7 -> 0.8 transition. - if(substr($ArrayData,0,2)=='a:' || substr($ArrayData,0,2)=='s:') - { - $dat = unserialize($ArrayData); - $ArrayData = $this->WriteArray($dat,FALSE); - } - - - $data = ""; - $ArrayData = '$data = '.trim($ArrayData).';'; - @eval($ArrayData); - if (!isset($data) || !is_array($data)) - { - trigger_error("Bad stored array data -

".htmlentities($ArrayData), E_USER_ERROR); - // return false; - } - return $data; + return e107::unserialize($ArrayData); } } diff --git a/e107_tests/tests/unit/ArrayDataTest.php b/e107_tests/tests/unit/ArrayDataTest.php new file mode 100644 index 000000000..d86f080c8 --- /dev/null +++ b/e107_tests/tests/unit/ArrayDataTest.php @@ -0,0 +1,79 @@ +ad = $this->make('ArrayData'); + } + + catch(Exception $e) + { + $this->assertTrue(false, $e->getMessage()); + } + + } + + public function testReadArray() + { + // e107 var_export test. + $string = "array ( + 'most_members_online' => 10, + 'most_guests_online' => 20, + 'most_online_datestamp' => 1534279911, + 'most_enabled' => true + )"; + + $expected = array ( + 'most_members_online' => 10, + 'most_guests_online' => 20, + 'most_online_datestamp' => 1534279911, + 'most_enabled' => true + ); + + $result = $this->ad->ReadArray($string); + $this->assertSame($expected, $result); + + // legacy Prefs test. + $string = 'a:4:{s:19:"most_members_online";i:10;s:18:"most_guests_online";i:20;s:21:"most_online_datestamp";i:1534279911;s:12:"most_enabled";b:1;}'; + $actual = $this->ad->ReadArray($string); + $this->assertSame($expected, $actual); + + + } + + public function testWriteArray() + { + // Test with addslashes enabled. + $input = array('one'=>'two', 'three'=>true); + $result = $this->ad->WriteArray($input); + $expected = 'array ( + \\\'one\\\' => \\\'two\\\', + \\\'three\\\' => true, +)'; + $this->assertSame($expected, $result); + + + // Test with addslashes disabled. + $result = $this->ad->WriteArray($input, false); + $expected = "array ( + 'one' => 'two', + 'three' => true, +)"; + + $this->assertSame($expected, $result); + + } + + + + + } diff --git a/e107_tests/tests/unit/e_prefTest.php b/e107_tests/tests/unit/e_prefTest.php new file mode 100644 index 000000000..1f35449c6 --- /dev/null +++ b/e107_tests/tests/unit/e_prefTest.php @@ -0,0 +1,166 @@ +pref = $this->make('e_pref'); + } + + catch(Exception $e) + { + $this->assertTrue(false, $e->getMessage()); + } + + $this->pref->__construct('core'); + $this->pref->load(); + + } + +/* public function testRemoveData() + { + + } + + public function testClearPrefCache() + { + + } + + public function testValidate() + { + + } + + public function testReset() + { + + } + + public function test__construct() + { + + } + + public function testSetPref() + { + + } + + public function testLoadData() + { + + } + + public function testSave() + { + + } + + public function testGet() + { + + } + + public function testRemovePref() + { + + } + + public function testLoad() + { + + } + + public function testSetOptionSerialize() + { + + } + + public function testRemove() + { + + } + + public function testSetData() + { + + } + + public function testAddData() + { + + } + + public function testDelete() + { + + } + + public function testUpdatePref() + { + + }*/ + + public function testGetPref() + { + $result = $this->pref->getPref(); + + $this->assertIsArray($result); + $this->assertArrayHasKey('maintainance_flag', $result); + + } +/* + public function testSetOptionBackup() + { + + } + + public function testSet() + { + + } + + public function testUpdate() + { + + } + + public function testAdd() + { + + } +*/ + public function testAddPref() + { + $this->pref->addPref('test_preference', "my custom preference"); + + $result = $this->pref->get('test_preference'); + $expected = "my custom preference"; + $this->assertSame($expected, $result); + + // test multidimentional + $this->pref->addPref('test_list/key1', "value1"); + $this->pref->addPref('test_list/key2', "value2"); + $result = $this->pref->get('test_list'); + $expected = array ( + 'key1' => 'value1', + 'key2' => 'value2', + ); + + $this->assertSame($expected, $result); + + } + + + + + }