Commit a689294c authored by colemanw's avatar colemanw

Api4SelectQuery - Refactor field handling for looser coupling and more flexibility

Getting ready to support groupBy, having and sql functions
parent 822e499f
......@@ -210,7 +210,7 @@ class PostSelectQuerySubscriber implements EventSubscriberInterface {
$selectFields = [];
foreach ($selects as $select) {
$selectAlias = $query->getFkSelectAliases()[$select];
$selectAlias = str_replace('`', '', $query->getField($select)['sql_name']);
$fieldAlias = substr($select, strrpos($select, '.') + 1);
$selectFields[$fieldAlias] = $selectAlias;
}
......
This diff is collapsed.
......@@ -59,7 +59,7 @@ class CustomGroupJoinable extends Joinable {
if (!$this->entityFields) {
$fields = CustomField::get()
->setCheckPermissions(FALSE)
->setSelect(['custom_group.name', 'custom_group_id', 'name', 'label', 'data_type', 'html_type', 'is_required', 'is_searchable', 'is_search_range', 'weight', 'is_active', 'is_view', 'option_group_id', 'default_value', 'date_format', 'time_format', 'start_date_years', 'end_date_years'])
->setSelect(['custom_group.name', '*'])
->addWhere('custom_group.table_name', '=', $this->getTargetTable())
->execute();
foreach ($fields as $field) {
......
......@@ -37,11 +37,6 @@ class CustomFieldSpec extends FieldSpec {
*/
protected $tableName;
/**
* @var string
*/
protected $columnName;
/**
* @inheritDoc
*/
......@@ -116,22 +111,4 @@ class CustomFieldSpec extends FieldSpec {
return $this;
}
/**
* @return string
*/
public function getCustomFieldColumnName() {
return $this->columnName;
}
/**
* @param string $customFieldColumnName
*
* @return $this
*/
public function setCustomFieldColumnName($customFieldColumnName) {
$this->columnName = $customFieldColumnName;
return $this;
}
}
......@@ -104,6 +104,11 @@ class FieldSpec {
*/
protected $permission;
/**
* @var string
*/
protected $columnName;
/**
* Aliases for the valid data types
*
......@@ -122,7 +127,7 @@ class FieldSpec {
*/
public function __construct($name, $entity, $dataType = 'String') {
$this->entity = $entity;
$this->setName($name);
$this->name = $this->columnName = $name;
$this->setDataType($dataType);
}
......@@ -428,6 +433,23 @@ class FieldSpec {
return $this;
}
/**
* @return string
*/
public function getColumnName() {
return $this->columnName;
}
/**
* @param string $columnName
*
* @return $this
*/
public function setColumnName($columnName) {
$this->columnName = $columnName;
return $this;
}
/**
* @param array $values
* @return array
......
......@@ -61,8 +61,8 @@ class SpecFormatter {
}
else {
$field->setCustomTableName($data['custom_group.table_name']);
$field->setCustomFieldColumnName($data['column_name']);
}
$field->setColumnName($data['column_name']);
$field->setCustomFieldId($data['id'] ?? NULL);
$field->setCustomGroupName($data['custom_group.name']);
$field->setTitle($data['label'] ?? NULL);
......
......@@ -131,7 +131,7 @@ class SpecGatherer {
->setCheckPermissions(FALSE)
->addWhere('custom_group.extends', 'IN', $extends)
->addWhere('custom_group.is_multiple', '=', '0')
->setSelect(['custom_group.name', 'custom_group_id', 'name', 'label', 'data_type', 'html_type', 'is_searchable', 'is_search_range', 'weight', 'is_active', 'is_view', 'option_group_id', 'default_value', 'date_format', 'time_format', 'start_date_years', 'end_date_years', 'help_pre', 'help_post'])
->setSelect(['custom_group.name', '*'])
->execute();
foreach ($customFields as $fieldArray) {
......@@ -147,7 +147,7 @@ class SpecGatherer {
private function getCustomGroupFields($customGroup, RequestSpec $specification) {
$customFields = CustomField::get()
->addWhere('custom_group.name', '=', $customGroup)
->setSelect(['custom_group.name', 'custom_group_id', 'name', 'label', 'data_type', 'html_type', 'is_searchable', 'is_search_range', 'weight', 'is_active', 'is_view', 'option_group_id', 'default_value', 'custom_group.table_name', 'column_name', 'date_format', 'time_format', 'start_date_years', 'end_date_years', 'help_pre', 'help_post'])
->setSelect(['custom_group.name', 'custom_group.table_name', '*'])
->execute();
foreach ($customFields as $fieldArray) {
......
......@@ -33,9 +33,6 @@ class ContactApiKeyTest extends \api\v4\UnitTestCase {
\CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM', 'add contacts', 'edit api keys', 'view all contacts', 'edit all contacts'];
$key = \CRM_Utils_String::createRandom(16, \CRM_Utils_String::ALPHANUMERIC);
$isSafe = function ($mixed) use ($key) {
if ($mixed instanceof Result) {
$mixed = $mixed->getArrayCopy();
}
return strpos(json_encode($mixed), $key) === FALSE;
};
......@@ -69,28 +66,31 @@ class ContactApiKeyTest extends \api\v4\UnitTestCase {
$this->assertFalse($isSafe($email), "Should reveal secret details ($key): " . var_export($email, 1));
// Remove permission and we should not see the key
\CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM'];
\CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM', 'view debug output', 'view all contacts'];
$result = Contact::get()
->addWhere('id', '=', $contact['id'])
->addSelect('api_key')
->execute()
->first();
$this->assertTrue(empty($result['api_key']));
$this->assertTrue($isSafe($result), "Should NOT reveal secret details ($key): " . var_export($result, 1));
->setDebug(TRUE)
->execute();
$this->assertContains('api_key', $result->debug['undefined_fields']);
$this->assertArrayNotHasKey('api_key', $result[0]);
$this->assertTrue($isSafe($result[0]), "Should NOT reveal secret details ($key): " . var_export($result[0], 1));
// Also not available via join
$email = Email::get()
->addSelect('contact.api_key')
->addWhere('id', '=', $contact['email']['id'])
->execute()->first();
$this->assertTrue(empty($email['contact.api_key']));
$this->assertTrue($isSafe($email), "Should NOT reveal secret details ($key): " . var_export($email, 1));
->setDebug(TRUE)
->execute();
$this->assertContains('contact.api_key', $email->debug['undefined_fields']);
$this->assertArrayNotHasKey('contact.api_key', $email[0]);
$this->assertTrue($isSafe($email[0]), "Should NOT reveal secret details ($key): " . var_export($email[0], 1));
$result = Contact::get()
->addWhere('id', '=', $contact['id'])
->execute()
->first();
$this->assertTrue(empty($result['api_key']));
$this->assertArrayNotHasKey('api_key', $result);
$this->assertTrue($isSafe($result), "Should NOT reveal secret details ($key): " . var_export($result, 1));
}
......
......@@ -68,6 +68,7 @@ class SpecFormatterTest extends UnitTestCase {
'name' => $name,
'data_type' => 'String',
'html_type' => 'Multi-Select',
'column_name' => $name,
];
/** @var \Civi\Api4\Service\Spec\CustomFieldSpec $field */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment