Commit | Line | Data |
---|---|---|
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 |
20 | namespace api\v4\Action; |
21 | ||
22 | use api\v4\UnitTestCase; | |
10619fd1 CW |
23 | use Civi\Api4\Activity; |
24 | use Civi\Api4\Contact; | |
25 | use Civi\Api4\CustomField; | |
26 | use Civi\Api4\CustomGroup; | |
19b53e5b C |
27 | |
28 | /** | |
29 | * @group headless | |
30 | */ | |
31 | class ChainTest extends UnitTestCase { | |
32 | ||
cacd9d67 | 33 | public function tearDown(): void { |
34 | CustomField::delete() | |
10619fd1 CW |
35 | ->setCheckPermissions(FALSE) |
36 | ->addWhere('name', '=', 'FavPerson') | |
37 | ->addChain('group', CustomGroup::delete()->addWhere('name', '=', 'TestActCus')) | |
38 | ->execute(); | |
39 | parent::tearDown(); | |
40 | } | |
41 | ||
19b53e5b C |
42 | public function testGetActionsWithFields() { |
43 | $actions = \Civi\Api4\Activity::getActions() | |
44 | ->addChain('fields', \Civi\Api4\Activity::getFields()->setAction('$name'), 'name') | |
45 | ->execute() | |
46 | ->indexBy('name'); | |
47 | ||
48 | $this->assertEquals('Array', $actions['getActions']['fields']['params']['data_type']); | |
49 | } | |
50 | ||
51 | public function testGetEntityWithActions() { | |
52 | $entities = \Civi\Api4\Entity::get() | |
53 | ->addSelect('name') | |
54 | ->setChain([ | |
55 | 'actions' => ['$name', 'getActions', ['select' => ['name']], 'name'], | |
56 | ]) | |
57 | ->execute() | |
58 | ->indexBy('name'); | |
59 | ||
60 | $this->assertArrayHasKey('replace', $entities['Contact']['actions']); | |
61 | $this->assertArrayHasKey('getLinks', $entities['Entity']['actions']); | |
62 | $this->assertArrayNotHasKey('replace', $entities['Entity']['actions']); | |
63 | } | |
64 | ||
65 | public function testContactCreateWithGroup() { | |
66 | $firstName = uniqid('cwtf'); | |
67 | $lastName = uniqid('cwtl'); | |
68 | ||
10619fd1 | 69 | $contact = Contact::create() |
19b53e5b C |
70 | ->addValue('first_name', $firstName) |
71 | ->addValue('last_name', $lastName) | |
72 | ->addChain('group', \Civi\Api4\Group::create()->addValue('title', '$display_name'), 0) | |
73 | ->addChain('add_to_group', \Civi\Api4\GroupContact::create()->addValue('contact_id', '$id')->addValue('group_id', '$group.id'), 0) | |
74 | ->addChain('check_group', \Civi\Api4\GroupContact::get()->addWhere('group_id', '=', '$group.id')) | |
75 | ->execute() | |
76 | ->first(); | |
77 | ||
78 | $this->assertCount(1, $contact['check_group']); | |
79 | $this->assertEquals($contact['id'], $contact['check_group'][0]['contact_id']); | |
80 | $this->assertEquals($contact['group']['id'], $contact['check_group'][0]['group_id']); | |
81 | } | |
82 | ||
10619fd1 CW |
83 | public function testWithContactRef() { |
84 | CustomGroup::create() | |
85 | ->setCheckPermissions(FALSE) | |
86 | ->addValue('name', 'TestActCus') | |
87 | ->addValue('extends', 'Activity') | |
88 | ->addChain('field1', CustomField::create() | |
89 | ->addValue('label', 'FavPerson') | |
90 | ->addValue('custom_group_id', '$id') | |
91 | ->addValue('html_type', 'Autocomplete-Select') | |
92 | ->addValue('data_type', 'ContactReference') | |
93 | ) | |
94 | ->execute(); | |
95 | ||
96 | $sourceId = Contact::create()->addValue('first_name', 'Source')->execute()->first()['id']; | |
97 | ||
98 | $created = Contact::create() | |
99 | ->setCheckPermissions(FALSE) | |
100 | ->addValue('first_name', 'Fav') | |
101 | ->addChain('activity', Activity::create() | |
102 | ->addValue('activity_type_id:name', 'Meeting') | |
103 | ->addValue('source_contact_id', $sourceId) | |
104 | ->addValue('TestActCus.FavPerson', '$id'), | |
105 | 0) | |
106 | ->execute()->first(); | |
107 | ||
108 | $found = Activity::get() | |
109 | ->addSelect('TestActCus.*') | |
110 | ->addWhere('id', '=', $created['activity']['id']) | |
111 | ->addChain('contact', Contact::get() | |
112 | // Test that we can access an array key with a dot in it (and it won't be confused with dot notation) | |
113 | ->addWhere('id', '=', '$TestActCus.FavPerson'), | |
114 | 0) | |
115 | ->addChain('contact2', Contact::get() | |
116 | // Test that we can access a value within an array using dot notation | |
117 | ->addWhere('id', '=', '$contact.id'), | |
118 | 0) | |
119 | ->execute()->first(); | |
120 | ||
121 | $this->assertEquals('Fav', $found['contact']['first_name']); | |
122 | $this->assertEquals('Fav', $found['contact2']['first_name']); | |
123 | } | |
124 | ||
19b53e5b | 125 | } |