CustomField - Reformat data when modifying field serialize property.
[civicrm-core.git] / tests / phpunit / api / v4 / Action / CustomFieldAlterTest.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\Activity;
23 use Civi\Api4\CustomField;
24 use Civi\Api4\CustomGroup;
25
26 /**
27 * @group headless
28 */
29 class CustomFieldAlterTest extends BaseCustomValueTest {
30
31 public function testChangeSerialize() {
32 $contact = $this->createEntity(['type' => 'Individual']);
33
34 $customGroup = CustomGroup::create(FALSE)
35 ->addValue('title', 'MyFieldsToAlter')
36 ->addValue('extends', 'Activity')
37 ->addChain('field1', CustomField::create()
38 ->addValue('custom_group_id', '$id')
39 ->addValue('label', 'TestOptions')
40 ->addValue('html_type', 'Select')
41 ->addValue('option_values', [
42 1 => 'One',
43 2 => 'Two',
44 3 => 'Three',
45 4 => 'Four',
46 ]), 0
47 )
48 ->addChain('field2', CustomField::create()
49 ->addValue('custom_group_id', '$id')
50 ->addValue('label', 'TestText')
51 ->addValue('html_type', 'Text'), 0
52 )
53 ->execute()
54 ->first();
55
56 Activity::save(FALSE)
57 ->setDefaults(['activity_type_id' => 1, 'source_contact_id' => $contact['id']])
58 ->addRecord(['subject' => 'A1', 'MyFieldsToAlter.TestText' => 'A1', 'MyFieldsToAlter.TestOptions' => '1'])
59 ->addRecord(['subject' => 'A2', 'MyFieldsToAlter.TestText' => 'A2', 'MyFieldsToAlter.TestOptions' => '2'])
60 ->addRecord(['subject' => 'A3', 'MyFieldsToAlter.TestText' => 'A3', 'MyFieldsToAlter.TestOptions' => ''])
61 ->addRecord(['subject' => 'A4', 'MyFieldsToAlter.TestText' => 'A4'])
62 ->execute();
63
64 $result = Activity::get(FALSE)
65 ->addWhere('MyFieldsToAlter.TestText', 'IS NOT NULL')
66 ->addSelect('custom.*', 'subject', 'MyFieldsToAlter.TestOptions:label')
67 ->execute()->indexBy('subject');
68
69 $this->assertCount(4, $result);
70 $this->assertEquals(1, $result['A1']['MyFieldsToAlter.TestOptions']);
71 $this->assertEquals(2, $result['A2']['MyFieldsToAlter.TestOptions']);
72 $this->assertEquals('One', $result['A1']['MyFieldsToAlter.TestOptions:label']);
73 $this->assertEquals('Two', $result['A2']['MyFieldsToAlter.TestOptions:label']);
74 $this->assertTrue(empty($result['A3']['MyFieldsToAlter.TestOptions']));
75 $this->assertTrue(empty($result['A4']['MyFieldsToAlter.TestOptions']));
76
77 // Change field to multiselect
78 CustomField::update(FALSE)
79 ->addWhere('id', '=', $customGroup['field1']['id'])
80 ->addValue('serialize', TRUE)
81 ->execute();
82
83 $result = Activity::get(FALSE)
84 ->addWhere('MyFieldsToAlter.TestText', 'IS NOT NULL')
85 ->addSelect('custom.*', 'subject', 'MyFieldsToAlter.TestOptions:label')
86 ->execute()->indexBy('subject');
87
88 $this->assertEquals([1], $result['A1']['MyFieldsToAlter.TestOptions']);
89 $this->assertEquals([2], $result['A2']['MyFieldsToAlter.TestOptions']);
90 $this->assertEquals(['One'], $result['A1']['MyFieldsToAlter.TestOptions:label']);
91 $this->assertTrue(empty($result['A3']['MyFieldsToAlter.TestOptions']));
92 $this->assertTrue(empty($result['A4']['MyFieldsToAlter.TestOptions']));
93
94 // Change back to single-select
95 CustomField::update(FALSE)
96 ->addWhere('id', '=', $customGroup['field1']['id'])
97 ->addValue('serialize', FALSE)
98 ->execute();
99
100 $result = Activity::get(FALSE)
101 ->addWhere('MyFieldsToAlter.TestText', 'IS NOT NULL')
102 ->addSelect('custom.*', 'subject', 'MyFieldsToAlter.TestOptions:label')
103 ->execute()->indexBy('subject');
104
105 $this->assertCount(4, $result);
106 $this->assertEquals(1, $result['A1']['MyFieldsToAlter.TestOptions']);
107 $this->assertEquals(2, $result['A2']['MyFieldsToAlter.TestOptions']);
108 $this->assertEquals('One', $result['A1']['MyFieldsToAlter.TestOptions:label']);
109 $this->assertEquals('Two', $result['A2']['MyFieldsToAlter.TestOptions:label']);
110 $this->assertTrue(empty($result['A3']['MyFieldsToAlter.TestOptions']));
111 $this->assertTrue(empty($result['A4']['MyFieldsToAlter.TestOptions']));
112 }
113
114 }