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, + 'mentionof' => 'https://komments.test:8890/phpunit', + 'replies' => [], + ], + ]; + + $result = $commentModeration->getCommentsOfPage($pageMock, 'published'); + $this->assertEquals($result, $expectedResult); + } /** * @group panel @@ -165,7 +146,7 @@ public function testGetCommentsOfPageNoPublic() { $pageMock = $this->getPageMock(false, [ 'kommentsInbox' => ' - - + - author: Unknown user avatar: > https://www.gravatar.com/avatar/c67cfe10182e385fcd4181c06334a527 @@ -183,7 +164,7 @@ public function testGetCommentsOfPageNoPublic() verified: "false" id: 1bfffa3f189b3c5b5d6f3ed3271d3342 spamlevel: 0 - - + - author: Verified User avatar: > https://www.gravatar.com/avatar/8b77c0a84579af62f82da07d9abedf56 @@ -201,7 +182,7 @@ public function testGetCommentsOfPageNoPublic() verified: "true" id: c62bc1426c1d39eb6d8a6b4f5b3ef3ee spamlevel: 0 - - + - author: SpamBot avatar: > https://www.gravatar.com/avatar/fa5d3b1755664ea7cb3c8ef1e00a5a52 @@ -218,7 +199,7 @@ public function testGetCommentsOfPageNoPublic() status: "false" verified: "false" id: 594a3bdc4947c1a8496d2beb8a065cb1 - spamlevel: 100' + spamlevel: 100', ]); $expectedResult = [ @@ -235,7 +216,9 @@ public function testGetCommentsOfPageNoPublic() 'published' => '2021-11-10 13:50', 'verified' => true, 'spamlevel' => 0, - 'status' => false + 'status' => false, + 'mentionof' => 'https://komments.test:8890/phpunit', + 'replies' => [], ], [ 'id' => '594a3bdc4947c1a8496d2beb8a065cb1', @@ -250,12 +233,14 @@ public function testGetCommentsOfPageNoPublic() 'published' => '2021-11-10 13:50', 'verified' => false, 'spamlevel' => 100, - 'status' => false + 'status' => false, + 'mentionof' => 'https://komments.test:8890/phpunit', + 'replies' => [], ], ]; - $baseUtils = new KommentBaseUtils(); - $result = $baseUtils->getCommentsOfPage($pageMock, 'pending'); + $commentModeration = new KommentModeration(); + $result = $commentModeration->getCommentsOfPage($pageMock, 'pending'); $this->assertEquals($result, $expectedResult); $this->assertCount(2, $result); @@ -282,12 +267,14 @@ public function testGetCommentsOfPageOnlySpam() 'published' => '2021-11-10 13:50', 'verified' => false, 'spamlevel' => 100, - 'status' => false + 'status' => false, + 'mentionof' => 'https://komments.test:8890/phpunit', + 'replies' => [], ], ]; - $baseUtils = new KommentBaseUtils(); - $result = $baseUtils->getCommentsOfPage($pageMock, 'spam'); + $commentModeration = new KommentModeration(); + $result = $commentModeration->getCommentsOfPage($pageMock, 'spam'); $this->assertEquals($result, $expectedResult); $this->assertCount(1, $result); @@ -295,29 +282,54 @@ public function testGetCommentsOfPageOnlySpam() /** * @group panel - * @testdox getCommentsCountOfPage - should get 2 comments + * @testdox convertInboxToCommentArray - should convert comment to panel comment array */ - public function testGetPendingCommentsCount() + public function testConvertSingleInboxCommentToCommentArray() { $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, + 'mentionof' => 'https://komments.test:8890/phpunit', + 'replies' => [], + ], + ]; + $commentModeration = new KommentModeration(); $baseUtils = new KommentBaseUtils(); - $result = $baseUtils->getCommentsCountOfPage($pageMock, 'pending'); + $inbox = $baseUtils->getAllCommentsOfPage($pageMock); + $filteredInbox = $baseUtils->filterCommentsByStatus($inbox, 'spam'); - $this->assertEquals(2, $result); + $result = $commentModeration->convertInboxToCommentArray($filteredInbox, $pageMock); + + $this->assertEquals($result, $expectedResult); } /** * @group panel - * @testdox getCommentsCountOfPage - should get 1 spam comment + * @testdox convertInboxToCommentArray - should convert to panel comment array */ - public function testGetSpamCommentsCount() + public function testConvertInboxToCommentArray() { $pageMock = $this->getPageMock(); - + $commentModeration = new KommentModeration(); $baseUtils = new KommentBaseUtils(); - $result = $baseUtils->getCommentsCountOfPage($pageMock, 'spam'); - $this->assertEquals(1, $result); + $inbox = $baseUtils->getAllCommentsOfPage($pageMock); + $result = $commentModeration->convertInboxToCommentArray($inbox, $pageMock); + + $this->assertEquals(count($result), 3); } } diff --git a/tests/utils/KommentsFrontendTest.php b/tests/utils/KommentsFrontendTest.php new file mode 100644 index 0000000..891ef01 --- /dev/null +++ b/tests/utils/KommentsFrontendTest.php @@ -0,0 +1,66 @@ +frontendUtilsMock = Mockery::mock('mauricerenck\Komments\KommentsFrontend[getAllCommentsOfPage]'); + $this->baseUtilsMock = Mockery::mock('mauricerenck\Komments\KommentBaseUtils[getAllLanguages]'); + } + + /** + * @group frontend + * @testdox kommentsAreExpired - should be expired + */ + public function testKommentsAreExpired() + { + $pageMock = $this->getPageMock(false, ['date' => '2019-01-01']); + $kommentsFrontend = new KommentsFrontend(1, 'date'); + $result = $kommentsFrontend->kommentsAreExpired($pageMock); + $this->assertTrue($result); + } + + /** + * @group frontend + * @testdox kommentsAreExpired - should not be expired + */ + public function testKommentsAreNotExpired() + { + $pageMock = $this->getPageMock(false, ['date' => '2035-01-01']); + $kommentsFrontend = new KommentsFrontend(1, 'date'); + $result = $kommentsFrontend->kommentsAreExpired($pageMock); + $this->assertFalse($result); + } + + /** + * @group frontend + * @testdox kommentsAreExpired - should handle missing date field + */ + public function testKommentsAreExpiredMissingDateField() + { + $pageMock = $this->getPageMock(false, ['date' => '2035-01-01']); + $kommentsFrontend = new KommentsFrontend(1, 'dateNotExisting'); + $result = $kommentsFrontend->kommentsAreExpired($pageMock); + $this->assertFalse($result); + } + + /** + * @group frontend + * @testdox kommentsAreExpired - should handle wrong type of date field + */ + public function testKommentsAreExpiredWrongTypeOfDateField() + { + $pageMock = $this->getPageMock(false, ['date' => '2035-01-01']); + $kommentsFrontend = new KommentsFrontend(1, 'textfield'); + $result = $kommentsFrontend->kommentsAreExpired($pageMock); + $this->assertFalse($result); + } +} diff --git a/tests/utils/UtilsBaseTest.php b/tests/utils/UtilsBaseTest.php new file mode 100644 index 0000000..c5af48e --- /dev/null +++ b/tests/utils/UtilsBaseTest.php @@ -0,0 +1,795 @@ +assertEquals(false, $baseUtils->getPageFromSlug('fake/page')); + } + + /** + * @group base + * @testdox getPageFromSlug - should get a page with the slug 'phpunit' + */ + public function testGetPageFromSlug() + { + $pageMock = $this->getPageMock(); + $baseUtils = new KommentBaseUtils(); + + $result = $baseUtils->getPageFromSlug('phpunit'); + $this->assertEquals($result->title()->raw(), 'phpunit'); + } + + /** + * @group base + * @testdox getPageFromSlug - should handle unkown page + */ + public function testGetPageUnknownFromSlug() + { + $pageMock = $this->getPageMock(); + $baseUtils = new KommentBaseUtils(); + + $result = $baseUtils->getPageFromSlug('unknown'); + $this->assertFalse($result); + } + + /** + * @group base + * @testdox getAllCommentsOfPage - should get all inboxes of page + */ + public function testGetAllInboxesOfMultilangPage() + { + $pageMock = $this->getPageMock(); + $baseUtils = new KommentBaseUtils(); + $result = $baseUtils->getAllCommentsOfPage($pageMock); + + $this->assertEquals(3, $result->count()); + } + + /** + * @group base + * @testdox getAllCommentsOfPage - should get a single inbox with language de + */ + public function testGetAllCommentsOfSingleLangPage() + { + $pageMock = $this->getPageMock(); + + $baseUtils = new KommentBaseUtils(['de']); + $result = $baseUtils->getAllCommentsOfPage($pageMock); + + $this->assertEquals(1, $result->count()); + } + + /** + * @group base + * @testdox getAllCommentsOfPage - should get a single inbox without language + */ + public function testGetAllCommentsOfNoLangPage() + { + $pageMock = $this->getPageMock(); + + $kommentsFrontend = new KommentBaseUtils([]); + $result = $kommentsFrontend->getAllCommentsOfPage($pageMock); + + $this->assertEquals(3, $result->count()); + } + + /** + * @group base + * @testdox filterCommentsByType - should get all types of comments + */ + public function testFilterCommentsByType() + { + $pageMock = $pageMock = $this->getPageMock(false, [ + 'kommentsInbox' => ' + - + author: Unknown user + avatar: > + https://www.gravatar.com/avatar/c67cfe10182e385fcd4181c06334a527 + authorurl: "" + authoremail: "" + kommenttype: REPLY + 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: LIKE + 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', + ]); + + $baseUtils = new KommentBaseUtils(); + $inbox = $baseUtils->getAllCommentsOfPage($pageMock); + $result = $baseUtils->filterCommentsByType($inbox); + + $this->assertEquals(3, $result->count()); + } + + /** + * @group base + * @testdox filterCommentsByType - should get comments only + */ + public function testFilterCommentsByTypeKomment() + { + $pageMock = $pageMock = $this->getPageMock(false, [ + 'kommentsInbox' => ' + - + author: Unknown user + avatar: > + https://www.gravatar.com/avatar/c67cfe10182e385fcd4181c06334a527 + authorurl: "" + authoremail: "" + kommenttype: REPLY + 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: LIKE + 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', + ]); + + $baseUtils = new KommentBaseUtils(); + $inbox = $baseUtils->getAllCommentsOfPage($pageMock); + $result = $baseUtils->filterCommentsByType($inbox, 'KOMMENT'); + + $this->assertEquals(1, $result->count()); + } + + /** + * @group base + * @testdox filterCommentsByType - should get likes only + */ + public function testFilterCommentsByTypeLike() + { + $pageMock = $pageMock = $this->getPageMock(false, [ + 'kommentsInbox' => ' + - + author: Unknown user + avatar: > + https://www.gravatar.com/avatar/c67cfe10182e385fcd4181c06334a527 + authorurl: "" + authoremail: "" + kommenttype: REPLY + 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: LIKE + 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', + ]); + + $baseUtils = new KommentBaseUtils(); + $inbox = $baseUtils->getAllCommentsOfPage($pageMock); + $result = $baseUtils->filterCommentsByType($inbox, 'LIKE'); + + $this->assertEquals(1, $result->count()); + } + + /** + * @group base + * @testdox filterCommentsByType - should get replies only + */ + public function testFilterCommentsByTypeReply() + { + $pageMock = $pageMock = $this->getPageMock(false, [ + 'kommentsInbox' => ' + - + author: Unknown user + avatar: > + https://www.gravatar.com/avatar/c67cfe10182e385fcd4181c06334a527 + authorurl: "" + authoremail: "" + kommenttype: REPLY + 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: LIKE + 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', + ]); + + $baseUtils = new KommentBaseUtils(); + $inbox = $baseUtils->getAllCommentsOfPage($pageMock); + $result = $baseUtils->filterCommentsByType($inbox, 'REPLY'); + + $this->assertEquals(1, $result->count()); + } + + /** + * @group base + * @testdox filterCommentsByStatus - should get all comments + */ + public function testFilterCommentsByStatus() + { + $pageMock = $pageMock = $this->getPageMock(false, [ + 'kommentsInbox' => ' + - + author: Unknown user + avatar: > + https://www.gravatar.com/avatar/c67cfe10182e385fcd4181c06334a527 + authorurl: "" + authoremail: "" + kommenttype: REPLY + 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: LIKE + 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', + ]); + + $baseUtils = new KommentBaseUtils(); + $inbox = $baseUtils->getAllCommentsOfPage($pageMock); + $result = $baseUtils->filterCommentsByStatus($inbox); + + $this->assertEquals(3, $result->count()); + } + + /** + * @group base + * @testdox filterCommentsByStatus - should get all published comments + */ + public function testFilterCommentsByStatusPublished() + { + $pageMock = $pageMock = $this->getPageMock(false, [ + 'kommentsInbox' => ' + - + author: Unknown user + avatar: > + https://www.gravatar.com/avatar/c67cfe10182e385fcd4181c06334a527 + authorurl: "" + authoremail: "" + kommenttype: REPLY + 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: LIKE + 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', + ]); + + $baseUtils = new KommentBaseUtils(); + $inbox = $baseUtils->getAllCommentsOfPage($pageMock); + $result = $baseUtils->filterCommentsByStatus($inbox, 'published'); + + $this->assertEquals(1, $result->count()); + } + + /** + * @group base + * @testdox filterCommentsByStatus - should get all pending comments + */ + public function testFilterCommentsByStatusPending() + { + $pageMock = $pageMock = $this->getPageMock(false, [ + 'kommentsInbox' => ' + - + author: Unknown user + avatar: > + https://www.gravatar.com/avatar/c67cfe10182e385fcd4181c06334a527 + authorurl: "" + authoremail: "" + kommenttype: REPLY + 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: LIKE + 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', + ]); + + $baseUtils = new KommentBaseUtils(); + $inbox = $baseUtils->getAllCommentsOfPage($pageMock); + $result = $baseUtils->filterCommentsByStatus($inbox, 'pending'); + + $this->assertEquals(2, $result->count()); + } + + /** + * @group base + * @testdox filterCommentsByStatus - should get all spam comments + */ + public function testFilterCommentsByStatusSpam() + { + $pageMock = $pageMock = $this->getPageMock(false, [ + 'kommentsInbox' => ' + - + author: Unknown user + avatar: > + https://www.gravatar.com/avatar/c67cfe10182e385fcd4181c06334a527 + authorurl: "" + authoremail: "" + kommenttype: REPLY + 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: LIKE + 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', + ]); + + $baseUtils = new KommentBaseUtils(); + $inbox = $baseUtils->getAllCommentsOfPage($pageMock); + $result = $baseUtils->filterCommentsByStatus($inbox, 'spam'); + + $this->assertEquals(1, $result->count()); + } + + /** + * @group panel + * @testdox getCommentsCountOfPage - should count 3 comments + */ + public function testGetCommentsCount() + { + $pageMock = $this->getPageMock(); + + $baseUtils = new KommentBaseUtils(); + $result = $baseUtils->getCommentsCountOfPage($pageMock); + + $this->assertEquals(3, $result); + } + + /** + * @group panel + * @testdox getCommentsCountOfPage - should count 1 spam comment + */ + public function testGetCommentsCountSpam() + { + $pageMock = $this->getPageMock(); + + $baseUtils = new KommentBaseUtils(); + $result = $baseUtils->getCommentsCountOfPage($pageMock, 'spam'); + + $this->assertEquals(1, $result); + } + + /** + * @group panel + * @testdox getCommentsCountOfPage - should count 2 pending comment + */ + public function testGetCommentsCountPending() + { + $pageMock = $this->getPageMock(); + + $baseUtils = new KommentBaseUtils(); + $result = $baseUtils->getCommentsCountOfPage($pageMock, 'pending'); + + $this->assertEquals(2, $result); + } + /** + * @group base + * @testdox getInboxByLanguage - should get english inbox with 3 comments + */ + public function testGetInboxByLanguageEn() + { + $pageMock = $this->getPageMock(); + + $baseUtils = new KommentBaseUtils(); + $result = $baseUtils->getInboxByLanguage($pageMock, 'en'); + $structure = $result->toStructure(); + + $this->assertEquals(3, $structure->count()); + } + + /** + * @group base + * @testdox getInboxByLanguage - should get german inbox with 1 comments + */ + public function testGetInboxByLanguageDe() + { + $pageMock = $this->getPageMock(); + + $baseUtils = new KommentBaseUtils(); + $result = $baseUtils->getInboxByLanguage($pageMock, 'de'); + $structure = $result->toStructure(); + + $this->assertEquals(1, $structure->count()); + } + + /** + * @group base + * @testdox getInboxByLanguage - should handle query without language + */ + public function testGetInboxByLanguageUnknown() + { + $pageMock = $this->getPageMock(); + + $baseUtils = new KommentBaseUtils(); + $result = $baseUtils->getInboxByLanguage($pageMock, null); + $structure = $result->toStructure(); + + $this->assertEquals(3, $structure->count()); + } + + /** + * @group base + * @testdox getCommentsCountOfPage - should get 3 comments + */ + public function testGetCommentsCountOfPage() + { + $pageMock = $this->getPageMock(); + + $baseUtils = new KommentBaseUtils(); + $result = $baseUtils->getCommentsCountOfPage($pageMock); + + $this->assertEquals(3, $result); + } + + /** + * @group base + * @testdox getCommentsCountOfPage - should count 1 published comment + */ + public function testGetCommentsCountOfPagePublished() + { + $pageMock = $this->getPageMock(); + + $baseUtils = new KommentBaseUtils(); + $result = $baseUtils->getCommentsCountOfPage($pageMock, 'published'); + + $this->assertEquals(1, $result); + } + + /** + * @group base + * @testdox getCommentsCountOfPage - should count 1 pending comment + */ + public function testGetCommentsCountOfPagePending() + { + $pageMock = $this->getPageMock(); + + $baseUtils = new KommentBaseUtils(); + $result = $baseUtils->getCommentsCountOfPage($pageMock, 'pending'); + + $this->assertEquals(2, $result); + } + + /** + * @group base + * @testdox getCommentsCountOfPage - should count 1 spam comment + */ + public function testGetCommentsCountOfPageSpam() + { + $pageMock = $this->getPageMock(); + + $baseUtils = new KommentBaseUtils(); + $result = $baseUtils->getCommentsCountOfPage($pageMock, 'spam'); + + $this->assertEquals(1, $result); + } +} diff --git a/utils/Frontend.php b/utils/Frontend.php new file mode 100644 index 0000000..d34980b --- /dev/null +++ b/utils/Frontend.php @@ -0,0 +1,132 @@ +baseUtils = new KommentBaseUtils(); + + $this->expireAfterNumOfDays = $expireAfterNumOfDays ?? option('mauricerenck.komments.auto-disable-komments', 0); + $this->dateField = $dateField ?? option('mauricerenck.komments.auto-disable-komments-datefield', 'date'); + } + + public function kommentsAreExpired($page) + { + if ($this->expireAfterNumOfDays === 0) { + return false; + } + + $dateFieldName = $this->dateField; + + if (is_null($page->$dateFieldName()) || $page->$dateFieldName()->exists() === false) { + return false; + } + + $publishDate = $page->$dateFieldName()->toDate(); + + if ($publishDate === 0) { + return false; + } + + $now = time(); + + if ($now - $publishDate > $this->expireAfterNumOfDays * 24 * 60 * 60) { + return true; + } + + return false; + } + + // TODO write tests + public function getCommentList($page): array + { + $inboxes = $this->baseUtils->getAllCommentsOfPage($page); + + $commentList = [ + 'likes' => new Structure(), + 'reposts' => new Structure(), + 'replies' => new Structure(), + 'mentions' => new Structure(), + 'comments' => new Structure(), + ]; + + $filteredInbox = $this->baseUtils->filterCommentsByType($inboxes, 'LIKE'); + if ($filteredInbox->count() > 0) { + $commentList['likes']->add($filteredInbox); + } + + $filteredInbox = $this->baseUtils->filterCommentsByType($inboxes, 'REPOST'); + if ($filteredInbox->count() > 0) { + $commentList['reposts']->add($filteredInbox); + } + + $filteredInbox = $this->baseUtils->filterCommentsByType($inboxes, 'MENTION'); + if ($filteredInbox->count() > 0) { + $commentList['mentions']->add($filteredInbox); + } + + $filteredInbox = $this->baseUtils->filterCommentsByType($inboxes, 'REPLY'); + if ($filteredInbox->count() > 0) { + $commentList['replies']->add($filteredInbox); + } + + $filteredInbox = $this->baseUtils->filterCommentsByType($inboxes, 'KOMMENT'); + if ($filteredInbox->count() > 0) { + $commentList['comments']->add($filteredInbox); + } + + return $commentList; + } + + // FIXME deprecated ? + public function convertToNestedComments($comments) + { + $nestedComments = []; + foreach ($comments as $comment) { + $nestedComments[$comment['id']] = $comment; + } + + foreach ($nestedComments as $reply) { + $mentionOf = $reply['mentionof']; + if (!empty($nestedComments[$mentionOf])) { + $nestedComments[$mentionOf]['replies'][] = $reply['id']; + } + } + + return $this->buildTree($nestedComments); + } + + // FIXME deprecated ? + public function buildTree($flatArray) + { + $tree = []; + + foreach ($flatArray as $key => $flat) { + $nodes = []; + $tree = []; + foreach ($flatArray as $key => &$node) { + $node['replies'] = []; + $id = $node['id']; + $parent_id = $node['mentionof']; + $nodes[$id] = &$node; + if (array_key_exists($parent_id, $nodes)) { + $nodes[$parent_id]['replies'][] = &$node; + } else { + $tree[] = &$node; + } + } + } + return $tree; + } + + private function transformToReply($komment) + { + deprecated('`transformToReply()` is deprecated. `transformToReply()` will be removed in future versions.'); + } +} diff --git a/utils/TestCaseMocked.php b/utils/TestCaseMocked.php index 7e6547c..1c29f9d 100644 --- a/utils/TestCaseMocked.php +++ b/utils/TestCaseMocked.php @@ -3,6 +3,7 @@ namespace mauricerenck\Komments; use Kirby\Cms\Page; +use Kirby\Uuid\Uuid; use PHPUnit\Framework\TestCase; class TestCaseMocked extends TestCase @@ -29,77 +30,116 @@ public function tearDown(): void } } - function getPageMock($draft = false, $content = []) + function getPageMock($draft = false, $contentEN = [], $contentDE = []) { - $defaultContent = [ + $defaultContentEN = [ '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' + - + 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); + $pageContentEN = array_merge($defaultContentEN, $contentEN); + + $defaultContentDE = [ + '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: "true" + verified: "false" + id: 1bfffa3f189b3c5b5d6f3ed3271d3342 + spamlevel: 0' +]; + + $pageContentDE = array_merge($defaultContentDE, $contentDE); $pageMock = Page::factory([ 'blueprint' => ['phpunit'], - 'content' => $pageContent, 'dirname' => 'phpunit-test', 'slug' => 'phpunit-test', 'isDraft' => $draft, 'template' => 'phpunit', + 'translations' => [ + 'en' => [ + 'code' => 'en', + 'content' => $pageContentEN, + 'uuid' => Uuid::generate(), + ], + 'de' => [ + 'code' => 'de', + 'content' => $pageContentDE, + ], + 'fr' => [ + 'code' => 'fr', + 'content' => [], + ], + ] ]); return $pageMock; diff --git a/utils/base.php b/utils/base.php index a583ee0..971ab8c 100644 --- a/utils/base.php +++ b/utils/base.php @@ -2,13 +2,16 @@ namespace mauricerenck\Komments; -use json_decode; -use Structure; -use f; -use Kirby\Http\Remote; +use Kirby\Cms\Structure; class KommentBaseUtils { + public function __construct(private ?array $languageCodes = null) + { + $this->languageCodes = $languageCodes; + } + + // TESTED public function getPageFromSlug(string $pageSlug) { $page = page($pageSlug); @@ -19,213 +22,107 @@ public function getPageFromSlug(string $pageSlug) return $page; } - public function kommentsAreExpired($page) + // NO NEED TO TEST + public function getAllLanguages() { - $expireAfterNumOfDays = option('mauricerenck.komments.auto-disable-komments', 0); - - if ($expireAfterNumOfDays === 0) { - return false; + if (!is_null($this->languageCodes)) { + return $this->languageCodes; } - $dateField = option('mauricerenck.komments.auto-disable-komments-datefield', 'date'); - $publishDate = $page->$dateField()->toDate(); - $now = time(); + // this method is used for easy mocking in tests + $languages = kirby()->languages(); - if (($now - $publishDate) > $expireAfterNumOfDays * 24 * 60 * 60) { - return true; + $languageCodes = []; + foreach ($languages as $language) { + $languageCodes[] = $language->code(); } - return false; + return $languageCodes; } - public function parseKomments($komments) + // TESTED + public function getAllCommentsOfPage($page) { - $structuredKomments = ['replies' => [], 'reposts' => [], 'mentions' => [], 'likes' => []]; - $replies = []; - $replyTree = []; - $komments = $komments->toStructure(); - - 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; - } - } - } - - foreach ($replyTree as $reply) { - $mentionOf = $reply['mentionof']; - if (!empty($replyTree[$mentionOf])) { - $replyTree[$mentionOf]['replies'][] = $reply['id']; - } - } + $languageCodes = $this->getAllLanguages(); + $inboxes = new Structure(); - $structuredKomments['replies'] = $this->buildTree($replyTree); - - return $structuredKomments; - } + if (count($languageCodes) === 0) { + $inbox = $this->getInboxByLanguage($page); + $inboxes->add($inbox->toStructure()); - public function buildTree($flatArray) - { - $tree = []; - - foreach ($flatArray as $key => $flat) { - $nodes = []; - $tree = []; - foreach ($flatArray as $key => &$node) { - $node['replies'] = []; - $id = $node['id']; - $parent_id = $node['mentionof']; - $nodes[$id] = &$node; - if (array_key_exists($parent_id, $nodes)) { - $nodes[$parent_id]['replies'][] = &$node; - } else { - $tree[] = &$node; - } - } + return $inboxes; } - return $tree; - } - - public function getPendingKomments(): array - { - $pendingComments = []; - $collection = site()->index(); - foreach ($collection as $item) { - $pendingComments = array_merge($pendingComments, $this->getCommentsOfPage($item, 'pending')); + foreach ($languageCodes as $language) { + $inbox = $this->getInboxByLanguage($page, $language); + $inboxes->add($inbox->toStructure()); } - usort($pendingComments, function ($a, $b) { - return $b['published'] <=> $a['published']; - }); - - return $pendingComments; + return $inboxes; } - public function getCommentsOfPage($page, $filter = 'all') + // TESTED + public function filterCommentsByType($inbox, $type = 'all') { - if ($page->kommentsInbox()->isEmpty()) { - return []; + if ($type === 'all') { + return $inbox; } - $comments = []; - $allPageComments = $page->kommentsInbox()->toStructure(); - $filteredComments = []; + return $inbox->filterBy('kommentType', $type); + } - switch($filter) { + // TESTED + public function filterCommentsByStatus($inbox, $status = 'all') + { + switch ($status) { + case 'published': + return $inbox->filterBy('status', 'true'); case 'pending': - $filteredComments = $allPageComments->filterBy('status', 'false'); - break; + return $inbox->filterBy('status', 'false'); case 'spam': - $filteredComments = $allPageComments->filterBy('spamlevel', '!=', '0'); - break; + return $inbox->filterBy('spamlevel', '!=', '0'); default: - $filteredComments = $allPageComments; - break; + return $inbox; } - - 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 + // TESTED + public function getInboxByLanguage($page, $language = null) { if ($page->kommentsInbox()->isEmpty()) { - return 0; + return null; } - $allPageComments = $page->kommentsInbox()->toStructure(); - $filteredComments = []; + if (is_null($language)) { + return $page->kommentsInbox(); + } - switch($filter) { - case 'pending': - $filteredComments = $allPageComments->filterBy('status', 'false'); - break; - case 'spam': - $filteredComments = $allPageComments->filterBy('spamlevel', '!=', '0'); - break; - default: - $filteredComments = $allPageComments; - break; + if (is_string($language)) { + return $page->content($language)->kommentsInbox(); } - return $filteredComments->count(); + return null; } - public function getPendingCommentCount(): int + // TESTED + public function getCommentsCountOfPage($page, $filter = 'all'): int { - $collection = site()->index(); - $pendingKomments = 0; + $inbox = $this->getAllCommentsOfPage($page); + $filteredInbox = $this->filterCommentsByStatus($inbox, $filter); - foreach ($collection as $item) { - $pendingKomments += $this->getCommentsCountOfPage($item, 'pending'); - } - - return $pendingKomments; + return $filteredInbox->count(); } - public function getSpamCommentCount(): int + // TODO TEST NOT POSSIBLE + public function getSiteWideCommentCount(?string $filter = 'all'): int { $collection = site()->index(); - $spamComments = 0; + $pendingKomments = 0; foreach ($collection as $item) { - $spamComments += $this->getCommentsCountOfPage($item, 'spam'); + $pendingKomments += $this->getCommentsCountOfPage($item, $filter); } - return $spamComments; - } - - private function transformToReply($komment) - { - return [ - 'id' => $komment->id(), - 'avatar' => $komment->avatar()->value(), - 'author' => $komment->author()->value(), - 'authorurl' => $komment->authorUrl()->value(), - 'source' => $komment->source()->value(), - 'target' => $komment->target()->value(), - 'mentionof' => $komment->mentionOf()->value(), - 'property' => $komment->property()->value(), - 'komment' => $komment->komment(), - 'quote' => $komment->quote(), - 'kommenttype' => $komment->kommenttype()->value(), - 'published' => $komment->published(), - 'status' => $komment->status()->value(), - 'wmsource' => $komment->wmsource()->value(), - 'wmtarget' => $komment->wmtarget()->value(), - 'wmproperty' => $komment->wmproperty()->value(), - 'verified' => $komment->verified()->value(), - 'replies' => [] - ]; + return $pendingKomments; } } diff --git a/utils/moderation.php b/utils/moderation.php index 64377bb..ecd7b96 100644 --- a/utils/moderation.php +++ b/utils/moderation.php @@ -2,68 +2,12 @@ namespace mauricerenck\Komments; -use in_array; use Exception; -use json_decode; -use json_encode; use Kirby\Data\yaml; class KommentModeration { - private $cookie_name; - - public function __construct() - { - $this->cookie_name = 'komments-in-moderation'; - } - - public function addCookieToModerationList($uid) - { - $cookie_value = []; - - if (isset($_COOKIE[$this->cookie_name])) { - $cookie_value = json_decode($_COOKIE[$this->cookie_name]); - } - - $cookie_value[] = $uid; - - setcookie($this->cookie_name, json_encode($cookie_value), [ - 'expires' => time() + (86400 * 256), - 'path' => '/', - 'secure' => true, - 'samesite' => 'None' - ]); - } - - public function getModerationListFromCookie() - { - if (isset($_COOKIE[$this->cookie_name])) { - return json_decode($_COOKIE[$this->cookie_name]); - } - - return false; - } - - public function pageHasQueuedKomments($uid, $kommenStatus) - { - $kommentsAwaitingModeration = $this->getModerationListFromCookie(); - - if (!$kommentsAwaitingModeration) { - return false; - } - - if (in_array($uid, $kommentsAwaitingModeration)) { - if ($kommenStatus->isTrue()) { - $this->removeUidFromCookie($kommentsAwaitingModeration, $uid); - return false; - } - - return true; - } - - return false; - } - + // TODO write tests public function markAsSpam($pageSlug, $kommentId, $isSpam) { try { @@ -77,7 +21,8 @@ public function markAsSpam($pageSlug, $kommentId, $isSpam) $fieldData = $targetPage->kommentsInbox()->yaml(); for ($i = 0; $i < count($fieldData); $i++) { - if (isset($fieldData[$i]['id'])) { // backward compatibility + if (isset($fieldData[$i]['id'])) { + // backward compatibility if ($fieldData[$i]['id'] === $kommentId) { if ($isSpam) { $fieldData[$i]['status'] = false; @@ -95,13 +40,14 @@ public function markAsSpam($pageSlug, $kommentId, $isSpam) $kirby = kirby(); $kirby->impersonate('kirby'); $targetPage->update([ - 'kommentsInbox' => $fieldData + 'kommentsInbox' => $fieldData, ]); } catch (\Throwable $th) { throw $th; } } + // TODO write tests public function markAsVerified($pageSlug, $kommentId, $isVerified) { try { @@ -115,7 +61,8 @@ public function markAsVerified($pageSlug, $kommentId, $isVerified) $fieldData = $targetPage->kommentsInbox()->yaml(); for ($i = 0; $i < count($fieldData); $i++) { - if (isset($fieldData[$i]['id'])) { // backward compatibility + if (isset($fieldData[$i]['id'])) { + // backward compatibility if ($fieldData[$i]['id'] === $kommentId) { $fieldData[$i]['verified'] = $isVerified; } @@ -127,13 +74,14 @@ public function markAsVerified($pageSlug, $kommentId, $isVerified) $kirby = kirby(); $kirby->impersonate('kirby'); $targetPage->update([ - 'kommentsInbox' => $fieldData + 'kommentsInbox' => $fieldData, ]); } catch (\Throwable $th) { throw $th; } } + // TODO write tests public function publish($pageSlug, $kommentId, $publish) { try { @@ -147,7 +95,8 @@ public function publish($pageSlug, $kommentId, $publish) $fieldData = $targetPage->kommentsInbox()->yaml(); for ($i = 0; $i < count($fieldData); $i++) { - if (isset($fieldData[$i]['id'])) { // backward compatibility + if (isset($fieldData[$i]['id'])) { + // backward compatibility if ($fieldData[$i]['id'] === $kommentId) { $fieldData[$i]['status'] = $publish; } @@ -159,13 +108,14 @@ public function publish($pageSlug, $kommentId, $publish) $kirby = kirby(); $kirby->impersonate('kirby'); $targetPage->update([ - 'kommentsInbox' => $fieldData + 'kommentsInbox' => $fieldData, ]); } catch (\Throwable $th) { throw $th; } } + // TODO write tests public function delete($pageSlug, $kommentId) { try { @@ -180,7 +130,8 @@ public function delete($pageSlug, $kommentId) $newFieldData = []; for ($i = 0; $i < count($fieldData); $i++) { - if (isset($fieldData[$i]['id'])) { // backward compatibility + if (isset($fieldData[$i]['id'])) { + // backward compatibility if ($fieldData[$i]['id'] !== $kommentId) { $newFieldData[] = $fieldData[$i]; } @@ -192,16 +143,70 @@ public function delete($pageSlug, $kommentId) $kirby = kirby(); $kirby->impersonate('kirby'); $targetPage->update([ - 'kommentsInbox' => $newFieldData + 'kommentsInbox' => $newFieldData, ]); } catch (\Throwable $th) { throw $th; } } - private function removeUidFromCookie($kommentsAwaitingModeration, $uid) + // TESTING NOT POSSIBLE RIGHT NOW + public function getSiteWideComments(?string $filter = 'all'): array { - $cleanedUids = array_diff($kommentsAwaitingModeration, [$uid]); - setcookie($this->cookie_name, json_encode($cleanedUids), time() + (86400 * 256), '/'); + $comments = []; + $collection = site()->index(); + + foreach ($collection as $item) { + $comments = array_merge($comments, $this->getCommentsOfPage($item, $filter)); + } + + usort($comments, function ($a, $b) { + return $b['published'] <=> $a['published']; + }); + + return $comments; + } + + // TESTED + public function getCommentsOfPage($page, $filter = null, $language = null) + { + $baseUtils = new KommentBaseUtils(); + $allPageInboxes = $baseUtils->getAllCommentsOfPage($page); + + if (is_null($allPageInboxes)) { + return []; + } + + $filteredComments = $baseUtils->filterCommentsByStatus($allPageInboxes, $filter); + + return $this->convertInboxToCommentArray($filteredComments, $page); + } + + // TESTED + public function convertInboxToCommentArray($inbox, $page) + { + $comments = []; + + foreach ($inbox as $entry) { + $comments[] = [ + 'id' => $entry->id(), + 'slug' => $page->id(), + 'author' => $entry->author()->value(), + 'authorUrl' => $entry->authorUrl()->value(), + 'komment' => kirbytext(nl2br(html($entry->komment()))), + 'kommentType' => $entry->kommenttype()->value() ?? 'komment', + 'image' => $entry->avatar()->value(), + 'title' => $page->title()->value(), + 'url' => $page->panel()->url(), + 'published' => date('Y-m-d H:i', strtotime($entry->published())), + 'verified' => $entry->verified()->toBool(false), + 'spamlevel' => $entry->spamlevel()->value() ?? 0, + 'status' => $entry->status()->toBool(false), + 'mentionof' => $entry->mentionof()->value() ?? null, + 'replies' => [], + ]; + } + + return $comments; } } diff --git a/utils/notifications.php b/utils/notifications.php index e39039a..8646c4f 100644 --- a/utils/notifications.php +++ b/utils/notifications.php @@ -2,15 +2,14 @@ namespace mauricerenck\Komments; - class KommentNotificationUtils { private $pendingComments; public function sendNotifications() { - $kommentUtils = new KommentBaseUtils(); - $this->pendingComments = $kommentUtils->getPendingKomments(); + $kommentModeration = new KommentModeration(); + $this->pendingComments = $kommentModeration->getSiteWideComments('pending'); $this->sendEmailNotification(); } @@ -34,8 +33,8 @@ public function sendEmailNotification() 'template' => 'newcomments', 'data' => [ 'pendingComments' => $pendingCommentsCount, - 'panelUrl' => $panelUrl - ] + 'panelUrl' => $panelUrl, + ], ]); } } diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 27bbbb3..22952ac 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -17,6 +17,7 @@ 'mauricerenck\\Komments\\KommentModeration' => $baseDir . '/utils/moderation.php', 'mauricerenck\\Komments\\KommentNotificationUtils' => $baseDir . '/utils/notifications.php', 'mauricerenck\\Komments\\KommentReceiver' => $baseDir . '/utils/receiveKomment.php', + 'mauricerenck\\Komments\\KommentsFrontend' => $baseDir . '/utils/Frontend.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 f4a0cb1..d2af377 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -54,6 +54,7 @@ class ComposerStaticInit60056fecad9d217d765a4dde54e59e86 'mauricerenck\\Komments\\KommentModeration' => __DIR__ . '/../..' . '/utils/moderation.php', 'mauricerenck\\Komments\\KommentNotificationUtils' => __DIR__ . '/../..' . '/utils/notifications.php', 'mauricerenck\\Komments\\KommentReceiver' => __DIR__ . '/../..' . '/utils/receiveKomment.php', + 'mauricerenck\\Komments\\KommentsFrontend' => __DIR__ . '/../..' . '/utils/Frontend.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 afdf217..f24fb13 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -1,8 +1,8 @@ array( 'name' => 'mauricerenck/komments', - 'pretty_version' => '1.13.1', - 'version' => '1.13.1.0', + 'pretty_version' => '1.13.2', + 'version' => '1.13.2.0', 'reference' => NULL, 'type' => 'kirby-plugin', 'install_path' => __DIR__ . '/../../', @@ -29,8 +29,8 @@ 'dev_requirement' => false, ), 'mauricerenck/komments' => array( - 'pretty_version' => '1.13.1', - 'version' => '1.13.1.0', + 'pretty_version' => '1.13.2', + 'version' => '1.13.2.0', 'reference' => NULL, 'type' => 'kirby-plugin', 'install_path' => __DIR__ . '/../../',