From a51ed7175f6c4cb704c6ab3d80730710ff69d90f Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 15 Sep 2021 20:33:28 -0400 Subject: [PATCH] Fixes dev/core#2840 bug in recent items sidebar/menu --- CRM/Utils/Recent.php | 6 ++--- .../phpunit/api/v4/Action/RecentItemsTest.php | 23 ++++++++++++++----- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/CRM/Utils/Recent.php b/CRM/Utils/Recent.php index 64636385199d..d4fcf923ec69 100644 --- a/CRM/Utils/Recent.php +++ b/CRM/Utils/Recent.php @@ -152,11 +152,11 @@ private static function removeItems(array $props) { self::$_recent = array_filter(self::$_recent, function($item) use ($props) { foreach ($props as $key => $val) { - if (isset($item[$key]) && $item[$key] == $val) { - return FALSE; + if (isset($item[$key]) && $item[$key] != $val) { + return TRUE; } } - return TRUE; + return FALSE; }); } diff --git a/tests/phpunit/api/v4/Action/RecentItemsTest.php b/tests/phpunit/api/v4/Action/RecentItemsTest.php index 1ea4a0b2292c..4c81a618e13b 100644 --- a/tests/phpunit/api/v4/Action/RecentItemsTest.php +++ b/tests/phpunit/api/v4/Action/RecentItemsTest.php @@ -30,21 +30,32 @@ class RecentItemsTest extends UnitTestCase { public function testAddDeleteActivity(): void { $cid = $this->createLoggedInUser(); - $aid = Activity::create(FALSE) + $aid1 = Activity::create(FALSE) ->addValue('activity_type_id:name', 'Meeting') ->addValue('source_contact_id', $cid) ->addValue('subject', 'Hello recent!') ->execute()->first()['id']; - - $this->assertEquals(1, $this->getRecentItemCount(['type' => 'Activity', 'id' => $aid])); - + $this->assertEquals(1, $this->getRecentItemCount(['type' => 'Activity', 'id' => $aid1])); $this->assertStringContainsString('Hello recent!', \CRM_Utils_Recent::get()[0]['title']); - Activity::delete(FALSE)->addWhere('id', '=', $aid)->execute(); + $aid2 = Activity::create(FALSE) + ->addValue('activity_type_id:name', 'Meeting') + ->addValue('source_contact_id', $cid) + ->addValue('subject', 'Goodbye recent!') + ->execute()->first()['id']; + $this->assertEquals(1, $this->getRecentItemCount(['type' => 'Activity', 'id' => $aid2])); + $this->assertStringContainsString('Goodbye recent!', \CRM_Utils_Recent::get()[0]['title']); + + Activity::delete(FALSE)->addWhere('id', '=', $aid1)->execute(); - $this->assertEquals(0, $this->getRecentItemCount(['type' => 'Activity', 'id' => $aid])); + $this->assertEquals(0, $this->getRecentItemCount(['type' => 'Activity', 'id' => $aid1])); + $this->assertEquals(1, $this->getRecentItemCount(['type' => 'Activity', 'id' => $aid2])); } + /** + * @param array $props + * @return int + */ private function getRecentItemCount($props) { $count = 0; foreach (\CRM_Utils_Recent::get() as $item) {