Merge pull request #21210 from colemanw/deprecateDel
[civicrm-core.git] / tests / phpunit / api / v4 / Action / RecentItemsTest.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\UnitTestCase;
23 use Civi\Api4\Activity;
24
25 /**
26 * @group headless
27 */
28 class RecentItemsTest extends UnitTestCase {
29
30 public function testAddDeleteActivity(): void {
31 $cid = $this->createLoggedInUser();
32
33 $aid = Activity::create(FALSE)
34 ->addValue('activity_type_id:name', 'Meeting')
35 ->addValue('source_contact_id', $cid)
36 ->addValue('subject', 'Hello recent!')
37 ->execute()->first()['id'];
38
39 $this->assertEquals(1, $this->getRecentItemCount(['type' => 'Activity', 'id' => $aid]));
40
41 $this->assertStringContainsString('Hello recent!', \CRM_Utils_Recent::get()[0]['title']);
42
43 Activity::delete(FALSE)->addWhere('id', '=', $aid)->execute();
44
45 $this->assertEquals(0, $this->getRecentItemCount(['type' => 'Activity', 'id' => $aid]));
46 }
47
48 private function getRecentItemCount($props) {
49 $count = 0;
50 foreach (\CRM_Utils_Recent::get() as $item) {
51 foreach ($props as $key => $val) {
52 if (($item[$key] ?? NULL) != $val) {
53 continue 2;
54 }
55 }
56 ++$count;
57 }
58 return $count;
59 }
60
61 }