diff --git a/cf2/Fields/Handlers/FileUpload.php b/cf2/Fields/Handlers/FileUpload.php index d9f783aa6..4bcf85e8f 100644 --- a/cf2/Fields/Handlers/FileUpload.php +++ b/cf2/Fields/Handlers/FileUpload.php @@ -18,21 +18,14 @@ class FileUpload */ protected $form; - /** - * @var TransientApiContract - */ - protected $transientApi; - /** * @var UploaderContract */ protected $uploader; - public function __construct(array $field, array $form, TransientApiContract $transientApi, UploaderContract $uploader ) + public function __construct(array $field, array $form, UploaderContract $uploader ) { - //$field[ 'type' ] = FileFieldType::getCf1Identifier(); $this->field = $field; $this->form = $form; - $this->transientApi = $transientApi; $this->uploader = $uploader; } @@ -41,14 +34,10 @@ public function __construct(array $field, array $form, TransientApiContract $tra * @param array $files * @param array $hashes * @param $controlCode - * @return mixed + * @return array * @throws \Exception */ public function processFiles(array $files,array $hashes, $controlCode ){ - $transdata = call_user_func([$this->transientApi, 'getTransient'],$controlCode); - if( ! is_array( $transdata ) ){ - $transdata = []; - } $i = 0; foreach ($files as $file) { @@ -85,9 +74,8 @@ public function processFiles(array $files,array $hashes, $controlCode ){ } - call_user_func([$this->transientApi, 'setTransient'],$controlCode, array_merge( $transdata, $uploads ), 24 * 60 * 60 ); - return $controlCode; + return $uploads; } } \ No newline at end of file diff --git a/cf2/RestApi/File/CreateFile.php b/cf2/RestApi/File/CreateFile.php index 287a50650..6766be9cd 100644 --- a/cf2/RestApi/File/CreateFile.php +++ b/cf2/RestApi/File/CreateFile.php @@ -93,18 +93,28 @@ public function createItem(\WP_REST_Request $request) } $hashes = $request->get_param( 'hashes'); $controlCode = $request->get_param( 'control' ); + $transientApi = new Cf1TransientsApi(); $handler = new FileUpload( $field, $this->getForm(), - new Cf1TransientsApi(), new Cf1FileUploader() ); try{ - $controlCode = $handler->processFiles($files,$hashes,$controlCode); + $uploads = $handler->processFiles($files,$hashes,$controlCode); + $transdata = is_array( $transientApi->getTransient( $controlCode ) ) + ? $transientApi->getTransient( $controlCode ) + : []; + + $transientApi->setTransient( $controlCode, array_merge( + $transdata, + $uploads + ), DAY_IN_SECONDS ); }catch( \Exception $e ){ return new \WP_REST_Response(['message' => $e->getMessage() ],$e->getCode() ); } + + if( is_wp_error( $controlCode ) ){ return $controlCode; } diff --git a/tests/Integration/FileUploadTest.php b/tests/Integration/FileUploadTest.php index cb882d523..fb69b68d4 100644 --- a/tests/Integration/FileUploadTest.php +++ b/tests/Integration/FileUploadTest.php @@ -48,12 +48,18 @@ public function testProcessFile() $handler = new FileUpload( $field, $field, - new Cf1TransientsApi(), new Cf1FileUploader() ); - $returnedControlCode = $handler->processFiles($files, [md5_file($this->test_file)], 'f1' ); - $this->assertEquals( 'f1', $returnedControlCode ); + $uploads = $handler->processFiles($files, [md5_file($this->test_file)], 'f1' ); + $this->assertTrue( is_array( $uploads )); + $this->assertEquals( 1, count($uploads ) ); + } + + public function testFilterDirectoryForUpload(){ + add_filter( 'caldera_forms_upload_directory', function(){ + return 'form-uploads'; + }); } } diff --git a/tests/Integration/RestApi/CreateFileTest.php b/tests/Integration/RestApi/CreateFileTest.php index 16d8b0568..35f8eb445 100644 --- a/tests/Integration/RestApi/CreateFileTest.php +++ b/tests/Integration/RestApi/CreateFileTest.php @@ -180,6 +180,7 @@ public function testInvalidNonce(){ * * @since 1.8.0 * + * @group now * @group cf2 * @group file * @group field diff --git a/tests/Integration/Transients/Cf1TransientsApiTest.php b/tests/Integration/Transients/Cf1TransientsApiTest.php new file mode 100644 index 000000000..ab19d5219 --- /dev/null +++ b/tests/Integration/Transients/Cf1TransientsApiTest.php @@ -0,0 +1,73 @@ + 'barts' + ]; + \Caldera_Forms_Transient::set_transient($id, $data, 699 ); + $api = new Cf1TransientsApi(); + $api->deleteTransient($id); + $this->assertFalse( $api->getTransient($id) ); + } + + /** + * Test that new Transients API wrapper is interoperable with old API's set method + * + * @since 1.8.0 + * + * @group transients + * + * @covers \calderawp\calderaforms\cf2\Transients\Cf1TransientsApi::getTransient() + */ + public function testGetTransient() + { + $id = rand() . '2'; + $data = [ + 'foo' => 'barts' + ]; + \Caldera_Forms_Transient::set_transient($id, $data, 699 ); + $api = new Cf1TransientsApi(); + $this->assertEquals( $data, $api->getTransient($id) ); + } + + /** + * Test that new Transients API wrapper is interoperable with old API's get method + * + * @since 1.8.0 + * + * @group transients + * + * @covers \calderawp\calderaforms\cf2\Transients\Cf1TransientsApi::setTransient() + */ + public function testSetTransient() + { + $id = rand() . '3'; + $data = [ + 'foo' => 'barts' + ]; + $api = new Cf1TransientsApi(); + $api->setTransient($id, $data, 699); + $this->assertEquals( $data, \Caldera_Forms_Transient::get_transient ($id) ); + } + +} diff --git a/tests/Unit/Fields/Handlers/FileUpload/FileUploadTest.php b/tests/Unit/Fields/Handlers/FileUpload/FileUploadTest.php index b2acc8355..5284760a8 100644 --- a/tests/Unit/Fields/Handlers/FileUpload/FileUploadTest.php +++ b/tests/Unit/Fields/Handlers/FileUpload/FileUploadTest.php @@ -11,22 +11,23 @@ class FileUploadTest extends TestCase { /** + * Test setup of properties + * + * @since 1.8.0 + * * @cover \calderawp\calderaforms\cf2\Fields\Handlers\FileUpload::__construct() * @cover \calderawp\calderaforms\cf2\Fields\Handlers\FileUpload::$field * @cover \calderawp\calderaforms\cf2\Fields\Handlers\FileUpload::$form - * @cover \calderawp\calderaforms\cf2\Fields\Handlers\FileUpload::$transientApi * @cover \calderawp\calderaforms\cf2\Fields\Handlers\FileUpload::$uploader */ public function test__construct() { $field = [ 'ID' => 'fld1']; $form = ['ID' => 'cd1' ]; - $transients = new Cf1TransientsApi(); $uploader = new MockUploader(); - $handler = new FileUpload($field,$form, $transients,$uploader); + $handler = new FileUpload($field,$form,$uploader); $this->assertAttributeEquals( $field,'field', $handler ); $this->assertAttributeEquals( $form,'form', $handler ); - $this->assertAttributeEquals( $transients,'transientApi', $handler ); $this->assertAttributeEquals( $uploader,'uploader', $handler ); } } diff --git a/tests/Util/Mocks/MockUploader.php b/tests/Util/Mocks/MockUploader.php index 17c1e4c03..e7f70c709 100644 --- a/tests/Util/Mocks/MockUploader.php +++ b/tests/Util/Mocks/MockUploader.php @@ -7,7 +7,7 @@ class MockUploader implements UploaderContract { - public static function upload($file, array $args = array()) + public function upload($file, array $args = array()) { // TODO: Implement upload() method. }