APIv4 - Fix saving NULL as custom field value
[civicrm-core.git] / tests / phpunit / api / v4 / Action / BasicCustomFieldTest.php
index 89846c1ee2a1089398f6ed6d577004892f13b938..240ce3a9432f0ffe74e283f5e7f500f72c8e7d1e 100644 (file)
@@ -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'));
+  }
+
 }