Skip to content

Commit

Permalink
test: added tests for class Rating
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Mar 29, 2024
1 parent d201575 commit dbedb62
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 32 deletions.
2 changes: 1 addition & 1 deletion phpmyfaq/admin/stat.ratings.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
}
}

$ratingData = $ratings->getAllRatings();
$ratingData = $ratings->getAll();
$numberOfRatings = is_countable($ratingData) ? count($ratingData) : 0;
$currentCategory = 0;

Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/faq.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@
'linkToPdf' => $faqServices->getPdfLink(),
'saveVotingID' => $faqId,
'msgAverageVote' => Translation::get('msgAverageVote'),
'renderVotingResult' => $rating->getVotingResult($faqId),
'renderVotingResult' => $rating->get($faqId),
'switchLanguage' => $faqHelper->renderChangeLanguageSelector($faq, $currentCategory),
'msgVoteUsability' => Translation::get('msgVoteUsability'),
'msgVoteBad' => Translation::get('msgVoteBad'),
Expand Down
15 changes: 8 additions & 7 deletions phpmyfaq/src/phpMyFAQ/Controller/Frontend/VotingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use Exception;
use phpMyFAQ\Controller\AbstractController;
use phpMyFAQ\Entity\Vote;
use phpMyFAQ\Filter;
use phpMyFAQ\Rating;
use phpMyFAQ\Session;
Expand Down Expand Up @@ -49,22 +50,22 @@ public function create(Request $request): JsonResponse
if (isset($vote) && $rating->check($faqId, $userIp) && $vote > 0 && $vote < 6) {
$session->userTracking('save_voting', $faqId);

$votingData = [
'record_id' => $faqId,
'vote' => $vote,
'user_ip' => $userIp,
];
$votingData = new Vote();
$votingData
->setFaqId($faqId)
->setVote($vote)
->setIp($userIp);

if ($rating->getNumberOfVotings($faqId) === 0) {
$rating->addVoting($votingData);
$rating->create($votingData);
} else {
$rating->update($votingData);
}

return $this->json(
[
'success' => Translation::get('msgVoteThanks'),
'rating' => $rating->getVotingResult($faqId),
'rating' => $rating->get($faqId),
],
Response::HTTP_OK
);
Expand Down
90 changes: 90 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Entity/Vote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/**
* Vote entity class.
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
* @copyright 2024 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2024-03-29
*/

namespace phpMyFAQ\Entity;

use DateTime;

class Vote
{
private int $id;

private int $faqId;

private int $vote;

private int $users;

private DateTime $createdAt;

private string $ip;

public function getId(): int
{
return $this->id;
}
public function setId(int $id): Vote
{
$this->id = $id;
return $this;
}
public function getFaqId(): int
{
return $this->faqId;
}
public function setFaqId(int $faqId): Vote
{
$this->faqId = $faqId;
return $this;
}
public function getVote(): int
{
return $this->vote;
}
public function setVote(int $vote): Vote
{
$this->vote = $vote;
return $this;
}
public function getUsers(): int
{
return $this->users;
}
public function setUsers(int $users): Vote
{
$this->users = $users;
return $this;
}
public function getCreatedAt(): DateTime
{
return $this->createdAt;
}
public function setCreatedAt(DateTime $createdAt): Vote
{
$this->createdAt = $createdAt;
return $this;
}
public function getIp(): string
{
return $this->ip;
}
public function setIp(string $ip): Vote
{
$this->ip = $ip;
return $this;
}
}
35 changes: 18 additions & 17 deletions phpmyfaq/src/phpMyFAQ/Rating.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace phpMyFAQ;

use phpMyFAQ\Entity\Vote;
use phpMyFAQ\Language\Plurals;

/**
Expand Down Expand Up @@ -44,7 +45,7 @@ public function __construct(private Configuration $configuration)
*
* @return array
*/
public function getAllRatings(): array
public function getAll(): array
{
$ratings = [];

Expand Down Expand Up @@ -150,7 +151,7 @@ public function getAllRatings(): array
/**
* Calculates the rating of the user voting.
*/
public function getVotingResult(int $id): string
public function get(int $id): string
{
$query = sprintf(
'SELECT (vote/usr) as voting, usr FROM %sfaqvoting WHERE artikel = %d',
Expand Down Expand Up @@ -212,50 +213,50 @@ public function getNumberOfVotings(int $recordId): int
/**
* Adds a new voting record.
*
* @param array $votingData
* @param Vote $votingData
* @return bool
*/
public function addVoting(array $votingData): bool
public function create(Vote $votingData): bool
{
$query = sprintf(
"INSERT INTO %sfaqvoting VALUES (%d, %d, %d, 1, %d, '%s')",
Database::getTablePrefix(),
$this->configuration->getDb()->nextId(Database::getTablePrefix() . 'faqvoting', 'id'),
$votingData['record_id'],
$votingData['vote'],
$votingData->getFaqId(),
$votingData->getVote(),
$_SERVER['REQUEST_TIME'],
$this->configuration->getDb()->escape($votingData['user_ip'])
$this->configuration->getDb()->escape($votingData->getIp())
);
$this->configuration->getDb()->query($query);

return true;
return (bool)$this->configuration->getDb()->query($query);
}

/**
* Updates an existing voting record.
*
* @param array $votingData
* @param Vote $votingData
* @return bool
*/
public function update(array $votingData): bool
public function update(Vote $votingData): bool
{
$query = sprintf(
"UPDATE %sfaqvoting SET vote = vote + %d, usr = usr + 1, datum = %d, ip = '%s' WHERE artikel = %d",
Database::getTablePrefix(),
$votingData['vote'],
$votingData->getVote(),
$_SERVER['REQUEST_TIME'],
$this->configuration->getDb()->escape($votingData['user_ip']),
$votingData['record_id']
$this->configuration->getDb()->escape($votingData->getIp()),
$votingData->getFaqId()
);
$this->configuration->getDb()->query($query);

return true;
return (bool) $this->configuration->getDb()->query($query);
}

/**
* Deletes all votes.
*/
public function deleteAll(): bool
{
return $this->configuration->getDb()->query(
return (bool) $this->configuration->getDb()->query(
sprintf('DELETE FROM %sfaqvoting', Database::getTablePrefix())
);
}
Expand Down
10 changes: 4 additions & 6 deletions tests/phpMyFAQ/QuestionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
namespace phpMyFAQ;

use phpMyFAQ\Database\Sqlite3;
use phpMyFAQ\Setup\Update;
use PHPUnit\Framework\TestCase;

class QuestionTest extends TestCase
{
private Configuration $configuration;
private Sqlite3 $dbHandle;
private Question $question;
protected function setUp(): void
Expand All @@ -17,12 +15,12 @@ protected function setUp(): void

$this->dbHandle = new Sqlite3();
$this->dbHandle->connect(PMF_TEST_DIR . '/test.db', '', '');
$this->configuration = new Configuration($this->dbHandle);
$language = new Language($this->configuration);
$configuration = new Configuration($this->dbHandle);
$language = new Language($configuration);
$language->setLanguage(false, 'en');
$this->configuration->setLanguage($language);
$configuration->setLanguage($language);

$this->question = new Question($this->configuration);
$this->question = new Question($configuration);
}

protected function tearDown(): void
Expand Down
130 changes: 130 additions & 0 deletions tests/phpMyFAQ/RatingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace phpMyFAQ;

use phpMyFAQ\Core\Exception;
use phpMyFAQ\Database\Sqlite3;
use phpMyFAQ\Entity\Vote;
use PHPUnit\Framework\TestCase;

class RatingTest extends TestCase
{
private Sqlite3 $dbHandle;
private Rating $rating;

/**
* @throws Exception
*/
protected function setUp(): void
{
parent::setUp();

Translation::create()
->setLanguagesDir(PMF_TRANSLATION_DIR)
->setDefaultLanguage('en')
->setCurrentLanguage('en')
->setMultiByteLanguage();

$this->dbHandle = new Sqlite3();
$this->dbHandle->connect(PMF_TEST_DIR . '/test.db', '', '');
$configuration = new Configuration($this->dbHandle);
$language = new Language($configuration);
$language->setLanguage(false, 'en');
$configuration->setLanguage($language);

$this->rating = new Rating($configuration);
}

protected function tearDown(): void
{
$this->dbHandle->query('DELETE FROM faqvoting');
}

public function testCreate(): void
{
$votingData = new Vote();
$votingData
->setFaqId(1)
->setVote(5)
->setIp('127.0.0.1');

$this->assertTrue($this->rating->create($votingData));
}

public function testGet(): void
{
$votingData = new Vote();
$votingData
->setFaqId(1)
->setVote(5)
->setIp('127.0.0.1');

$this->rating->create($votingData);

$this->assertEquals(' <span data-rating="5">5</span> (1 Vote)', $this->rating->get(1));
}

public function testCheck(): void
{
$votingData = new Vote();
$votingData
->setFaqId(1)
->setVote(5)
->setIp('127.0.0.1');

$this->rating->create($votingData);

$this->assertFalse($this->rating->check(1, '127.0.0.1'));
}

public function testGetNumberOfVotings(): void
{
$votingData = new Vote();
$votingData
->setFaqId(1)
->setVote(5)
->setIp('127.0.0.1');

$this->rating->create($votingData);

$votingData = new Vote();
$votingData
->setFaqId(1)
->setVote(4)
->setIp('127.0.0.2');

$this->rating->update($votingData);

$this->assertEquals(2, $this->rating->getNumberOfVotings(1));
}

public function testUpdate(): void
{
$votingData = new Vote();
$votingData
->setFaqId(1)
->setVote(5)
->setIp('127.0.0.1');

$this->rating->create($votingData);

$votingData = new Vote();
$votingData
->setFaqId(1)
->setVote(4)
->setIp('127.0.0.2');

$this->assertTrue($this->rating->update($votingData));
}

public function testDeleteAll(): void
{
$votingData = new Vote();
$votingData->setFaqId(1)->setVote(5)->setIp('127.0.0.1');

$this->rating->create($votingData);

$this->assertTrue($this->rating->deleteAll());
$this->assertEquals(' <span data-rating="0">0</span> (0 Votes)', $this->rating->get(1));
}
}

0 comments on commit dbedb62

Please sign in to comment.