diff --git a/site/blueprints/pages/phpunit.yml b/site/blueprints/pages/phpunit.yml new file mode 100644 index 0000000..b987904 --- /dev/null +++ b/site/blueprints/pages/phpunit.yml @@ -0,0 +1,8 @@ +options: + changeSlug: false + update: true + +sections: + phpunit: + komments: + extends: sections/komments \ No newline at end of file diff --git a/tests/base/UtilsBaseTest.php b/tests/base/UtilsBaseTest.php index a157be7..2491e9b 100644 --- a/tests/base/UtilsBaseTest.php +++ b/tests/base/UtilsBaseTest.php @@ -1,10 +1,20 @@ assertEquals(100, $pendingComments[2]['spamlevel']); } - public function testNeverExpireByDefault() + // public function testNeverExpireByDefault() + // { + // $pageMock = $this->getMockBuilder(Page::class) + // ->enableProxyingToOriginalMethods(); + + // $baseUtils = new KommentBaseUtils(); + // $this->assertEquals(0, $baseUtils->kommentsAreExpired($pageMock)); + // } + + // public function testShouldNotBeExpired() + // { + // $pageMock = $this->getMockBuilder(Page::class) + // ->enableProxyingToOriginalMethods(); + + // c::set('mauricerenck.komments.auto-disable-komments', 10); + // $baseUtils = new KommentBaseUtils(); + // $this->assertEquals(0, $baseUtils->kommentsAreExpired($pageMock)); + // } + + // public function testReturnCommentCount() + // { + // $pageMock = $this->getMockBuilder(Page::class) + // ->enableProxyingToOriginalMethods(); + + // $baseUtils = new KommentBaseUtils(); + // $this->assertEquals(3, $baseUtils->getPendingCommentCount()); + // } + + // public function testReturnSpamCommentCount() + // { + // $pageMock = $this->getMockBuilder(Page::class) + // ->enableProxyingToOriginalMethods(); + + // $baseUtils = new KommentBaseUtils(); + // $this->assertEquals(1, $baseUtils->getSpamCommentCount()); + // } + + /** + * @group panel + * @testdox getCommentsOfPage - should get 3 comments + */ + public function testGetCommentsOfPage() + { + $pageMock = $this->getPageMock(); + $baseUtils = new KommentBaseUtils(); + + $expectedResult = [ + [ + 'id' => '1bfffa3f189b3c5b5d6f3ed3271d3342', + 'slug' => 'phpunit-test', + 'author' => 'Unknown user', + 'authorUrl' => '', + 'komment' => '

A regular comment of an unkown user

', + 'kommentType' => 'KOMMENT', + 'image' => 'https://www.gravatar.com/avatar/c67cfe10182e385fcd4181c06334a527', + 'title' => 'phpunit-test', + 'url' => '/panel/pages/phpunit-test', + 'published' => '2021-11-10 13:50', + 'verified' => false, + 'spamlevel' => 0, + 'status' => false + ], + [ + 'id' => 'c62bc1426c1d39eb6d8a6b4f5b3ef3ee', + 'slug' => 'phpunit-test', + 'author' => 'Verified User', + 'authorUrl' => 'https://maurice-renck.de', + 'komment' => '

A regular comment of a verified user

', + 'kommentType' => 'KOMMENT', + 'image' => 'https://www.gravatar.com/avatar/8b77c0a84579af62f82da07d9abedf56', + 'title' => 'phpunit-test', + 'url' => '/panel/pages/phpunit-test', + 'published' => '2021-11-10 13:50', + 'verified' => true, + 'spamlevel' => 0, + 'status' => true + ], + [ + 'id' => '594a3bdc4947c1a8496d2beb8a065cb1', + 'slug' => 'phpunit-test', + 'author' => 'SpamBot', + 'authorUrl' => '', + 'komment' => '

A spam comment by an evil bot

