impl. tests and fix validation status code

This commit is contained in:
Dennis Eichhorn 2020-10-06 21:51:40 +02:00
parent b6342b6b60
commit b48502a604
5 changed files with 289 additions and 129 deletions

View File

@ -18,6 +18,7 @@ use Modules\Admin\Models\NullAccount;
use Modules\Editor\Models\EditorDoc;
use Modules\Editor\Models\EditorDocMapper;
use Modules\Tag\Models\NullTag;
use phpOMS\Message\Http\RequestStatusCode;
use phpOMS\Message\NotificationLevel;
use phpOMS\Message\RequestAbstract;
use phpOMS\Message\ResponseAbstract;
@ -80,6 +81,7 @@ final class ApiController extends Controller
{
if (!empty($val = $this->validateEditorCreate($request))) {
$response->set('editor_create', new FormValidation($val));
$response->getHeader()->setStatusCode(RequestStatusCode::R_400);
return;
}

View File

@ -0,0 +1,191 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace Modules\Editor\tests\Controller;
use Model\CoreSettings;
use Modules\Admin\Models\AccountPermission;
use Modules\Admin\Models\NullAccount;
use Modules\Editor\Controller\ApiController;
use phpOMS\Account\Account;
use phpOMS\Account\AccountManager;
use phpOMS\Account\PermissionType;
use phpOMS\Application\ApplicationAbstract;
use phpOMS\Dispatcher\Dispatcher;
use phpOMS\Event\EventManager;
use phpOMS\Message\Http\HttpRequest;
use phpOMS\Message\Http\HttpResponse;
use phpOMS\Message\Http\RequestStatusCode;
use phpOMS\Module\ModuleManager;
use phpOMS\Router\WebRouter;
use phpOMS\Uri\HttpUri;
use phpOMS\Utils\TestUtils;
use Modules\Editor\Models\EditorDocMapper;
use Modules\Tag\Models\Tag;
use Modules\Tag\Models\TagMapper;
use Modules\Editor\Models\EditorDoc;
use Modules\Editor\Models\NullEditorDoc;
/**
* @internal
*/
class ApiControllerTest extends \PHPUnit\Framework\TestCase
{
protected ApplicationAbstract $app;
protected ApiController $module;
protected function setUp() : void
{
$this->app = new class() extends ApplicationAbstract
{
protected string $appName = 'Api';
};
$this->app->dbPool = $GLOBALS['dbpool'];
$this->app->orgId = 1;
$this->app->appName = 'Backend';
$this->app->accountManager = new AccountManager($GLOBALS['session']);
$this->app->appSettings = new CoreSettings($this->app->dbPool->get());
$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');
$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('Editor');
TestUtils::setMember($this->module, 'app', $this->app);
}
/**
* @covers Modules\Editor\Controller\ApiController
* @group module
*/
public function testCreateEditorDoc() : void
{
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->getHeader()->setAccount(1);
$request->setData('title', 'Controller Test Title');
$request->setData('plain', 'Controller Test Description');
$this->module->apiEditorCreate($request, $response);
self::assertEquals('Controller Test Title', $response->get('')['response']->getTitle());
self::assertGreaterThan(0, $response->get('')['response']->getId());
}
public function testCreateEditorDocWithExistingTag() : void
{
$tag = new Tag();
$tag->setTitle('EditorDocTest');
$tagId = TagMapper::create($tag);
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->getHeader()->setAccount(1);
$request->setData('title', 'Controller Test With Tag');
$request->setData('plain', 'Controller Test Description');
$request->setData('tag', '[' . $tagId . ']');
$this->module->apiEditorCreate($request, $response);
self::assertEquals('Controller Test With Tag', $response->get('')['response']->getTitle());
self::assertGreaterThan(0, $response->get('')['response']->getId());
}
public function testInvalidEditorDocCreateRequest() : void
{
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->getHeader()->setAccount(1);
$request->setData('title', 'Controller Test Title');
$this->module->apiEditorCreate($request, $response);
self::assertEquals(RequestStatusCode::R_400, $response->getHeader()->getStatusCode());
}
public function testUpdateEditorDoc() : void
{
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->getHeader()->setAccount(1);
$request->setData('id', '1');
$request->setData('title', 'Changed Title');
$this->module->apiEditorUpdate($request, $response);
self::assertEquals('Changed Title', $response->get('')['response']->getTitle());
self::assertEquals('Changed Title', EditorDocMapper::get(1)->getTitle());
self::assertEquals(1, $response->get('')['response']->getId());
}
public function testGetEditorDoc() : void
{
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->getHeader()->setAccount(1);
$request->setData('id', '1');
$this->module->apiEditorGet($request, $response);
self::assertEquals('Changed Title', $response->get('')['response']->getTitle());
}
public function testDeleteEditorDoc() : void
{
$doc = new EditorDoc();
$doc->setTitle('TestTitle');
$doc->setContent('TestContent');
$doc->setCreatedBy(new NullAccount(1));
$docId = EditorDocMapper::create($doc);
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->getHeader()->setAccount(1);
$request->setData('id', $docId);
$this->module->apiEditorDelete($request, $response);
self::assertEquals($docId, $response->get('')['response']->getId());
self::assertInstanceOf(NullEditorDoc::class, EditorDocMapper::get($docId));
}
}

View File

@ -1,101 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace Modules\Editor\tests;
use Model\CoreSettings;
use Modules\Admin\Models\AccountPermission;
use Modules\Editor\Controller\ApiController;
use phpOMS\Account\Account;
use phpOMS\Account\AccountManager;
use phpOMS\Account\PermissionType;
use phpOMS\Application\ApplicationAbstract;
use phpOMS\Dispatcher\Dispatcher;
use phpOMS\Event\EventManager;
use phpOMS\Message\Http\HttpRequest;
use phpOMS\Message\Http\HttpResponse;
use phpOMS\Module\ModuleManager;
use phpOMS\Router\WebRouter;
use phpOMS\Uri\HttpUri;
use phpOMS\Utils\TestUtils;
/**
* @internal
*/
class ControllerTest extends \PHPUnit\Framework\TestCase
{
protected ApplicationAbstract $app;
protected ApiController $module;
protected function setUp() : void
{
$this->app = new class() extends ApplicationAbstract
{
protected string $appName = 'Api';
};
$this->app->dbPool = $GLOBALS['dbpool'];
$this->app->orgId = 1;
$this->app->appName = 'Backend';
$this->app->accountManager = new AccountManager($GLOBALS['session']);
$this->app->appSettings = new CoreSettings($this->app->dbPool->get());
$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');
$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('Editor');
TestUtils::setMember($this->module, 'app', $this->app);
}
/**
* @covers Modules\Editor\Controller\ApiController
* @group module
*/
public function testCreateEditor() : void
{
$response = new HttpResponse();
$request = new HttpRequest(new HttpUri(''));
$request->getHeader()->setAccount(1);
$request->setData('title', 'Controller Test Title');
$request->setData('plain', 'Controller Test Description');
$this->module->apiEditorCreate($request, $response);
self::assertEquals('Controller Test Title', $response->get('')['response']->getTitle());
self::assertGreaterThan(0, $response->get('')['response']->getId());
}
}

View File

@ -16,53 +16,87 @@ namespace Modules\Tasks\tests\Models;
use Modules\Admin\Models\NullAccount;
use Modules\Editor\Models\EditorDoc;
use Modules\Tag\Models\Tag;
/**
* @internal
*/
class EditorDocTest extends \PHPUnit\Framework\TestCase
{
private EditorDoc $doc;
public function setUp() : void
{
$this->doc = new EditorDoc();
}
public function testDefault() : void
{
$doc = new EditorDoc();
self::assertEquals(0, $doc->getId());
self::assertEquals(0, $doc->getCreatedBy()->getId());
self::assertEquals('', $doc->getTitle());
self::assertEquals('', $doc->getContent());
self::assertEquals('', $doc->getPlain());
self::assertEquals((new \DateTime('now'))->format('Y-m-d'), $doc->getCreatedAt()->format('Y-m-d'));
self::assertEquals(0, $this->doc->getId());
self::assertEquals(0, $this->doc->getCreatedBy()->getId());
self::assertEquals('', $this->doc->getTitle());
self::assertEquals('', $this->doc->getContent());
self::assertEquals('', $this->doc->getPlain());
self::assertEquals([], $this->doc->getTags());
self::assertEquals((new \DateTime('now'))->format('Y-m-d'), $this->doc->getCreatedAt()->format('Y-m-d'));
}
public function testSetGet() : void
public function testCreatedByInputOutput() : void
{
$doc = new EditorDoc();
$this->doc->setCreatedBy(new NullAccount(1));
self::assertEquals(1, $this->doc->getCreatedBy()->getId());
}
$doc->setCreatedBy(new NullAccount(1));
self::assertEquals(1, $doc->getCreatedBy()->getId());
public function testTitleInputOutput() : void
{
$this->doc->setTitle('Title');
self::assertEquals('Title', $this->doc->getTitle());
}
$doc->setTitle('Title');
self::assertEquals('Title', $doc->getTitle());
public function testContentInputOutput() : void
{
$this->doc->setContent('Content');
self::assertEquals('Content', $this->doc->getContent());
}
$doc->setContent('Content');
self::assertEquals('Content', $doc->getContent());
public function testPlainInputOutput() : void
{
$this->doc->setPlain('Plain');
self::assertEquals('Plain', $this->doc->getPlain());
}
$doc->setPlain('Plain');
self::assertEquals('Plain', $doc->getPlain());
public function testPathInputOutput() : void
{
$this->doc->setPath('/some/test/path');
self::assertEquals('/some/test/path', $this->doc->getPath());
}
$doc->setPath('/some/path');
self::assertEquals('/some/path', $doc->getPath());
public function testTagInputOutput() : void
{
$tag = new Tag();
$tag->setTitle('Tag');
$this->doc->addTag($tag);
self::assertCount(1, $this->doc->getTags());
}
public function testSerialization() : void
{
$this->doc->setCreatedBy(new NullAccount(1));
$this->doc->setTitle('Title');
$this->doc->setContent('Content');
$this->doc->setPlain('Plain');
$this->doc->setPath('/some/path');
$arr = [
'id' => 0,
'title' => $doc->getTitle(),
'plain' => $doc->getPlain(),
'content' => $doc->getContent(),
'createdAt' => $doc->getCreatedAt(),
'createdBy' => $doc->getCreatedBy(),
'title' => $this->doc->getTitle(),
'plain' => $this->doc->getPlain(),
'content' => $this->doc->getContent(),
'createdAt' => $this->doc->getCreatedAt(),
'createdBy' => $this->doc->getCreatedBy(),
];
self::assertEquals($arr, $doc->toArray());
self::assertEquals($arr, $doc->jsonSerialize());
self::assertEquals(\json_encode($arr), $doc->__toString());
self::assertEquals($arr, $this->doc->toArray());
self::assertEquals($arr, $this->doc->jsonSerialize());
self::assertEquals(\json_encode($arr), $this->doc->__toString());
}
}

View File

@ -0,0 +1,34 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace Modules\Editor\tests\Models;
use Modules\Editor\Models\NullEditorDoc;
/**
* @internal
*/
final class NullEditorDocTest extends \PHPUnit\Framework\TestCase
{
public function testNull() : void
{
self::assertInstanceOf('\Modules\Editor\Models\EditorDoc', new NullEditorDoc());
}
public function testId() : void
{
$null = new NullEditorDoc(2);
self::assertEquals(2, $null->getId());
}
}