Merge pull request #15293 from ixiam/dev#issue-826
[civicrm-core.git] / tests / phpunit / api / v4 / Action / CreateCustomValueTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2019
33 * $Id$
34 *
35 */
36
37
38 namespace api\v4\Action;
39
40 use Civi\Api4\CustomField;
41 use Civi\Api4\CustomGroup;
42 use Civi\Api4\OptionGroup;
43 use Civi\Api4\OptionValue;
44
45 /**
46 * @group headless
47 */
48 class CreateCustomValueTest extends BaseCustomValueTest {
49
50 public function testGetWithCustomData() {
51 $optionValues = ['r' => 'Red', 'g' => 'Green', 'b' => 'Blue'];
52
53 $customGroup = CustomGroup::create()
54 ->setCheckPermissions(FALSE)
55 ->addValue('name', 'MyContactFields')
56 ->addValue('extends', 'Contact')
57 ->execute()
58 ->first();
59
60 CustomField::create()
61 ->setCheckPermissions(FALSE)
62 ->addValue('label', 'Color')
63 ->addValue('option_values', $optionValues)
64 ->addValue('custom_group_id', $customGroup['id'])
65 ->addValue('html_type', 'Select')
66 ->addValue('data_type', 'String')
67 ->execute();
68
69 $customField = CustomField::get()
70 ->setCheckPermissions(FALSE)
71 ->addWhere('label', '=', 'Color')
72 ->execute()
73 ->first();
74
75 $this->assertNotNull($customField['option_group_id']);
76 $optionGroupId = $customField['option_group_id'];
77
78 $optionGroup = OptionGroup::get()
79 ->setCheckPermissions(FALSE)
80 ->addWhere('id', '=', $optionGroupId)
81 ->execute()
82 ->first();
83
84 $this->assertEquals('Color', $optionGroup['title']);
85
86 $createdOptionValues = OptionValue::get()
87 ->setCheckPermissions(FALSE)
88 ->addWhere('option_group_id', '=', $optionGroupId)
89 ->execute()
90 ->getArrayCopy();
91
92 $values = array_column($createdOptionValues, 'value');
93 $labels = array_column($createdOptionValues, 'label');
94 $createdOptionValues = array_combine($values, $labels);
95
96 $this->assertEquals($optionValues, $createdOptionValues);
97 }
98
99 }