1
0
mirror of https://github.com/e107inc/e107.git synced 2025-01-17 20:58:30 +01:00

More user extended tests, including {USER_EXTENDED} shortcode.

This commit is contained in:
Cameron 2021-01-10 10:06:42 -08:00
parent 9c0226fe25
commit 4012ead989
3 changed files with 381 additions and 40 deletions

View File

@ -8,11 +8,12 @@
/**
* @param $parm
* @usage {USER_EXTENDED=<field_name>.[text|value|icon|text_value].<user_id>}
* @example {USER_EXTENDED=user_gender.value.5} will show the value of the extended field user_gender for user #5
* @example {USER_EXTENDED=gender.value.5} will show the value of the extended field user_gender for user #5
* @return bool|string
*/
function user_extended_shortcode($parm)
{
$currentUser = e107::user();
$tp = e107::getParser();

View File

@ -44,6 +44,7 @@ class e107_user_extended
public $systemCount = 0; // Count of system fields - always zero ATM
public $userCount = 0; // Count of non-system fields
private $fieldAttributes = array(); // Field Permissionss with field name as key.
private $lastError;
public function __construct()
{
@ -206,9 +207,14 @@ class e107_user_extended
$target['_FIELD_TYPES'] = array(); // We should always want to recreate the array, even if it exists
foreach ($target['data'] as $k => $v)
{
if (isset($this->nameIndex[$k]))
// if (isset($this->nameIndex[$k]))
// {
if($type = $this->getDbFieldType($k))
{
switch ($this->fieldDefinitions[$this->nameIndex[$k]]['user_extended_struct_type'])
$target['_FIELD_TYPES'][$k] = $type;
}
/* switch ($this->fieldDefinitions[$this->nameIndex[$k]]['user_extended_struct_type'])
{
case EUF_TEXT :
case EUF_DB_FIELD :
@ -231,12 +237,58 @@ class e107_user_extended
case EUF_INTEGER :
$target['_FIELD_TYPES'][$k] = 'int';
break;
}
}
}*/
// }
}
}
/**
* Given the field name, returns the database FIELD_TYPE
* @param $fieldname
* @return string|null
*/
public function getDBFieldType($fieldname)
{
if(strpos($fieldname, 'user_') !== 0)
{
// $fieldname = 'user_'. $fieldname;
var_dump($fieldname);
}
if (!isset($this->nameIndex[$fieldname]))
{
return null;
}
$ret = null;
$index = $this->nameIndex[$fieldname];
switch ($this->fieldDefinitions[$index]['user_extended_struct_type'])
{
case EUF_TEXT :
case EUF_DB_FIELD :
case EUF_TEXTAREA :
case EUF_RICHTEXTAREA :
case EUF_DROPDOWN :
case EUF_DATE :
case EUF_LANGUAGE :
case EUF_PREDEFINED :
case EUF_RADIO :
$ret = 'todb';
break;
case EUF_CHECKBOX :
$ret = 'array';
break;
case EUF_INTEGER :
$ret = 'int';
break;
}
return $ret;
}
/**
* For all UEFs not in the target array, adds the default value
@ -743,6 +795,9 @@ class e107_user_extended
return $this->user_extended_add($name, '_system_', $type, $source, '', $default, 0, 255, 255, 255, 0, 0);
}
function user_extended_add($name, $text='', $type='', $parms='', $values='', $default='', $required='', $read='', $write='', $applicable='', $order='', $parent='')
{
@ -798,28 +853,29 @@ class e107_user_extended
$sql->gen('ALTER TABLE #user_extended ADD user_'.$tp -> toDB($name, true).' '.$field_info);
}
/* TODO
$extStructInsert = array(
'user_extended_struct_id' => '_NULL_',
'user_extended_struct_name' => '',
'user_extended_struct_text' => '',
'user_extended_struct_type' => '',
'user_extended_struct_parms' => '',
'user_extended_struct_values' => '',
'user_extended_struct_default' => '',
'user_extended_struct_read' => '',
'user_extended_struct_write' => '',
'user_extended_struct_required' => '',
'user_extended_struct_signup' => '',
'user_extended_struct_applicable' => '',
'user_extended_struct_order' => '',
'user_extended_struct_parent' => ''
// 'user_extended_struct_id' => null,
'user_extended_struct_name' => $tp -> toDB($name, true),
'user_extended_struct_text' => (string) $tp -> toDB($text, true),
'user_extended_struct_type' => (int) $type,
'user_extended_struct_parms' => (string) $tp -> toDB($parms, true),
'user_extended_struct_values' => (string) $tp -> toDB($values, true),
'user_extended_struct_default' => (string) $tp -> toDB($default, true),
'user_extended_struct_read' => (int) $read,
'user_extended_struct_write' => (int) $write,
'user_extended_struct_required' => (int) $required,
'user_extended_struct_signup' => '0',
'user_extended_struct_applicable' => (int) $applicable,
'user_extended_struct_order' => (int) $order,
'user_extended_struct_parent' => (int) $parent
);
*/
if(!$this->user_extended_field_exist($name))
{
$sql->insert('user_extended_struct',"null,'".$tp -> toDB($name, true)."','".$tp -> toDB($text, true)."','".intval($type)."','".$tp -> toDB($parms, true)."','".$tp -> toDB($values, true)."', '".$tp -> toDB($default, true)."', '".intval($read)."', '".intval($write)."', '".intval($required)."', '0', '".intval($applicable)."', '".intval($order)."', '".intval($parent)."'");
$sql->insert('user_extended_struct', $extStructInsert);
// $sql->insert('user_extended_struct',"null,'".$tp -> toDB($name, true)."','".$tp -> toDB($text, true)."','".intval($type)."','".$tp -> toDB($parms, true)."','".$tp -> toDB($values, true)."', '".$tp -> toDB($default, true)."', '".intval($read)."', '".intval($write)."', '".intval($required)."', '0', '".intval($applicable)."', '".intval($order)."', '".intval($parent)."'");
}
if($this->user_extended_field_exist($name))
@ -827,6 +883,8 @@ class e107_user_extended
return true;
}
echo $sql->getLastErrorText()."\n\n";
return false;
}
@ -1289,7 +1347,7 @@ class e107_user_extended
}
/**
* Proxy Method to retrieve the value of an extended field
* Replacement Method for user_extended_getvalue(); Returns extended field data in the original posted format.
* @param int $uid
* @param string $field_name
* @param mixed $ifnotset [optional]
@ -1297,7 +1355,39 @@ class e107_user_extended
*/
function get($uid, $field_name, $ifnotset=false)
{
return $this->user_extended_getvalue($uid, $field_name, $ifnotset);
$uid = (int) $uid;
if(strpos($field_name, 'user_') !== 0)
{
$field_name = 'user_' . $field_name;
}
$uinfo = e107::user($uid);
if(!isset($uinfo[$field_name]))
{
return $ifnotset;
}
$type = $this->getDbFieldType($field_name);
switch($type)
{
case "int":
$ret = (int) $uinfo[$field_name];
break;
case "array":
$ret = e107::unserialize($uinfo[$field_name]); // code
break;
default:
$ret = $uinfo[$field_name];
// code to be executed if n is different from all labels;
}
return $ret;
}
@ -1336,6 +1426,12 @@ class e107_user_extended
$tp = e107::getParser();
$uid = (int)$uid;
$target = array('data' => array('user_'.$field_name => $newvalue));
$this->addFieldTypes($target);
$fieldType = isset($target['_FIELD_TYPES']['user_'.$field_name]) ? $target['_FIELD_TYPES']['user_'.$field_name] : $fieldType;
switch($fieldType)
{
case 'int':
@ -1346,20 +1442,43 @@ class e107_user_extended
$newvalue = "'".$sql->escape($newvalue)."'";
break;
case 'array':
if(is_array($newvalue))
{
$newvalue = "'".e107::serialize($newvalue, true)."'";
}
else
{
$newvalue = "'". (string) $newvalue."'";
}
break;
default:
$newvalue = "'".$tp->toDB($newvalue)."'";
break;
}
if(substr($field_name, 0, 5) != 'user_')
if(strpos($field_name, 'user_') !== 0)
{
$field_name = 'user_'.$field_name;
}
$qry = "
INSERT INTO `#user_extended` (user_extended_id, {$field_name})
VALUES ({$uid}, {$newvalue})
ON DUPLICATE KEY UPDATE {$field_name} = {$newvalue}
";
return $sql->gen($qry);
if(!$result = $sql->gen($qry))
{
$this->lastError = $sql->getLastErrorText();
echo (ADMIN) ? $this->lastError : '';
}
e107::setRegistry('core/e107/user/'.$uid); // reset the registry since the values changed.
return $result;
}
@ -1376,13 +1495,21 @@ class e107_user_extended
*/
function user_extended_getvalue($uid, $field_name, $ifnotset=false)
{
$uid = intval($uid);
if(substr($field_name, 0, 5) != 'user_')
$uid = (int) $uid;
if(strpos($field_name, 'user_') !== 0)
{
$field_name = 'user_'.$field_name;
$field_name = 'user_' . $field_name;
}
$uinfo = e107::user($uid);
if (!isset($uinfo[$field_name])) return $ifnotset;
if(!isset($uinfo[$field_name]))
{
return $ifnotset;
}
return $uinfo[$field_name];
}

