Merge pull request #17163 from jitendrapurohit/core-1731
[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 * $Id$
18 *
19 */
20
21
22 namespace api\v4\Action;
23
24 use Civi\Api4\CustomField;
25 use Civi\Api4\CustomGroup;
26 use Civi\Api4\OptionGroup;
27 use Civi\Api4\OptionValue;
28
29 /**
30 * @group headless
31 */
32 class CreateCustomValueTest extends BaseCustomValueTest {
33
34 public function testGetWithCustomData() {
35 $optionValues = ['r' => 'Red', 'g' => 'Green', 'b' => 'Blue'];
36
37 $customGroup = CustomGroup::create()
38 ->setCheckPermissions(FALSE)
39 ->addValue('name', 'MyContactFields')
40 ->addValue('extends', 'Contact')
41 ->execute()
42 ->first();
43
44 CustomField::create()
45 ->setCheckPermissions(FALSE)
46 ->addValue('label', 'Color')
47 ->addValue('option_values', $optionValues)
48 ->addValue('custom_group_id', $customGroup['id'])
49 ->addValue('html_type', 'Select')
50 ->addValue('data_type', 'String')
51 ->execute();
52
53 $customField = CustomField::get()
54 ->setCheckPermissions(FALSE)
55 ->addWhere('label', '=', 'Color')
56 ->execute()
57 ->first();
58
59 $this->assertNotNull($customField['option_group_id']);
60 $optionGroupId = $customField['option_group_id'];
61
62 $optionGroup = OptionGroup::get()
63 ->setCheckPermissions(FALSE)
64 ->addWhere('id', '=', $optionGroupId)
65 ->execute()
66 ->first();
67
68 $this->assertEquals('Color', $optionGroup['title']);
69
70 $createdOptionValues = OptionValue::get()
71 ->setCheckPermissions(FALSE)
72 ->addWhere('option_group_id', '=', $optionGroupId)
73 ->execute()
74 ->getArrayCopy();
75
76 $values = array_column($createdOptionValues, 'value');
77 $labels = array_column($createdOptionValues, 'label');
78 $createdOptionValues = array_combine($values, $labels);
79
80 $this->assertEquals($optionValues, $createdOptionValues);
81 }
82
83 }