1
0
mirror of https://github.com/e107inc/e107.git synced 2025-01-17 12:48:24 +01:00

Some tests added.

This commit is contained in:
Cameron 2020-12-20 19:41:16 -08:00
parent f950b32774
commit 0f4b3d29cb
3 changed files with 253 additions and 34 deletions

View File

@ -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 - <br /><br />".htmlentities($ArrayData), E_USER_ERROR);
// return false;
}
return $data;
return e107::unserialize($ArrayData);
}
}

View File

@ -0,0 +1,79 @@
<?php
class ArrayDataTest extends \Codeception\Test\Unit
{
/** @var ArrayData */
protected $ad;
protected function _before()
{
try
{
$this->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);
}
}

View File

@ -0,0 +1,166 @@
<?php
class e_prefTest extends \Codeception\Test\Unit
{
/** @var e_pref */
protected $pref;
protected function _before()
{
try
{
$this->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);
}
}