4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace api\v
4\Action
;
22 use api\v
4\UnitTestCase
;
23 use Civi\Api4\Activity
;
24 use Civi\Api4\Contact
;
25 use Civi\Api4\CustomField
;
26 use Civi\Api4\CustomGroup
;
31 class ChainTest
extends UnitTestCase
{
33 public function tearDown(): void
{
35 ->setCheckPermissions(FALSE)
36 ->addWhere('name', '=', 'FavPerson')
37 ->addChain('group', CustomGroup
::delete()->addWhere('name', '=', 'TestActCus'))
42 public function testGetActionsWithFields() {
43 $actions = \Civi\Api4\Activity
::getActions()
44 ->addChain('fields', \Civi\Api4\Activity
::getFields()->setAction('$name'), 'name')
48 $this->assertEquals('Array', $actions['getActions']['fields']['params']['data_type']);
51 public function testGetEntityWithActions() {
52 $entities = \Civi\Api4\Entity
::get()
55 'actions' => ['$name', 'getActions', ['select' => ['name']], 'name'],
60 $this->assertArrayHasKey('replace', $entities['Contact']['actions']);
61 $this->assertArrayHasKey('getLinks', $entities['Entity']['actions']);
62 $this->assertArrayNotHasKey('replace', $entities['Entity']['actions']);
65 public function testContactCreateWithGroup() {
66 $firstName = uniqid('cwtf');
67 $lastName = uniqid('cwtl');
69 $contact = Contact
::create()
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'))
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']);
83 public function testWithContactRef() {
85 ->setCheckPermissions(FALSE)
86 ->addValue('title', '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')
96 $sourceId = Contact
::create()->addValue('first_name', 'Source')->execute()->first()['id'];
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'),
106 ->execute()->first();
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'),
115 ->addChain('contact2', Contact
::get()
116 // Test that we can access a value within an array using dot notation
117 ->addWhere('id', '=', '$contact.id'),
119 ->execute()->first();
121 $this->assertEquals('Fav', $found['contact']['first_name']);
122 $this->assertEquals('Fav', $found['contact2']['first_name']);