diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 257aa4b85..81547d3bb 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -56,6 +56,7 @@ define('PATH_SMARTY_CACHE', PATH_TRUNK . 'shared/compiled/smarty/cache'); define('PATH_THIRDPARTY', PATH_TRUNK . 'thirdparty/'); define("URL_KEY", 'c0l0s40pt1mu59r1m3'); define("PATH_XMLFORM", PATH_CORE . "xmlform" . PATH_SEP); +define('PATH_DOCUMENT', PATH_WORKSPACE . '/files/'); // Set Time Zone $_SESSION['__SYSTEM_UTC_TIME_ZONE__'] = (int) (env('MAIN_SYSTEM_UTC_TIME_ZONE', 'workflow')) == 1; diff --git a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/CasesTest.php b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/CasesTest.php index dcea64514..55c762947 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/CasesTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/CasesTest.php @@ -4,6 +4,7 @@ namespace ProcessMaker\BusinessModel; use Exception; use G; +use ProcessMaker\BusinessModel\Cases; use ProcessMaker\Model\Application; use ProcessMaker\Model\Delegation; use ProcessMaker\Model\Documents; @@ -29,8 +30,8 @@ class CasesTest extends TestCase Documents::truncate(); Application::truncate(); User::where('USR_ID', '=', 1) - ->where('USR_ID', '=', 2) - ->delete(); + ->where('USR_ID', '=', 2) + ->delete(); } /** @@ -132,4 +133,104 @@ class CasesTest extends TestCase // Remove the path created G::rm_dir($pathCase); } + + /** + * It tests the uploadFiles method + * + * @covers \ProcessMaker\BusinessModel\Cases::uploadFiles() + * @test + */ + public function it_should_test_upload_files_method() + { + $user = factory(User::class)->create(); + $application = factory(Application::class)->create([ + 'APP_CUR_USER' => $user->USR_UID + ]); + $delegation = factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID + ]); + $varName = "/tmp/test.pdf"; + fopen($varName, "w"); + $_FILES = ["form" => + [ + "name" => ["test"], + "type" => ["application/pdf"], + "tmp_name" => ["/tmp/test.pdf"], + "error" => [0], + "size" => [0] + ]]; + + $case = new Cases(); + + // Call the uploadFiles method, sending the delIndex + $res = $case->uploadFiles($user->USR_UID, $application->APP_UID, $varName, -1, null, $delegation->DEL_INDEX); + // Asserts the result is not empy + $this->assertNotEmpty($res); + + //Call the uploadFiles method, without the delIndex + $res = $case->uploadFiles($user->USR_UID, $application->APP_UID, $varName); + // Asserts the result is not empy + $this->assertNotEmpty($res); + } + + /** + * It tests the exception in the uploadFiles method + * + * @covers \ProcessMaker\BusinessModel\Cases::uploadFiles() + * @test + */ + public function it_should_test_exception_in_upload_files_method() + { + $user = factory(User::class)->create(); + $application = factory(Application::class)->create([ + 'APP_CUR_USER' => $user->USR_UID + ]); + factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID + ]); + $varName = "/tmp/test.pdf"; + fopen($varName, "w"); + + $_FILES = []; + $case = new Cases(); + + // Asserts an exception is expected + $this->expectExceptionMessage("**ID_ERROR_UPLOAD_FILE_CONTACT_ADMINISTRATOR**"); + // Call the uploadFiles method + $case->uploadFiles($user->USR_UID, $application->APP_UID, $varName); + } + + /** + * It tests the exception in uploadFiles method + * + * @covers \ProcessMaker\BusinessModel\Cases::uploadFiles() + * @test + */ + public function it_should_test_the_exception_in_upload_files_method() + { + $user = factory(User::class)->create(); + $application = factory(Application::class)->create([ + 'APP_CUR_USER' => $user->USR_UID + ]); + $delegation = factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID + ]); + $varName = "/tmp/test.pdf"; + fopen($varName, "w"); + $_FILES = ["form" => + [ + "name" => ["test"], + "type" => ["application/pdf"], + "tmp_name" => ["/tmp/test.pdf"], + "error" => [UPLOAD_ERR_INI_SIZE], + "size" => [0] + ]]; + + $case = new Cases(); + + // Asserts there is an exception for the file + $this->expectExceptionMessage("The uploaded file exceeds the upload_max_filesize directive in php.ini"); + // Call the uploadFiles method + $case->uploadFiles($user->USR_UID, $application->APP_UID, $varName, -1, null, $delegation->DEL_INDEX); + } } diff --git a/tests/unit/workflow/engine/src/ProcessMaker/Services/Api/CasesTest.php b/tests/unit/workflow/engine/src/ProcessMaker/Services/Api/CasesTest.php index a0afdbdb5..f20ac2124 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/Services/Api/CasesTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/Services/Api/CasesTest.php @@ -7,6 +7,9 @@ use Luracast\Restler\Defaults; use Luracast\Restler\HumanReadableCache; use Luracast\Restler\RestException; use Maveriks\Extension\Restler; +use ProcessMaker\Model\Application; +use ProcessMaker\Model\Delegation; +use ProcessMaker\Model\User; use ProcessMaker\Services\Api\Cases; use RBAC; use ReflectionClass; @@ -213,4 +216,74 @@ class CasesTest extends TestCase $this->assertTrue($expected); } + + /** + * Test the uploadDocumentToCase method + * + * @covers ProcessMaker\Services\Api\Cases::uploadDocumentToCase + * @test + */ + public function test_upload_document_to_case_method() + { + $user = factory(User::class)->create(); + $application = factory(Application::class)->create([ + 'APP_CUR_USER' => $user->USR_UID + ]); + $delegation = factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID + ]); + $varName = "/tmp/test.pdf"; + + $varName = "/tmp/test.pdf"; + fopen($varName, "w"); + $_FILES = ["form" => + [ + "name" => ["test"], + "type" => ["application/pdf"], + "tmp_name" => ["/tmp/test.pdf"], + "error" => [0], + "size" => [0] + ]]; + + $case = new Cases(); + + //Call the uploadDocumentToCase method without a post delindex + $res = $case->uploadDocumentToCase($application->APP_UID, $varName); + //Asserts the result is not empty + $this->assertNotEmpty($res); + $_POST['delIndex'] = $delegation->DEL_INDEX; + //Call the uploadDocumentToCase method with a post delindex + $res = $case->uploadDocumentToCase($application->APP_UID, $varName, -1, null, $delegation->DEL_INDEX); + //Asserts the result is not empty + $this->assertNotEmpty($res); + } + + /** + * Test the exception in the uploadDocumentToCase method + * + * @covers ProcessMaker\Services\Api\Cases::uploadDocumentToCase + * @test + */ + public function test_exception_upload_document_to_case_method() + { + $user = factory(User::class)->create(); + $application = factory(Application::class)->create([ + 'APP_CUR_USER' => $user->USR_UID + ]); + $delegation = factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID + ]); + $varName = "/tmp/test.pdf"; + + $varName = "/tmp/test.pdf"; + fopen($varName, "w"); + $_FILES = []; + + $case = new Cases(); + + //Asserts the expected exception + $this->expectExceptionMessage("**ID_ERROR_UPLOAD_FILE_CONTACT_ADMINISTRATOR**"); + //Call the uploadDocumentToCase method without a post delindex + $res = $case->uploadDocumentToCase($application->APP_UID, $varName); + } } diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php b/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php index b021baa02..8eb634c41 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php @@ -3783,17 +3783,22 @@ class Cases * @param string $varName * @param mixed $inpDocUid * @param string $appDocUid + * @param int $delegationIndex * * @return array * @throws Exception */ - public function uploadFiles($userUid, $appUid, $varName, $inpDocUid = -1, $appDocUid = null) + public function uploadFiles($userUid, $appUid, $varName, $inpDocUid = -1, $appDocUid = null, $delegationIndex = null) { $response = []; if (isset($_FILES["form"]["name"]) && count($_FILES["form"]["name"]) > 0) { // Get the delIndex related to the case $cases = new ClassesCases(); - $delIndex = $cases->getCurrentDelegation($appUid, $userUid); + if (!empty($delegationIndex)) { + $delIndex = $delegationIndex; + } else { + $delIndex = $cases->getCurrentDelegation($appUid, $userUid); + } // Get information about the user $user = new ModelUsers(); $userCreator = $user->loadDetailed($userUid)['USR_FULLNAME']; diff --git a/workflow/engine/src/ProcessMaker/Services/Api/Cases.php b/workflow/engine/src/ProcessMaker/Services/Api/Cases.php index e6e433505..af67f6785 100644 --- a/workflow/engine/src/ProcessMaker/Services/Api/Cases.php +++ b/workflow/engine/src/ProcessMaker/Services/Api/Cases.php @@ -1409,6 +1409,7 @@ class Cases extends Api * @param string $var_name * @param string $doc_uid * @param string $app_doc_uid + * @param int $delIndex {@from body} * * @return array * @throws RestException @@ -1416,12 +1417,16 @@ class Cases extends Api * @access protected * @class AccessControl {@permission PM_CASES} */ - public function uploadDocumentToCase($app_uid, $var_name, $doc_uid = '-1', $app_doc_uid = null) + public function uploadDocumentToCase($app_uid, $var_name, $doc_uid = '-1', $app_doc_uid = null, $delIndex = null) { try { $userUid = $this->getUserId(); $case = new BmCases(); - $response = $case->uploadFiles($userUid, $app_uid, $var_name, $doc_uid, $app_doc_uid); + if (isset($delIndex)) { + $response = $case->uploadFiles($userUid, $app_uid, $var_name, $doc_uid, $app_doc_uid, $delIndex); + } else { + $response = $case->uploadFiles($userUid, $app_uid, $var_name, $doc_uid, $app_doc_uid); + } } catch (ExceptionRestApi $e) { throw new RestException($e->getCode(), $e->getMessage()); } catch (Exception $e) {