diff --git a/tests/utils/ReceiveKommentTest.php b/tests/utils/ReceiveKommentTest.php index a831b02..2491408 100644 --- a/tests/utils/ReceiveKommentTest.php +++ b/tests/utils/ReceiveKommentTest.php @@ -1,4 +1,5 @@ 'This is my comment', 'url' => '', 'email' => 'test@phpunit.de', @@ -69,7 +69,7 @@ public function testSetConvertToWebmention() 'replyTo' => '', 'replyHandle' => '', 'cts' => 10, - ); + ]; $expectedResult = [ 'type' => 'KOMMENT', @@ -90,7 +90,48 @@ public function testSetConvertToWebmention() $kommentReceiver = new KommentReceiver(); $webmention = $kommentReceiver->convertToWebmention($commentMock, page('phpunit')); - + $this->assertEquals($expectedResult, $webmention); } + + public function testGetAuthorData() + { + $kommentReceiver = new KommentReceiver(); + + $expectedResult = [ + 'name' => 'John Doe', + 'avatar' => 'https://web.site/avatar.jpg', + 'url' => 'https://web.site', + 'email' => 'hello@test.tld' + ]; + + $author = $kommentReceiver->getAuthorData([ + 'name' => 'John Doe', + 'avatar' => 'https://web.site/avatar.jpg', + 'url' => 'https://web.site', + 'email' => 'hello@test.tld' + ]); + + $this->assertEquals($expectedResult, $author); + } + + public function testGetAuthorDataHandlesMissingData() + { + $kommentReceiver = new KommentReceiver(); + + $expectedResult = [ + 'name' => 'John Doe', + 'avatar' => 'https://web.site/avatar.jpg', + 'url' => null, + 'email' => null + ]; + + $author = $kommentReceiver->getAuthorData([ + 'name' => 'John Doe', + 'avatar' => 'https://web.site/avatar.jpg', + 'email' => '' + ]); + + $this->assertEquals($expectedResult, $author); + } } diff --git a/utils/receiveKomment.php b/utils/receiveKomment.php index af7453d..1e905a7 100644 --- a/utils/receiveKomment.php +++ b/utils/receiveKomment.php @@ -45,13 +45,14 @@ public function convertToWebmention($formData, $targetPage) public function createKomment($webmention, $spamlevel = 0, $isVerified = false, $autoPublish = false) { $publishComments = $isVerified || $autoPublish; + $author = $this->getAuthorData($webmention['author']); return [ - 'id' => md5($webmention['target'] . $webmention['author']['name'] . $webmention['published']), - 'avatar' => $this->setUrl($webmention['author']['avatar']), - 'author' => $webmention['author']['name'], - 'authorUrl' => $this->setUrl($webmention['author']['url']), - 'authorEmail' => $this->setEmail($webmention['author']['email']), + 'id' => md5($webmention['target'] . $author['name'] . $webmention['published']), + 'avatar' => $this->setUrl($author['avatar']), + 'author' => $author['name'], + 'authorUrl' => $this->setUrl($author['url']), + 'authorEmail' => $this->setEmail($author['email']), 'source' => $this->setUrl($webmention['source']), 'target' => $this->setUrl($webmention['target']), 'mentionOf' => (!isset($webmention['mentionOf']) || is_null($webmention['mentionOf'])) ? $this->setUrl($webmention['target']) : $webmention['mentionOf'], @@ -66,6 +67,15 @@ public function createKomment($webmention, $spamlevel = 0, $isVerified = false, ]; } + public function getAuthorData(array $author) { + return [ + 'name' => $author['name'] ?? null, + 'avatar' => $author['avatar'] ?? null, + 'url' => $author['url'] ?? null, + 'email' => $author['email'] ?? null, + ]; + } + public function getPageFromUrl(string $url) { if (V::url($url)) {