Merge pull request #11190 from wmortada/CRM-21344
[civicrm-core.git] / tests / phpunit / api / v3 / ActivityCaseTest.php
1 <?php
2
3 /**
4 * Test Activity.get API with the case_id field
5 *
6 * @package CiviCRM_APIv3
7 * @group headless
8 */
9 class 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
39 $this->_case = $this->callAPISuccess('case', 'create', array(
40 'case_type_id' => $this->caseTypeId,
41 'subject' => __CLASS__,
42 'contact_id' => $this->_cid,
43 ));
44
45 $this->_otherActivity = $this->callAPISuccess('Activity', 'create', array(
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.',
49 ));
50 }
51
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);
58 $subjectArr = array(
59 "[case #{$this->_case['id']}] test activity recording under case with id",
60 "[case #{$hash}] test activity recording under case with id",
61 );
62 foreach ($subjectArr as $subject) {
63 $activity = $this->callAPISuccess('Activity', 'create', array(
64 'source_contact_id' => $this->_cid,
65 'activity_type_id' => 'Phone Call',
66 'subject' => $subject,
67 ));
68 $case = $this->callAPISuccessGetSingle('Activity', array('return' => array("case_id"), 'id' => $activity['id']));
69 //Check if case id is present for the activity.
70 $this->assertEquals($this->_case['id'], $case['case_id'][0]);
71 }
72 }
73
74 public function testGet() {
75 $this->assertTrue(is_numeric($this->_case['id']));
76 $this->assertTrue(is_numeric($this->_otherActivity['id']));
77
78 $getByCaseId = $this->callAPIAndDocument('Activity', 'get', array(
79 'case_id' => $this->_case['id'],
80 ), __FUNCTION__, __FILE__);
81 $this->assertNotEmpty($getByCaseId['values']);
82 $getByCaseId_ids = array_keys($getByCaseId['values']);
83
84 $getByCaseNotNull = $this->callAPIAndDocument('Activity', 'get', array(
85 'case_id' => array('IS NOT NULL' => 1),
86 ), __FUNCTION__, __FILE__);
87 $this->assertNotEmpty($getByCaseNotNull['values']);
88 $getByCaseNotNull_ids = array_keys($getByCaseNotNull['values']);
89
90 $getByCaseNull = $this->callAPIAndDocument('Activity', 'get', array(
91 'case_id' => array('IS NULL' => 1),
92 ), __FUNCTION__, __FILE__);
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);
99 $this->assertEquals(array(), array_intersect($getByCaseId_ids, $getByCaseNull_ids));
100 }
101
102 public function testActivityGetWithCaseInfo() {
103 $activities = $this->callAPISuccess('Activity', 'get', array(
104 'sequential' => 1,
105 'case_id' => $this->_case['id'],
106 'return' => array('case_id', 'case_id.subject'),
107 ));
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
113 }