7e2bb75962b99d3d8629aac4d3a56318fa20f2ae
[civicrm-core.git] / tests / phpunit / api / v4 / Action / ChainTest.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 api\v4\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;
28
29 /**
30 * @group headless
31 */
32 class ChainTest extends Api4TestBase implements TransactionalInterface {
33
34 public function tearDown(): void {
35 CustomField::delete()
36 ->setCheckPermissions(FALSE)
37 ->addWhere('name', '=', 'FavPerson')
38 ->addChain('group', CustomGroup::delete()->addWhere('name', '=', 'TestActCus'))
39 ->execute();
40 parent::tearDown();
41 }
42
43 public function testGetActionsWithFields() {
44 $actions = \Civi\Api4\Activity::getActions()
45 ->addChain('fields', \Civi\Api4\Activity::getFields()->setAction('$name'), 'name')
46 ->execute()
47 ->indexBy('name');
48
49 $this->assertEquals('Array', $actions['getActions']['fields']['params']['data_type']);
50 }
51
52 public function testGetEntityWithActions() {
53 $entities = \Civi\Api4\Entity::get()
54 ->addSelect('name')
55 ->setChain([
56 'actions' => ['$name', 'getActions', ['select' => ['name']], 'name'],
57 ])
58 ->execute()
59 ->indexBy('name');
60
61 $this->assertArrayHasKey('replace', $entities['Contact']['actions']);
62 $this->assertArrayHasKey('getLinks', $entities['Entity']['actions']);
63 $this->assertArrayNotHasKey('replace', $entities['Entity']['actions']);
64 }
65
66 public function testContactCreateWithGroup() {
67 $firstName = uniqid('cwtf');
68 $lastName = uniqid('cwtl');
69
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'))
76 ->execute()
77 ->first();
78
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']);
82 }
83
84 public function testWithContactRef() {
85 CustomGroup::create()
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')
94 )
95 ->execute();
96
97 $sourceId = Contact::create()->addValue('first_name', 'Source')->execute()->first()['id'];
98
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'),
106 0)
107 ->execute()->first();
108
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'),
115 0)
116 ->addChain('contact2', Contact::get()
117 // Test that we can access a value within an array using dot notation
118 ->addWhere('id', '=', '$contact.id'),
119 0)
120 ->execute()->first();
121
122 $this->assertEquals('Fav', $found['contact']['first_name']);
123 $this->assertEquals('Fav', $found['contact2']['first_name']);
124 }
125
126 }