', + 'kommentType' => 'KOMMENT', + 'image' => 'https://www.gravatar.com/avatar/fa5d3b1755664ea7cb3c8ef1e00a5a52', + 'title' => 'phpunit-test', + 'url' => '/panel/pages/phpunit-test', + 'published' => '2021-11-10 13:50', + 'verified' => false, + 'spamlevel' => 100, + 'status' => false + ], + ]; + + $result = $baseUtils->getCommentsOfPage($pageMock); + $this->assertEquals($result, $expectedResult); + } + + + /** + * @group panel + * @testdox getCommentsOfPage - should get 2 pending comments, no public + */ + public function testGetCommentsOfPageNoPublic() { - $pageMock = $this->getMockBuilder(Page::class) - ->enableProxyingToOriginalMethods(); + $pageMock = $this->getPageMock(false, [ + 'kommentsInbox' => ' + - + author: Unknown user + avatar: > + https://www.gravatar.com/avatar/c67cfe10182e385fcd4181c06334a527 + authorurl: "" + authoremail: "" + kommenttype: KOMMENT + quote: "" + komment: A regular comment of an unkown user + source: https://komments.test:8890/phpunit + target: https://komments.test:8890/phpunit + mentionof: https://komments.test:8890/phpunit + property: KOMMENT + published: 2021-11-10 13:50:00 + status: "true" + verified: "false" + id: 1bfffa3f189b3c5b5d6f3ed3271d3342 + spamlevel: 0 + - + author: Verified User + avatar: > + https://www.gravatar.com/avatar/8b77c0a84579af62f82da07d9abedf56 + authorurl: https://maurice-renck.de + authoremail: "" + kommenttype: KOMMENT + quote: "" + komment: A regular comment of a verified user + source: https://komments.test:8890/phpunit + target: https://komments.test:8890/phpunit + mentionof: https://komments.test:8890/phpunit + property: KOMMENT + published: 2021-11-10 13:50:00 + status: "false" + verified: "true" + id: c62bc1426c1d39eb6d8a6b4f5b3ef3ee + spamlevel: 0 + - + author: SpamBot + avatar: > + https://www.gravatar.com/avatar/fa5d3b1755664ea7cb3c8ef1e00a5a52 + authorurl: "" + authoremail: "" + kommenttype: KOMMENT + quote: "" + komment: A spam comment by an evil bot + source: https://komments.test:8890/phpunit + target: https://komments.test:8890/phpunit + mentionof: https://komments.test:8890/phpunit + property: KOMMENT + published: 2021-11-10 13:50:00 + status: "false" + verified: "false" + id: 594a3bdc4947c1a8496d2beb8a065cb1 + spamlevel: 100' + ]); + + $expectedResult = [ + [ + 'id' => 'c62bc1426c1d39eb6d8a6b4f5b3ef3ee', + 'slug' => 'phpunit-test', + 'author' => 'Verified User', + 'authorUrl' => 'https://maurice-renck.de', + 'komment' => '

A regular comment of a verified user

', + 'kommentType' => 'KOMMENT', + 'image' => 'https://www.gravatar.com/avatar/8b77c0a84579af62f82da07d9abedf56', + 'title' => 'phpunit-test', + 'url' => '/panel/pages/phpunit-test', + 'published' => '2021-11-10 13:50', + 'verified' => true, + 'spamlevel' => 0, + 'status' => false + ], + [ + 'id' => '594a3bdc4947c1a8496d2beb8a065cb1', + 'slug' => 'phpunit-test', + 'author' => 'SpamBot', + 'authorUrl' => '', + 'komment' => '

A spam comment by an evil bot

', + 'kommentType' => 'KOMMENT', + 'image' => 'https://www.gravatar.com/avatar/fa5d3b1755664ea7cb3c8ef1e00a5a52', + 'title' => 'phpunit-test', + 'url' => '/panel/pages/phpunit-test', + 'published' => '2021-11-10 13:50', + 'verified' => false, + 'spamlevel' => 100, + 'status' => false + ], + ]; $baseUtils = new KommentBaseUtils(); - $this->assertEquals(0, $baseUtils->kommentsAreExpired($pageMock)); + $result = $baseUtils->getCommentsOfPage($pageMock, 'pending'); + + $this->assertEquals($result, $expectedResult); + $this->assertCount(2, $result); } - public function testShouldNotBeExpired() + /** + * @group panel + * @testdox getCommentsOfPage - should get 1 spam comment + */ + public function testGetCommentsOfPageOnlySpam() { - $pageMock = $this->getMockBuilder(Page::class) - ->enableProxyingToOriginalMethods(); + $pageMock = $this->getPageMock(); + $expectedResult = [ + [ + 'id' => '594a3bdc4947c1a8496d2beb8a065cb1', + 'slug' => 'phpunit-test', + 'author' => 'SpamBot', + 'authorUrl' => '', + 'komment' => '

