From a629fcacdb748f3811308748f7fdd89d3b74e8f4 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Tue, 16 Nov 2021 00:05:43 +0100 Subject: [PATCH] cs fixes, bug fixes, code coverage --- .../Shop/Themes/Default/tpl/header.tpl.php | 18 +- Controller/ApiController.php | 44 +--- Models/NullTicketAttribute.php | 1 + Models/Ticket.php | 23 ++ Models/TicketAttribute.php | 18 +- Models/TicketAttributeType.php | 45 +--- Models/TicketAttributeTypeL11n.php | 31 +-- Models/TicketAttributeValue.php | 38 ++- Models/TicketElement.php | 7 +- Theme/Backend/support-ticket.tpl.php | 4 +- Views/TicketView.php | 26 ++- .../Api/ApiControllerAttributeTrait.php | 221 ++++++++++++++++++ .../Api/ApiControllerTicketTrait.php | 158 +++++++++++++ tests/Controller/ApiControllerTest.php | 93 ++++++++ tests/Models/SupportAppTest.php | 62 +++++ tests/Models/TicketAttributeTest.php | 63 +++++ tests/Models/TicketAttributeTypeL11nTest.php | 87 +++++++ tests/Models/TicketAttributeTypeTest.php | 82 +++++++ tests/Models/TicketAttributeValueTest.php | 133 +++++++++++ tests/Models/TicketElementTest.php | 66 ++++++ tests/Models/TicketTest.php | 118 ++++++++++ tests/Views/TicketViewTest.php | 45 ++++ 22 files changed, 1279 insertions(+), 104 deletions(-) create mode 100644 tests/Controller/Api/ApiControllerAttributeTrait.php create mode 100644 tests/Controller/Api/ApiControllerTicketTrait.php create mode 100644 tests/Controller/ApiControllerTest.php create mode 100644 tests/Models/SupportAppTest.php create mode 100644 tests/Models/TicketAttributeTest.php create mode 100644 tests/Models/TicketAttributeTypeL11nTest.php create mode 100644 tests/Models/TicketAttributeTypeTest.php create mode 100644 tests/Models/TicketAttributeValueTest.php create mode 100644 tests/Models/TicketElementTest.php create mode 100644 tests/Models/TicketTest.php create mode 100644 tests/Views/TicketViewTest.php diff --git a/Admin/Install/Application/Shop/Themes/Default/tpl/header.tpl.php b/Admin/Install/Application/Shop/Themes/Default/tpl/header.tpl.php index 6b96bdf..a904833 100755 --- a/Admin/Install/Application/Shop/Themes/Default/tpl/header.tpl.php +++ b/Admin/Install/Application/Shop/Themes/Default/tpl/header.tpl.php @@ -16,13 +16,13 @@ use phpOMS\Uri\UriFactory; ?>
- - + +
\ No newline at end of file diff --git a/Controller/ApiController.php b/Controller/ApiController.php index 6876067..a936664 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -40,7 +40,6 @@ use phpOMS\Message\NotificationLevel; use phpOMS\Message\RequestAbstract; use phpOMS\Message\ResponseAbstract; use phpOMS\Model\Message\FormValidation; -use phpOMS\Utils\Parser\Markdown\Markdown; /** * Api controller for the tickets module. @@ -294,7 +293,7 @@ final class ApiController extends Controller public function apiTicketElementSet(RequestAbstract $request, ResponseAbstract $response, $data = null) : void { $old = clone TicketElementMapper::get((int) $request->getData('id')); - $new = $this->updateTicketElementFromRequest($request); + $new = $this->updateTicketElementFromRequest($request, $response); $this->updateModel($request->header->account, $old, $new, TicketElementMapper::class, 'ticketelement', $request->getOrigin()); //$this->updateModel($request->header->account, $ticket, $ticket, TicketMapper::class, 'ticket', $request->getOrigin()); @@ -310,31 +309,12 @@ final class ApiController extends Controller * * @since 1.0.0 */ - private function updateTicketElementFromRequest(RequestAbstract $request) : TicketElementMapper + private function updateTicketElementFromRequest(RequestAbstract $request, ResponseAbstract $response) : TicketElementMapper { - $element = TicketElementMapper::get((int) ($request->getData('id'))); - $element->taskElement->due = new \DateTime((string) ($request->getData('due') ?? $element->getDue()->format('Y-m-d H:i:s'))); - $element->taskElement->description = Markdown::parse((string) ($request->getData('plain') ?? $element->taskElement->descriptionRaw)); - $element->taskElement->descriptionRaw = (string) ($request->getData('plain') ?? $element->taskElement->descriptionRaw); - $element->taskElement->setStatus((int) ($request->getData('status') ?? $element->taskElement->getStatus())); + $element = TicketElementMapper::get((int) ($request->getData('id'))); - $tos = $request->getData('to') ?? $request->header->account; - if (!\is_array($tos)) { - $tos = [$tos]; - } - - $ccs = $request->getData('cc') ?? []; - if (!\is_array($ccs)) { - $ccs = [$ccs]; - } - - foreach ($tos as $to) { - $element->taskElement->addTo($to); - } - - foreach ($ccs as $cc) { - $element->taskElement->addCC($cc); - } + $request->setData('id', $element->task, true); + $this->app->moduleManager->get('Tasks')->apiTaskElementSet($request, $response); return $element; } @@ -517,8 +497,8 @@ final class ApiController extends Controller */ private function createTicketAttributeTypeL11nFromRequest(RequestAbstract $request) : TicketAttributeTypeL11n { - $attrL11n = new TicketAttributeTypeL11n(); - $attrL11n->setType((int) ($request->getData('type') ?? 0)); + $attrL11n = new TicketAttributeTypeL11n(); + $attrL11n->type = (int) ($request->getData('type') ?? 0); $attrL11n->setLanguage((string) ( $request->getData('language') ?? $request->getLanguage() )); @@ -571,7 +551,6 @@ final class ApiController extends Controller } $attrType = $this->createTicketAttributeTypeFromRequest($request); - $attrType->setL11n($request->getData('title'), $request->getData('language')); $this->createModel($request->header->account, $attrType, TicketAttributeTypeMapper::class, 'attr_type', $request->getOrigin()); $this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Attribute type', 'Attribute type successfully created', $attrType); @@ -589,9 +568,9 @@ final class ApiController extends Controller private function createTicketAttributeTypeFromRequest(RequestAbstract $request) : TicketAttributeType { $attrType = new TicketAttributeType(); - $attrType->name = (string) ($request->getData('name') ?? ''); - $attrType->setFields((int) ($request->getData('fields') ?? 0)); - $attrType->setCustom((bool) ($request->getData('custom') ?? false)); + $attrType->setL11n((string) ($request->getData('title') ?? ''), $request->getData('language') ?? ISO639x1Enum::_EN); + $attrType->fields = (int) ($request->getData('fields') ?? 0); + $attrType->custom = (bool) ($request->getData('custom') ?? false); return $attrType; } @@ -608,8 +587,7 @@ final class ApiController extends Controller private function validateTicketAttributeTypeCreate(RequestAbstract $request) : array { $val = []; - if (($val['name'] = empty($request->getData('name'))) - || ($val['title'] = empty($request->getData('title'))) + if (($val['title'] = empty($request->getData('title'))) ) { return $val; } diff --git a/Models/NullTicketAttribute.php b/Models/NullTicketAttribute.php index 6787ddb..dd04fa5 100755 --- a/Models/NullTicketAttribute.php +++ b/Models/NullTicketAttribute.php @@ -33,6 +33,7 @@ final class NullTicketAttribute extends TicketAttribute */ public function __construct(int $id = 0) { + parent::__construct(); $this->id = $id; } } diff --git a/Models/Ticket.php b/Models/Ticket.php index 7563be6..6ae95b4 100755 --- a/Models/Ticket.php +++ b/Models/Ticket.php @@ -208,4 +208,27 @@ class Ticket { return $this->attributes; } + + /** + * {@inheritdoc} + */ + public function toArray() : array + { + return [ + 'id' => $this->id, + 'task' => $this->task, + 'app' => $this->app, + 'for' => $this->for, + 'ticketElements' => $this->ticketElements, + 'attributes' => $this->attributes, + ]; + } + + /** + * {@inheritdoc} + */ + public function jsonSerialize() + { + return $this->toArray(); + } } diff --git a/Models/TicketAttribute.php b/Models/TicketAttribute.php index 09a560c..190875c 100755 --- a/Models/TicketAttribute.php +++ b/Models/TicketAttribute.php @@ -58,6 +58,17 @@ class TicketAttribute implements \JsonSerializable, ArrayableInterface */ public TicketAttributeValue $value; + /** + * Constructor. + * + * @since 1.0.0 + */ + public function __construct() + { + $this->type = new TicketAttributeType(); + $this->value = new TicketAttributeValue(); + } + /** * Get id * @@ -75,7 +86,12 @@ class TicketAttribute implements \JsonSerializable, ArrayableInterface */ public function toArray() : array { - return []; + return [ + 'id' => $this->id, + 'ticket' => $this->ticket, + 'type' => $this->type, + 'value' => $this->value, + ]; } /** diff --git a/Models/TicketAttributeType.php b/Models/TicketAttributeType.php index 882a513..917f6f2 100755 --- a/Models/TicketAttributeType.php +++ b/Models/TicketAttributeType.php @@ -49,7 +49,7 @@ class TicketAttributeType implements \JsonSerializable, ArrayableInterface * @var int * @since 1.0.0 */ - protected int $fields = 0; + public int $fields = 0; /** * Is a custom value allowed (e.g. custom string) @@ -57,7 +57,7 @@ class TicketAttributeType implements \JsonSerializable, ArrayableInterface * @var bool * @since 1.0.0 */ - protected bool $custom = false; + public bool $custom = false; public string $validationPattern = ''; @@ -66,9 +66,9 @@ class TicketAttributeType implements \JsonSerializable, ArrayableInterface /** * Localization * - * @var TicketAttributeTypeL11n + * @var string | TicketAttributeTypeL11n */ - protected string|TicketAttributeTypeL11n $l11n; + protected string | TicketAttributeTypeL11n $l11n; /** * Possible default attribute values @@ -134,40 +134,19 @@ class TicketAttributeType implements \JsonSerializable, ArrayableInterface return $this->l11n instanceof TicketAttributeTypeL11n ? $this->l11n->title : $this->l11n; } - /** - * Set fields - * - * @param int $fields Fields - * - * @return void - * - * @since 1.0.0 - */ - public function setFields(int $fields) : void - { - $this->fields = $fields; - } - - /** - * Set custom - * - * @param bool $custom FieldsCustom - * - * @return void - * - * @since 1.0.0 - */ - public function setCustom(bool $custom) : void - { - $this->custom = $custom; - } - /** * {@inheritdoc} */ public function toArray() : array { - return []; + return [ + 'id' => $this->id, + 'name' => $this->name, + 'fields' => $this->fields, + 'custom' => $this->custom, + 'validationPattern' => $this->validationPattern, + 'isRequired' => $this->isRequired, + ]; } /** diff --git a/Models/TicketAttributeTypeL11n.php b/Models/TicketAttributeTypeL11n.php index f8ebc8e..13f50be 100755 --- a/Models/TicketAttributeTypeL11n.php +++ b/Models/TicketAttributeTypeL11n.php @@ -41,7 +41,7 @@ class TicketAttributeTypeL11n implements \JsonSerializable, ArrayableInterface * @var int|TicketAttributeType * @since 1.0.0 */ - protected int|TicketAttributeType $type = 0; + public int | TicketAttributeType $type = 0; /** * Language. @@ -88,29 +88,15 @@ class TicketAttributeTypeL11n implements \JsonSerializable, ArrayableInterface } /** - * Get attribute type + * Get language * - * @return int|TicketAttributeType + * @return string * * @since 1.0.0 */ - public function getType() : int | TicketAttributeType + public function getLanguage() : string { - return $this->type; - } - - /** - * Set type. - * - * @param int $type Type id - * - * @return void - * - * @since 1.0.0 - */ - public function setType(int $type) : void - { - $this->type = $type; + return $this->language; } /** @@ -132,7 +118,12 @@ class TicketAttributeTypeL11n implements \JsonSerializable, ArrayableInterface */ public function toArray() : array { - return []; + return [ + 'id' => $this->id, + 'title' => $this->title, + 'type' => $this->type, + 'language' => $this->language, + ]; } /** diff --git a/Models/TicketAttributeValue.php b/Models/TicketAttributeValue.php index cb6ab53..971dadc 100755 --- a/Models/TicketAttributeValue.php +++ b/Models/TicketAttributeValue.php @@ -21,7 +21,7 @@ use phpOMS\Localization\ISO639x1Enum; /** * Ticket attribute value class. * - * The relation with the type/ticket is defined in the TicketAttribute class. + * The relation with the type/supplier is defined in the TicketAttribute class. * * @package Modules\Support\Models * @license OMS License 1.0 @@ -189,6 +189,18 @@ class TicketAttributeValue implements \JsonSerializable, ArrayableInterface $this->language = $language; } + /** + * Get language + * + * @return string + * + * @since 1.0.0 + */ + public function getLanguage() : string + { + return $this->language; + } + /** * Set country * @@ -203,12 +215,34 @@ class TicketAttributeValue implements \JsonSerializable, ArrayableInterface $this->country = $country; } + /** + * Get country + * + * @return string + * + * @since 1.0.0 + */ + public function getCountry() : string + { + return $this->country; + } + /** * {@inheritdoc} */ public function toArray() : array { - return []; + return [ + 'id' => $this->id, + 'type' => $this->type, + 'valueInt' => $this->valueInt, + 'valueStr' => $this->valueStr, + 'valueDec' => $this->valueDec, + 'valueDat' => $this->valueDat, + 'isDefault' => $this->isDefault, + 'language' => $this->language, + 'country' => $this->country, + ]; } /** diff --git a/Models/TicketElement.php b/Models/TicketElement.php index e374547..c7b92ee 100644 --- a/Models/TicketElement.php +++ b/Models/TicketElement.php @@ -87,7 +87,12 @@ class TicketElement implements \JsonSerializable */ public function toArray() : array { - return $this->taskElement->toArray(); + return [ + 'id' => $this->id, + 'time' => $this->time, + 'ticket' => $this->ticket, + 'taskElement' => $this->taskElement, + ]; } /** diff --git a/Theme/Backend/support-ticket.tpl.php b/Theme/Backend/support-ticket.tpl.php index bc7acef..f904003 100755 --- a/Theme/Backend/support-ticket.tpl.php +++ b/Theme/Backend/support-ticket.tpl.php @@ -19,8 +19,8 @@ use Modules\Tasks\Models\TaskStatus; use phpOMS\Uri\UriFactory; /** - * @var \phpOMS\Views\View $this - * @var \Modules\Support\Models\Ticket $ticket + * @var \Modules\Support\Vies\TicketView $this + * @var \Modules\Support\Models\Ticket $ticket */ $ticket = $this->getData('ticket'); $task = $ticket->task; diff --git a/Views/TicketView.php b/Views/TicketView.php index 21fdecd..eaa126d 100644 --- a/Views/TicketView.php +++ b/Views/TicketView.php @@ -14,6 +14,8 @@ declare(strict_types=1); namespace Modules\Support\Views; +use Modules\Media\Models\Media; +use Modules\Media\Models\NullMedia; use Modules\Profile\Models\ProfileMapper; use Modules\Tasks\Models\TaskStatus; use phpOMS\Uri\UriFactory; @@ -29,6 +31,24 @@ use phpOMS\Views\View; */ class TicketView extends View { + /** + * User profile image. + * + * @var Media + * @since 1.0.0 + */ + public Media $defaultProfileImage; + + /** + * Constructor + * + * @since 1.0.0 + */ + public function __construct() + { + $this->defaultProfileImage = new NullMedia(); + } + /** * Get the profile image * @@ -44,11 +64,11 @@ class TicketView extends View { $profile = ProfileMapper::getFor($account, 'account'); - if ($profile === null || $profile->image->getPath() === '') { - return UriFactory::build('Web/Backend/img/user_default_' . \mt_rand(1, 6) . '.png'); + if (($profile instanceof NullProfile) || $profile->image->getPath() === '') { + return UriFactory::build('{/prefix}' . $this->defaultProfileImage->getPath()); } - return UriFactory::build($profile->image->getPath()); + return UriFactory::build('{/prefix}' . $profile->image->getPath()); } /** diff --git a/tests/Controller/Api/ApiControllerAttributeTrait.php b/tests/Controller/Api/ApiControllerAttributeTrait.php new file mode 100644 index 0000000..8c2f06a --- /dev/null +++ b/tests/Controller/Api/ApiControllerAttributeTrait.php @@ -0,0 +1,221 @@ +header->account = 1; + $request->setData('title', 'EN:1'); + $request->setData('language', ISO639x1Enum::_EN); + + $this->module->apiTicketAttributeTypeCreate($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\Support\Controller\ApiController + * @group module + */ + public function testApiTicketAttributeTypeL11nCreate() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('title', 'DE:2'); + $request->setData('type', '1'); + $request->setData('language', ISO639x1Enum::_DE); + + $this->module->apiTicketAttributeTypeL11nCreate($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\Support\Controller\ApiController + * @group module + */ + public function testApiTicketAttributeValueIntCreate() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('default', '1'); + $request->setData('attributetype', '1'); + $request->setData('type', AttributeValueType::_INT); + $request->setData('value', '1'); + $request->setData('language', ISO639x1Enum::_DE); + $request->setData('country', ISO3166TwoEnum::_DEU); + + $this->module->apiTicketAttributeValueCreate($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\Support\Controller\ApiController + * @group module + */ + public function testApiTicketAttributeValueStrCreate() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('type', AttributeValueType::_STRING); + $request->setData('value', '1'); + $request->setData('language', ISO639x1Enum::_DE); + $request->setData('country', ISO3166TwoEnum::_DEU); + + $this->module->apiTicketAttributeValueCreate($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\Support\Controller\ApiController + * @group module + */ + public function testApiTicketAttributeValueFloatCreate() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('type', AttributeValueType::_FLOAT); + $request->setData('value', '1.1'); + $request->setData('language', ISO639x1Enum::_DE); + $request->setData('country', ISO3166TwoEnum::_DEU); + + $this->module->apiTicketAttributeValueCreate($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\Support\Controller\ApiController + * @group module + */ + public function testApiTicketAttributeValueDatCreate() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('type', AttributeValueType::_DATETIME); + $request->setData('value', '2020-08-02'); + $request->setData('language', ISO639x1Enum::_DE); + $request->setData('country', ISO3166TwoEnum::_DEU); + + $this->module->apiTicketAttributeValueCreate($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\Support\Controller\ApiController + * @group module + */ + public function testApiTicketAttributeCreate() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('ticket', '1'); + $request->setData('value', '1'); + $request->setData('type', '1'); + + $this->module->apiTicketAttributeCreate($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\Support\Controller\ApiController + * @group module + */ + public function testApiTicketAttributeValueCreateInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiTicketAttributeValueCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } + + /** + * @covers Modules\Support\Controller\ApiController + * @group module + */ + public function testApiTicketAttributeTypeCreateInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiTicketAttributeTypeCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } + + /** + * @covers Modules\Support\Controller\ApiController + * @group module + */ + public function testApiTicketAttributeTypeL11nCreateInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiTicketAttributeTypeL11nCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } + + /** + * @covers Modules\Support\Controller\ApiController + * @group module + */ + public function testApiTicketAttributeCreateInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiTicketAttributeCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } +} diff --git a/tests/Controller/Api/ApiControllerTicketTrait.php b/tests/Controller/Api/ApiControllerTicketTrait.php new file mode 100644 index 0000000..bf30692 --- /dev/null +++ b/tests/Controller/Api/ApiControllerTicketTrait.php @@ -0,0 +1,158 @@ +header->account = 1; + $request->setData('name', 'TestSupportApp'); + + $this->module->apiSupportAppCreate($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\Support\Controller\ApiController + * @group module + */ + public function testApiSupportAppCreateInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiSupportAppCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } + + /** + * @covers Modules\Support\Controller\ApiController + * @group module + */ + public function testApiTicketCreate() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('title', 'Test Ticket'); + $request->setData('plain', 'Test **content** here.'); + $request->setData('forward', '1'); + $request->setData('for', '1'); + + $this->module->apiTicketCreate($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\Support\Controller\ApiController + * @group module + */ + public function testApiTicketGet() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('id', '1'); + + $this->module->apiTicketGet($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\Support\Controller\ApiController + * @group module + */ + public function testApiTicketCreateInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiTicketCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } + + /** + * @covers Modules\Support\Controller\ApiController + * @group module + */ + public function testApiTicketElementCreate() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('ticket', '1'); + $request->setData('time', '10'); + $request->setData('status', TaskStatus::WORKING); + $request->setData('due', (new \DateTime('now'))->format('Y-m-d H:i:s')); + + $this->module->apiTicketElementCreate($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\Support\Controller\ApiController + * @group module + */ + public function testApiTicketElementGet() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('id', '1'); + + $this->module->apiTicketElementGet($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + } + + /** + * @covers Modules\Support\Controller\ApiController + * @group module + */ + public function testApiTicketElementCreateInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiTicketElementCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } +} diff --git a/tests/Controller/ApiControllerTest.php b/tests/Controller/ApiControllerTest.php new file mode 100644 index 0000000..a94d829 --- /dev/null +++ b/tests/Controller/ApiControllerTest.php @@ -0,0 +1,93 @@ +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('Support'); + + TestUtils::setMember($this->module, 'app', $this->app); + } + + use ApiControllerTicketTrait; + use ApiControllerAttributeTrait; +} diff --git a/tests/Models/SupportAppTest.php b/tests/Models/SupportAppTest.php new file mode 100644 index 0000000..d0e23b1 --- /dev/null +++ b/tests/Models/SupportAppTest.php @@ -0,0 +1,62 @@ +app = new SupportApp(); + } + + /** + * @covers Modules\Support\Models\SupportApp + * @group module + */ + public function testDefault() : void + { + self::assertEquals(0, $this->app->getId()); + self::assertEquals('', $this->app->name); + } + + /** + * @covers Modules\Support\Models\SupportApp + * @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/TicketAttributeTest.php b/tests/Models/TicketAttributeTest.php new file mode 100644 index 0000000..3fb232d --- /dev/null +++ b/tests/Models/TicketAttributeTest.php @@ -0,0 +1,63 @@ +attribute = new TicketAttribute(); + } + + /** + * @covers Modules\Support\Models\TicketAttribute + * @group module + */ + public function testDefault() : void + { + self::assertEquals(0, $this->attribute->getId()); + self::assertInstanceOf('\Modules\Support\Models\TicketAttributeType', $this->attribute->type); + self::assertInstanceOf('\Modules\Support\Models\TicketAttributeValue', $this->attribute->value); + } + + /** + * @covers Modules\Support\Models\TicketAttribute + * @group module + */ + public function testSerialize() : void + { + $serialized = $this->attribute->jsonSerialize(); + + self::assertEquals( + [ + 'id', + 'ticket', + 'type', + 'value', + ], + \array_keys($serialized) + ); + } +} diff --git a/tests/Models/TicketAttributeTypeL11nTest.php b/tests/Models/TicketAttributeTypeL11nTest.php new file mode 100644 index 0000000..da95b00 --- /dev/null +++ b/tests/Models/TicketAttributeTypeL11nTest.php @@ -0,0 +1,87 @@ +l11n = new TicketAttributeTypeL11n(); + } + + /** + * @covers Modules\Support\Models\TicketAttributeTypeL11n + * @group module + */ + public function testDefault() : void + { + self::assertEquals(0, $this->l11n->getId()); + self::assertEquals('', $this->l11n->title); + self::assertEquals(0, $this->l11n->type); + self::assertEquals(ISO639x1Enum::_EN, $this->l11n->getLanguage()); + } + + /** + * @covers Modules\Support\Models\TicketAttributeTypeL11n + * @group module + */ + public function testNameInputOutput() : void + { + $this->l11n->title = 'TestName'; + self::assertEquals('TestName', $this->l11n->title); + } + + /** + * @covers Modules\Support\Models\TicketAttributeTypeL11n + * @group module + */ + public function testLanguageInputOutput() : void + { + $this->l11n->setLanguage(ISO639x1Enum::_DE); + self::assertEquals(ISO639x1Enum::_DE, $this->l11n->getLanguage()); + } + + /** + * @covers Modules\Support\Models\TicketAttributeTypeL11n + * @group module + */ + public function testSerialize() : void + { + $this->l11n->title = 'Title'; + $this->l11n->type = 2; + $this->l11n->setLanguage(ISO639x1Enum::_DE); + + self::assertEquals( + [ + 'id' => 0, + 'title' => 'Title', + 'type' => 2, + 'language' => ISO639x1Enum::_DE, + ], + $this->l11n->jsonSerialize() + ); + } +} diff --git a/tests/Models/TicketAttributeTypeTest.php b/tests/Models/TicketAttributeTypeTest.php new file mode 100644 index 0000000..32ddff5 --- /dev/null +++ b/tests/Models/TicketAttributeTypeTest.php @@ -0,0 +1,82 @@ +type = new TicketAttributeType(); + } + + /** + * @covers Modules\Support\Models\TicketAttributeType + * @group module + */ + public function testDefault() : void + { + self::assertEquals(0, $this->type->getId()); + self::assertEquals('', $this->type->getL11n()); + } + + /** + * @covers Modules\Support\Models\TicketAttributeType + * @group module + */ + public function testL11nInputOutput() : void + { + $this->type->setL11n('Test'); + self::assertEquals('Test', $this->type->getL11n()); + + $this->type->setL11n(new TicketAttributeTypeL11n(0, 'NewTest')); + self::assertEquals('NewTest', $this->type->getL11n()); + } + + /** + * @covers Modules\Support\Models\TicketAttributeType + * @group module + */ + public function testSerialize() : void + { + $this->type->name = 'Title'; + $this->type->fields = 2; + $this->type->custom = true; + $this->type->validationPattern = '\d*'; + $this->type->isRequired = true; + + self::assertEquals( + [ + 'id' => 0, + 'name' => 'Title', + 'fields' => 2, + 'custom' => true, + 'validationPattern' => '\d*', + 'isRequired' => true, + ], + $this->type->jsonSerialize() + ); + } +} diff --git a/tests/Models/TicketAttributeValueTest.php b/tests/Models/TicketAttributeValueTest.php new file mode 100644 index 0000000..a03ce76 --- /dev/null +++ b/tests/Models/TicketAttributeValueTest.php @@ -0,0 +1,133 @@ +value = new TicketAttributeValue(); + } + + /** + * @covers Modules\Support\Models\TicketAttributeValue + * @group module + */ + public function testDefault() : void + { + self::assertEquals(0, $this->value->getId()); + self::assertNull($this->value->getValue()); + } + + /** + * @covers Modules\Support\Models\TicketAttributeValue + * @group module + */ + public function testLanguageInputOutput() : void + { + $this->value->setLanguage(ISO639x1Enum::_DE); + self::assertEquals(ISO639x1Enum::_DE, $this->value->getLanguage()); + } + + /** + * @covers Modules\Support\Models\TicketAttributeValue + * @group module + */ + public function testCountryInputOutput() : void + { + $this->value->setCountry(ISO3166TwoEnum::_DEU); + self::assertEquals(ISO3166TwoEnum::_DEU, $this->value->getCountry()); + } + + /** + * @covers Modules\Support\Models\TicketAttributeValue + * @group module + */ + public function testValueIntInputOutput() : void + { + $this->value->setValue(1); + self::assertEquals(1, $this->value->getValue()); + } + + /** + * @covers Modules\Support\Models\TicketAttributeValue + * @group module + */ + public function testValueFloatInputOutput() : void + { + $this->value->setValue(1.1); + self::assertEquals(1.1, $this->value->getValue()); + } + + /** + * @covers Modules\Support\Models\TicketAttributeValue + * @group module + */ + public function testValueStringInputOutput() : void + { + $this->value->setValue('test'); + self::assertEquals('test', $this->value->getValue()); + } + + /** + * @covers Modules\Support\Models\TicketAttributeValue + * @group module + */ + public function testValueDateInputOutput() : void + { + $this->value->setValue($dat = new \DateTime('now')); + self::assertEquals($dat->format('Y-m-d'), $this->value->getValue()->format('Y-m-d')); + } + + /** + * @covers Modules\Support\Models\TicketAttributeValue + * @group module + */ + public function testSerialize() : void + { + $this->value->type = 1; + $this->value->setValue('test'); + $this->value->isDefault = true; + $this->value->setLanguage(ISO639x1Enum::_DE); + $this->value->setCountry(ISO3166TwoEnum::_DEU); + + self::assertEquals( + [ + 'id' => 0, + 'type' => 1, + 'valueInt' => null, + 'valueStr' => 'test', + 'valueDec' => null, + 'valueDat' => null, + 'isDefault' => true, + 'language' => ISO639x1Enum::_DE, + 'country' => ISO3166TwoEnum::_DEU, + ], + $this->value->jsonSerialize() + ); + } +} diff --git a/tests/Models/TicketElementTest.php b/tests/Models/TicketElementTest.php new file mode 100644 index 0000000..c1b9e85 --- /dev/null +++ b/tests/Models/TicketElementTest.php @@ -0,0 +1,66 @@ +element = new TicketElement(); + } + + /** + * @covers Modules\Support\Models\TicketElement + * @group module + */ + public function testDefault() : void + { + self::assertEquals(0, $this->element->getId()); + self::assertEquals(0, $this->element->time); + self::assertEquals(0, $this->element->ticket); + } + + /** + * @covers Modules\Support\Models\TicketElement + * @group module + */ + public function testSerialize() : void + { + $this->element->time = 10; + $this->element->ticket = 2; + + $serialized = $this->element->jsonSerialize(); + unset($serialized['taskElement']); + + self::assertEquals( + [ + 'id' => 0, + 'time' => 10, + 'ticket' => 2, + ], + $serialized + ); + } +} diff --git a/tests/Models/TicketTest.php b/tests/Models/TicketTest.php new file mode 100644 index 0000000..1d5679d --- /dev/null +++ b/tests/Models/TicketTest.php @@ -0,0 +1,118 @@ +ticket = new Ticket(); + } + + /** + * @covers Modules\Support\Models\Ticket + * @group module + */ + public function testDefault() : void + { + self::assertEquals(0, $this->ticket->getId()); + self::assertNull($this->ticket->for); + self::assertEquals([], $this->ticket->getTicketElements()); + self::assertEquals([], $this->ticket->getAttributes()); + self::assertInstanceOf('\Modules\Tasks\Models\Task', $this->ticket->task); + self::assertInstanceOf('\Modules\Support\Models\TicketElement', $this->ticket->getTicketElement(999)); + self::assertInstanceOf('\Modules\Support\Models\SupportApp', $this->ticket->app); + } + + /** + * @covers Modules\Support\Models\Ticket + * @group module + */ + public function testElementInputOutput() : void + { + $element1 = new TicketElement(); + $element1->description = '1'; + + $element2 = new TicketElement(); + $element2->description = '2'; + + self::assertEquals(0, $this->ticket->addElement($element1)); + self::assertEquals(1, $this->ticket->addElement($element2)); + self::assertCount(2, $this->ticket->getTicketElements()); + self::assertEquals($element1, $this->ticket->getTicketElement(0)); + self::assertEquals([$element2, $element1], $this->ticket->invertTicketElements()); + } + + /** + * @covers Modules\Support\Models\Ticket + * @group module + */ + public function testElementRemove() : void + { + $element1 = new TicketElement(); + $element1->description = '1'; + + $this->ticket->addElement($element1); + self::assertCount(1, $this->ticket->getTicketElements()); + self::assertTrue($this->ticket->removeElement(0)); + self::assertCount(0, $this->ticket->getTicketElements()); + self::assertFalse($this->ticket->removeElement(0)); + } + + /** + * @covers Modules\Support\Models\Ticket + * @group module + */ + public function testAttributeInputOutput() : void + { + $attr = new TicketAttribute(); + $this->ticket->addAttribute($attr); + + self::assertCount(1, $this->ticket->getAttributes()); + } + + /** + * @covers Modules\Support\Models\Ticket + * @group module + */ + public function testSerialize() : void + { + $serialized = $this->ticket->jsonSerialize(); + unset($serialized['task']); + unset($serialized['app']); + + self::assertEquals( + [ + 'id' => 0, + 'for' => null, + 'ticketElements' => [], + 'attributes' => [], + ], + $serialized + ); + } +} diff --git a/tests/Views/TicketViewTest.php b/tests/Views/TicketViewTest.php new file mode 100644 index 0000000..8c1fd43 --- /dev/null +++ b/tests/Views/TicketViewTest.php @@ -0,0 +1,45 @@ +getAccountImage(999)); + } + + /** + * @covers Modules\Support\Views\TicketView + * @group framework + */ + public function testAccountImageUrl() : void + { + $view = new TicketView(); + + self::assertEquals('Web/Backend/img/default-user.jpg', $view->getAccountImage(1)); + } +}