diff --git a/Admin/Install/Application/QA/Themes/Default/tpl/header.tpl.php b/Admin/Install/Application/QA/Themes/Default/tpl/header.tpl.php index 6b96bdf..a904833 100755 --- a/Admin/Install/Application/QA/Themes/Default/tpl/header.tpl.php +++ b/Admin/Install/Application/QA/Themes/Default/tpl/header.tpl.php @@ -16,13 +16,13 @@ use phpOMS\Uri\UriFactory; ?>
- - + +
\ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index cab9f5e..ad8944e 100755 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -12,9 +12,7 @@ If you have a good idea for improvement feel free to create a new issue with all ### Issues -Feel free to grab any open issue implement it and create a new pull request. Most issues can be found in the `Project.md` file in the `Docs` repository. - -The issue information can be used to provide additional information such as priority, difficulty and type. For your first issue try to find a issue marked `[d:first]` or `[d:beginner]`. +Feel free to grab any open issue implement it and create a new pull request. Most issues can be found in the code marked with `@todo` or in the [PROJECT.md](https://github.com/Orange-Management/Docs/blob/master/Project/PROJECT.md) file. ### Code Style diff --git a/Controller/ApiController.php b/Controller/ApiController.php index 75d8df1..beb9d9c 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -271,7 +271,8 @@ final class ApiController extends Controller if (!empty($uploadedFiles = $request->getFiles() ?? [])) { $uploaded = $this->app->moduleManager->get('Media')->uploadFiles( - [''], + [], + [], $uploadedFiles, $request->header->account, __DIR__ . '/../../../Modules/Media/Files/Modules/QA', diff --git a/Models/QAAnswer.php b/Models/QAAnswer.php index 01de241..080d316 100755 --- a/Models/QAAnswer.php +++ b/Models/QAAnswer.php @@ -132,58 +132,6 @@ class QAAnswer implements \JsonSerializable return $this->id; } - /** - * Get the answer - * - * @return string - * - * @since 1.0.0 - */ - public function getAnswer() : string - { - return $this->answer; - } - - /** - * Set the answer - * - * @param string $answer Answer - * - * @return void - * - * @since 1.0.0 - */ - public function setAnswer(string $answer) : void - { - $this->answer = $answer; - } - - /** - * Get the question - * - * @return QAQuestion - * - * @since 1.0.0 - */ - public function getQuestion() : QAQuestion - { - return $this->question; - } - - /** - * Set the question - * - * @param QAQuestion $question Question - * - * @return void - * - * @since 1.0.0 - */ - public function setQuestion(QAQuestion $question) : void - { - $this->question = $question; - } - /** * Get the status * @@ -210,20 +158,6 @@ class QAAnswer implements \JsonSerializable $this->status = $status; } - /** - * Set the answer as accepted - * - * @param bool $accepted Accepted - * - * @return void - * - * @since 1.0.0 - */ - public function setAccepted(bool $accepted) : void - { - $this->isAccepted = $accepted; - } - /** * Get the total vote score * @@ -261,6 +195,32 @@ class QAAnswer implements \JsonSerializable return 0; } + /** + * Get all votes + * + * @return QAVnswerVote[] + * + * @since 1.0.0 + */ + public function getVotes() : array + { + return $this->votes; + } + + /** + * Add vote + * + * @param QAAnswerVote $vote Vote + * + * @return void + * + * @since 1.0.0 + */ + public function addVote(QAAnswerVote $vote) : void + { + $this->votes[] = $vote; + } + /** * Get all media * @@ -290,8 +250,27 @@ class QAAnswer implements \JsonSerializable /** * {@inheritdoc} */ - public function jsonSerialize() : array + public function toArray() : array { - return []; + return [ + 'id' => $this->id, + 'status' => $this->status, + 'answer' => $this->answer, + 'answerRaw' => $this->answerRaw, + 'question' => $this->question, + 'isAccepted' => $this->isAccepted, + 'createdBy' => $this->createdBy, + 'createdAt' => $this->createdAt, + 'votes' => $this->votes, + 'media' => $this->media, + ]; + } + + /** + * {@inheritdoc} + */ + public function jsonSerialize() + { + return $this->toArray(); } } diff --git a/Models/QAQuestion.php b/Models/QAQuestion.php index 37d94f9..2e9ff92 100755 --- a/Models/QAQuestion.php +++ b/Models/QAQuestion.php @@ -18,6 +18,7 @@ use Modules\Media\Models\Media; use Modules\Profile\Models\NullProfile; use Modules\Profile\Models\Profile; use Modules\Tag\Models\Tag; +use phpOMS\Localization\ISO639x1Enum; /** * QA question class. @@ -75,7 +76,7 @@ class QAQuestion implements \JsonSerializable * @var string * @since 1.0.0 */ - private string $language = ''; + private string $language = ISO639x1Enum::_EN; /** * Created by. @@ -156,7 +157,7 @@ class QAQuestion implements \JsonSerializable } /** - * Finds all accounts in Question + * Finds all accounts in the question * e.g. asked by and all accoounts who answered * * @return array @@ -217,24 +218,6 @@ class QAQuestion implements \JsonSerializable $this->language = $language; } - /** - * Is the question answered? - * - * @return bool - * - * @since 1.0.0 - */ - public function isAnswered() : bool - { - foreach ($this->answers as $answer) { - if ($answer->isAccepted()) { - return true; - } - } - - return false; - } - /** * Get the status * @@ -262,11 +245,49 @@ class QAQuestion implements \JsonSerializable } /** - * Get tags + * Adding new tag. * - * @return array + * @param Tag $tag Tag + * + * @return int * * @since 1.0.0 + */ + public function addTag(Tag $tag) : int + { + $this->tags[] = $tag; + + \end($this->tags); + $key = (int) \key($this->tags); + \reset($this->tags); + + return $key; + } + + /** + * Remove Tag from list. + * + * @param int $id Tag + * + * @return bool + * + * @since 1.0.0 + */ + public function removeTag($id) : bool + { + if (isset($this->tags[$id])) { + unset($this->tags[$id]); + + return true; + } + + return false; + } + + /** + * Get task elements. + * + * @return Tag[] * * @since 1.0.0 */ @@ -276,27 +297,17 @@ class QAQuestion implements \JsonSerializable } /** - * Add tag to question + * Get task elements. * - * @param int|Tag $tag Tag + * @param int $id Element id + * + * @return Tag * * @since 1.0.0 */ - public function addTag(int | Tag $tag) : void + public function getTag(int $id) : Tag { - $this->tags[] = $tag; - } - - /** - * Set tags to question - * - * @param array $tags Tags - * - * @since 1.0.0 - */ - public function setTags(array $tags) : void - { - $this->tags = $tags; + return $this->tags[$id] ?? new NullTag(); } /** @@ -328,6 +339,32 @@ class QAQuestion implements \JsonSerializable return $score; } + /** + * Get all votes + * + * @return QAVnswerVote[] + * + * @since 1.0.0 + */ + public function getVotes() : array + { + return $this->votes; + } + + /** + * Add vote + * + * @param QAQuestionVote $vote Vote + * + * @return void + * + * @since 1.0.0 + */ + public function addVote(QAQuestionVote $vote) : void + { + $this->votes[] = $vote; + } + /** * Get the vote score from an account * @@ -431,8 +468,30 @@ class QAQuestion implements \JsonSerializable /** * {@inheritdoc} */ - public function jsonSerialize() : array + public function toArray() : array { - return []; + return [ + 'id' => $this->id, + 'name' => $this->name, + 'status' => $this->status, + 'question' => $this->question, + 'questionRaw' => $this->questionRaw, + 'language' => $this->language, + 'createdBy' => $this->createdBy, + 'createdAt' => $this->createdAt, + 'app' => $this->app, + 'tags' => $this->tags, + 'answers' => $this->votes, + 'votes' => $this->votes, + 'media' => $this->media, + ]; + } + + /** + * {@inheritdoc} + */ + public function jsonSerialize() + { + return $this->toArray(); } } diff --git a/tests/Controller/ApiControllerTest.php b/tests/Controller/ApiControllerTest.php new file mode 100644 index 0000000..aba6b5b --- /dev/null +++ b/tests/Controller/ApiControllerTest.php @@ -0,0 +1,333 @@ +app = new class() extends ApplicationAbstract + { + protected string $appName = 'Api'; + }; + + $this->app->dbPool = $GLOBALS['dbpool']; + $this->app->orgId = 1; + $this->app->accountManager = new AccountManager($GLOBALS['session']); + $this->app->appSettings = new CoreSettings(); + $this->app->moduleManager = new ModuleManager($this->app, __DIR__ . '/../../../../Modules/'); + $this->app->dispatcher = new Dispatcher($this->app); + $this->app->eventManager = new EventManager($this->app->dispatcher); + $this->app->eventManager->importFromFile(__DIR__ . '/../../../../Web/Api/Hooks.php'); + $this->app->sessionManager = new HttpSession(36000); + + $account = new Account(); + TestUtils::setMember($account, 'id', 1); + + $permission = new AccountPermission(); + $permission->setUnit(1); + $permission->setApp('backend'); + $permission->setPermission( + PermissionType::READ + | PermissionType::CREATE + | PermissionType::MODIFY + | PermissionType::DELETE + | PermissionType::PERMISSION + ); + + $account->addPermission($permission); + + $this->app->accountManager->add($account); + $this->app->router = new WebRouter(); + + $this->module = $this->app->moduleManager->get('QA'); + + TestUtils::setMember($this->module, 'app', $this->app); + } + + /** + * @covers Modules\QA\Controller\ApiController + * @group module + */ + public function testApiQAAppCreate() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('name', 'TestQAApp'); + + $this->module->apiQAAppCreate($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\QA\Controller\ApiController + * @group module + */ + public function testApiQAAppCreateInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiQAAppCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } + + /** + * @covers Modules\QA\Controller\ApiController + * @group module + */ + public function testApiQAQuestionCreate() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('title', 'Test Question'); + $request->setData('plain', 'Question content'); + $request->setData('language', ISO639x1Enum::_EN); + $request->setData('status', QAQuestionStatus::ACTIVE); + $request->setData('tags', '[{"title": "TestTitle", "color": "#f0f", "language": "en"}, {"id": 1}]'); + + if (!\is_file(__DIR__ . '/test_tmp.md')) { + \copy(__DIR__ . '/test.md', __DIR__ . '/test_tmp.md'); + } + + TestUtils::setMember($request, 'files', [ + 'file1' => [ + 'name' => 'test.md', + 'type' => MimeType::M_TXT, + 'tmp_name' => __DIR__ . '/test_tmp.md', + 'error' => \UPLOAD_ERR_OK, + 'size' => \filesize(__DIR__ . '/test_tmp.md'), + ], + ]); + + $request->setData('media', \json_encode([1])); + + $this->module->apiQAQuestionCreate($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\QA\Controller\ApiController + * @group module + */ + public function testApiQAQuestionCreateInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiQAQuestionCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } + + /** + * @covers Modules\QA\Controller\ApiController + * @group module + */ + public function testApiQAAnswerCreate() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('question', '1'); + $request->setData('plain', 'Answer content'); + $request->setData('status', QAAnswerStatus::ACTIVE); + + if (!\is_file(__DIR__ . '/test_tmp.md')) { + \copy(__DIR__ . '/test.md', __DIR__ . '/test_tmp.md'); + } + + TestUtils::setMember($request, 'files', [ + 'file1' => [ + 'name' => 'test.md', + 'type' => MimeType::M_TXT, + 'tmp_name' => __DIR__ . '/test_tmp.md', + 'error' => \UPLOAD_ERR_OK, + 'size' => \filesize(__DIR__ . '/test_tmp.md'), + ], + ]); + + $request->setData('media', \json_encode([1])); + + $this->module->apiQAAnswerCreate($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\QA\Controller\ApiController + * @group module + */ + public function testApiChangeAnsweredStatus() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('id', '1'); + $request->setData('accepted', '1'); + + $this->module->apiChangeAnsweredStatus($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\QA\Controller\ApiController + * @group module + */ + public function testApiQAAnswerCreateInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiQAAnswerCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } + + /** + * @covers Modules\QA\Controller\ApiController + * @group module + */ + public function testApiChangeQAQuestionVote() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('id', '1'); + $request->setData('type', '-1'); + + $this->module->apiChangeQAQuestionVote($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('id', '1'); + $request->setData('type', '1'); + + $this->module->apiChangeQAQuestionVote($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\QA\Controller\ApiController + * @group module + */ + public function testApiChangeQAQuestionVoteInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiChangeQAQuestionVote($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } + + /** + * @covers Modules\QA\Controller\ApiController + * @group module + */ + public function testApiChangeQAAnswerVote() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('id', '1'); + $request->setData('type', '-1'); + + $this->module->apiChangeQAAnswerVote($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('id', '1'); + $request->setData('type', '1'); + + $this->module->apiChangeQAAnswerVote($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\QA\Controller\ApiController + * @group module + */ + public function testApiChangeQAAnswerVoteInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiChangeQAAnswerVote($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } +} diff --git a/tests/Controller/test.md b/tests/Controller/test.md new file mode 100644 index 0000000..d35f959 --- /dev/null +++ b/tests/Controller/test.md @@ -0,0 +1,3 @@ +# Test Title + +Some **test** text. \ No newline at end of file diff --git a/tests/Models/4QAAnswerMapperTest.php b/tests/Models/QAAnswerMapperTest.php old mode 100755 new mode 100644 similarity index 75% rename from tests/Models/4QAAnswerMapperTest.php rename to tests/Models/QAAnswerMapperTest.php index 14dc39e..1f97bb0 --- a/tests/Models/4QAAnswerMapperTest.php +++ b/tests/Models/QAAnswerMapperTest.php @@ -20,7 +20,6 @@ use Modules\QA\Models\NullQAQuestion; use Modules\QA\Models\QAAnswer; use Modules\QA\Models\QAAnswerMapper; use Modules\QA\Models\QAAnswerStatus; -use phpOMS\Utils\RnG\Text; /** * @internal @@ -28,6 +27,7 @@ use phpOMS\Utils\RnG\Text; final class QAAnswerMapperTest extends \PHPUnit\Framework\TestCase { /** + * @depends Modules\QA\tests\Models\QAQuestionMapperTest::testCRUD * @covers Modules\QA\Models\QAAnswerMapper * @group module */ @@ -35,19 +35,19 @@ final class QAAnswerMapperTest extends \PHPUnit\Framework\TestCase { $answer = new QAAnswer(); - $answer->setAnswer('Answer content'); + $answer->answer = 'Answer content'; $answer->setStatus(QAAnswerStatus::ACTIVE); - $answer->createdBy = new Profile(new NullAccount(1)); - $answer->setQuestion(new NullQAQuestion(1)); - $answer->setAccepted(true); + $answer->createdBy = new Profile(new NullAccount(1)); + $answer->question = new NullQAQuestion(1); + $answer->isAccepted = true; $id = QAAnswerMapper::create($answer); self::assertGreaterThan(0, $answer->getId()); self::assertEquals($id, $answer->getId()); $answerR = QAAnswerMapper::get($answer->getId()); - self::assertEquals($answer->getAnswer(), $answerR->getAnswer()); - self::assertEquals($answer->getQuestion()->getId(), $answerR->getQuestion()->getId()); + self::assertEquals($answer->answer, $answerR->answer); + self::assertEquals($answer->question->getId(), $answerR->question->getId()); self::assertEquals($answer->getStatus(), $answerR->getStatus()); self::assertEquals($answer->isAccepted, $answerR->isAccepted); self::assertEquals($answer->createdBy->account->getId(), $answerR->createdBy->account->getId()); diff --git a/tests/Models/QAAnswerTest.php b/tests/Models/QAAnswerTest.php index 5ae28c7..42a94a8 100755 --- a/tests/Models/QAAnswerTest.php +++ b/tests/Models/QAAnswerTest.php @@ -14,51 +14,108 @@ declare(strict_types=1); namespace Modules\QA\tests\Models; -use Modules\Profile\Models\NullProfile; -use Modules\QA\Models\NullQAQuestion; +use Modules\Admin\Models\NullAccount; +use Modules\Media\Models\Media; use Modules\QA\Models\QAAnswer; use Modules\QA\Models\QAAnswerStatus; +use Modules\QA\Models\QAAnswerVote; /** * @internal */ final class QAAnswerTest extends \PHPUnit\Framework\TestCase { + private QAAnswer $answer; + + /** + * {@inheritdoc} + */ + protected function setUp() : void + { + $this->answer = new QAAnswer(); + } + /** * @covers Modules\QA\Models\QAAnswer * @group module */ public function testDefault() : void { - $answer = new QAAnswer(); - - self::assertEquals(0, $answer->getId()); - self::assertEquals('', $answer->getAnswer()); - self::assertEquals(0, $answer->getQuestion()->getId()); - self::assertFalse($answer->isAccepted); - self::assertEquals(QAAnswerStatus::ACTIVE, $answer->getStatus()); - self::assertEquals(0, $answer->createdBy->getId()); - self::assertInstanceOf('\DateTimeImmutable', $answer->createdAt); + self::assertEquals(0, $this->answer->getId()); + self::assertEquals('', $this->answer->answer); + self::assertEquals(0, $this->answer->question->getId()); + self::assertFalse($this->answer->isAccepted); + self::assertEquals(QAAnswerStatus::ACTIVE, $this->answer->getStatus()); + self::assertEquals(0, $this->answer->createdBy->getId()); + self::assertEquals(0, $this->answer->getVoteScore()); + self::assertEquals(0, $this->answer->getAccountVoteScore(0)); + self::assertEquals([], $this->answer->getMedia()); + self::assertEquals([], $this->answer->getVotes()); + self::assertInstanceOf('\DateTimeImmutable', $this->answer->createdAt); } /** * @covers Modules\QA\Models\QAAnswer * @group module */ - public function testSetGet() : void + public function testStatusInputOutput() : void { - $answer = new QAAnswer(); + $this->answer->setStatus(QAAnswerStatus::ACTIVE); + self::assertEquals(QAAnswerStatus::ACTIVE, $this->answer->getStatus()); + } - $answer->setAnswer('Answer content'); - $answer->setStatus(QAAnswerStatus::ACTIVE); - $answer->setQuestion(new NullQAQuestion(3)); - $answer->createdBy = new NullProfile(1); - $answer->setAccepted(true); + /** + * @covers Modules\QA\Models\QAAnswer + * @group module + */ + public function testMediaInputOutput() : void + { + $this->answer->addMedia(new Media()); + self::assertCount(1, $this->answer->getMedia()); + } - self::assertEquals('Answer content', $answer->getAnswer()); - self::assertEquals(QAAnswerStatus::ACTIVE, $answer->getStatus()); - self::assertEquals(1, $answer->createdBy->getId()); - self::assertEquals(3, $answer->getQuestion()->getId()); - self::assertTrue($answer->isAccepted); + /** + * @covers Modules\QA\Models\QAAnswer + * @group module + */ + public function testVoteInputOutput() : void + { + $vote = new QAAnswerVote(); + $vote->createdBy = new NullAccount(1); + $vote->score = 1; + + $this->answer->addVote($vote); + self::assertCount(1, $this->answer->getVotes()); + self::assertEquals(1, $this->answer->getVoteScore()); + self::assertEquals(1, $this->answer->getAccountVoteScore(1)); + } + + /** + * @covers Modules\QA\Models\QAAnswer + * @group module + */ + public function testSerialize() : void + { + $this->answer->setStatus(QAAnswerStatus::ACTIVE); + $this->answer->answer = 'Answer'; + $this->answer->answerRaw = 'AnswerRaw'; + + $serialized = $this->answer->jsonSerialize(); + unset($serialized['question']); + unset($serialized['createdBy']); + unset($serialized['createdAt']); + + self::assertEquals( + [ + 'id' => 0, + 'status' => QAAnswerStatus::ACTIVE, + 'answer' => 'Answer', + 'answerRaw' => 'AnswerRaw', + 'isAccepted' => false, + 'votes' => [], + 'media' => [], + ], + $serialized + ); } } diff --git a/tests/Models/QAAnswerVoteMapperTest.php b/tests/Models/QAAnswerVoteMapperTest.php new file mode 100644 index 0000000..cf865a8 --- /dev/null +++ b/tests/Models/QAAnswerVoteMapperTest.php @@ -0,0 +1,48 @@ +answer = 1; + $vote->score = 1; + $vote->createdBy = new NullAccount(1); + + $id = QAAnswerVoteMapper::create($vote); + self::assertGreaterThan(0, $vote->getId()); + self::assertEquals($id, $vote->getId()); + + $voteR = QAAnswerVoteMapper::get($vote->getId()); + self::assertEquals($vote->answer, $voteR->answer); + self::assertEquals($vote->score, $voteR->score); + + self::assertEquals(1, QAAnswerVoteMapper::findVote(1, 1)->getId()); + } +} diff --git a/tests/Models/QAAnswerVoteTest.php b/tests/Models/QAAnswerVoteTest.php new file mode 100644 index 0000000..fa1628e --- /dev/null +++ b/tests/Models/QAAnswerVoteTest.php @@ -0,0 +1,44 @@ +vote = new QAAnswerVote(); + } + + /** + * @covers Modules\QA\Models\QAAnswerVote + * @group module + */ + public function testDefault() : void + { + self::assertEquals(0, $this->vote->getId()); + self::assertEquals(0, $this->vote->answer); + self::assertEquals(0, $this->vote->score); + } +} diff --git a/tests/Models/QAAppTest.php b/tests/Models/QAAppTest.php new file mode 100644 index 0000000..010f18c --- /dev/null +++ b/tests/Models/QAAppTest.php @@ -0,0 +1,62 @@ +app = new QAApp(); + } + + /** + * @covers Modules\QA\Models\QAApp + * @group module + */ + public function testDefault() : void + { + self::assertEquals(0, $this->app->getId()); + self::assertEquals('', $this->app->name); + } + + /** + * @covers Modules\QA\Models\QAApp + * @group module + */ + public function testSerialize() : void + { + $this->app->name = 'Test Title'; + + $serialized = $this->app->jsonSerialize(); + + self::assertEquals( + [ + 'id' => 0, + 'name' => 'Test Title', + ], + $serialized + ); + } +} diff --git a/tests/Models/QAHelperMapperTest.php b/tests/Models/QAHelperMapperTest.php new file mode 100644 index 0000000..8ce82b9 --- /dev/null +++ b/tests/Models/QAHelperMapperTest.php @@ -0,0 +1,33 @@ +question = new QAQuestion(); + } + /** * @covers Modules\QA\Models\QAQuestion * @group module */ public function testDefault() : void { - $question = new QAQuestion(); - - self::assertEquals(0, $question->getId()); - self::assertEquals('', $question->name); - self::assertEquals('', $question->question); - self::assertEquals(QAQuestionStatus::ACTIVE, $question->getStatus()); - self::assertEquals('', $question->getLanguage()); - self::assertEquals(0, $question->createdBy->getId()); - self::assertInstanceOf('\DateTimeImmutable', $question->createdAt); - self::assertEquals([], $question->getTags()); + self::assertEquals(0, $this->question->getId()); + self::assertEquals('', $this->question->name); + self::assertEquals('', $this->question->question); + self::assertEquals('', $this->question->questionRaw); + self::assertEquals(QAQuestionStatus::ACTIVE, $this->question->getStatus()); + self::assertEquals(ISO639x1Enum::_EN, $this->question->getLanguage()); + self::assertEquals(0, $this->question->createdBy->getId()); + self::assertInstanceOf('\DateTimeImmutable', $this->question->createdAt); + self::assertFalse($this->question->hasAccepted()); + self::assertEquals([0 => 0], $this->question->getAccounts()); // includes createdBy + self::assertEquals([], $this->question->getTags()); + self::assertEquals([], $this->question->getMedia()); + self::assertEquals([], $this->question->getAnswers()); + self::assertEquals([], $this->question->getAnswersByScore()); + self::assertEquals(0, $this->question->getVoteScore()); + self::assertEquals(0, $this->question->getAccountVoteScore(0)); + self::assertEquals(0, $this->question->getAnswerCount()); } /** * @covers Modules\QA\Models\QAQuestion * @group module */ - public function testSetGet() : void + public function testStatusInputOutput() : void { - $question = new QAQuestion(); + $this->question->setStatus(QAQuestionStatus::ACTIVE); + self::assertEquals(QAQuestionStatus::ACTIVE, $this->question->getStatus()); + } - $question->name = 'Question Name'; - $question->question = 'Question content'; - $question->setStatus(QAQuestionStatus::ACTIVE); - $question->createdBy = new NullProfile(1); - $question->setLanguage('en'); + /** + * @covers Modules\QA\Models\QAQuestion + * @group module + */ + public function testLanguageInputOutput() : void + { + $this->question->setLanguage(ISO639x1Enum::_DE); + self::assertEquals(ISO639x1Enum::_DE, $this->question->getLanguage()); + } - self::assertEquals('Question Name', $question->name); - self::assertEquals('Question content', $question->question); - self::assertEquals(QAQuestionStatus::ACTIVE, $question->getStatus()); - self::assertEquals('en', $question->getLanguage()); - self::assertEquals(1, $question->createdBy->getId()); + /** + * @covers Modules\QA\Models\QAQuestion + * @group module + */ + public function testMediaInputOutput() : void + { + $this->question->addMedia(new Media()); + self::assertCount(1, $this->question->getMedia()); + } + + /** + * @covers Modules\QA\Models\QAQuestion + * @group module + */ + public function testTagInputOutput() : void + { + $tag = new Tag(); + $tag->setL11n('Tag'); + + $this->question->addTag($tag); + self::assertEquals($tag, $this->question->getTag(0)); + self::assertCount(1, $this->question->getTags()); + } + + /** + * @covers Modules\QA\Models\QAQuestion + * @group module + */ + public function testTagRemove() : void + { + $tag = new Tag(); + $tag->setL11n('Tag'); + + $this->question->addTag($tag); + self::assertTrue($this->question->removeTag(0)); + self::assertCount(0, $this->question->getTags()); + self::assertFalse($this->question->removeTag(0)); + } + + /** + * @covers Modules\QA\Models\QAQuestion + * @group module + */ + public function testAnswerInputOutput() : void + { + $answer = new QAAnswer(); + $answer->createdBy = new NullProfile(1); + $answer->isAccepted = true; + + $this->question->addAnswer($answer); + $this->question->addAnswer(clone $answer); + self::assertTrue($this->question->hasAccepted()); + self::assertCount(1, $this->question->getAccounts()); + self::assertCount(2, $this->question->getAnswers()); + self::assertCount(2, $this->question->getAnswersByScore()); + self::assertEquals(2, $this->question->getAnswerCount()); + } + + /** + * @covers Modules\QA\Models\QAQuestion + * @group module + */ + public function testVoteInputOutput() : void + { + $vote = new QAQuestionVote(); + $vote->createdBy = new NullAccount(1); + $vote->score = 1; + + $this->question->addVote($vote); + self::assertCount(1, $this->question->getVotes()); + self::assertEquals(1, $this->question->getVoteScore()); + self::assertEquals(1, $this->question->getAccountVoteScore(1)); + } + + /** + * @covers Modules\QA\Models\QAQuestion + * @group module + */ + public function testSerialize() : void + { + $this->question->name = 'Test Title'; + $this->question->setStatus(QAQuestionStatus::ACTIVE); + $this->question->question = 'Question'; + $this->question->questionRaw = 'QuestionRaw'; + $this->question->setLanguage(ISO639x1Enum::_DE); + + $serialized = $this->question->jsonSerialize(); + unset($serialized['app']); + unset($serialized['createdBy']); + unset($serialized['createdAt']); + + self::assertEquals( + [ + 'id' => 0, + 'name' => 'Test Title', + 'status' => QAQuestionStatus::ACTIVE, + 'question' => 'Question', + 'questionRaw' => 'QuestionRaw', + 'language' => ISO639x1Enum::_DE, + 'tags' => [], + 'answers' => [], + 'votes' => [], + 'media' => [], + ], + $serialized + ); } } diff --git a/tests/Models/QAQuestionVoteMapperTest.php b/tests/Models/QAQuestionVoteMapperTest.php new file mode 100644 index 0000000..2fc66b8 --- /dev/null +++ b/tests/Models/QAQuestionVoteMapperTest.php @@ -0,0 +1,48 @@ +question = 1; + $vote->score = 1; + $vote->createdBy = new NullAccount(1); + + $id = QAQuestionVoteMapper::create($vote); + self::assertGreaterThan(0, $vote->getId()); + self::assertEquals($id, $vote->getId()); + + $voteR = QAQuestionVoteMapper::get($vote->getId()); + self::assertEquals($vote->question, $voteR->question); + self::assertEquals($vote->score, $voteR->score); + + self::assertEquals(1, QAQuestionVoteMapper::findVote(1, 1)->getId()); + } +} diff --git a/tests/Models/QAQuestionVoteTest.php b/tests/Models/QAQuestionVoteTest.php new file mode 100644 index 0000000..92532f9 --- /dev/null +++ b/tests/Models/QAQuestionVoteTest.php @@ -0,0 +1,44 @@ +vote = new QAQuestionVote(); + } + + /** + * @covers Modules\QA\Models\QAQuestionVote + * @group module + */ + public function testDefault() : void + { + self::assertEquals(0, $this->vote->getId()); + self::assertEquals(0, $this->vote->question); + self::assertEquals(0, $this->vote->score); + } +}