-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d821df
commit 1eae173
Showing
4 changed files
with
180 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |