Merge pull request #17163 from jitendrapurohit/core-1731
[civicrm-core.git] / tests / phpunit / api / v4 / Action / ChainTest.php
CommitLineData
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 * $Id$
18 *
19 */
20
21
19b53e5b
C
22namespace api\v4\Action;
23
24use api\v4\UnitTestCase;
25
26/**
27 * @group headless
28 */
29class ChainTest extends UnitTestCase {
30
31 public function testGetActionsWithFields() {
32 $actions = \Civi\Api4\Activity::getActions()
33 ->addChain('fields', \Civi\Api4\Activity::getFields()->setAction('$name'), 'name')
34 ->execute()
35 ->indexBy('name');
36
37 $this->assertEquals('Array', $actions['getActions']['fields']['params']['data_type']);
38 }
39
40 public function testGetEntityWithActions() {
41 $entities = \Civi\Api4\Entity::get()
42 ->addSelect('name')
43 ->setChain([
44 'actions' => ['$name', 'getActions', ['select' => ['name']], 'name'],
45 ])
46 ->execute()
47 ->indexBy('name');
48
49 $this->assertArrayHasKey('replace', $entities['Contact']['actions']);
50 $this->assertArrayHasKey('getLinks', $entities['Entity']['actions']);
51 $this->assertArrayNotHasKey('replace', $entities['Entity']['actions']);
52 }
53
54 public function testContactCreateWithGroup() {
55 $firstName = uniqid('cwtf');
56 $lastName = uniqid('cwtl');
57
58 $contact = \Civi\Api4\Contact::create()
59 ->addValue('first_name', $firstName)
60 ->addValue('last_name', $lastName)
61 ->addChain('group', \Civi\Api4\Group::create()->addValue('title', '$display_name'), 0)
62 ->addChain('add_to_group', \Civi\Api4\GroupContact::create()->addValue('contact_id', '$id')->addValue('group_id', '$group.id'), 0)
63 ->addChain('check_group', \Civi\Api4\GroupContact::get()->addWhere('group_id', '=', '$group.id'))
64 ->execute()
65 ->first();
66
67 $this->assertCount(1, $contact['check_group']);
68 $this->assertEquals($contact['id'], $contact['check_group'][0]['contact_id']);
69 $this->assertEquals($contact['group']['id'], $contact['check_group'][0]['group_id']);
70 }
71
72}