Skip to content

Commit

Permalink
Merge pull request #23970 from MegaphoneJon/false-not-zero
Browse files Browse the repository at this point in the history
APIv4 - Fix mishandling of boolean custom values
  • Loading branch information
seamuslee001 authored Jul 21, 2022
2 parents be3dc42 + 39bb0b7 commit ef6a669
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
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);
}

}

0 comments on commit ef6a669

Please sign in to comment.