Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIv4 - Fix mishandling of boolean custom values #23970

Merged
merged 2 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Civi/Api4/Generic/AbstractAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,12 @@ protected function formatWriteValues(&$record) {
$options = FormattingUtil::getPseudoconstantList($info['field'], $info['expr'], $record, 'create');
$record[$fieldName] = FormattingUtil::replacePseudoconstant($options, $info['val'], TRUE);
}
// The DAO works better with ints than booleans. See https://github.com/civicrm/civicrm-core/pull/23970
foreach ($record as $key => $value) {
if (is_bool($value)) {
$record[$key] = (int) $value;
}
}
}

/**
Expand Down
62 changes: 62 additions & 0 deletions tests/phpunit/api/v4/Custom/FalseNotEqualsZeroTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/


namespace api\v4\Custom;

use Civi\Api4\Contact;
use Civi\Api4\CustomField;
use Civi\Api4\CustomGroup;

/**
* @group headless
*/
class FalseNotEqualsZeroTest extends CustomTestBase {

public function testFalseNotEqualsZero() {

$customGroup = CustomGroup::create(FALSE)
->addValue('title', 'MyContactFields')
->addValue('extends', 'Contact')
->execute()
->first();

CustomField::create(FALSE)
->addValue('label', 'Lightswitch')
->addValue('custom_group_id', $customGroup['id'])
->addValue('html_type', 'Radio')
->addValue('data_type', 'Boolean')
->execute();

$contactId = $this->createTestRecord('Contact', [
'first_name' => 'Red',
'last_name' => 'Tester',
'contact_type' => 'Individual',
'MyContactFields.Lightswitch' => FALSE,
])['id'];

$result = Contact::get($contactId, 'Contact')
->addSelect('MyContactFields.Lightswitch')
->addWhere('id', '=', $contactId)
->execute()
->first()['MyContactFields.Lightswitch'];

$this->assertNotNull($result);
}

}