Merge pull request #23283 from eileenmcnaughton/import_saved_map
[civicrm-core.git] / tests / phpunit / api / v4 / Action / RecentItemsTest.php
CommitLineData
b1fdd118
CW
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
20namespace api\v4\Action;
21
46f571dd 22use api\v4\Api4TestBase;
b1fdd118 23use Civi\Api4\Activity;
0501e0ea 24use Civi\Api4\RecentItem;
46f571dd 25use Civi\Test\TransactionalInterface;
b1fdd118
CW
26
27/**
28 * @group headless
29 */
46f571dd 30class RecentItemsTest extends Api4TestBase implements TransactionalInterface {
b1fdd118 31
20b017b2 32 public function testAddDeleteActivity(): void {
b1fdd118
CW
33 $cid = $this->createLoggedInUser();
34
a51ed717 35 $aid1 = Activity::create(FALSE)
b1fdd118
CW
36 ->addValue('activity_type_id:name', 'Meeting')
37 ->addValue('source_contact_id', $cid)
38 ->addValue('subject', 'Hello recent!')
39 ->execute()->first()['id'];
a51ed717 40 $this->assertEquals(1, $this->getRecentItemCount(['type' => 'Activity', 'id' => $aid1]));
0501e0ea
CW
41 $recentItem = RecentItem::get(FALSE)->execute()->first();
42 $this->assertStringContainsString('Hello recent!', $recentItem['title']);
43 $this->assertStringContainsString("id=$aid1", $recentItem['view_url']);
44 $this->assertEquals('fa-slideshare', $recentItem['icon']);
20b017b2 45
a51ed717
CW
46 $aid2 = Activity::create(FALSE)
47 ->addValue('activity_type_id:name', 'Meeting')
48 ->addValue('source_contact_id', $cid)
49 ->addValue('subject', 'Goodbye recent!')
50 ->execute()->first()['id'];
0501e0ea
CW
51 $this->assertEquals(1, $this->getRecentItemCount(['type' => 'Activity', 'entity_id' => $aid2]));
52 $this->assertStringContainsString('Goodbye recent!', RecentItem::get(FALSE)->execute()[0]['title']);
a51ed717
CW
53
54 Activity::delete(FALSE)->addWhere('id', '=', $aid1)->execute();
b1fdd118 55
0501e0ea
CW
56 $this->assertEquals(0, $this->getRecentItemCount(['entity_type' => 'Activity', 'entity_id' => $aid1]));
57 $this->assertEquals(1, $this->getRecentItemCount(['entity_type' => 'Activity', 'entity_id' => $aid2]));
b1fdd118
CW
58 }
59
a51ed717
CW
60 /**
61 * @param array $props
62 * @return int
63 */
b1fdd118 64 private function getRecentItemCount($props) {
0501e0ea
CW
65 $recent = RecentItem::get(FALSE);
66 foreach ($props as $key => $val) {
67 $recent->addWhere($key, '=', $val);
b1fdd118 68 }
0501e0ea 69 return $recent->execute()->count();
b1fdd118
CW
70 }
71
72}