Skip to content

Commit

Permalink
APIv4 - Emit notices about deprecated join aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Jun 27, 2021
1 parent 2b145b8 commit c0f9cc6
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 28 deletions.
3 changes: 3 additions & 0 deletions Civi/Api4/Service/Schema/Joiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public function autoJoin(Api4SelectQuery $query, array $joinPath, $side = 'LEFT'
if ($joinEntity && !$query->checkEntityAccess($joinEntity)) {
throw new UnauthorizedException('Cannot join to ' . $joinEntity);
}
if ($link->isDeprecated()) {
\CRM_Core_Error::deprecatedWarning("Deprecated join alias '$alias' used in APIv4 get. Should be changed to '{$alias}_id'");
}

$bao = $joinEntity ? CoreUtil::getBAOFromApiName($joinEntity) : NULL;
$conditions = $link->getConditionsForJoin($baseTableAlias);
Expand Down
12 changes: 1 addition & 11 deletions Civi/Test/Api3TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public function runApi4Legacy($v3Entity, $v3Action, $v3Params = []) {
$v4Params['language'] = $language;
}
$toRemove = ['option.', 'return', 'api.', 'format.'];
$chains = $joins = $custom = [];
$chains = $custom = [];
foreach ($v3Params as $key => $val) {
foreach ($toRemove as $remove) {
if (strpos($key, $remove) === 0) {
Expand Down Expand Up @@ -426,12 +426,6 @@ public function runApi4Legacy($v3Entity, $v3Action, $v3Params = []) {
if ($v4Entity != 'Setting' && !in_array('id', $v4Params['select'])) {
$v4Params['select'][] = 'id';
}
// Convert join syntax
foreach ($v4Params['select'] as $idx => $select) {
if (strstr($select, '_id.')) {
$joins[$select] = $v4Params['select'][$idx] = str_replace('_id.', '.', $select);
}
}
}
if ($options['limit'] && $v4Entity != 'Setting') {
$v4Params['limit'] = $options['limit'];
Expand Down Expand Up @@ -598,10 +592,6 @@ public function runApi4Legacy($v3Entity, $v3Action, $v3Params = []) {
foreach ($chains as $key => $params) {
$result[$index][$key] = $this->runApi4LegacyChain($key, $params, $v4Entity, $row, $sequential);
}
// Convert join format
foreach ($joins as $api3Key => $api4Key) {
$result[$index][$api3Key] = $result[$index][$api4Key] ?? NULL;
}
// Resolve custom field names
foreach ($custom as $group => $fields) {
foreach ($fields as $field => $v3FieldName) {
Expand Down
17 changes: 0 additions & 17 deletions tests/phpunit/api/v4/Entity/ContactJoinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,6 @@ public function setUpHeadless() {
return parent::setUpHeadless();
}

public function testContactJoinDeprecated() {
$contact = $this->getReference('test_contact_1');
$entitiesToTest = ['Address', 'OpenID', 'IM', 'Website', 'Email', 'Phone'];

foreach ($entitiesToTest as $entity) {
$results = civicrm_api4($entity, 'get', [
'where' => [['contact_id', '=', $contact['id']]],
// Deprecated syntax (new syntax is `contact_id.*` not `contact.*`)
'select' => ['contact.*_name', 'contact.id'],
]);
foreach ($results as $result) {
$this->assertEquals($contact['id'], $result['contact.id']);
$this->assertEquals($contact['display_name'], $result['contact.display_name']);
}
}
}

public function testContactJoin() {
$contact = $this->getReference('test_contact_1');
$entitiesToTest = ['Address', 'OpenID', 'IM', 'Website', 'Email', 'Phone'];
Expand Down

0 comments on commit c0f9cc6

Please sign in to comment.