| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class CRM_Core_BAO_CustomValueTableMultipleTest |
| 5 | * @group headless |
| 6 | */ |
| 7 | class CRM_Core_BAO_CustomValueTableMultipleTest extends CiviUnitTestCase { |
| 8 | |
| 9 | public function setUp() { |
| 10 | parent::setUp(); |
| 11 | } |
| 12 | |
| 13 | public function testCustomGroupMultipleSingle() { |
| 14 | $contactID = $this->individualCreate(); |
| 15 | $customGroup = $this->customGroupCreate(['is_multiple' => 1]); |
| 16 | $fields = [ |
| 17 | 'custom_group_id' => $customGroup['id'], |
| 18 | 'dataType' => 'String', |
| 19 | 'htmlType' => 'Text', |
| 20 | ]; |
| 21 | $customField = $this->customFieldCreate($fields); |
| 22 | |
| 23 | $params = [ |
| 24 | 'entityID' => $contactID, |
| 25 | "custom_{$customField['id']}_-1" => 'First String', |
| 26 | ]; |
| 27 | CRM_Core_BAO_CustomValueTable::setValues($params); |
| 28 | |
| 29 | $newParams = [ |
| 30 | 'entityID' => $contactID, |
| 31 | "custom_{$customField['id']}" => 1, |
| 32 | ]; |
| 33 | $result = CRM_Core_BAO_CustomValueTable::getValues($newParams); |
| 34 | |
| 35 | $this->assertEquals($params["custom_{$customField['id']}_-1"], $result["custom_{$customField['id']}_1"]); |
| 36 | $this->assertEquals($params['entityID'], $result['entityID']); |
| 37 | |
| 38 | $updateParams = [ |
| 39 | 'id' => 1, |
| 40 | 'entityID' => $contactID, |
| 41 | "custom_{$customField['id']}" => 2, |
| 42 | ]; |
| 43 | CRM_Core_BAO_CustomValueTable::setValues($updateParams); |
| 44 | |
| 45 | $criteria = [ |
| 46 | 'id' => 1, |
| 47 | 'entityID' => $contactID, |
| 48 | ]; |
| 49 | $result = CRM_Core_BAO_CustomValueTable::getValues($criteria); |
| 50 | $this->assertEquals(2, $result["custom_{$customField['id']}_1"]); |
| 51 | |
| 52 | $this->customFieldDelete($customField['id']); |
| 53 | $this->customGroupDelete($customGroup['id']); |
| 54 | $this->contactDelete($contactID); |
| 55 | } |
| 56 | |
| 57 | public function testCustomGroupMultipleDouble() { |
| 58 | $contactID = $this->individualCreate(); |
| 59 | $customGroup = $this->customGroupCreate(['is_multiple' => 1]); |
| 60 | $fields = [ |
| 61 | 'custom_group_id' => $customGroup['id'], |
| 62 | 'dataType' => 'String', |
| 63 | 'htmlType' => 'Text', |
| 64 | ]; |
| 65 | $customField = $this->customFieldCreate($fields); |
| 66 | |
| 67 | $params = [ |
| 68 | 'entityID' => $contactID, |
| 69 | "custom_{$customField['id']}_-1" => 'First String', |
| 70 | "custom_{$customField['id']}_-2" => 'Second String', |
| 71 | ]; |
| 72 | CRM_Core_BAO_CustomValueTable::setValues($params); |
| 73 | |
| 74 | $newParams = [ |
| 75 | 'entityID' => $contactID, |
| 76 | "custom_{$customField['id']}" => 1, |
| 77 | ]; |
| 78 | $result = CRM_Core_BAO_CustomValueTable::getValues($newParams); |
| 79 | |
| 80 | $this->assertEquals($params["custom_{$customField['id']}_-1"], $result["custom_{$customField['id']}_1"]); |
| 81 | $this->assertEquals($params["custom_{$customField['id']}_-2"], $result["custom_{$customField['id']}_2"]); |
| 82 | $this->assertEquals($params['entityID'], $result['entityID']); |
| 83 | |
| 84 | $this->customFieldDelete($customField['id']); |
| 85 | $this->customGroupDelete($customGroup['id']); |
| 86 | $this->contactDelete($contactID); |
| 87 | } |
| 88 | |
| 89 | public function testCustomGroupMultipleUpdate() { |
| 90 | $contactID = $this->individualCreate(); |
| 91 | $customGroup = $this->customGroupCreate(['is_multiple' => 1]); |
| 92 | $fields = [ |
| 93 | 'custom_group_id' => $customGroup['id'], |
| 94 | 'dataType' => 'String', |
| 95 | 'htmlType' => 'Text', |
| 96 | ]; |
| 97 | $customField = $this->customFieldCreate($fields); |
| 98 | |
| 99 | $params = [ |
| 100 | 'entityID' => $contactID, |
| 101 | "custom_{$customField['id']}_-1" => 'First String', |
| 102 | "custom_{$customField['id']}_-2" => 'Second String', |
| 103 | "custom_{$customField['id']}_-3" => 'Third String', |
| 104 | ]; |
| 105 | CRM_Core_BAO_CustomValueTable::setValues($params); |
| 106 | |
| 107 | $newParams = [ |
| 108 | 'entityID' => $contactID, |
| 109 | "custom_{$customField['id']}_1" => 'Updated First String', |
| 110 | "custom_{$customField['id']}_3" => 'Updated Third String', |
| 111 | ]; |
| 112 | CRM_Core_BAO_CustomValueTable::setValues($newParams); |
| 113 | |
| 114 | $getParams = [ |
| 115 | 'entityID' => $contactID, |
| 116 | "custom_{$customField['id']}" => 1, |
| 117 | ]; |
| 118 | $result = CRM_Core_BAO_CustomValueTable::getValues($getParams); |
| 119 | |
| 120 | $this->assertEquals($newParams["custom_{$customField['id']}_1"], $result["custom_{$customField['id']}_1"]); |
| 121 | $this->assertEquals($params["custom_{$customField['id']}_-2"], $result["custom_{$customField['id']}_2"]); |
| 122 | $this->assertEquals($newParams["custom_{$customField['id']}_3"], $result["custom_{$customField['id']}_3"]); |
| 123 | $this->assertEquals($params['entityID'], $result['entityID']); |
| 124 | |
| 125 | $this->customFieldDelete($customField['id']); |
| 126 | $this->customGroupDelete($customGroup['id']); |
| 127 | $this->contactDelete($contactID); |
| 128 | } |
| 129 | |
| 130 | public function testCustomGroupMultipleOldFormat() { |
| 131 | $contactID = $this->individualCreate(); |
| 132 | $customGroup = $this->customGroupCreate(['is_multiple' => 1]); |
| 133 | $fields = [ |
| 134 | 'custom_group_id' => $customGroup['id'], |
| 135 | 'dataType' => 'String', |
| 136 | 'htmlType' => 'Text', |
| 137 | ]; |
| 138 | $customField = $this->customFieldCreate($fields); |
| 139 | |
| 140 | $params = [ |
| 141 | 'entityID' => $contactID, |
| 142 | "custom_{$customField['id']}" => 'First String', |
| 143 | ]; |
| 144 | CRM_Core_BAO_CustomValueTable::setValues($params); |
| 145 | |
| 146 | $newParams = [ |
| 147 | 'entityID' => $contactID, |
| 148 | "custom_{$customField['id']}" => 1, |
| 149 | ]; |
| 150 | $result = CRM_Core_BAO_CustomValueTable::getValues($newParams); |
| 151 | |
| 152 | $this->assertEquals($params["custom_{$customField['id']}"], $result["custom_{$customField['id']}_1"]); |
| 153 | $this->assertEquals($params['entityID'], $result['entityID']); |
| 154 | |
| 155 | $this->customFieldDelete($customField['id']); |
| 156 | $this->customGroupDelete($customGroup['id']); |
| 157 | $this->contactDelete($contactID); |
| 158 | } |
| 159 | |
| 160 | } |