4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace api\v
4\Action
;
22 use api\v
4\Api4TestBase
;
23 use Civi\Api4\Activity
;
24 use Civi\Api4\RecentItem
;
25 use Civi\Test\TransactionalInterface
;
30 class RecentItemsTest
extends Api4TestBase
implements TransactionalInterface
{
32 public function testAddDeleteActivity(): void
{
33 $cid = $this->createLoggedInUser();
35 $aid1 = Activity
::create(FALSE)
36 ->addValue('activity_type_id:name', 'Meeting')
37 ->addValue('source_contact_id', $cid)
38 ->addValue('subject', 'Hello recent!')
39 ->execute()->first()['id'];
40 $this->assertEquals(1, $this->getRecentItemCount(['type' => 'Activity', 'id' => $aid1]));
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']);
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'];
51 $this->assertEquals(1, $this->getRecentItemCount(['type' => 'Activity', 'entity_id' => $aid2]));
52 $this->assertStringContainsString('Goodbye recent!', RecentItem
::get(FALSE)->execute()[0]['title']);
54 Activity
::delete(FALSE)->addWhere('id', '=', $aid1)->execute();
56 $this->assertEquals(0, $this->getRecentItemCount(['entity_type' => 'Activity', 'entity_id' => $aid1]));
57 $this->assertEquals(1, $this->getRecentItemCount(['entity_type' => 'Activity', 'entity_id' => $aid2]));
64 private function getRecentItemCount($props) {
65 $recent = RecentItem
::get(FALSE);
66 foreach ($props as $key => $val) {
67 $recent->addWhere($key, '=', $val);
69 return $recent->execute()->count();