A spam comment by an evil bot

', + 'kommentType' => 'KOMMENT', + 'image' => 'https://www.gravatar.com/avatar/fa5d3b1755664ea7cb3c8ef1e00a5a52', + 'title' => 'phpunit-test', + 'url' => '/panel/pages/phpunit-test', + 'published' => '2021-11-10 13:50', + 'verified' => false, + 'spamlevel' => 100, + 'status' => false + ], + ]; - c::set('mauricerenck.komments.auto-disable-komments', 10); $baseUtils = new KommentBaseUtils(); - $this->assertEquals(0, $baseUtils->kommentsAreExpired($pageMock)); + $result = $baseUtils->getCommentsOfPage($pageMock, 'spam'); + + $this->assertEquals($result, $expectedResult); + $this->assertCount(1, $result); } - public function testReturnCommentCount() + /** + * @group panel + * @testdox getCommentsCountOfPage - should get 2 comments + */ + public function testGetPendingCommentsCount() { - $pageMock = $this->getMockBuilder(Page::class) - ->enableProxyingToOriginalMethods(); + $pageMock = $this->getPageMock(); $baseUtils = new KommentBaseUtils(); - $this->assertEquals(3, $baseUtils->getPendingCommentCount()); + $result = $baseUtils->getCommentsCountOfPage($pageMock, 'pending'); + + $this->assertEquals(2, $result); } - public function testReturnSpamCommentCount() + /** + * @group panel + * @testdox getCommentsCountOfPage - should get 1 spam comment + */ + public function testGetSpamCommentsCount() { - $pageMock = $this->getMockBuilder(Page::class) - ->enableProxyingToOriginalMethods(); + $pageMock = $this->getPageMock(); $baseUtils = new KommentBaseUtils(); - $this->assertEquals(1, $baseUtils->getSpamCommentCount()); + $result = $baseUtils->getCommentsCountOfPage($pageMock, 'spam'); + + $this->assertEquals(1, $result); } } diff --git a/utils/TestCaseMocked.php b/utils/TestCaseMocked.php new file mode 100644 index 0000000..7e6547c --- /dev/null +++ b/utils/TestCaseMocked.php @@ -0,0 +1,107 @@ +impersonate('kirby'); + $existingPage->delete(true); + } + } + + public function tearDown(): void + { + parent::tearDown(); + + $existingPage = page('phpunit-test'); + if (!is_null($existingPage)) { + kirby()->impersonate('kirby'); + $existingPage->delete(true); + } + } + + function getPageMock($draft = false, $content = []) + { + $defaultContent = [ + 'Textfield' => "Hello World", + 'kommentsInbox' => ' +- + author: Unknown user + avatar: > + https://www.gravatar.com/avatar/c67cfe10182e385fcd4181c06334a527 + authorurl: "" + authoremail: "" + kommenttype: KOMMENT + quote: "" + komment: A regular comment of an unkown user + source: https://komments.test:8890/phpunit + target: https://komments.test:8890/phpunit + mentionof: https://komments.test:8890/phpunit + property: KOMMENT + published: 2021-11-10 13:50:00 + status: "false" + verified: "false" + id: 1bfffa3f189b3c5b5d6f3ed3271d3342 + spamlevel: 0 +- + author: Verified User + avatar: > + https://www.gravatar.com/avatar/8b77c0a84579af62f82da07d9abedf56 + authorurl: https://maurice-renck.de + authoremail: "" + kommenttype: KOMMENT + quote: "" + komment: A regular comment of a verified user + source: https://komments.test:8890/phpunit + target: https://komments.test:8890/phpunit + mentionof: https://komments.test:8890/phpunit + property: KOMMENT + published: 2021-11-10 13:50:00 + status: "true" + verified: "true" + id: c62bc1426c1d39eb6d8a6b4f5b3ef3ee + spamlevel: 0 +- + author: SpamBot + avatar: > + https://www.gravatar.com/avatar/fa5d3b1755664ea7cb3c8ef1e00a5a52 + authorurl: "" + authoremail: "" + kommenttype: KOMMENT + quote: "" + komment: A spam comment by an evil bot + source: https://komments.test:8890/phpunit + target: https://komments.test:8890/phpunit + mentionof: https://komments.test:8890/phpunit + property: KOMMENT + published: 2021-11-10 13:50:00 + status: "false" + verified: "false" + id: 594a3bdc4947c1a8496d2beb8a065cb1 + spamlevel: 100' + ]; + + $pageContent = array_merge($defaultContent, $content); + + + $pageMock = Page::factory([ + 'blueprint' => ['phpunit'], + 'content' => $pageContent, + 'dirname' => 'phpunit-test', + 'slug' => 'phpunit-test', + 'isDraft' => $draft, + 'template' => 'phpunit', + ]); + + return $pageMock; + } +} diff --git a/utils/base.php b/utils/base.php index ecd804e..e71cc4d 100644 --- a/utils/base.php +++ b/utils/base.php @@ -37,7 +37,7 @@ public function getPluginVersion() ]; } catch (\Throwable $th) { throw 'Could not get package information'; - return[ + return [ 'local' => '', 'latest' => '', 'updateAvailable' => false, @@ -75,10 +75,18 @@ public function parseKomments($komments) foreach ($komments as $komment) { if ($komment->status()->isTrue()) { switch ($komment->kommenttype()->raw()) { - case 'LIKE': $structuredKomments['likes'][] = $komment; break; - case 'REPOST': $structuredKomments['reposts'][] = $komment; break; - case 'MENTION': $structuredKomments['mentions'][] = $komment; break; - default: $replyTree[$komment->id()] = $this->transformToReply($komment); break; + case 'LIKE': + $structuredKomments['likes'][] = $komment; + break; + case 'REPOST': + $structuredKomments['reposts'][] = $komment; + break; + case 'MENTION': + $structuredKomments['mentions'][] = $komment; + break; + default: + $replyTree[$komment->id()] = $this->transformToReply($komment); + break; } } } @@ -119,41 +127,85 @@ public function buildTree($flatArray) public function getPendingKomments(): array { - $pendingKomments = []; + $pendingComments = []; $collection = site()->index(); - $komments = new Structure(); - $key = 0; foreach ($collection as $item) { - if ($item->kommentsInbox()->isNotEmpty()) { - foreach ($item->kommentsInbox()->yaml() as $komment) { - $komment['spamlevel'] = (isset($komment['spamlevel'])) ? $komment['spamlevel'] : 0; // backward compatiblity - if (($komment['status'] === 'false' || $komment['status'] === false)) { - $pendingKomments[] = [ - 'id' => $komment['id'], - 'slug' => $item->id(), - 'author' => $komment['author'], - 'authorUrl' => $komment['authorurl'], - 'komment' => kirbytext(nl2br(html($komment['komment']))), - 'kommentType' => (isset($komment['kommenttype'])) ? $komment['kommenttype'] : 'komment', // backward compatiblity - 'image' => $komment['avatar'], - 'title' => (string) $item->title(), - 'url' => $item->panel()->url(), - 'published' => date('Y-m-d H:i', strtotime($komment['published'])), - 'verified' => ($komment['verified'] === true || $komment['verified'] === 'true') ? true : false, - 'spamlevel' => $komment['spamlevel'], - 'status' => ($komment['status'] === true || $komment['status'] === 'true') ? true : false, - ]; - } - } - } + $pendingComments = array_merge($pendingComments, $this->getCommentsOfPage($item, 'pending')); } - usort($pendingKomments, function ($a, $b) { + usort($pendingComments, function ($a, $b) { return $b['published'] <=> $a['published']; }); - return $pendingKomments; + return $pendingComments; + } + + public function getCommentsOfPage($page, $filter = 'all') + { + if ($page->kommentsInbox()->isEmpty()) { + return []; + } + + $comments = []; + $allPageComments = $page->kommentsInbox()->toStructure(); + $filteredComments = []; + + switch($filter) { + case 'pending': + $filteredComments = $allPageComments->filterBy('status', 'false'); + break; + case 'spam': + $filteredComments = $allPageComments->filterBy('spamlevel', '!=', '0'); + break; + default: + $filteredComments = $allPageComments; + break; + } + + foreach ($filteredComments as $komment) { + $comments[] = [ + 'id' => $komment->id(), + 'slug' => $page->id(), + 'author' => $komment->author()->value(), + 'authorUrl' => $komment->authorUrl()->value(), + 'komment' => kirbytext(nl2br(html($komment->komment()))), + 'kommentType' => $komment->kommenttype()->value() ?? 'komment', + 'image' => $komment->avatar()->value(), + 'title' => $page->title()->value(), + 'url' => $page->panel()->url(), + 'published' => date('Y-m-d H:i', strtotime($komment->published())), + 'verified' => $komment->verified()->toBool(false), + 'spamlevel' => $komment->spamlevel()->value() ?? 0, + 'status' => $komment->status()->toBool(false), + ]; + } + + return $comments; + } + + public function getCommentsCountOfPage($page, $filter = 'all'): int + { + if ($page->kommentsInbox()->isEmpty()) { + return 0; + } + + $allPageComments = $page->kommentsInbox()->toStructure(); + $filteredComments = []; + + switch($filter) { + case 'pending': + $filteredComments = $allPageComments->filterBy('status', 'false'); + break; + case 'spam': + $filteredComments = $allPageComments->filterBy('spamlevel', '!=', '0'); + break; + default: + $filteredComments = $allPageComments; + break; + } + + return $filteredComments->count(); } public function getPendingCommentCount(): int @@ -162,13 +214,7 @@ public function getPendingCommentCount(): int $pendingKomments = 0; foreach ($collection as $item) { - if ($item->kommentsInbox()->isNotEmpty()) { - foreach ($item->kommentsInbox()->yaml() as $komment) { - if (($komment['status'] === 'false' || $komment['status'] === false)) { - $pendingKomments++; - } - } - } + $pendingKomments += $this->getCommentsCountOfPage($item, 'pending'); } return $pendingKomments; @@ -180,14 +226,7 @@ public function getSpamCommentCount(): int $spamComments = 0; foreach ($collection as $item) { - if ($item->kommentsInbox()->isNotEmpty()) { - foreach ($item->kommentsInbox()->yaml() as $komment) { - $komment['spamlevel'] = (isset($komment['spamlevel'])) ? $komment['spamlevel'] : 0; // backward compatiblity - if ($komment['spamlevel'] > 0) { - $spamComments++; - } - } - } + $spamComments += $this->getCommentsCountOfPage($item, 'spam'); } return $spamComments; diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 7df2f63..27bbbb3 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -18,5 +18,6 @@ 'mauricerenck\\Komments\\KommentNotificationUtils' => $baseDir . '/utils/notifications.php', 'mauricerenck\\Komments\\KommentReceiver' => $baseDir . '/utils/receiveKomment.php', 'mauricerenck\\Komments\\MastodonSender' => $baseDir . '/utils/sendMastodon.php', + 'mauricerenck\\Komments\\TestCaseMocked' => $baseDir . '/utils/TestCaseMocked.php', 'mauricerenck\\Komments\\WebmentionSender' => $baseDir . '/utils/sendWebmention.php', ); diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 7e8ae27..f4a0cb1 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -55,6 +55,7 @@ class ComposerStaticInit60056fecad9d217d765a4dde54e59e86 'mauricerenck\\Komments\\KommentNotificationUtils' => __DIR__ . '/../..' . '/utils/notifications.php', 'mauricerenck\\Komments\\KommentReceiver' => __DIR__ . '/../..' . '/utils/receiveKomment.php', 'mauricerenck\\Komments\\MastodonSender' => __DIR__ . '/../..' . '/utils/sendMastodon.php', + 'mauricerenck\\Komments\\TestCaseMocked' => __DIR__ . '/../..' . '/utils/TestCaseMocked.php', 'mauricerenck\\Komments\\WebmentionSender' => __DIR__ . '/../..' . '/utils/sendWebmention.php', ); diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php index c52146f..58cb44f 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -1,8 +1,8 @@ array( 'name' => 'mauricerenck/komments', - 'pretty_version' => '1.12.2', - 'version' => '1.12.2.0', + 'pretty_version' => '1.13.1', + 'version' => '1.13.1.0', 'reference' => NULL, 'type' => 'kirby-plugin', 'install_path' => __DIR__ . '/../../', @@ -29,8 +29,8 @@ 'dev_requirement' => false, ), 'mauricerenck/komments' => array( - 'pretty_version' => '1.12.2', - 'version' => '1.12.2.0', + 'pretty_version' => '1.13.1', + 'version' => '1.13.1.0', 'reference' => NULL, 'type' => 'kirby-plugin', 'install_path' => __DIR__ . '/../../',