Merge pull request #17294 from agh1/sr-rel-perms
[civicrm-core.git] / tests / phpunit / api / v3 / ActivityCaseTest.php
CommitLineData
45cebbe4
TO
1<?php
2
3/**
4 * Test Activity.get API with the case_id field
5 *
6 * @package CiviCRM_APIv3
7 * @group headless
8 */
9class api_v3_ActivityCaseTest extends CiviCaseTestCase {
10 protected $_params;
11 protected $_entity;
12 protected $_cid;
13
14 /**
15 * @var array
16 * APIv3 Result (Case.create)
17 */
18 protected $_case;
19
20 /**
21 * @var array
22 * APIv3 Result (Activity.create)
23 */
24 protected $_otherActivity;
25
26 /**
27 * Test setup for every test.
28 *
29 * Connect to the database, truncate the tables that will be used
30 * and redirect stdin to a temporary file.
31 */
32 public function setUp() {
33 $this->_entity = 'case';
34
35 parent::setUp();
36
37 $this->_cid = $this->individualCreate();
38
9099cab3 39 $this->_case = $this->callAPISuccess('case', 'create', [
45cebbe4
TO
40 'case_type_id' => $this->caseTypeId,
41 'subject' => __CLASS__,
42 'contact_id' => $this->_cid,
9099cab3 43 ]);
45cebbe4 44
9099cab3 45 $this->_otherActivity = $this->callAPISuccess('Activity', 'create', [
45cebbe4
TO
46 'source_contact_id' => $this->_cid,
47 'activity_type_id' => 'Phone Call',
48 'subject' => 'Ask not what your API can do for you, but what you can do for your API.',
9099cab3 49 ]);
45cebbe4
TO
50 }
51
2019dea3
JP
52 /**
53 * Test activity creation on case based
54 * on id or hash present in case subject.
55 */
56 public function testActivityCreateOnCase() {
57 $hash = substr(sha1(CIVICRM_SITE_KEY . $this->_case['id']), 0, 7);
9099cab3 58 $subjectArr = [
2019dea3
JP
59 "[case #{$this->_case['id']}] test activity recording under case with id",
60 "[case #{$hash}] test activity recording under case with id",
9099cab3 61 ];
2019dea3 62 foreach ($subjectArr as $subject) {
9099cab3 63 $activity = $this->callAPISuccess('Activity', 'create', [
2019dea3
JP
64 'source_contact_id' => $this->_cid,
65 'activity_type_id' => 'Phone Call',
66 'subject' => $subject,
9099cab3
CW
67 ]);
68 $case = $this->callAPISuccessGetSingle('Activity', ['return' => ["case_id"], 'id' => $activity['id']]);
2019dea3
JP
69 //Check if case id is present for the activity.
70 $this->assertEquals($this->_case['id'], $case['case_id'][0]);
71 }
72 }
73
45cebbe4
TO
74 public function testGet() {
75 $this->assertTrue(is_numeric($this->_case['id']));
76 $this->assertTrue(is_numeric($this->_otherActivity['id']));
77
9099cab3 78 $getByCaseId = $this->callAPIAndDocument('Activity', 'get', [
45cebbe4 79 'case_id' => $this->_case['id'],
9099cab3 80 ], __FUNCTION__, __FILE__);
45cebbe4
TO
81 $this->assertNotEmpty($getByCaseId['values']);
82 $getByCaseId_ids = array_keys($getByCaseId['values']);
83
9099cab3
CW
84 $getByCaseNotNull = $this->callAPIAndDocument('Activity', 'get', [
85 'case_id' => ['IS NOT NULL' => 1],
86 ], __FUNCTION__, __FILE__);
45cebbe4
TO
87 $this->assertNotEmpty($getByCaseNotNull['values']);
88 $getByCaseNotNull_ids = array_keys($getByCaseNotNull['values']);
89
9099cab3
CW
90 $getByCaseNull = $this->callAPIAndDocument('Activity', 'get', [
91 'case_id' => ['IS NULL' => 1],
92 ], __FUNCTION__, __FILE__);
45cebbe4
TO
93 $this->assertNotEmpty($getByCaseNull['values']);
94 $getByCaseNull_ids = array_keys($getByCaseNull['values']);
95
96 $this->assertTrue(in_array($this->_otherActivity['id'], $getByCaseNull_ids));
97 $this->assertNotTrue(in_array($this->_otherActivity['id'], $getByCaseId_ids));
98 $this->assertEquals($getByCaseId_ids, $getByCaseNotNull_ids);
9099cab3 99 $this->assertEquals([], array_intersect($getByCaseId_ids, $getByCaseNull_ids));
45cebbe4
TO
100 }
101
5394ee60 102 public function testActivityGetWithCaseInfo() {
9099cab3 103 $activities = $this->callAPISuccess('Activity', 'get', [
5394ee60
CW
104 'sequential' => 1,
105 'case_id' => $this->_case['id'],
9099cab3
CW
106 'return' => ['case_id', 'case_id.subject'],
107 ]);
5394ee60
CW
108 $this->assertEquals(__CLASS__, $activities['values'][0]['case_id.subject']);
109 // Note - case_id is always an array
110 $this->assertEquals($this->_case['id'], $activities['values'][0]['case_id'][0]);
111 }
112
45cebbe4 113}