Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin committed Sep 8, 2023
1 parent 9d821df commit 1eae173
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 20 deletions.
47 changes: 27 additions & 20 deletions tests/codeception/_support/UnitTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,33 @@

namespace questions;

/**
* Inherited Methods
* @method void wantToTest($text)
* @method void wantTo($text)
* @method void execute($callable)
* @method void expectTo($prediction)
* @method void expect($prediction)
* @method void amGoingTo($argumentation)
* @method void am($role)
* @method void lookForwardTo($achieveValue)
* @method void comment($description)
* @method void pause()
*
* @SuppressWarnings(PHPMD)
*/
class UnitTester extends \UnitTester
use humhub\modules\questions\models\Question;
use humhub\modules\questions\models\QuestionAnswer;
use humhub\modules\space\models\Space;
use tests\codeception\_support\HumHubDbTestCase;

class UnitTester extends HumHubDbTestCase
{
use _generated\UnitTesterActions;

/**
* Define custom actions here
*/
public function createQuestion(string $headline, string $description = '', int $spaceId = 1): ?Question
{
$space = Space::findOne(['id' => $spaceId]);

$question = new Question($space);
$question->question = $headline;
$question->description = $description;
$question->save();

return $question;
}

public function createAnswer(string $text, Question $question): ?QuestionAnswer
{
$answer = new QuestionAnswer();
$answer->question_id = $question->id;
$answer->answer = $text;
$answer->save();

return $answer;
}
}
62 changes: 62 additions & 0 deletions tests/codeception/unit/AnswerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace questions;

class AnswerTest extends UnitTester
{
public function testCreateAnswers()
{
$this->becomeUser('Admin');

$question = $this->createQuestion('Question?', 'Description.');
$answerService = $question->getAnswerService();

$answer1 = $this->createAnswer('Answer 1', $question);
$this->assertFalse($answer1->isNewRecord);
$this->assertTrue($question->canAnswer());
$this->assertTrue($answerService->canSelectBest());

$this->becomeUser('User1');
$this->assertFalse($question->canAnswer());
$this->assertFalse($answerService->canSelectBest());

$this->becomeUser('User2');
$this->assertTrue($question->canAnswer());
$answer2 = $this->createAnswer('Answer 2', $question);
$this->assertFalse($answer2->isNewRecord);
$this->assertFalse($answerService->canSelectBest());
}

public function testSelectBestAnswer()
{
$this->becomeUser('Admin');

$question = $this->createQuestion('Question?', 'Description.');
$answerService = $question->getAnswerService();

$answer1 = $this->createAnswer('Answer 1', $question);
$answer2 = $this->createAnswer('Answer 2', $question);
$answer3 = $this->createAnswer('Answer 3', $question);

$this->assertEquals(3, $answerService->getCount());
$this->assertEquals(3, $answerService->getExceptBestQuery()->count());
$this->assertNull($answerService->getBest());

$this->assertTrue($answerService->changeBest($answer1));
$this->assertEquals($answerService->getBest()->id, $answer1->id);
$this->assertEquals(2, $answerService->getExceptBestQuery()->count());

$this->assertTrue($answerService->changeBest($answer3));
$this->assertEquals($answerService->getBest()->id, $answer3->id);
$this->assertEquals(2, $answerService->getExceptBestQuery()->count());

$this->assertTrue($answerService->changeBest($answer3));
$this->assertNull($answerService->getBest());
$this->assertEquals(3, $answerService->getExceptBestQuery()->count());
}
}
28 changes: 28 additions & 0 deletions tests/codeception/unit/QuestionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace questions;

class QuestionTest extends UnitTester
{
public function testCreateQuestion()
{
$this->becomeUser('Admin');

$question = $this->createQuestion('Sample question title?', 'Sample question description.');

$this->assertFalse($question->isNewRecord);
$answerService = $question->getAnswerService();

$this->assertTrue($question->canAnswer());
$this->assertTrue($answerService->canSelectBest());

$this->becomeUser('User2');
$this->assertTrue($question->canAnswer());
$this->assertFalse($answerService->canSelectBest());
}
}
63 changes: 63 additions & 0 deletions tests/codeception/unit/VotingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace questions;

use humhub\modules\questions\models\QuestionAnswer;
use humhub\modules\questions\models\QuestionAnswerVote;

class VotingTest extends UnitTester
{
public function testVoting()
{
$this->becomeUser('Admin');

$question = $this->createQuestion('Question?', 'Description.');
$answerService = $question->getAnswerService();

$answer1 = $this->createAnswer('Answer 1', $question);
$voteService = $answer1->getVoteService();
$this->assertTrue($answerService->canVote());
$this->assertFalse($voteService->canVote());

$this->becomeUser('User2');
$this->assertTrue($voteService->canVote());
$this->assertEquals(0, $voteService->getSummary());
$this->assertNull($voteService->getVote());
$this->assertTrue($voteService->vote('up'));
$this->assertEquals(1, $voteService->getSummary());
$vote = $voteService->getVote();
$this->assertEquals(QuestionAnswerVote::TYPE_UP, $vote->type);
$this->assertTrue($voteService->isVotedWithType('up'));

$answer2 = $this->createAnswer('Answer 2', $question);
$voteService = $answer2->getVoteService();

$this->becomeUser('Admin');
$this->assertTrue($voteService->canVote());
$this->assertTrue($voteService->vote('down'));
$this->assertEquals(-1, $voteService->getSummary());

$this->becomeUser('User3');
$this->assertTrue($voteService->canVote());
$this->assertTrue($voteService->vote('down'));
$this->assertEquals(-2, $voteService->getSummary());
$this->assertTrue($voteService->isVotedWithType('down'));
}

public function testCheckVotingService()
{
$answer = new QuestionAnswer();
$voteService = $answer->getVoteService();

$this->assertEquals(QuestionAnswerVote::TYPE_UP, $voteService->normalizeVoteType('up'));
$this->assertEquals(QuestionAnswerVote::TYPE_UP, $voteService->normalizeVoteType(1));
$this->assertEquals(QuestionAnswerVote::TYPE_DOWN, $voteService->normalizeVoteType('down'));
$this->assertEquals(QuestionAnswerVote::TYPE_DOWN, $voteService->normalizeVoteType(-1));
$this->assertNull($voteService->normalizeVoteType('left'));
}
}

0 comments on commit 1eae173

Please sign in to comment.