diff --git a/app/code/Magento/AdminNotification/Model/ResourceModel/Inbox.php b/app/code/Magento/AdminNotification/Model/ResourceModel/Inbox.php index 40b089d947136..ac6807007f79f 100644 --- a/app/code/Magento/AdminNotification/Model/ResourceModel/Inbox.php +++ b/app/code/Magento/AdminNotification/Model/ResourceModel/Inbox.php @@ -98,12 +98,17 @@ public function parse(\Magento\AdminNotification\Model\Inbox $object, array $dat if (empty($item['url'])) { $select->where('url IS NULL'); + unset($item['url']); } else { $select->where('url = ?', $item['url']); } if (isset($item['internal'])) { - $row = false; + if ($item['internal']) { + $row = false; + } else { + $row = $connection->fetchRow($select); + } unset($item['internal']); } else { $row = $connection->fetchRow($select); diff --git a/app/code/Magento/AdminNotification/Test/Unit/Model/ResourceModel/InboxTest.php b/app/code/Magento/AdminNotification/Test/Unit/Model/ResourceModel/InboxTest.php new file mode 100644 index 0000000000000..8980d6cc299a5 --- /dev/null +++ b/app/code/Magento/AdminNotification/Test/Unit/Model/ResourceModel/InboxTest.php @@ -0,0 +1,214 @@ +getMockBuilder(Select::class) + ->disableOriginalConstructor() + ->getMock(); + $mockedSelect->method('from')->willReturnSelf(); + $mockedSelect->method('where')->willReturnSelf(); + $this->mockedConnection = $this->getMockBuilder(AdapterInterface::class) + ->getMock(); + $this->mockedConnection + ->method('select') + ->willReturn($mockedSelect); + /** @var ResourceConnection|MockObject $mockedResourceConnection */ + $mockedResourceConnection = $this->getMockBuilder(ResourceConnection::class) + ->disableOriginalConstructor() + ->getMock(); + $mockedResourceConnection + ->method('getConnection') + ->willReturn($this->mockedConnection); + /** @var Context|MockObject $mockedContext */ + $mockedContext = $this->getMockBuilder(Context::class) + ->disableOriginalConstructor() + ->getMock(); + $mockedContext + ->method('getResources') + ->willReturn($mockedResourceConnection); + $this->objectManagerHelper = new ObjectManagerHelper($this); + $this->inbox = $this->objectManagerHelper->getObject( + Inbox::class, + [ + 'context' => $mockedContext, + ] + ); + } + + public function testParse() + { + // Setup: + $mockedInbox = $this->getMockBuilder(\Magento\AdminNotification\Model\Inbox::class) + ->disableOriginalConstructor() + ->getMock(); + $fakeData = [ + [ + 'severity' => 1, + 'date_added' => '2021-01-01 12:00:00', + 'title' => 'Foo', + 'description' => 'Bar', + 'url' => 'http://www.example.com', + 'internal' => true, + ], + ]; + + // Set expectations: + $this->mockedConnection + ->expects($this->never()) + ->method('fetchRow'); + $this->mockedConnection + ->expects($this->once()) + ->method('insert') + ->with( + null, + [ + 'severity' => 1, + 'date_added' => '2021-01-01 12:00:00', + 'title' => 'Foo', + 'description' => 'Bar', + 'url' => 'http://www.example.com', + ] + ); + + // Exercise: + $this->inbox->parse($mockedInbox, $fakeData); + } + + public function testNotificationWithoutUrlIsNulled() + { + // Setup: + $mockedInbox = $this->getMockBuilder(\Magento\AdminNotification\Model\Inbox::class) + ->disableOriginalConstructor() + ->getMock(); + $fakeData = [ + [ + 'severity' => 1, + 'date_added' => '2021-01-01 12:00:00', + 'title' => 'Foo', + 'description' => 'Bar', + 'url' => '', + 'internal' => true, + ], + ]; + + // Set expectations: + $this->mockedConnection + ->expects($this->never()) + ->method('fetchRow'); + $this->mockedConnection + ->expects($this->once()) + ->method('insert') + ->with( + null, + [ + 'severity' => 1, + 'date_added' => '2021-01-01 12:00:00', + 'title' => 'Foo', + 'description' => 'Bar', + ] + ); + + // Exercise: + $this->inbox->parse($mockedInbox, $fakeData); + } + + public function testInternalNotificationsWithIdenticalDataAreStored() + { + // Setup: + $mockedInbox = $this->getMockBuilder(\Magento\AdminNotification\Model\Inbox::class) + ->disableOriginalConstructor() + ->getMock(); + $fakeData = [ + [ + 'severity' => 1, + 'date_added' => '2021-01-01 12:00:00', + 'title' => 'Foo', + 'description' => 'Bar', + 'internal' => true, + ], + ]; + + // Set expectations: + $this->mockedConnection + ->expects($this->never()) + ->method('fetchRow'); + $this->mockedConnection + ->expects($this->once()) + ->method('insert') + ->with( + null, + [ + 'severity' => 1, + 'date_added' => '2021-01-01 12:00:00', + 'title' => 'Foo', + 'description' => 'Bar', + ] + ); + + // Exercise: + $this->inbox->parse($mockedInbox, $fakeData); + } + + public function testNonInternalNotificationsWithIdenticalDataAreNotStored() + { + // Setup: + $mockedInbox = $this->getMockBuilder(\Magento\AdminNotification\Model\Inbox::class) + ->disableOriginalConstructor() + ->getMock(); + $fakeData = [ + [ + 'severity' => 1, + 'date_added' => '2021-01-01 12:00:00', + 'title' => 'Foo', + 'description' => 'Bar', + 'internal' => false, + ], + ]; + $this->mockedConnection + ->method('fetchRow') + ->willReturn($fakeData); + + // Set expectations: + $this->mockedConnection + ->expects($this->never()) + ->method('insert'); + + // Exercise: + $this->inbox->parse($mockedInbox, $fakeData); + } +}