diff --git a/package.json b/package.json index a62a09b7e142ae84db22d527aa4ceb6488205188..f15e9b783cf2aab505eede384ff8ee8c2f3efe78 100755 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "jquery.nicescroll": "~3.6.8", "jstree-bootstrap-theme": "^1.0.1", "ngx-cookie-service": "^2.1.0", - "ngx-pipes": "^2.4.2", + "ngx-pipes": "^2.4.5", "photoswipe": "^4.1.2", "tinymce": "^4.8.5", "tooltipster": "^4.2.6", @@ -33,7 +33,7 @@ "devDependencies": { "@angular-devkit/build-angular": "^0.12.2", "@angular/animations": "^7.2.9", - "@angular/cdk": "^7.3.4", + "@angular/cdk": "^7.3.5", "@angular/cli": "^7.3.6", "@angular/common": "^7.2.9", "@angular/compiler": "^7.2.9", @@ -41,7 +41,7 @@ "@angular/core": "^7.2.9", "@angular/forms": "^7.2.9", "@angular/http": "^7.2.9", - "@angular/material": "^7.3.4", + "@angular/material": "^7.3.5", "@angular/platform-browser": "^7.2.9", "@angular/platform-browser-dynamic": "^7.2.9", "@angular/platform-server": "^7.2.9", diff --git a/rest/index.php b/rest/index.php index 605779a47008565432dec1cd8fed2947c71892ee..e5e21cf1069489d741b9f6c50d95639f7f36fb1f 100755 --- a/rest/index.php +++ b/rest/index.php @@ -281,11 +281,12 @@ $app->get('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/act $app->get('/resources/{resId}/users/{userId}/isDestinationChanging', \Action\controllers\PreProcessActionController::class . ':isDestinationChanging'); //shipping -$app->get('/shippings', \Shipping\controllers\ShippingController::class . ':get'); -$app->get('/shippings/{id}', \Shipping\controllers\ShippingController::class . ':getById'); -$app->post('/shippings', \Shipping\controllers\ShippingController::class . ':create'); -$app->put('/shippings/{id}', \Shipping\controllers\ShippingController::class . ':update'); -$app->delete('/shippings/{id}', \Shipping\controllers\ShippingController::class . ':delete'); +$app->get('/administration/shippings', \Shipping\controllers\ShippingController::class . ':get'); +$app->get('/administration/shippings/new', \Shipping\controllers\ShippingController::class . ':initShipping'); +$app->get('/administration/shippings/{id}', \Shipping\controllers\ShippingController::class . ':getById'); +$app->post('/administration/shippings', \Shipping\controllers\ShippingController::class . ':create'); +$app->put('/administration/shippings/{id}', \Shipping\controllers\ShippingController::class . ':update'); +$app->delete('/administration/shippings/{id}', \Shipping\controllers\ShippingController::class . ':delete'); //SignatureBook $app->get('/signatureBook/users/{userId}/groups/{groupId}/baskets/{basketId}/resources', \SignatureBook\controllers\SignatureBookController::class . ':getResources'); diff --git a/src/app/shipping/controllers/ShippingController.php b/src/app/shipping/controllers/ShippingController.php index c2c2d74d9d43c8d92596c275be0a6aaecd03d7dc..0716f247d60c04f2da4d43718c378b3efd238113 100755 --- a/src/app/shipping/controllers/ShippingController.php +++ b/src/app/shipping/controllers/ShippingController.php @@ -12,12 +12,14 @@ namespace Shipping\controllers; +use Entity\models\EntityModel; +use Group\models\ServiceModel; use History\controllers\HistoryController; use Respect\Validation\Validator; use Shipping\models\ShippingModel; -use Group\models\ServiceModel; use Slim\Http\Request; use Slim\Http\Response; +use SrcCore\models\PasswordModel; class ShippingController { @@ -27,7 +29,7 @@ class ShippingController return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); } - return $response->withJson(['shippings' => ShippingModel::get(['id', 'label', 'description', 'options', 'fee', 'entity_ids'])]); + return $response->withJson(['shippings' => ShippingModel::get(['id', 'label', 'description', 'options', 'fee', 'entities'])]); } public function getById(Request $request, Response $response, array $aArgs) @@ -44,6 +46,21 @@ class ShippingController if (empty($shippingInfo)) { return $response->withStatus(400)->withJson(['errors' => 'Shipping does not exist']); } + + $shippingInfo['account'] = (array)json_decode($shippingInfo['account']); + $shippingInfo['account']['password'] = ''; + $shippingInfo['options'] = (array)json_decode($shippingInfo['options']); + $shippingInfo['fee'] = (array)json_decode($shippingInfo['fee']); + $shippingInfo['entities'] = (array)json_decode($shippingInfo['entities']); + + $entities = EntityModel::getAllowedEntitiesByUserId(['userId' => 'superadmin']); + foreach ($entities as $key => $entity) { + $entities[$key]['state']['selected'] = false; + if (in_array($entity['id'], $shippingInfo['entities'])) { + $entities[$key]['state']['selected'] = true; + } + } + $shippingInfo['entities'] = $entities; return $response->withJson($shippingInfo); } @@ -61,6 +78,14 @@ class ShippingController return $response->withStatus(400)->withJson(['errors' => $errors]); } + if (!empty($body['account']['password'])) { + $body['account']['password'] = PasswordModel::encrypt(['password' => $body['account']['password']]); + } + + $body['options'] = json_encode($body['options']); + $body['fee'] = json_encode($body['fee']); + $body['entities'] = json_encode($body['entities']); + $body['account'] = json_encode($body['account']); $id = ShippingModel::create($body); HistoryController::add([ @@ -88,6 +113,18 @@ class ShippingController return $response->withStatus(500)->withJson(['errors' => $errors]); } + if (!empty($body['account']['password'])) { + $body['account']['password'] = PasswordModel::encrypt(['password' => $body['account']['password']]); + } else { + $shippingInfo = ShippingModel::getById(['id' => $aArgs['id'], 'select' => ['account']]); + $body['account']['password'] = $shippingInfo['account']->password; + } + + $body['options'] = json_encode($body['options']); + $body['fee'] = json_encode($body['fee']); + $body['entities'] = json_encode($body['entities']); + $body['account'] = json_encode($body['account']); + ShippingModel::update($body); HistoryController::add([ @@ -108,7 +145,7 @@ class ShippingController } if (!Validator::intVal()->validate($aArgs['id'])) { - return $response->withStatus(400)->withJson(['errors' => 'Route id is not an integer']); + return $response->withStatus(400)->withJson(['errors' => 'id is not an integer']); } $shippingInfo = ShippingModel::getById(['id' => $aArgs['id'], 'select' => ['label']]); @@ -122,10 +159,11 @@ class ShippingController 'info' => _SHIPPING_DELETED. ' : ' . $shippingInfo['label'] ]); - return $response->withJson(['shippings' => ShippingModel::get()]); + $shippings = ShippingModel::get(['select' => ['id', 'label', 'description', 'options', 'fee', 'entities']]); + return $response->withJson(['shippings' => $shippings]); } - protected function control($aArgs, $mode) + protected function checkData($aArgs, $mode) { $errors = []; @@ -138,6 +176,12 @@ class ShippingController if (empty($shippingInfo)) { $errors[] = 'Shipping does not exist'; } + } else { + if (!empty($aArgs['account'])) { + if (!Validator::notEmpty()->validate($aArgs['account']['id']) || !Validator::notEmpty()->validate($aArgs['account']['password'])) { + $errors[] = 'account id or password is empty'; + } + } } if (!Validator::notEmpty()->validate($aArgs['label']) || @@ -150,17 +194,33 @@ class ShippingController } if (!empty($aArgs['entities'])) { - if (Validator::arrayType()->validate($aArgs['entities'])) { + if (!Validator::arrayType()->validate($aArgs['entities'])) { $errors[] = 'entities must be an array'; } - } - - if (!empty($aArgs['account'])) { - if (Validator::notEmpty()->validate($aArgs['id']) && Validator::notEmpty()->validate($aArgs['password'])) { - $errors[] = 'account id or password is empty'; + foreach ($aArgs['entities'] as $entity) { + $info = EntityModel::getByEntityId(['entityId' => $entity, 'select' => ['id']]); + if (empty($info)) { + $errors[] = $entity . ' does not exists'; + } } } return $errors; } + + public function initShipping(Request $request, Response $response) + { + if (!ServiceModel::hasService(['id' => 'admin_shippings', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) { + return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); + } + + $entities = EntityModel::getAllowedEntitiesByUserId(['userId' => 'superadmin']); + foreach ($entities as $key => $entity) { + $entities[$key]['state']['selected'] = false; + } + + return $response->withJson([ + 'entities' => $entities, + ]); + } } diff --git a/src/app/shipping/models/ShippingModelAbstract.php b/src/app/shipping/models/ShippingModelAbstract.php index b11ffcdc513b6ae97ba62d511aae6b85f35ba371..9e52a20a82f91262c14aeaa70c834bc29b63707c 100755 --- a/src/app/shipping/models/ShippingModelAbstract.php +++ b/src/app/shipping/models/ShippingModelAbstract.php @@ -81,7 +81,7 @@ abstract class ShippingModelAbstract 'description' => $aArgs['description'], 'options' => $aArgs['options'], 'fee' => $aArgs['fee'], - 'entity_ids' => $aArgs['entity_ids'], + 'entities' => $aArgs['entities'], 'account' => $aArgs['account'], ], 'where' => ['id = ?'], diff --git a/src/frontend/app/administration/shipping/shippings-administration.component.ts b/src/frontend/app/administration/shipping/shippings-administration.component.ts index 6fc6d99281b80b5e0e694d207f0ee06defece98f..06461b6fb6731d0b5e0540401ab2f2db625f71e3 100644 --- a/src/frontend/app/administration/shipping/shippings-administration.component.ts +++ b/src/frontend/app/administration/shipping/shippings-administration.component.ts @@ -55,7 +55,7 @@ export class ShippingsAdministrationComponent implements OnInit { this.loading = true; - this.http.get('../../rest/shippings') + this.http.get('../../rest/administration/shippings') .subscribe((data: any) => { this.shippings = data.shippings; @@ -72,7 +72,7 @@ export class ShippingsAdministrationComponent implements OnInit { let r = confirm(this.lang.deleteMsg); if (r) { - this.http.delete('../../rest/shippings/' + id) + this.http.delete('../../rest/administration/shippings/' + id) .subscribe((data: any) => { this.shippings = data.shippings; this.dataSource = new MatTableDataSource(this.shippings); diff --git a/test/unitTests/app/shipping/ShippingControllerTest.php b/test/unitTests/app/shipping/ShippingControllerTest.php new file mode 100644 index 0000000000000000000000000000000000000000..e4bc49440bd15a8d18b9c73ee5848dcc65df03c4 --- /dev/null +++ b/test/unitTests/app/shipping/ShippingControllerTest.php @@ -0,0 +1,165 @@ +<?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. +* +*/ + +use PHPUnit\Framework\TestCase; + +class ShippingControllerTest extends TestCase +{ + private static $id = null; + + public function testCreate() + { + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + $shipping = new \Shipping\controllers\ShippingController(); + + $aArgs = [ + 'label' => 'TEST', + 'description' => 'description du TEST', + 'options' => [ + 'shaping' => ['color', 'both_sides', 'address_page'], + 'envelopMode' => 'small_simple', + 'sendMode' => 'fast' + ], + 'fee' => ['first_page' => 1, 'next_page' => 2, 'postage_price' => 12], + 'account' => ['id' => 'toto', 'password' => '1234'] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + + $response = $shipping->create($fullRequest, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody()); + + $this->assertInternalType("int", $responseBody->shippingId); + self::$id = $responseBody->shippingId; + + ####### FAIL ########## + $aArgs = [ + 'description' => 'description du TEST', + 'options' => [ + 'shaping' => ['color', 'both_sides', 'address_page'], + 'envelopMode' => 'small_simple', + 'sendMode' => 'fast' + ], + 'fee' => ['first_page' => 1, 'next_page' => 2, 'postage_price' => 12], + 'account' => ['id' => 'toto', 'password' => ''] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + + $response = $shipping->create($fullRequest, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame('account id or password is empty', $responseBody->errors[0]); + $this->assertSame('label is empty or too long', $responseBody->errors[1]); + } + + public function testGetById() + { + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + $shipping = new \Shipping\controllers\ShippingController(); + + $response = $shipping->getById($request, new \Slim\Http\Response(), ['id' => self::$id]); + $responseBody = json_decode((string)$response->getBody()); + + $this->assertNotEmpty($responseBody); + $this->assertSame('TEST', $responseBody->label); + $this->assertSame('description du TEST', $responseBody->description); + $this->assertSame('color', $responseBody->options->shaping[0]); + $this->assertSame('both_sides', $responseBody->options->shaping[1]); + $this->assertSame('address_page', $responseBody->options->shaping[2]); + $this->assertSame('small_simple', $responseBody->options->envelopMode); + $this->assertSame('fast', $responseBody->options->sendMode); + $this->assertSame(1, $responseBody->fee->first_page); + $this->assertSame(2, $responseBody->fee->next_page); + $this->assertSame(12, $responseBody->fee->postage_price); + + ######## ERROR ############# + $response = $shipping->getById($request, new \Slim\Http\Response(), ['id' => 999999999]); + $responseBody = json_decode((string)$response->getBody()); + $this->assertSame('Shipping does not exist', $responseBody->errors); + } + + public function testGetList() + { + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + $shipping = new \Shipping\controllers\ShippingController(); + + $response = $shipping->get($request, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody()); + + $this->assertNotNull($responseBody->shippings); + + foreach ($responseBody->shippings as $value) { + $this->assertInternalType("int", $value->id); + } + } + + public function testUpdate() + { + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $aArgs = [ + 'label' => 'TEST 2', + 'description' => 'description du test 2', + 'options' => [ + 'shaping' => ['color', 'address_page'], + 'envelopMode' => 'big_simple', + 'sendMode' => 'fast' + ], + 'fee' => ['first_page' => 10, 'next_page' => 20, 'postage_price' => 12], + 'account' => ['id' => 'toto', 'password' => '1234'] + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + + $shipping = new \Shipping\controllers\ShippingController(); + $response = $shipping->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame('success', $responseBody->success); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + $shipping = new \Shipping\controllers\ShippingController(); + + $response = $shipping->getById($request, new \Slim\Http\Response(), ['id' => self::$id]); + $responseBody = json_decode((string)$response->getBody()); + + $this->assertNotEmpty($responseBody); + $this->assertSame('TEST 2', $responseBody->label); + $this->assertSame('description du test 2', $responseBody->description); + $this->assertSame('color', $responseBody->options->shaping[0]); + $this->assertSame('address_page', $responseBody->options->shaping[1]); + $this->assertSame('big_simple', $responseBody->options->envelopMode); + $this->assertSame('fast', $responseBody->options->sendMode); + $this->assertSame(10, $responseBody->fee->first_page); + $this->assertSame(20, $responseBody->fee->next_page); + $this->assertSame(12, $responseBody->fee->postage_price); + } + + public function testDelete() + { + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + $shipping = new \Shipping\controllers\ShippingController(); + + $response = $shipping->delete($request, new \Slim\Http\Response(), ['id' => self::$id]); + $responseBody = json_decode((string)$response->getBody()); + + $this->assertInternalType("array", $responseBody->shippings); + + ##### FAIL ###### + $response = $shipping->delete($request, new \Slim\Http\Response(), ['id' => 'myid']); + $responseBody = json_decode((string)$response->getBody()); + $this->assertSame('id is not an integer', $responseBody->errors); + } +}