X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fapi%2Fv4%2FAction%2FBasicCustomFieldTest.php;h=240ce3a9432f0ffe74e283f5e7f500f72c8e7d1e;hb=540c0ec9988c9bb1ec1d4b29e72966e63814e6be;hp=89846c1ee2a1089398f6ed6d577004892f13b938;hpb=d0d0202f44a8eb660bd35de8dbb007b7cd66a1a6;p=civicrm-core.git diff --git a/tests/phpunit/api/v4/Action/BasicCustomFieldTest.php b/tests/phpunit/api/v4/Action/BasicCustomFieldTest.php index 89846c1ee2..240ce3a943 100644 --- a/tests/phpunit/api/v4/Action/BasicCustomFieldTest.php +++ b/tests/phpunit/api/v4/Action/BasicCustomFieldTest.php @@ -84,6 +84,18 @@ class BasicCustomFieldTest extends BaseCustomValueTest { ->first(); $this->assertEquals('Blue', $contact['MyIndividualFields.FavColor']); + + // Try setting to null + Contact::update() + ->addWhere('id', '=', $contactId) + ->addValue('MyIndividualFields.FavColor', NULL) + ->execute(); + $contact = Contact::get(FALSE) + ->addSelect('MyIndividualFields.FavColor') + ->addWhere('id', '=', $contactId) + ->execute() + ->first(); + $this->assertEquals(NULL, $contact['MyIndividualFields.FavColor']); } public function testWithTwoFields() { @@ -385,4 +397,47 @@ class BasicCustomFieldTest extends BaseCustomValueTest { $this->assertEquals($optionGroupCount, OptionGroup::get(FALSE)->selectRowCount()->execute()->count()); } + public function testUpdateWeights() { + $getValues = function($groupName) { + return CustomField::get(FALSE) + ->addWhere('custom_group_id.name', '=', $groupName) + ->addOrderBy('weight') + ->execute()->indexBy('name')->column('weight'); + }; + + // Create 2 custom groups. Control group is to ensure updating one doesn't affect the other + foreach (['controlGroup', 'experimentalGroup'] as $groupName) { + $customGroups[$groupName] = CustomGroup::create(FALSE) + ->addValue('name', $groupName) + ->addValue('extends', 'Individual') + ->execute()->first(); + $sampleData = [ + ['label' => 'One'], + ['label' => 'Two'], + ['label' => 'Three'], + ['label' => 'Four'], + ]; + CustomField::save(FALSE) + ->setRecords($sampleData) + ->addDefault('custom_group_id.name', $groupName) + ->addDefault('html_type', 'Text') + ->execute(); + // Default weights should have been set during create + $this->assertEquals(['One' => 1, 'Two' => 2, 'Three' => 3, 'Four' => 4], $getValues($groupName)); + } + + // Ensure default weights were set for custom groups + $this->assertEquals($customGroups['controlGroup']['weight'] + 1, $customGroups['experimentalGroup']['weight']); + + // Move third option to second position + CustomField::update(FALSE) + ->addWhere('custom_group_id.name', '=', 'experimentalGroup') + ->addWhere('name', '=', 'Three') + ->addValue('weight', 2) + ->execute(); + // Experimental group should be updated, control group should not + $this->assertEquals(['One' => 1, 'Three' => 2, 'Two' => 3, 'Four' => 4], $getValues('experimentalGroup')); + $this->assertEquals(['One' => 1, 'Two' => 2, 'Three' => 3, 'Four' => 4], $getValues('controlGroup')); + } + }