Merge pull request #17920 from eileenmcnaughton/dupe
[civicrm-core.git] / tests / phpunit / api / v4 / Action / CreateCustomValueTest.php
CommitLineData
19b53e5b
C
1<?php
2
380f3545
TO
3/*
4 +--------------------------------------------------------------------+
7d61e75f 5 | Copyright CiviCRM LLC. All rights reserved. |
380f3545 6 | |
7d61e75f
TO
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 |
380f3545
TO
10 +--------------------------------------------------------------------+
11 */
12
13/**
14 *
15 * @package CRM
ca5cec67 16 * @copyright CiviCRM LLC https://civicrm.org/licensing
380f3545
TO
17 */
18
19
19b53e5b
C
20namespace api\v4\Action;
21
22use Civi\Api4\CustomField;
23use Civi\Api4\CustomGroup;
24use Civi\Api4\OptionGroup;
25use Civi\Api4\OptionValue;
26
27/**
28 * @group headless
29 */
30class CreateCustomValueTest extends BaseCustomValueTest {
31
32 public function testGetWithCustomData() {
33 $optionValues = ['r' => 'Red', 'g' => 'Green', 'b' => 'Blue'];
34
fe806431 35 $customGroup = CustomGroup::create(FALSE)
19b53e5b
C
36 ->addValue('name', 'MyContactFields')
37 ->addValue('extends', 'Contact')
38 ->execute()
39 ->first();
40
fe806431 41 CustomField::create(FALSE)
19b53e5b
C
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
fe806431 49 $customField = CustomField::get(FALSE)
19b53e5b
C
50 ->addWhere('label', '=', 'Color')
51 ->execute()
52 ->first();
53
54 $this->assertNotNull($customField['option_group_id']);
55 $optionGroupId = $customField['option_group_id'];
56
fe806431 57 $optionGroup = OptionGroup::get(FALSE)
19b53e5b
C
58 ->addWhere('id', '=', $optionGroupId)
59 ->execute()
60 ->first();
61
62 $this->assertEquals('Color', $optionGroup['title']);
63
fe806431 64 $createdOptionValues = OptionValue::get(FALSE)
19b53e5b
C
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}