Merge pull request #17911 from agh1/memberdetailreportautorenew
[civicrm-core.git] / tests / phpunit / api / v4 / Action / CreateCustomValueTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18
19
20 namespace api\v4\Action;
21
22 use Civi\Api4\CustomField;
23 use Civi\Api4\CustomGroup;
24 use Civi\Api4\OptionGroup;
25 use Civi\Api4\OptionValue;
26
27 /**
28 * @group headless
29 */
30 class CreateCustomValueTest extends BaseCustomValueTest {
31
32 public function testGetWithCustomData() {
33 $optionValues = ['r' => 'Red', 'g' => 'Green', 'b' => 'Blue'];
34
35 $customGroup = CustomGroup::create(FALSE)
36 ->addValue('name', 'MyContactFields')
37 ->addValue('extends', 'Contact')
38 ->execute()
39 ->first();
40
41 CustomField::create(FALSE)
42 ->addValue('label', 'Color')
43 ->addValue('option_values', $optionValues)
44 ->addValue('custom_group_id', $customGroup['id'])
45 ->addValue('html_type', 'Select')
46 ->addValue('data_type', 'String')
47 ->execute();
48
49 $customField = CustomField::get(FALSE)
50 ->addWhere('label', '=', 'Color')
51 ->execute()
52 ->first();
53
54 $this->assertNotNull($customField['option_group_id']);
55 $optionGroupId = $customField['option_group_id'];
56
57 $optionGroup = OptionGroup::get(FALSE)
58 ->addWhere('id', '=', $optionGroupId)
59 ->execute()
60 ->first();
61
62 $this->assertEquals('Color', $optionGroup['title']);
63
64 $createdOptionValues = OptionValue::get(FALSE)
65 ->addWhere('option_group_id', '=', $optionGroupId)
66 ->execute()
67 ->getArrayCopy();
68
69 $values = array_column($createdOptionValues, 'value');
70 $labels = array_column($createdOptionValues, 'label');
71 $createdOptionValues = array_combine($values, $labels);
72
73 $this->assertEquals($optionValues, $createdOptionValues);
74 }
75
76 }