diff --git a/cf2/Fields/Handlers/Cf1FileUploader.php b/cf2/Fields/Handlers/Cf1FileUploader.php index e05ddd0b0..b7a3464d9 100644 --- a/cf2/Fields/Handlers/Cf1FileUploader.php +++ b/cf2/Fields/Handlers/Cf1FileUploader.php @@ -2,9 +2,16 @@ namespace calderawp\calderaforms\cf2\Fields\Handlers; +use calderawp\calderaforms\cf2\Fields\Handlers\UploaderContract; -class Cf1FileUploader +class Cf1FileUploader implements UploaderContract { + /** @inheritdoc */ + public function upload($file, array $args = array()) + { + return \Caldera_Forms_Files::upload($file,$args); + } + } \ No newline at end of file diff --git a/cf2/Fields/Handlers/FileUpload.php b/cf2/Fields/Handlers/FileUpload.php index 6d275deb3..d9f783aa6 100644 --- a/cf2/Fields/Handlers/FileUpload.php +++ b/cf2/Fields/Handlers/FileUpload.php @@ -2,6 +2,7 @@ namespace calderawp\calderaforms\cf2\Fields\Handlers; +use calderawp\calderaforms\cf2\Fields\FieldTypes\FileFieldType; use calderawp\calderaforms\cf2\Transients\TransientApiContract; @@ -21,11 +22,18 @@ class FileUpload * @var TransientApiContract */ protected $transientApi; - public function __construct(array $field, array $form, TransientApiContract $transientApi ) + + /** + * @var UploaderContract + */ + protected $uploader; + public function __construct(array $field, array $form, TransientApiContract $transientApi, UploaderContract $uploader ) { + //$field[ 'type' ] = FileFieldType::getCf1Identifier(); $this->field = $field; $this->form = $form; $this->transientApi = $transientApi; + $this->uploader = $uploader; } diff --git a/cf2/Fields/Handlers/UploaderContract.php b/cf2/Fields/Handlers/UploaderContract.php index d4769c39e..1baf45c9c 100644 --- a/cf2/Fields/Handlers/UploaderContract.php +++ b/cf2/Fields/Handlers/UploaderContract.php @@ -4,7 +4,9 @@ namespace calderawp\calderaforms\cf2\Fields\Handlers; -class UploaderContract +interface UploaderContract { + public function upload($file, array $args = array()); + } \ No newline at end of file diff --git a/cf2/RestApi/File/CreateFile.php b/cf2/RestApi/File/CreateFile.php index 6070e333f..287a50650 100644 --- a/cf2/RestApi/File/CreateFile.php +++ b/cf2/RestApi/File/CreateFile.php @@ -5,8 +5,10 @@ use calderawp\calderaforms\cf2\Fields\FieldTypes\FileFieldType; +use calderawp\calderaforms\cf2\Fields\Handlers\Cf1FileUploader; use calderawp\calderaforms\cf2\Fields\Handlers\FileUpload; use calderawp\calderaforms\cf2\Transients\Cf1TransientsApi; +use calderawp\calderaforms\cf2\Fields\Handlers\UploaderContract; class CreateFile extends File { @@ -84,25 +86,29 @@ public function createItem(\WP_REST_Request $request) $this->setFormById($formId); $fieldId = $request->get_param('fieldId'); $field = \Caldera_Forms_Field_Util::get_field($fieldId,$this->getForm()); - $uploads = []; - $uploader = $this->getUploader($field); + $uploader = new Cf1FileUploader(); + if( is_wp_error( $uploader ) ){ + /** @var \WP_Error $uploader */ + return new \WP_REST_Response(['message' => $uploader->get_error_message() ],$uploader->get_error_code() ); + } $hashes = $request->get_param( 'hashes'); $controlCode = $request->get_param( 'control' ); $handler = new FileUpload( $field, $this->getForm(), - new Cf1TransientsApi() + new Cf1TransientsApi(), + new Cf1FileUploader() ); try{ $controlCode = $handler->processFiles($files,$hashes,$controlCode); }catch( \Exception $e ){ return new \WP_REST_Response(['message' => $e->getMessage() ],$e->getCode() ); - } if( is_wp_error( $controlCode ) ){ return $controlCode; } + $response = rest_ensure_response([ 'control' => $controlCode ]); @@ -111,20 +117,6 @@ public function createItem(\WP_REST_Request $request) return $response; } - /** - * @param $field - * @return callable|\WP_Error - */ - protected function getUploader($field) - { - $uploader = \Caldera_Forms_Files::get_upload_handler( $this->getForm(), $field ); - if( ! is_callable( $uploader) ){ - return new \WP_Error( 'invalid-upload-handler', sprintf( __( 'Invalid file upload handler. See %s', 'caldera-forms'), 'https://calderaforms.com/doc/alternative-file-upload-directory/') ); - } - - return $uploader; - } - } \ No newline at end of file diff --git a/tests/Integration/FileUploadTest.php b/tests/Integration/FileUploadTest.php index 1b5d7c4b7..cb882d523 100644 --- a/tests/Integration/FileUploadTest.php +++ b/tests/Integration/FileUploadTest.php @@ -2,9 +2,58 @@ namespace calderawp\calderaforms\Tests\IntegrationFields\Handlers; +use calderawp\calderaforms\cf2\Fields\Handlers\Cf1FileUploader; use calderawp\calderaforms\cf2\Fields\Handlers\FileUpload; +use calderawp\calderaforms\cf2\Transients\Cf1TransientsApi; +use calderawp\calderaforms\Tests\Integration\TestCase; -class FileUploadTest extends \PHPUnit_Framework_TestCase +class FileUploadTest extends TestCase { + protected $test_file; + + public function setUp() + { + $orig_file = __DIR__ . '/screenshot.jpeg'; + $this->test_file = '/tmp/screenshot.jpg'; + copy($orig_file, $this->test_file); + parent::setUp(); + } + + /** + * @throws \Exception + * @group now + */ + public function testProcessFile() + { + + $formId = 'cf2_file'; + $fieldId = 'cf2_file_1'; + $form = \Caldera_Forms_Forms::get_form( $formId ); + $field = \Caldera_Forms_Field_Util::get_field($fieldId,$form); + $control = \Caldera_Forms_Field_Util::generate_file_field_unique_id( + $field, + $form + ); + + $files = [ + [ + 'file' => file_get_contents($this->test_file), + 'name' => 'screenshot.jpeg', + 'size' => filesize($this->test_file), + 'tmp_name' => $this->test_file, + ] + ]; + + $handler = new FileUpload( + $field, + $field, + new Cf1TransientsApi(), + new Cf1FileUploader() + ); + $returnedControlCode = $handler->processFiles($files, [md5_file($this->test_file)], 'f1' ); + $this->assertEquals( 'f1', $returnedControlCode ); + + + } } diff --git a/tests/Integration/RestApi/CreateFileTest.php b/tests/Integration/RestApi/CreateFileTest.php index b30b3e0d4..16d8b0568 100644 --- a/tests/Integration/RestApi/CreateFileTest.php +++ b/tests/Integration/RestApi/CreateFileTest.php @@ -4,12 +4,15 @@ namespace calderawp\calderaforms\Tests\Integration\RestApi; + class CreateFileTest extends RestApiTestCase { - + /** + * @var string + */ protected $test_file; - + /** @inheritdoc */ public function setUp() { $orig_file = __DIR__ . '/screenshot.jpeg'; diff --git a/tests/Integration/screenshot.jpeg b/tests/Integration/screenshot.jpeg new file mode 100644 index 000000000..bcf2ea588 Binary files /dev/null and b/tests/Integration/screenshot.jpeg differ diff --git a/tests/Unit/Fields/Handlers/FileUpload/FileUploadTest.php b/tests/Unit/Fields/Handlers/FileUpload/FileUploadTest.php index 648621289..b2acc8355 100644 --- a/tests/Unit/Fields/Handlers/FileUpload/FileUploadTest.php +++ b/tests/Unit/Fields/Handlers/FileUpload/FileUploadTest.php @@ -5,6 +5,7 @@ use calderawp\calderaforms\cf2\Fields\Handlers\FileUpload; use calderawp\calderaforms\cf2\Transients\Cf1TransientsApi; use calderawp\calderaforms\Tests\Unit\TestCase; +use calderawp\calderaforms\Tests\Util\Mocks\MockUploader; class FileUploadTest extends TestCase { @@ -14,15 +15,18 @@ class FileUploadTest extends TestCase * @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(); - $handler = new FileUpload($field,$form, $transients); + $uploader = new MockUploader(); + $handler = new FileUpload($field,$form, $transients,$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 dfaae740e..17c1e4c03 100644 --- a/tests/Util/Mocks/MockUploader.php +++ b/tests/Util/Mocks/MockUploader.php @@ -2,9 +2,14 @@ namespace calderawp\calderaforms\Tests\Util\Mocks; +use calderawp\calderaforms\cf2\Fields\Handlers\UploaderContract; - -class MockUploader +class MockUploader implements UploaderContract { + public static function upload($file, array $args = array()) + { + // TODO: Implement upload() method. + } + } \ No newline at end of file