Skip to content

Commit

Permalink
test: added tests for Comments class
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Mar 24, 2024
1 parent dcd037e commit c234952
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 6 deletions.
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ This is a log of major user-visible changes in each phpMyFAQ release.
- updated bundled dependencies (Thorsten)
- fixed minor bugs (Thorsten)

### phpMyFAQ v3.2.6 - unreleased

### phpMyFAQ v3.2.5 - 2024-02-05

- fixed multiple security vulnerabilities (Thorsten)
Expand Down
5 changes: 3 additions & 2 deletions phpmyfaq/src/phpMyFAQ/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace phpMyFAQ;

use DateTimeInterface;
use Exception;
use phpMyFAQ\Entity\CommentType;
use phpMyFAQ\Services\Gravatar;
Expand Down Expand Up @@ -116,7 +117,7 @@ public function getCommentsData(int $referenceId, string $type): array
->setId($row->id_comment)
->setRecordId($row->id)
->setComment($row->comment)
->setDate(Date::createIsoDate($row->datum, DATE_ISO8601, false))
->setDate(Date::createIsoDate($row->datum, DateTimeInterface::ATOM, false))
->setUsername($row->usr)
->setEmail($row->email)
->setType($type);
Expand Down Expand Up @@ -156,7 +157,7 @@ private function showShortComment(int $id, string $comment): string
/**
* Adds a new comment.
*/
public function addComment(Comment $comment): bool
public function create(Comment $comment): bool
{
$query = sprintf(
"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ public function create(Request $request): JsonResponse
->setUsername($username)
->setEmail($mailer)
->setComment(nl2br(strip_tags((string) $commentText)))
->setDate($request->server->get('REQUEST_TIME'));
->setDate($request->server->get('tim'));

if ($comment->addComment($commentEntity)) {
if ($comment->create($commentEntity)) {
$notification = new Notification($this->configuration);
if ('faq' == $type) {
$notification->sendFaqCommentNotification($faq, $commentEntity);
Expand Down
79 changes: 79 additions & 0 deletions tests/phpMyFAQ/CommentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace phpMyFAQ;

use DateTime;
use DateTimeInterface;
use phpMyFAQ\Database\Sqlite3;
use phpMyFAQ\Entity\Comment;
use phpMyFAQ\Entity\CommentType;
use PHPUnit\Framework\TestCase;

class CommentsTest extends TestCase
{
private Comments $comments;

protected function setUp(): void
{
parent::setUp();

$dbHandle = new Sqlite3();
$dbHandle->connect(PMF_TEST_DIR . '/test.db', '', '');
$configuration = new Configuration($dbHandle);

$this->comments = new Comments($configuration);
}

protected function tearDown(): void
{
parent::tearDown();

$this->comments->delete(CommentType::FAQ, 1);
}

public function testCreate(): void
{
$comment = $this->getComment();

$this->assertTrue($this->comments->create($comment));
}

public function testDelete(): void
{
$comment = $this->getComment();
$this->comments->create($comment);

$this->assertTrue($this->comments->delete(CommentType::FAQ, 1));
}

public function testGetNumberOfComments(): void
{
$comment = $this->getComment();
$this->comments->create($comment);

$this->assertSame([1 => 1], $this->comments->getNumberOfComments());
}

public function testGetAllComments(): void
{
$comment = $this->getComment();
$this->comments->create($comment);

$this->assertCount(1, $this->comments->getAllComments());
}

private function getComment(): Comment
{
$comment = new Comment();
$comment
->setRecordId(1)
->setType(CommentType::FAQ)
->setUsername('testUser')
->setEmail('test@example.org')
->setComment('This is a test comment')
->setDate((string)time())
->setHelped(true);

return $comment;
}
}

0 comments on commit c234952

Please sign in to comment.