Skip to content
Snippets Groups Projects
Commit 206d7a31 authored by Henri Queneau's avatar Henri Queneau
Browse files

FEAT #5802 add dev controller, models, UnitaireTest

parent ac37af4d
No related branches found
No related tags found
No related merge requests found
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/
namespace MaarchTest;
//use Core\Models\DatabaseModel;
use PHPUnit\Framework\TestCase;
class NotificationControllerTest extends TestCase
{
private static $id = null;
public function testReadAll()
{
// TEST GET // READ // NEED TO HAVE RED IN BDD
$NotificationController = new \Notifications\Controllers\NotificationController();
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = $NotificationController->get($request, new \Slim\Http\Response());
$responseBody = json_decode((string)$response->getBody());
$this->assertSame('RED', $responseBody->notifications[6]->notification_id);
//var_dump($responseBody);
//var_dump($responseBody->notifications[6]->notification_id);
}
public function testCreate()
{
// CREATE
$NotificationController = new \Notifications\Controllers\NotificationController();
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$aArgs = [
'notification_id' => 'warning5',
'description' => 'Alerte aux gogoles',
'is_enabled' => 'Y',
'event_id' => 'users%',
'notification_mode' => 'EMAIL',
'template_id' => '4',
'rss_url_template' => 'http://localhost/maarch_entreprise',
'diffusion_type' => 'user',
'diffusion_properties' => 'superadmin',
'attachfor_type' => 'zz',
'attachfor_properties' => 'cc'
];
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
$response = $NotificationController->create($fullRequest, new \Slim\Http\Response());
$responseBody = json_decode((string)$response->getBody());
$this->assertInternalType("int", $responseBody->notification_sid);
$notification_sid = $responseBody->notification_sid;
self::$id = $notification_sid;
unset($responseBody->notification_sid);
$aCompare = json_decode(json_encode($compare), false);
$this->assertSame('warning5', $responseBody->notification_id);
$this->assertSame('Alerte aux gogoles', $responseBody->description);
$this->assertSame('Y', $responseBody->is_enabled);
$this->assertSame('users%', $responseBody->event_id);
$this->assertSame('EMAIL', $responseBody->notification_mode);
$this->assertSame(4, $responseBody->template_id);
$this->assertSame('http://localhost/maarch_entreprise', $responseBody->rss_url_template);
$this->assertSame('user', $responseBody->diffusion_type);
$this->assertSame('superadmin', $responseBody->diffusion_properties);
$this->assertSame('zz', $responseBody->attachfor_type);
$this->assertSame('cc', $responseBody->attachfor_properties);
}
public function testCreateFail1()
{
//Fail Create 1
$NotificationController = new \Notifications\Controllers\NotificationController();
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$aArgs = [
'notification_id' => '',
'description' => 'Alerte aux gogoles',
'is_enabled' => 'Y',
'event_id' => 'users%',
'notification_mode' => 'EMAIL',
'template_id' => '4',
'rss_url_template' => 'http://localhost/maarch_entreprise',
'diffusion_type' => 'user',
'diffusion_properties' => 'superadmin',
'attachfor_type' => 'zz',
'attachfor_properties' => 'cc'
];
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
$response = $NotificationController->create($fullRequest, new \Slim\Http\Response());
$responseBody = json_decode((string)$response->getBody());
$this->assertSame('Notification error : notification_id is empty', $responseBody->errors);
}
public function testCreateFail2()
{
//Fail Create 2
$NotificationController = new \Notifications\Controllers\NotificationController();
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$aArgs = [
'notification_id' => 'warning5',
'description' => 'Alerte aux gogoles',
'is_enabled' => 'Y',
'event_id' => 'users%',
'notification_mode' => 'EMAIL',
'template_id' => '4',
'rss_url_template' => 'http://localhost/maarch_entreprise',
'diffusion_type' => 'user',
'diffusion_properties' => 'superadmin',
'attachfor_type' => 'zz',
'attachfor_properties' => 'cc'
];
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
$response = $NotificationController->create($fullRequest, new \Slim\Http\Response());
$responseBody = json_decode((string)$response->getBody());
$this->assertSame('Notification error : id already exist', $responseBody->errors);
}
public function testUpdate()
{
// UPDATE
$NotificationController = new \Notifications\Controllers\NotificationController();
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$aArgs = [
'notification_id' => 'warning5',
'description' => 'BOUBOUP',
'is_enabled' => 'Y',
'event_id' => 'users%',
'notification_mode' => 'EMAIL',
'template_id' => 4,
'rss_url_template' => 'http://localhost/maarch_entreprise',
'diffusion_type' => 'user',
'diffusion_properties' => 'superadmin',
'attachfor_type' => 'NN',
'attachfor_type' => 'NC',
];
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
$response = $NotificationController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]);
$responseBody = json_decode((string)$response->getBody());
//$this->assertSame(_NOTIFICATION_UPDATED, $responseBody->success);
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = $NotificationController->getById($request, new \Slim\Http\Response(), ['id' => 'warning5']);
$responseBody = json_decode((string)$response->getBody());
//var_dump($responseBody->notifications);
//self::$id = $notification_sid;
$this->assertSame(self::$id, $responseBody->notifications->notification_sid);
$this->assertSame('warning5', $responseBody->notifications->notification_id);
$this->assertSame('BOUBOUP', $responseBody->notifications->description);
$this->assertSame('Y', $responseBody->notifications->is_enabled);
$this->assertSame('users%', $responseBody->notifications->event_id);
$this->assertSame('EMAIL', $responseBody->notifications->notification_mode);
$this->assertSame(4, $responseBody->notifications->template_id);
$this->assertSame('user', $responseBody->notifications->diffusion_type);
}
public function testRead(){
//READ
$NotificationController = new \Notifications\Controllers\NotificationController();
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = $NotificationController->getById($request, new \Slim\Http\Response(), ['id' => 'warning5']);
$responseBody = json_decode((string)$response->getBody());
$this->assertSame(self::$id, $responseBody->notifications->notification_sid);
$this->assertSame('warning5', $responseBody->notifications->notification_id);
$this->assertSame('BOUBOUP', $responseBody->notifications->description);
$this->assertSame('Y', $responseBody->notifications->is_enabled);
$this->assertSame('users%', $responseBody->notifications->event_id);
$this->assertSame('EMAIL', $responseBody->notifications->notification_mode);
$this->assertSame(4, $responseBody->notifications->template_id);
$this->assertSame('user', $responseBody->notifications->diffusion_type);
}
// public function testReadFail(){
// //I CANT READ BECAUSE NO ID
// $NotificationController = new \Notifications\Controllers\NotificationController();
// $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
// $request = \Slim\Http\Request::createFromEnvironment($environment);
// $response = $NotificationController->getById($request, new \Slim\Http\Response(), ['id' => '']);
// $responseBody = json_decode((string)$response->getBody());
// $this->assertSame('notification_id is empty', $responseBody->errors);
// }
public function testReadFail2(){
//I CANT READ BECAUSE NO EXIST
$NotificationController = new \Notifications\Controllers\NotificationController();
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = $NotificationController->getById($request, new \Slim\Http\Response(), ['id' => 'BamBam']);
$responseBody = json_decode((string)$response->getBody());
$this->assertSame('Notification not found', $responseBody->errors);
}
// public function testDeleteFail()
// {
// $NotificationController = new \Notifications\Controllers\NotificationController();
// // DELETE
// $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']);
// $request = \Slim\Http\Request::createFromEnvironment($environment);
// $response = $NotificationController->deleteNotification($request, new \Slim\Http\Response(), ['id' => '2245']);
// $responseBody = json_decode((string)$response->getBody());
// var_dump($responseBody);
// //$this->assertSame(_DELETED_NOTIFICATION, $responseBody->success);
// }
public function testDelete()
{
$NotificationController = new \Notifications\Controllers\NotificationController();
// DELETE
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = $NotificationController->delete($request, new \Slim\Http\Response(), ['id' => self::$id]);
$responseBody = json_decode((string)$response->getBody());
$this->assertSame(_DELETED_NOTIFICATION, $responseBody->success);
}
}
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/
/**
* @brief Notifications Controller
* @author dev@maarch.org
* @ingroup notifications
*/
namespace Notifications\Controllers;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Respect\Validation\Validator;
use Notifications\Models\NotificationModel;
use Core\Models\ServiceModel;
use Core\Models\LangModel;
use Core\Controllers\HistoryController;
class NotificationController
{
public function get(RequestInterface $request, ResponseInterface $response)
{
if (!ServiceModel::hasService(['id' => 'admin_notif', 'userId' => $_SESSION['user']['UserId'], 'location' => 'notifications', 'type' => 'admin'])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$notification['notifications'] = NotificationModel::get(['select' => ['notification_sid', 'notification_id', 'description', 'is_enabled', 'event_id', 'notification_mode', 'template_id', 'diffusion_type']]);
return $response->withJson($notification);
}
public function getById(RequestInterface $request, ResponseInterface $response, $aArgs)
{
if (!ServiceModel::hasService(['id' => 'admin_notif', 'userId' => $_SESSION['user']['UserId'], 'location' => 'notifications', 'type' => 'admin'])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$notification['notifications'] = NotificationModel::getById(['notificationId' => $aArgs['id'], 'select' => ['notification_sid', 'notification_id', 'description', 'is_enabled', 'event_id', 'notification_mode', 'template_id', 'diffusion_type']]);
if (empty($notification['notifications'])) {
return $response->withStatus(400)->withJson(['errors' => 'Notification not found']);
}
return $response->withJson($notification);
}
public function create(RequestInterface $request, ResponseInterface $response)
{
if (!ServiceModel::hasService(['id' => 'admin_notif', 'userId' => $_SESSION['user']['UserId'], 'location' => 'notifications', 'type' => 'admin'])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$data = $request->getParams();
if(empty($data['notification_id'])){
return $response->withStatus(400)->withJson(['errors' => 'Notification error : notification_id is empty']);
}
$notificationInDb = NotificationModel::getById(['notificationId' => $data['notification_id'], 'select' => ['notification_sid']]);
if($data){
if(is_int($notificationInDb['notification_sid'])){
return $response->withStatus(400)->withJson(['errors' => 'Notification error : id already exist']);
}elseif(strlen($data[is_enabled]) > 1 && $data[is_enabled]){
return $response->withStatus(400)->withJson(['errors' => 'Notification error : bad value for is_enabled ']);
}elseif(strlen($data[description]) > 255){
return $response->withStatus(400)->withJson(['errors' => 'Notification error : description is too long ']);
}elseif(strlen($data[event_id]) > 255 && is_string($data[event_id])){
return $response->withStatus(400)->withJson(['errors' => 'Notification error : event_id is too long ']);
}elseif(strlen($data[notification_mode]) > 30){
return $response->withStatus(400)->withJson(['errors' => 'Notification error : notification_mode is too long ']);
}elseif(Validator::intType()->notEmpty()->validate($data[template_id])){
return $response->withStatus(400)->withJson(['errors' => 'Notification error : template_id not a int ']);
}elseif(!is_string($data[rss_url_template])){
return $response->withStatus(400)->withJson(['errors' => 'Notification error : rss_url_template is not in good format ']);
}elseif(!is_string($data[diffusion_type])){
return $response->withStatus(400)->withJson(['errors' => 'Notification error : diffusion_type is a int ']);
}elseif(!is_string($data[diffusion_properties])){
return $response->withStatus(400)->withJson(['errors' => 'Notification error : template_id note a int ']);
}
if (NotificationModel::create($data)) {
HistoryController::add([
'table_name' => 'notifications',
'record_id' => $data['notification_id'],
'event_type' => 'ADD',
'event_id' => 'notificationsadd',
'info' => _ADD_NOTIFICATIONS . ' : ' . $data['notification_id']
]);
return $response->withJson(NotificationModel::getById(['notificationId' => $data['notification_id']]));
} else {
return $response->withStatus(400)->withJson(['errors' => 'Notification Create Error']);
}
}
}
public function update(RequestInterface $request, ResponseInterface $response, $aArgs)
{
if (!ServiceModel::hasService(['id' => 'admin_notif', 'userId' => $_SESSION['user']['UserId'], 'location' => 'notifications', 'type' => 'admin'])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$data = $request->getParams();
$data['notification_sid'] = $aArgs['id'];
//$aArgs = self::manageValue($request);
//$errors = $this->control($aArgs, 'update');
if (!empty($errors)) {
return $response->withStatus(500)->withJson(['errors' => $errors]);
}
NotificationModel::update($data);
//var_dump($aArgs);
$notification = NotificationModel::getById(['notificationId' => $data['notification_id']]);
HistoryController::add([
'table_name' => 'notifications',
'record_id' => $notification['notification_id'],
'event_type' => 'UP',
'event_id' => 'notificationsup',
'info' => _MODIFY_NOTIFICATIONS . ' : ' . $notification['notification_id']
]);
return $response->withJson(['notification'=> $notification]);
}
public function delete(RequestInterface $request, ResponseInterface $response, $aArgs)
{
if (!ServiceModel::hasService(['id' => 'admin_notif', 'userId' => $_SESSION['user']['UserId'], 'location' => 'notifications', 'type' => 'admin'])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
NotificationModel::delete(['notification_sid' => $aArgs['id']]);
HistoryController::add([
'table_name' => 'notifications',
'record_id' => $aArgs['id'],
'event_type' => 'DEL',
'event_id' => 'notificationsdel',
'info' => _DELETE_NOTIFICATIONS . ' : ' . $aArgs['id']
]);
return $response->withJson(['success' => _DELETED_NOTIFICATION]);
}
}
\ No newline at end of file
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/
/**
* @brief Notifications Model
* @author dev@maarch.org
* @ingroup Module
*/
namespace Notifications\Models;
class NotificationModel extends NotificationModelAbstract
{
// Do your stuff in this class
}
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/
/**
* @brief Notifications Model
* @author dev@maarch.org
* @ingroup Module
*/
namespace Notifications\Models;
use Core\Models\DatabaseModel;
use Core\Models\ValidatorModel;
class NotificationModelAbstract
{
public static function get(array $aArgs = [])
{
ValidatorModel::notEmpty($aArgs, ['select']);
$aNotifications = DatabaseModel::select([
'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
'table' => ['notifications']
]);
return $aNotifications;
}
public static function getById(array $aArgs = [])
{
ValidatorModel::notEmpty($aArgs, ['notificationId']);
$aNotification = DatabaseModel::select([
'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
'table' => ['notifications'],
'where' => ['notification_id = ?'],
'data' => [$aArgs['notificationId']]
]);
if (empty($aNotification[0])) {
return [];
}
return $aNotification[0];
}
public static function delete(array $aArgs = [])
{
ValidatorModel::notEmpty($aArgs, ['notification_sid']);
ValidatorModel::intVal($aArgs, ['notification_sid']);
DatabaseModel::delete([
'table' => 'notifications',
'where' => ['notification_sid = ?'],
'data' => [$aArgs['notification_sid']],
]);
return true;
}
public static function create(array $aArgs = [])
{
ValidatorModel::notEmpty($aArgs, ['notification_id', 'description', 'is_enabled', 'event_id', 'notification_mode', 'template_id', 'diffusion_type', 'diffusion_properties']);
ValidatorModel::intVal($aArgs, ['template_id']);
ValidatorModel::stringType($aArgs, ['notification_id','description','is_enabled','event_id','notification_mode',]);
DatabaseModel::insert([
'table' => 'notifications',
'columnsValues' => [
'notification_id' => $aArgs['notification_id'],
'description' => $aArgs['description'],
'is_enabled' => $aArgs['is_enabled'],
'event_id' => $aArgs['event_id'],
'notification_mode' => $aArgs['notification_mode'],
'template_id' => $aArgs['template_id'],
'rss_url_template' => $aArgs['rss_url_template'],
'diffusion_type' => $aArgs['diffusion_type'],
'diffusion_properties' => $aArgs['diffusion_properties'],
'attachfor_type' => $aArgs['attachfor_type'],
'attachfor_properties' => $aArgs['attachfor_properties']
]
]);
return true;
}
public static function update(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['notification_sid']);
ValidatorModel::intVal($aArgs, ['notification_sid']);
$where['notification_sid'] = $aArgs['notification_sid'];
//unset($aArgs['notification_id']);
unset($aArgs['notification_sid']);
$aReturn = DatabaseModel::update([
'table' => 'notifications',
'set' => $aArgs,
'where' => ['notification_sid = ?'],
'data' => [$where['notification_sid']]
]);
return $aReturn;
}
}
......@@ -109,6 +109,17 @@ $app->post('/initialize', \Core\Controllers\CoreController::class . ':initialize
//Administration
$app->get('/administration', \Core\Controllers\CoreController::class . ':getAdministration');
$app->get('/administration/users', \Core\Controllers\UserController::class . ':getUsersForAdministration');
$app->get('/administration/users/new', \Core\Controllers\UserController::class . ':getNewUserForAdministration');
$app->get('/administration/users/{id}', \Core\Controllers\UserController::class . ':getUserForAdministration');
//notifications
$app->get('/notifications', \Notifications\Controllers\NotificationController::class . ':get');
$app->get('/notifications/{id}', \Notifications\Controllers\NotificationController::class . ':getById');
$app->delete('/notifications/{id}', \Notifications\Controllers\NotificationController::class . ':delete');
$app->post('/notifications', \Notifications\Controllers\NotificationController::class . ':create');
$app->put('/notifications/{id}', \Notifications\Controllers\NotificationController::class . ':update');
//status
$app->get('/administration/status', \Core\Controllers\StatusController::class . ':getList');
$app->get('/administration/status/new', \Core\Controllers\StatusController::class . ':getNewInformations');
$app->get('/administration/status/{identifier}', \Core\Controllers\StatusController::class . ':getByIdentifier');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment