Merge pull request #22487 from mattwire/repeattransactiontemplatecontribution
[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\Api4TestBase;
23 use Civi\Api4\Activity;
24 use Civi\Api4\RecentItem;
25 use Civi\Test\TransactionalInterface;
26
27 /**
28 * @group headless
29 */
30 class RecentItemsTest extends Api4TestBase implements TransactionalInterface {
31
32 public function testAddDeleteActivity(): void {
33 $cid = $this->createLoggedInUser();
34
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']);
45
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']);
53
54 Activity::delete(FALSE)->addWhere('id', '=', $aid1)->execute();
55
56 $this->assertEquals(0, $this->getRecentItemCount(['entity_type' => 'Activity', 'entity_id' => $aid1]));
57 $this->assertEquals(1, $this->getRecentItemCount(['entity_type' => 'Activity', 'entity_id' => $aid2]));
58 }
59
60 /**
61 * @param array $props
62 * @return int
63 */
64 private function getRecentItemCount($props) {
65 $recent = RecentItem::get(FALSE);
66 foreach ($props as $key => $val) {
67 $recent->addWhere($key, '=', $val);
68 }
69 return $recent->execute()->count();
70 }
71
72 }