View File

@ -29,15 +29,14 @@
$this->assertTrue(false, "Couldn't load e107_user_extended object");
}
$this->ue->__construct();
$this->typeArray = array(
'text' => EUF_TEXT,
'homepage' => EUF_TEXT,
'radio' => EUF_RADIO,
'dropdown' => EUF_DROPDOWN,
'db field' => EUF_DB_FIELD,
'dbfield' => EUF_DB_FIELD,
'textarea' => EUF_TEXTAREA,
'integer' => EUF_INTEGER,
'date' => EUF_DATE,
@ -50,18 +49,205 @@
'richtextarea' => EUF_RICHTEXTAREA,
);
$this->structValues = array(
'dropdown' => 'drop1,drop2,drop3',
'dbfield' => 'core_media_cat,media_cat_id,media_cat_title,media_cat_order',
'list' => 'timezones',
'radio' => 'M => UE_LAN_MALE,F => UE_LAN_FEMALE',
'checkbox' => 'check1,check2,check3'
);
$this->structDefault = array(
'dropdown' => 'drop2',
'dbfield' => '3',
// 'list' => 'timezones',
'radio' => 'F',
'checkbox' => 'check2'
);
// Add a field of each type.
foreach($this->typeArray as $k=>$v)
{
$this->ue->user_extended_add($k, ucfirst($k), $v );
$value = (isset($this->structValues[$k])) ? $this->structValues[$k] : null;
$insert = array(
'name' => $k,
'text' => ucfirst($k),
'type' => $v,
'parms' => null,
'values' => (isset($this->structValues[$k])) ? $this->structValues[$k] : null,
'default' => (isset($this->structDefault[$k])) ? $this->structDefault[$k] : null,
);
$this->ue->user_extended_add($insert);
// $this->ue->user_extended_add($k, ucfirst($k), $v , null, $value);
}
// As $_POSTED.
$this->userValues = array(
'text' => 'Some Text',
'homepage' => 'https://e107.org',
'radio' => 'M',
'dropdown' => 'drop3',
'dbfield' => '5',
'textarea' => 'Text area value',
'integer' => 21,
'date' => '2001-01-11',
'language' => 'English',
'list' => 'America/Aruba',
'checkbox' => array ( 0 => 'value2', 1 => 'value3'),
'predefined' => 'predefined', // Used in plugin installation routine.
// 'addon' => EUF_ADDON,
'country' => 'us',
'richtextarea' => '<b>Rich text</b>',
);
$this->ue->init();
}
public function testSetGet()
{
// set them all first.
foreach($this->userValues as $field => $value)
{
$this->ue->set(1, $field, $value); // set user extended value for user_id: 1.
}
foreach($this->userValues as $field => $value)
{
$result = $this->ue->get(1, $field); // retrieve value for $field of user_id: 1.
$this->assertSame($this->userValues[$field], $result);
}
}
/**
* Test the {USER_EXTENDED} shortcode.
*/
public function testUserExtendedShortcode()
{
foreach($this->userValues as $field => $value)
{
$this->ue->set(1, $field, $value); // set user extended value for user_id: 1.
}
$legacyExpectedValues = array (
'text' => 'Some Text',
'homepage' => 'https://e107.org',
'radio' => 'M',
'dropdown' => 'drop3',
'dbfield' => 'News',
'textarea' => 'Text area value',
'integer' => '21',
'date' => '2001-01-11',
'language' => 'English',
'list' => 'America/Aruba (-04:00)',
'checkbox' => 'value2, value3',
'predefined' => 'predefined',
'country' => 'United States',
'richtextarea' => '<b>Rich text</b>',
);
$tp = e107::getParser();
foreach($this->userValues as $field => $value)
{
$parm = $field.'.value.1';
$result = $tp->parseTemplate('{USER_EXTENDED='.$parm.'}', true); // retrieve value for $field of user_id: 1.
$this->assertEquals($legacyExpectedValues[$field], $result);
}
$legacyExpectedLabels = array (
'text' => 'Text',
'homepage' => 'Homepage',
'radio' => 'Radio',
'dropdown' => 'Dropdown',
'dbfield' => 'Dbfield',
'textarea' => 'Textarea',
'integer' => 'Integer',
'date' => 'Date',
'language' => 'Language',
'list' => 'List',
'checkbox' => 'Checkbox',
'predefined' => 'Predefined',
'country' => 'Country',
'richtextarea' => 'Richtextarea',
);
foreach($this->userValues as $field => $value)
{
$parm = $field.'.text.1';
$result = $tp->parseTemplate('{USER_EXTENDED='.$parm.'}', true); // retrieve value for $field of user_id: 1.
$this->assertEquals($legacyExpectedLabels[$field], $result);
}
$legacyExpectedLabelValues = array (
'text' => 'Text: Some Text',
'homepage' => 'Homepage: https://e107.org',
'radio' => 'Radio: M',
'dropdown' => 'Dropdown: drop3',
'dbfield' => 'Dbfield: News',
'textarea' => 'Textarea: Text area value',
'integer' => 'Integer: 21',
'date' => 'Date: 2001-01-11',
'language' => 'Language: English',
'list' => 'List: America/Aruba (-04:00)',
'checkbox' => 'Checkbox: value2, value3',
'predefined' => 'Predefined: predefined',
'country' => 'Country: United States',
'richtextarea' => 'Richtextarea: <b>Rich text</b>',
);
foreach($this->userValues as $field => $value)
{
$parm = $field.'.text_value.1';
$result = $tp->parseTemplate('{USER_EXTENDED='.$parm.'}', true); // retrieve value for $field of user_id: 1.
$this->assertEquals($legacyExpectedLabelValues[$field], $result);
}
$legacyExpectedIcons = array (
'text' => '',
'homepage' => 'e107_images/user_icons/user_homepage.png\' style=\'width:16px; height:16px\' alt=\'\' />',
'radio' => '',
'dropdown' => '',
'dbfield' => '',
'textarea' => '',
'integer' => '',
'date' => '',
'language' => '',
'list' => '',
'checkbox' => '',
'predefined' => '',
'country' => '',
'richtextarea' => '',
);
foreach($this->userValues as $field => $value)
{
$parm = $field.'.icon.1';
$result = $tp->parseTemplate('{USER_EXTENDED='.$parm.'}', true); // retrieve value for $field of user_id: 1.
$this->assertStringContainsString($legacyExpectedIcons[$field], $result);
}
}
public function testGetStructure()
{
e107::setRegistry('core/userextended/structure'); // clear the registry.
@ -133,7 +319,7 @@
'user_text' => "Some text",
'user_radio' => "1",
'user_dropdown' => "drop-value-1",
'user_db field' => "extra",
'user_dbfield' => "extra",
'user_textarea' => "Some text",
'user_integer' => "3",
'user_date' => "2000-01-03",
@ -152,7 +338,7 @@
'user_text' => 'Some text',
'user_radio' => '1',
'user_dropdown' => 'drop-value-1',
'user_db field' => 'extra',
'user_dbfield' => 'extra',
'user_textarea' => 'Some text',
'user_integer' => 3,
'user_date' => '2000-01-03',
@ -236,12 +422,39 @@
{
}
*/
public function testAddFieldTypes()
{
$data = array();
foreach($this->userValues as $k=>$v)
{
$data['user_'.$k] = $v;
}
$target = array('data'=>$data);
$this->ue->addFieldTypes($target);
$this->assertNotEmpty($target['_FIELD_TYPES']);
$expected = array (
'user_text' => 'todb',
'user_homepage' => 'todb',
'user_radio' => 'todb',
'user_dropdown' => 'todb',
'user_dbfield' => 'todb',
'user_textarea' => 'todb',
'user_integer' => 'int',
'user_date' => 'todb',
'user_language' => 'todb',
'user_list' => 'todb',
'user_checkbox' => 'array',
'user_richtextarea' => 'todb',
);
$this->assertSame($expected, $target['_FIELD_TYPES']);
}
/*
public function testUser_extended_setvalue()
{