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\Api4TestBase
;
23 use Civi\Api4\Activity
;
24 use Civi\Api4\Contact
;
25 use Civi\Api4\CustomField
;
26 use Civi\Api4\CustomGroup
;
27 use Civi\Test\TransactionalInterface
;
32 class ChainTest
extends Api4TestBase
implements TransactionalInterface
{
34 public function tearDown(): void
{
36 ->setCheckPermissions(FALSE)
37 ->addWhere('name', '=', 'FavPerson')
38 ->addChain('group', CustomGroup
::delete()->addWhere('name', '=', 'TestActCus'))
43 public function testGetActionsWithFields() {
44 $actions = \Civi\Api4\Activity
::getActions()
45 ->addChain('fields', \Civi\Api4\Activity
::getFields()->setAction('$name'), 'name')
49 $this->assertEquals('Array', $actions['getActions']['fields']['params']['data_type']);
52 public function testGetEntityWithActions() {
53 $entities = \Civi\Api4\Entity
::get()
56 'actions' => ['$name', 'getActions', ['select' => ['name']], 'name'],
61 $this->assertArrayHasKey('replace', $entities['Contact']['actions']);
62 $this->assertArrayHasKey('getLinks', $entities['Entity']['actions']);
63 $this->assertArrayNotHasKey('replace', $entities['Entity']['actions']);
66 public function testContactCreateWithGroup() {
67 $firstName = uniqid('cwtf');
68 $lastName = uniqid('cwtl');
70 $contact = Contact
::create()
71 ->addValue('first_name', $firstName)
72 ->addValue('last_name', $lastName)
73 ->addChain('group', \Civi\Api4\Group
::create()->addValue('title', '$display_name'), 0)
74 ->addChain('add_to_group', \Civi\Api4\GroupContact
::create()->addValue('contact_id', '$id')->addValue('group_id', '$group.id'), 0)
75 ->addChain('check_group', \Civi\Api4\GroupContact
::get()->addWhere('group_id', '=', '$group.id'))
79 $this->assertCount(1, $contact['check_group']);
80 $this->assertEquals($contact['id'], $contact['check_group'][0]['contact_id']);
81 $this->assertEquals($contact['group']['id'], $contact['check_group'][0]['group_id']);
84 public function testWithContactRef() {
86 ->setCheckPermissions(FALSE)
87 ->addValue('title', 'TestActCus')
88 ->addValue('extends', 'Activity')
89 ->addChain('field1', CustomField
::create()
90 ->addValue('label', 'FavPerson')
91 ->addValue('custom_group_id', '$id')
92 ->addValue('html_type', 'Autocomplete-Select')
93 ->addValue('data_type', 'ContactReference')
97 $sourceId = Contact
::create()->addValue('first_name', 'Source')->execute()->first()['id'];
99 $created = Contact
::create()
100 ->setCheckPermissions(FALSE)
101 ->addValue('first_name', 'Fav')
102 ->addChain('activity', Activity
::create()
103 ->addValue('activity_type_id:name', 'Meeting')
104 ->addValue('source_contact_id', $sourceId)
105 ->addValue('TestActCus.FavPerson', '$id'),
107 ->execute()->first();
109 $found = Activity
::get()
110 ->addSelect('TestActCus.*')
111 ->addWhere('id', '=', $created['activity']['id'])
112 ->addChain('contact', Contact
::get()
113 // Test that we can access an array key with a dot in it (and it won't be confused with dot notation)
114 ->addWhere('id', '=', '$TestActCus.FavPerson'),
116 ->addChain('contact2', Contact
::get()
117 // Test that we can access a value within an array using dot notation
118 ->addWhere('id', '=', '$contact.id'),
120 ->execute()->first();
122 $this->assertEquals('Fav', $found['contact']['first_name']);
123 $this->assertEquals('Fav', $found['contact2']['first_name']);