Merge pull request #20093 from larssandergreen/mailings-AB-test-improvements
[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 $aid1 = 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 $this->assertEquals(1, $this->getRecentItemCount(['type' => 'Activity', 'id' => $aid1]));
39 $this->assertStringContainsString('Hello recent!', \CRM_Utils_Recent::get()[0]['title']);
40
41 $aid2 = Activity::create(FALSE)
42 ->addValue('activity_type_id:name', 'Meeting')
43 ->addValue('source_contact_id', $cid)
44 ->addValue('subject', 'Goodbye recent!')
45 ->execute()->first()['id'];
46 $this->assertEquals(1, $this->getRecentItemCount(['type' => 'Activity', 'id' => $aid2]));
47 $this->assertStringContainsString('Goodbye recent!', \CRM_Utils_Recent::get()[0]['title']);
48
49 Activity::delete(FALSE)->addWhere('id', '=', $aid1)->execute();
50
51 $this->assertEquals(0, $this->getRecentItemCount(['type' => 'Activity', 'id' => $aid1]));
52 $this->assertEquals(1, $this->getRecentItemCount(['type' => 'Activity', 'id' => $aid2]));
53 }
54
55 /**
56 * @param array $props
57 * @return int
58 */
59 private function getRecentItemCount($props) {
60 $count = 0;
61 foreach (\CRM_Utils_Recent::get() as $item) {
62 foreach ($props as $key => $val) {
63 if (($item[$key] ?? NULL) != $val) {
64 continue 2;
65 }
66 }
67 ++$count;
68 }
69 return $count;
70 }
71
72 }