diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/ProcessCategory.php b/workflow/engine/src/ProcessMaker/BusinessModel/ProcessCategory.php index f88293cff..4684ea299 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/ProcessCategory.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/ProcessCategory.php @@ -4,11 +4,11 @@ namespace ProcessMaker\BusinessModel; class ProcessCategory { private $arrayFieldDefinition = array( - "CAT_UID" => array("fieldName" => "CATEGORY_UID", "type" => "string", "required" => false, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "processCategoryUid"), + "CAT_UID" => array("fieldName" => "CATEGORY_UID", "type" => "string", "required" => false, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "categoryUid"), - "CAT_PARENT" => array("fieldName" => "CATEGORY_PARENT", "type" => "string", "required" => false, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "processCategoryParent"), - "CAT_NAME" => array("fieldName" => "CATEGORY_NAME", "type" => "string", "required" => true, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "processCategoryName"), - "CAT_ICON" => array("fieldName" => "CATEGORY_ICON", "type" => "string", "required" => false, "empty" => true, "defaultValues" => array(), "fieldNameAux" => "processCategoryIcon") + "CAT_PARENT" => array("fieldName" => "CATEGORY_PARENT", "type" => "string", "required" => false, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "categoryParent"), + "CAT_NAME" => array("fieldName" => "CATEGORY_NAME", "type" => "string", "required" => true, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "categoryName"), + "CAT_ICON" => array("fieldName" => "CATEGORY_ICON", "type" => "string", "required" => false, "empty" => true, "defaultValues" => array(), "fieldNameAux" => "categoryIcon") ); private $formatFieldNameInUppercase = true; @@ -60,7 +60,7 @@ class ProcessCategory * * return void */ - public function setArrayFieldNameForException($arrayData) + public function setArrayFieldNameForException(array $arrayData) { try { foreach ($arrayData as $key => $value) { @@ -88,14 +88,244 @@ class ProcessCategory } /** - * Get criteria for Process Category + * Verify if exists the name of a Category * - * return object + * @param string $categoryName Name + * @param string $categoryUidExclude Unique id of Category to exclude + * + * return bool Return true if exists the name of a Category, false otherwise */ - public function getProcessCategoryCriteria() + public function existsName($categoryName, $categoryUidExclude = "") { try { $criteria = new \Criteria("workflow"); + + $criteria->addSelectColumn(\ProcessCategoryPeer::CATEGORY_UID); + + if ($categoryUidExclude != "") { + $criteria->add(\ProcessCategoryPeer::CATEGORY_UID, $categoryUidExclude, \Criteria::NOT_EQUAL); + } + + $criteria->add(\ProcessCategoryPeer::CATEGORY_NAME, $categoryName, \Criteria::EQUAL); + + $rsCriteria = \ProcessCategoryPeer::doSelectRS($criteria); + + if ($rsCriteria->next()) { + return true; + } else { + return false; + } + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Verify if does not exist the Category in table PROCESS_CATEGORY + * + * @param string $categoryUid Unique id of Category + * @param string $fieldNameForException Field name for the exception + * + * return void Throw exception if does not exist the Category in table PROCESS_CATEGORY + */ + public function throwExceptionIfNotExistsCategory($categoryUid, $fieldNameForException) + { + try { + $obj = \ProcessCategoryPeer::retrieveByPK($categoryUid); + + if (is_null($obj)) { + throw new \Exception(\G::LoadTranslation("ID_CATEGORY_NOT_EXIST", array($fieldNameForException, $categoryUid))); + } + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Verify if exists the name of a Category + * + * @param string $categoryName Name + * @param string $fieldNameForException Field name for the exception + * @param string $categoryUidExclude Unique id of Category to exclude + * + * return void Throw exception if exists the name of a Category + */ + public function throwExceptionIfExistsName($categoryName, $fieldNameForException, $categoryUidExclude = "") + { + try { + if ($this->existsName($categoryName, $categoryUidExclude)) { + throw new \Exception(\G::LoadTranslation("ID_CATEGORY_NAME_ALREADY_EXISTS", array($fieldNameForException, $categoryName))); + } + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Validate the data if they are invalid (INSERT and UPDATE) + * + * @param string $categoryUid Unique id of Category + * @param array $arrayData Data + * + * return void Throw exception if data has an invalid value + */ + public function throwExceptionIfDataIsInvalid($categoryUid, array $arrayData) + { + try { + //Set variables + $arrayCategoryData = ($categoryUid == "")? array() : $this->getCategory($categoryUid, true); + $flagInsert = ($categoryUid == "")? true : false; + + $arrayDataMain = array_merge($arrayCategoryData, $arrayData); + + //Verify data - Field definition + $process = new \ProcessMaker\BusinessModel\Process(); + + $process->throwExceptionIfDataNotMetFieldDefinition($arrayData, $this->arrayFieldDefinition, $this->arrayFieldNameForException, $flagInsert); + + //Verify data + if (isset($arrayData["CAT_NAME"])) { + $this->throwExceptionIfExistsName($arrayData["CAT_NAME"], $this->arrayFieldNameForException["categoryName"], $categoryUid); + } + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Create Category + * + * @param array $arrayData Data + * + * return array Return data of the new Category created + */ + public function create(array $arrayData) + { + try { + //Verify data + $process = new \ProcessMaker\BusinessModel\Process(); + $validator = new \ProcessMaker\BusinessModel\Validator(); + + $validator->throwExceptionIfDataIsEmpty($arrayData, "\$arrayData"); + + //Set data + $arrayData = array_change_key_case($arrayData, CASE_UPPER); + + unset($arrayData["CAT_UID"]); + + //Verify data + $this->throwExceptionIfDataIsInvalid("", $arrayData); + + //Create + $category = new \ProcessCategory(); + + $categoryUid = \ProcessMaker\Util\Common::generateUID(); + + $category->setNew(true); + $category->setCategoryUid($categoryUid); + $category->setCategoryName($arrayData["CAT_NAME"]); + + $result = $category->save(); + + //Return + return $this->getCategory($categoryUid); + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Update Category + * + * @param string $categoryUid Unique id of Category + * @param array $arrayData Data + * + * return array Return data of the Category updated + */ + public function update($categoryUid, array $arrayData) + { + try { + //Verify data + $process = new \ProcessMaker\BusinessModel\Process(); + $validator = new \ProcessMaker\BusinessModel\Validator(); + + $validator->throwExceptionIfDataIsEmpty($arrayData, "\$arrayData"); + + //Set data + $arrayData = array_change_key_case($arrayData, CASE_UPPER); + $arrayDataBackup = $arrayData; + + //Verify data + $this->throwExceptionIfNotExistsCategory($categoryUid, $this->arrayFieldNameForException["categoryUid"]); + + $this->throwExceptionIfDataIsInvalid($categoryUid, $arrayData); + + //Update + $category = new \ProcessCategory(); + + $category->setNew(false); + $category->setCategoryUid($categoryUid); + + if (isset($arrayData["CAT_NAME"])) { + $category->setCategoryName($arrayData["CAT_NAME"]); + } + + $result = $category->save(); + + $arrayData = $arrayDataBackup; + + //Return + if (!$this->formatFieldNameInUppercase) { + $arrayData = array_change_key_case($arrayData, CASE_LOWER); + } + + return $arrayData; + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Delete Category + * + * @param string $categoryUid Unique id of Category + * + * return void + */ + public function delete($categoryUid) + { + try { + //Verify data + $this->throwExceptionIfNotExistsCategory($categoryUid, $this->arrayFieldNameForException["categoryUid"]); + + $process = new \Process(); + + $arrayTotalProcessesByCategory = $process->getAllProcessesByCategory(); + + if (isset($arrayTotalProcessesByCategory[$categoryUid])) { + throw new \Exception(\G::LoadTranslation("ID_MSG_CANNOT_DELETE_CATEGORY")); + } + + //Delete + $category = new \ProcessCategory(); + + $category->setCategoryUid($categoryUid); + $category->delete(); + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Get criteria for Category + * + * return object + */ + public function getCategoryCriteria() + { + try { + $criteria = new \Criteria("workflow"); + $criteria->addSelectColumn(\ProcessCategoryPeer::CATEGORY_UID); $criteria->addSelectColumn(\ProcessCategoryPeer::CATEGORY_PARENT); $criteria->addSelectColumn(\ProcessCategoryPeer::CATEGORY_NAME); @@ -109,13 +339,13 @@ class ProcessCategory } /** - * Get data of a Process Category from a record + * Get data of a Category from a record * * @param array $record Record * - * return array Return an array with data Process Category + * return array Return an array with data Category */ - public function getProcessCategoryDataFromRecord($record) + public function getCategoryDataFromRecord(array $record) { try { return array( @@ -129,7 +359,7 @@ class ProcessCategory } /** - * Get all Process Categories + * Get all Categories * * @param array $arrayFilterData Data of the filters * @param string $sortField Field name to sort @@ -137,12 +367,12 @@ class ProcessCategory * @param int $start Start * @param int $limit Limit * - * return array Return an array with all Process Categories + * return array Return an array with all Categories */ - public function getCategories($arrayFilterData = null, $sortField = null, $sortDir = null, $start = null, $limit = null) + public function getCategories(array $arrayFilterData = null, $sortField = null, $sortDir = null, $start = null, $limit = null) { try { - $arrayProcessCategory = array(); + $arrayCategory = array(); //Verify data $process = new \ProcessMaker\BusinessModel\Process(); @@ -151,7 +381,7 @@ class ProcessCategory //Get data if (!is_null($limit) && $limit . "" == "0") { - return $arrayProcessCategory; + return $arrayCategory; } //Set variables @@ -160,7 +390,7 @@ class ProcessCategory $arrayTotalProcessesByCategory = $process->getAllProcessesByCategory(); //SQL - $criteria = $this->getProcessCategoryCriteria(); + $criteria = $this->getCategoryCriteria(); if (!is_null($arrayFilterData) && is_array($arrayFilterData) && isset($arrayFilterData["filter"]) && trim($arrayFilterData["filter"]) != "") { $criteria->add(\ProcessCategoryPeer::CATEGORY_NAME, "%" . $arrayFilterData["filter"] . "%", \Criteria::LIKE); @@ -170,7 +400,7 @@ class ProcessCategory $criteriaCount = clone $criteria; $criteriaCount->clearSelectColumns(); - $criteriaCount->addSelectColumn("COUNT(" . \ProcessCategoryPeer::CATEGORY_UID . ") AS NUM_REC"); + $criteriaCount->addAsColumn("NUM_REC", "COUNT(" . \ProcessCategoryPeer::CATEGORY_UID . ")"); $rsCriteriaCount = \ProcessCategoryPeer::doSelectRS($criteriaCount); $rsCriteriaCount->setFetchmode(\ResultSet::FETCHMODE_ASSOC); @@ -185,16 +415,10 @@ class ProcessCategory $sortField = strtoupper($sortField); $sortField = (isset($this->arrayFieldDefinition[$sortField]["fieldName"]))? $this->arrayFieldDefinition[$sortField]["fieldName"] : $sortField; - switch ($sortField) { - case "CATEGORY_UID": - case "CATEGORY_PARENT": - case "CATEGORY_NAME": - case "CATEGORY_ICON": - $sortField = \ProcessCategoryPeer::TABLE_NAME . "." . $sortField; - break; - default: - $sortField = \ProcessCategoryPeer::CATEGORY_NAME; - break; + if (in_array($sortField, array("CATEGORY_UID", "CATEGORY_PARENT", "CATEGORY_NAME", "CATEGORY_ICON"))) { + $sortField = \ProcessCategoryPeer::TABLE_NAME . "." . $sortField; + } else { + $sortField = \ProcessCategoryPeer::CATEGORY_NAME; } } else { $sortField = \ProcessCategoryPeer::CATEGORY_NAME; @@ -222,187 +446,59 @@ class ProcessCategory $row["CATEGORY_TOTAL_PROCESSES"] = (isset($arrayTotalProcessesByCategory[$row["CATEGORY_UID"]]))? $arrayTotalProcessesByCategory[$row["CATEGORY_UID"]] : 0; - $arrayProcessCategory[] = $this->getProcessCategoryDataFromRecord($row); + $arrayCategory[] = $this->getCategoryDataFromRecord($row); } //Return - return $arrayProcessCategory; + return $arrayCategory; } catch (\Exception $e) { throw $e; } } /** - * Get a Process Category + * Get data of a Category * - * @param string $cat_uid Category Id + * @param string $categoryUid Unique id of Category + * @param bool $flagGetRecord Value that set the getting * - * return array Return an object with the Process Category + * return array Return an array with data of a Category */ - public function getCategory($cat_uid) + public function getCategory($categoryUid, $flagGetRecord = false) { try { - $oProcessCategory = ''; - $process = new \Process(); - $oTotalProcessesByCategory = $process->getAllProcessesByCategory(); - $criteria = $this->getAProcessCategoryCriteria($cat_uid); - $criteriaCount = clone $criteria; - $criteriaCount->clearSelectColumns(); - $criteriaCount->addSelectColumn("COUNT(" . \ProcessCategoryPeer::CATEGORY_UID . ") AS NUM_REC"); - $rsCriteriaCount = \ProcessCategoryPeer::doSelectRS($criteriaCount); - $rsCriteriaCount->setFetchmode(\ResultSet::FETCHMODE_ASSOC); - $rsCriteriaCount->next(); + //Verify data + $this->throwExceptionIfNotExistsCategory($categoryUid, $this->arrayFieldNameForException["categoryUid"]); + + //Set variables + if (!$flagGetRecord) { + $process = new \Process(); + + $arrayTotalProcessesByCategory = $process->getAllProcessesByCategory(); + } + + //Get data + //SQL + $criteria = $this->getCategoryCriteria(); + + $criteria->add(\ProcessCategoryPeer::CATEGORY_UID, $categoryUid, \Criteria::EQUAL); + $rsCriteria = \ProcessCategoryPeer::doSelectRS($criteria); $rsCriteria->setFetchmode(\ResultSet::FETCHMODE_ASSOC); - while ($rsCriteria->next()) { - $row = $rsCriteria->getRow(); - $row["CATEGORY_TOTAL_PROCESSES"] = (isset($oTotalProcessesByCategory[$row["CATEGORY_UID"]]))? $oTotalProcessesByCategory[$row["CATEGORY_UID"]] : 0; - $oProcessCategory = $this->getProcessCategoryDataFromRecord($row); - } - //Return - if ($oProcessCategory != '') { - $oProcessCategory = array_change_key_case($oProcessCategory, CASE_LOWER); - $oResponse = json_decode(json_encode($oProcessCategory), false); - return $oResponse; - } else { - throw (new \Exception( 'The Category with cat_uid: '.$cat_uid.' doesn\'t exist!')); - } - } catch (\Exception $e) { - throw $e; - } - } - /** - * Post Process Category - * - * @param string $cat_name Name of Category - * - * return array - */ - public function addCategory($cat_name) - { - try { - require_once (PATH_TRUNK . "workflow" . PATH_SEP . "engine" . PATH_SEP . "classes". PATH_SEP . "model" . PATH_SEP . "ProcessCategory.php"); - if ($cat_name == '') { - throw (new \Exception( 'cat_name. Process Category name can\'t be null')); - } - $catName = trim( $cat_name ); - if ($this->existsName( $cat_name )) { - throw (new \Exception( 'cat_name. Duplicate Process Category name')); - } - $catUid = \G::GenerateUniqueID(); - $pcat = new \ProcessCategory(); - $pcat->setNew( true ); - $pcat->setCategoryUid( $catUid ); - $pcat->setCategoryName( $catName ); - $pcat->save(); - $oProcessCategory = $this->getCategory( $catUid ); - //Return - return $oProcessCategory; - } catch (\Exception $e) { - throw $e; - } - } - - /** - * Put Process Category - * - * @param string $cat_uid Category id - * @param string $cat_name Category Name - * - * return array - */ - public function updateCategory($cat_uid, $cat_name) - { - try { - require_once (PATH_TRUNK . "workflow" . PATH_SEP . "engine" . PATH_SEP . "classes". PATH_SEP . "model" . PATH_SEP . "ProcessCategory.php"); - $catUID = $cat_uid; - $catName = trim( $cat_name ); - if ($this->existsName( $cat_name )) { - throw (new \Exception( 'cat_name. Duplicate Process Category name')); - } - $pcat = new \ProcessCategory(); - $pcat->setNew( false ); - $pcat->setCategoryUid( $catUID ); - $pcat->setCategoryName( $catName ); - $pcat->save(); - $oProcessCategory = $this->getCategory( $cat_uid ); - //Return - $oResponse = json_decode(json_encode($oProcessCategory), false); - return $oResponse; - - } catch (\Exception $e) { - throw $e; - } - } - - /** - * Delete Process Category - * - * @param string $cat_uid Category id - * - * return array - */ - public function deleteCategory($cat_uid) - { - try { - require_once 'classes/model/ProcessCategory.php'; - $criteria = $this->getAProcessCategoryCriteria($cat_uid); - $rsCriteria = \ProcessCategoryPeer::doSelectRS($criteria); - $rsCriteria->setFetchmode(\ResultSet::FETCHMODE_ASSOC); $rsCriteria->next(); + $row = $rsCriteria->getRow(); - if ($row) { - $cat = new \ProcessCategory(); - $cat->setCategoryUid( $cat_uid ); - $cat->delete(); - } else { - throw (new \Exception( 'The Category with cat_uid: '.$cat_uid.' doesn\'t exist!')); + + if (!$flagGetRecord) { + $row["CATEGORY_TOTAL_PROCESSES"] = (isset($arrayTotalProcessesByCategory[$row["CATEGORY_UID"]]))? $arrayTotalProcessesByCategory[$row["CATEGORY_UID"]] : 0; } - } catch (\Exception $e) { - throw $e; - } - } - - /** - * Get criteria for Process Category - * - * return object - */ - public function getAProcessCategoryCriteria($cat_uid) - { - try { - $criteria = new \Criteria("workflow"); - $criteria->addSelectColumn(\ProcessCategoryPeer::CATEGORY_UID); - $criteria->addSelectColumn(\ProcessCategoryPeer::CATEGORY_PARENT); - $criteria->addSelectColumn(\ProcessCategoryPeer::CATEGORY_NAME); - $criteria->addSelectColumn(\ProcessCategoryPeer::CATEGORY_ICON); - $criteria->add(\ProcessCategoryPeer::CATEGORY_UID, $cat_uid); - return $criteria; - } catch (\Exception $e) { - throw $e; - } - } - - /** - * Checks if the name exists - * - * @param string $name Name - * - * return bool Return true if the name exists, false otherwise - */ - public function existsName($name) - { - try { - $criteria = new \Criteria("workflow"); - $criteria->add(\ProcessCategoryPeer::CATEGORY_NAME, $name, \Criteria::EQUAL); - $rsCriteria = \ProcessCategoryPeer::doSelectRS($criteria); - $rsCriteria->setFetchmode(\ResultSet::FETCHMODE_ASSOC); - $rsCriteria->next(); - return $rsCriteria->getRow(); + //Return + return (!$flagGetRecord)? $this->getCategoryDataFromRecord($row) : $row; } catch (\Exception $e) { throw $e; } } } + diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Role.php b/workflow/engine/src/ProcessMaker/BusinessModel/Role.php index c53053bf6..7006e79fe 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Role.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Role.php @@ -337,8 +337,6 @@ class Role public function update($roleUid, array $arrayData) { try { - $arrayDataBackup = $arrayData; - //Verify data $process = new \ProcessMaker\BusinessModel\Process(); $validator = new \ProcessMaker\BusinessModel\Validator(); @@ -347,6 +345,7 @@ class Role //Set data $arrayData = array_change_key_case($arrayData, CASE_UPPER); + $arrayDataBackup = $arrayData; $arrayRoleData = $this->getRole($roleUid); diff --git a/workflow/engine/src/ProcessMaker/Services/Api/ProcessCategory.php b/workflow/engine/src/ProcessMaker/Services/Api/ProcessCategory.php index 21a95b880..3d4f060bc 100644 --- a/workflow/engine/src/ProcessMaker/Services/Api/ProcessCategory.php +++ b/workflow/engine/src/ProcessMaker/Services/Api/ProcessCategory.php @@ -5,13 +5,29 @@ use \ProcessMaker\Services\Api; use \Luracast\Restler\RestException; /** - * Group Api Controller + * ProcessCategory Api Controller * * @protected */ class ProcessCategory extends Api { - private $formatFieldNameInUppercase = false; + private $category; + + /** + * Constructor of the class + * + * return void + */ + public function __construct() + { + try { + $this->category = new \ProcessMaker\BusinessModel\ProcessCategory(); + + $this->category->setFormatFieldNameInUppercase(false); + } catch (\Exception $e) { + throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); + } + } /** * @url GET /categories @@ -19,87 +35,76 @@ class ProcessCategory extends Api public function doGetCategories($filter = null, $start = null, $limit = null) { try { - $processCategory = new \ProcessMaker\BusinessModel\ProcessCategory(); - $processCategory->setFormatFieldNameInUppercase($this->formatFieldNameInUppercase); - - $response = $processCategory->getCategories(array("filter" => $filter), null, null, $start, $limit); + $response = $this->category->getCategories(array("filter" => $filter), null, null, $start, $limit); return $response; } catch (\Exception $e) { - throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage())); + throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); } } /** * @url GET /category/:cat_uid * - * @param string $cat_uid {@min 32}{@max 32} + * @param string $cat_uid {@min 32}{@max 32} */ public function doGetCategory($cat_uid) { try { - $processCategory = new \ProcessMaker\BusinessModel\ProcessCategory(); - $processCategory->setFormatFieldNameInUppercase($this->formatFieldNameInUppercase); - - $response = $processCategory->getCategory($cat_uid); + $response = $this->category->getCategory($cat_uid); return $response; } catch (\Exception $e) { - throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage())); + throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); } } /** * @url POST /category * - * @param string $cat_name + * @param array $request_data * + * @status 201 */ - public function doPostCategory($cat_name) + public function doPostCategory(array $request_data) { try { - $processCategory = new \ProcessMaker\BusinessModel\ProcessCategory(); - $response = $processCategory->addCategory($cat_name); + $arrayData = $this->category->create($request_data); + + $response = $arrayData; return $response; } catch (\Exception $e) { - throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage())); + throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); } } /** * @url PUT /category/:cat_uid * - * @param string $cat_uid {@min 32}{@max 32} - * @param string $cat_name - * + * @param string $cat_uid {@min 32}{@max 32} + * @param array $request_data */ - public function doPutCategory($cat_uid, $cat_name) + public function doPutCategory($cat_uid, array $request_data) { try { - $processCategory = new \ProcessMaker\BusinessModel\ProcessCategory(); - $response = $processCategory->updateCategory($cat_uid, $cat_name); - - return $response; + $arrayData = $this->category->update($cat_uid, $request_data); } catch (\Exception $e) { - throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage())); + throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); } } /** * @url DELETE /category/:cat_uid * - * @param string $cat_uid {@min 32}{@max 32} - * + * @param string $cat_uid {@min 32}{@max 32} */ public function doDeleteCategory($cat_uid) { try { - $processCategory = new \ProcessMaker\BusinessModel\ProcessCategory(); - $processCategory->deleteCategory($cat_uid); - + $this->category->delete($cat_uid); } catch (\Exception $e) { - throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage())); + throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); } } } diff --git a/workflow/engine/src/Tests/BusinessModel/ProcessCategoryTest.php b/workflow/engine/src/Tests/BusinessModel/ProcessCategoryTest.php index 742cae5f2..f03e5fb3e 100644 --- a/workflow/engine/src/Tests/BusinessModel/ProcessCategoryTest.php +++ b/workflow/engine/src/Tests/BusinessModel/ProcessCategoryTest.php @@ -2,213 +2,319 @@ namespace Tests\BusinessModel; if (!class_exists("Propel")) { - include_once (__DIR__ . "/../bootstrap.php"); + require_once(__DIR__ . "/../bootstrap.php"); } /** * Class ProcessCategoryTest * - * @package Tests/ProcessMaker/BusinessModel + * @package Tests\BusinessModel */ class ProcessCategoryTest extends \PHPUnit_Framework_TestCase { - protected static $arrayUid = array(); - protected $oCategory; + protected static $category; + protected static $numCategory = 2; /** * Set class for test * * @coversNothing - * - * @copyright Colosa - Bolivia */ - public function setUp() + public static function setUpBeforeClass() { - $this->oCategory = new \ProcessMaker\BusinessModel\ProcessCategory(); - } - - - public static function tearDownAfterClass() - { - foreach (self::$arrayUid as $processCategoryUid) { - if ($processCategoryUid != "") { - $processCategory = new \ProcessCategory(); - - $processCategory->setCategoryUid($processCategoryUid); - $processCategory->delete(); - } - } + self::$category = new \ProcessMaker\BusinessModel\ProcessCategory(); } /** - * Test error for incorrect value of category name in array + * Test create categories * - * @covers \BusinessModel\ProcessCategory::addCategory - * @expectedException Exception - * @expectedExceptionMessage cat_name. Process Category name can't be null + * @covers \ProcessMaker\BusinessModel\ProcessCategory::create * - * @copyright Colosa - Bolivia + * @return array */ - public function testAddCategoryErrorIncorrectValue() - { - $this->oCategory->addCategory(''); - } - - /** - * Test add Category - * - * @covers \BusinessModel\ProcessCategory::addCategory - * - * @copyright Colosa - Bolivia - */ - public function testAddCategory() - { - $response = $this->oCategory->addCategory('New Category Test'); - $this->assertTrue(is_object($response)); - $aResponse = json_decode(json_encode($response), true); - return $aResponse; - } - - /** - * Test error for incorrect value of category name in array - * - * @covers \BusinessModel\ProcessCategory::addCategory - * @expectedException Exception - * @expectedExceptionMessage cat_name. Duplicate Process Category name - * - * @copyright Colosa - Bolivia - */ - public function testAddCategoryErrorDuplicateValue() - { - $this->oCategory->addCategory('New Category Test'); - } - - /** - * Test error for incorrect value of category name in array - * - * @covers \BusinessModel\ProcessCategory::updateCategory - * @expectedException Exception - * @expectedExceptionMessage cat_name. Duplicate Process Category name - * - * @copyright Colosa - Bolivia - */ - public function testUpdateCategoryErrorDuplicateValue() - { - $this->oCategory->addCategory('New Category Test'); - } - - /** - * Test put Category - * - * @covers \BusinessModel\ProcessCategory::updateCategory - * @depends testAddCategory - * @param array $aResponse - * - * @copyright Colosa - Bolivia - */ - public function testUpdateCategory(array $aResponse) - { - $response = $this->oCategory->updateCategory($aResponse["cat_uid"], 'Name Update Category Test'); - $this->assertTrue(is_object($response)); - } - - /** - * Test error for incorrect value of category id - * - * @covers \BusinessModel\ProcessCategory::getCategory - * @expectedException Exception - * @expectedExceptionMessage The Category with cat_uid: 12345678912345678912345678912345678 doesn't exist! - * - * @copyright Colosa - Bolivia - */ - public function testGetErrorValue() - { - $this->oCategory->getCategory('12345678912345678912345678912345678'); - } - - /** - * Test get Category - * - * @covers \BusinessModel\ProcessCategory::getCategory - * @depends testAddCategory - * @param array $aResponse - * - * @copyright Colosa - Bolivia - */ - public function testGetCategory(array $aResponse) - { - $response = $this->oCategory->getCategory($aResponse["cat_uid"]); - $this->assertTrue(is_object($response)); - } - - /** - * Test error for incorrect value of category id - * - * @covers \BusinessModel\ProcessCategory::deleteCategory - * @expectedException Exception - * @expectedExceptionMessage The Category with cat_uid: 12345678912345678912345678912345678 doesn't exist! - * - * @copyright Colosa - Bolivia - */ - public function testDeleteErrorValue() - { - $this->oCategory->deleteCategory('12345678912345678912345678912345678'); - } - - /** - * Test delete Category - * - * @covers \BusinessModel\ProcessCategory::deleteCategory - * @depends testAddCategory - * @param array $aResponse - * - * @copyright Colosa - Bolivia - */ - public function testDeleteCategory(array $aResponse) - { - $response = $this->oCategory->deleteCategory($aResponse["cat_uid"]); - $this->assertTrue(empty($response)); - } - public function testCreate() { - try { - $processCategory = new \ProcessCategory(); + $arrayRecord = array(); - $processCategoryUid = \G::GenerateUniqueID(); + //Create + for ($i = 0; $i <= self::$numCategory - 1; $i++) { + $arrayData = array( + "CAT_NAME" => "PHPUnit My Category " . $i + ); - $processCategory->setNew(true); - $processCategory->setCategoryUid($processCategoryUid); - $processCategory->setCategoryName("PHPUnit Category"); - $processCategory->save(); - } catch (\Exception $e) { - $processCategoryUid = ""; + $arrayCategory = self::$category->create($arrayData); + + $this->assertTrue(is_array($arrayCategory)); + $this->assertNotEmpty($arrayCategory); + + $this->assertTrue(isset($arrayCategory["CAT_UID"])); + + $arrayRecord[] = $arrayCategory; } - self::$arrayUid[] = $processCategoryUid; + //Create - Japanese characters + $arrayData = array( + "CAT_NAME" => "テスト(PHPUnitの)", + ); - $this->assertNotEmpty($processCategoryUid); + $arrayCategory = self::$category->create($arrayData); + + $this->assertTrue(is_array($arrayCategory)); + $this->assertNotEmpty($arrayCategory); + + $this->assertTrue(isset($arrayCategory["CAT_UID"])); + + $arrayRecord[] = $arrayCategory; + + //Return + return $arrayRecord; } - - public function testGetCategories() + /** + * Test update categories + * + * @covers \ProcessMaker\BusinessModel\ProcessCategory::update + * + * @depends testCreate + * @param array $arrayRecord Data of the categories + */ + public function testUpdate(array $arrayRecord) { - $processCategory = new \ProcessMaker\BusinessModel\ProcessCategory(); + $arrayData = array("CAT_NAME" => "PHPUnit My Category 1..."); - $arrayProcessCategory = $processCategory->getCategories(); + $arrayCategory = self::$category->update($arrayRecord[1]["CAT_UID"], $arrayData); - $this->assertNotEmpty($arrayProcessCategory); + $arrayCategory = self::$category->getCategory($arrayRecord[1]["CAT_UID"]); - $arrayProcessCategory = $processCategory->getCategories(null, null, null, 0, 0); + $this->assertTrue(is_array($arrayCategory)); + $this->assertNotEmpty($arrayCategory); - $this->assertEmpty($arrayProcessCategory); + $this->assertEquals($arrayCategory["CAT_NAME"], $arrayData["CAT_NAME"]); + } - $arrayProcessCategory = $processCategory->getCategories(array("filter" => "PHP")); + /** + * Test get categories + * + * @covers \ProcessMaker\BusinessModel\ProcessCategory::getCategories + * + * @depends testCreate + * @param array $arrayRecord Data of the categories + */ + public function testGetCategories(array $arrayRecord) + { + $arrayCategory = self::$category->getCategories(); - $this->assertNotEmpty($arrayProcessCategory); + $this->assertNotEmpty($arrayCategory); - $this->assertEquals($arrayProcessCategory[0]["CAT_NAME"], "PHPUnit Category"); - $this->assertEquals($arrayProcessCategory[0]["CAT_TOTAL_PROCESSES"], 0); + $arrayCategory = self::$category->getCategories(null, null, null, 0, 0); + + $this->assertEmpty($arrayCategory); + + $arrayCategory = self::$category->getCategories(array("filter" => "PHPUnit")); + + $this->assertTrue(is_array($arrayCategory)); + $this->assertNotEmpty($arrayCategory); + + $this->assertEquals($arrayCategory[0]["CAT_UID"], $arrayRecord[0]["CAT_UID"]); + $this->assertEquals($arrayCategory[0]["CAT_NAME"], $arrayRecord[0]["CAT_NAME"]); + } + + /** + * Test get category + * + * @covers \ProcessMaker\BusinessModel\ProcessCategory::getCategory + * + * @depends testCreate + * @param array $arrayRecord Data of the categories + */ + public function testGetCategory(array $arrayRecord) + { + //Get + $arrayCategory = self::$category->getCategory($arrayRecord[0]["CAT_UID"]); + + $this->assertTrue(is_array($arrayCategory)); + $this->assertNotEmpty($arrayCategory); + + $this->assertEquals($arrayCategory["CAT_UID"], $arrayRecord[0]["CAT_UID"]); + $this->assertEquals($arrayCategory["CAT_NAME"], $arrayRecord[0]["CAT_NAME"]); + + //Get - Japanese characters + $arrayCategory = self::$category->getCategory($arrayRecord[self::$numCategory]["CAT_UID"]); + + $this->assertTrue(is_array($arrayCategory)); + $this->assertNotEmpty($arrayCategory); + + $this->assertEquals($arrayCategory["CAT_UID"], $arrayRecord[self::$numCategory]["CAT_UID"]); + $this->assertEquals($arrayCategory["CAT_NAME"], "テスト(PHPUnitの)"); + } + + /** + * Test exception for empty data + * + * @covers \ProcessMaker\BusinessModel\ProcessCategory::create + * + * @expectedException Exception + * @expectedExceptionMessage Invalid value for "$arrayData", it can not be empty. + */ + public function testCreateExceptionEmptyData() + { + $arrayData = array(); + + $arrayCategory = self::$category->create($arrayData); + } + + /** + * Test exception for required data (CAT_NAME) + * + * @covers \ProcessMaker\BusinessModel\ProcessCategory::create + * + * @expectedException Exception + * @expectedExceptionMessage Undefined value for "CAT_NAME", it is required. + */ + public function testCreateExceptionRequiredDataCatName() + { + $arrayData = array( + "CAT_NAMEX" => "PHPUnit My Category N" + ); + + $arrayCategory = self::$category->create($arrayData); + } + + /** + * Test exception for invalid data (CAT_NAME) + * + * @covers \ProcessMaker\BusinessModel\ProcessCategory::create + * + * @expectedException Exception + * @expectedExceptionMessage Invalid value for "CAT_NAME", it can not be empty. + */ + public function testCreateExceptionInvalidDataCatName() + { + $arrayData = array( + "CAT_NAME" => "" + ); + + $arrayCategory = self::$category->create($arrayData); + } + + /** + * Test exception for category name existing + * + * @covers \ProcessMaker\BusinessModel\ProcessCategory::create + * + * @expectedException Exception + * @expectedExceptionMessage The category name with CAT_NAME: "PHPUnit My Category 0" already exists. + */ + public function testCreateExceptionExistsCatName() + { + $arrayData = array( + "CAT_NAME" => "PHPUnit My Category 0" + ); + + $arrayCategory = self::$category->create($arrayData); + } + + /** + * Test exception for empty data + * + * @covers \ProcessMaker\BusinessModel\ProcessCategory::update + * + * @expectedException Exception + * @expectedExceptionMessage Invalid value for "$arrayData", it can not be empty. + */ + public function testUpdateExceptionEmptyData() + { + $arrayData = array(); + + $arrayCategory = self::$category->update("", $arrayData); + } + + /** + * Test exception for invalid category UID + * + * @covers \ProcessMaker\BusinessModel\ProcessCategory::update + * + * @expectedException Exception + * @expectedExceptionMessage The category with CAT_UID: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' does not exist. + */ + public function testUpdateExceptionInvalidCatUid() + { + $arrayData = array( + "CAT_NAME" => "PHPUnit My Category N" + ); + + $arrayCategory = self::$category->update("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", $arrayData); + } + + /** + * Test exception for invalid data (CAT_NAME) + * + * @covers \ProcessMaker\BusinessModel\ProcessCategory::update + * + * @depends testCreate + * @param array $arrayRecord Data of the categories + * + * @expectedException Exception + * @expectedExceptionMessage Invalid value for "CAT_NAME", it can not be empty. + */ + public function testUpdateExceptionInvalidDataCatName(array $arrayRecord) + { + $arrayData = array( + "CAT_NAME" => "" + ); + + $arrayCategory = self::$category->update($arrayRecord[0]["CAT_UID"], $arrayData); + } + + /** + * Test exception for category name existing + * + * @covers \ProcessMaker\BusinessModel\ProcessCategory::update + * + * @depends testCreate + * @param array $arrayRecord Data of the categories + * + * @expectedException Exception + * @expectedExceptionMessage The category name with CAT_NAME: "PHPUnit My Category 0" already exists. + */ + public function testUpdateExceptionExistsCatName(array $arrayRecord) + { + $arrayData = $arrayRecord[0]; + + $arrayCategory = self::$category->update($arrayRecord[1]["CAT_UID"], $arrayData); + } + + /** + * Test delete categories + * + * @covers \ProcessMaker\BusinessModel\ProcessCategory::delete + * + * @depends testCreate + * @param array $arrayRecord Data of the categories + */ + public function testDelete(array $arrayRecord) + { + foreach ($arrayRecord as $value) { + self::$category->delete($value["CAT_UID"]); + } + + $arrayCategory = self::$category->getCategories(array("filter" => "PHPUnit")); + + $this->assertTrue(is_array($arrayCategory)); + $this->assertEmpty($arrayCategory); + } + + /** + * Test exception for invalid category UID + * + * @covers \ProcessMaker\BusinessModel\ProcessCategory::delete + * + * @expectedException Exception + * @expectedExceptionMessage The category with CAT_UID: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' does not exist. + */ + public function testDeleteExceptionInvalidCatUid() + { + self::$category->delete("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); } }