diff --git a/cf2/Fields/Handlers/Cf1FileUploader.php b/cf2/Fields/Handlers/Cf1FileUploader.php index b7a3464d9..f5d317877 100644 --- a/cf2/Fields/Handlers/Cf1FileUploader.php +++ b/cf2/Fields/Handlers/Cf1FileUploader.php @@ -14,4 +14,13 @@ public function upload($file, array $args = array()) return \Caldera_Forms_Files::upload($file,$args); } + public function addFilter($fieldId, $formId, $private) + { + \Caldera_Forms_Files::add_upload_filter($fieldId,$formId,$private); + } + + public function removeFilter() + { + \Caldera_Forms_Files::remove_upload_filter(); + } } \ No newline at end of file diff --git a/cf2/Fields/Handlers/FileUpload.php b/cf2/Fields/Handlers/FileUpload.php index 4bcf85e8f..9a9dc450d 100644 --- a/cf2/Fields/Handlers/FileUpload.php +++ b/cf2/Fields/Handlers/FileUpload.php @@ -33,27 +33,23 @@ public function __construct(array $field, array $form, UploaderContract $uploade /** * @param array $files * @param array $hashes - * @param $controlCode * @return array * @throws \Exception */ - public function processFiles(array $files,array $hashes, $controlCode ){ + public function processFiles(array $files,array $hashes ){ $i = 0; foreach ($files as $file) { - if (!\Caldera_Forms_Files::is_private($this->field)) { - $uploadArgs = array( - 'private' => false, - 'field_id' => $this->field['ID'], - 'form_id' => $this->form['ID'] - ); - } else { + $isPrivate = \Caldera_Forms_Files::is_private($this->field); + + $uploadArgs = array( 'private' => true, 'field_id' => $this->field['ID'], 'form_id' => $this->form['ID'] ); - } + + $expected = $hashes[$i]; $actual = md5_file( $file['tmp_name'] ); @@ -63,7 +59,16 @@ public function processFiles(array $files,array $hashes, $controlCode ){ } + $this->uploader + ->addFilter( + $this->field[ 'ID' ], + $this->form[ 'ID' ], + $isPrivate + ); + + $upload = wp_handle_upload($file, array( 'test_form' => false, 'action' => 'foo' ) ); + $this->uploader->removeFilter(); if( !empty( $field['config']['media_lib'] ) ){ \Caldera_Forms_Files::add_to_media_library( $upload, $field ); } diff --git a/cf2/Fields/Handlers/UploaderContract.php b/cf2/Fields/Handlers/UploaderContract.php index 1baf45c9c..2763c6a4a 100644 --- a/cf2/Fields/Handlers/UploaderContract.php +++ b/cf2/Fields/Handlers/UploaderContract.php @@ -9,4 +9,18 @@ interface UploaderContract public function upload($file, array $args = array()); + /** + * @since 1.8.0 + * + * @param string $fieldId The field ID for file field + * @param string $formId The form ID + * @param boolean $private + * @return void + */ + public function addFilter($fieldId, $formId, $private ); + /** + * @since 1.8.0 + * @return void + */ + public function removeFilter(); } \ No newline at end of file diff --git a/cf2/RestApi/File/CreateFile.php b/cf2/RestApi/File/CreateFile.php index 6766be9cd..483b68f38 100644 --- a/cf2/RestApi/File/CreateFile.php +++ b/cf2/RestApi/File/CreateFile.php @@ -100,7 +100,7 @@ public function createItem(\WP_REST_Request $request) new Cf1FileUploader() ); try{ - $uploads = $handler->processFiles($files,$hashes,$controlCode); + $uploads = $handler->processFiles($files,$hashes); $transdata = is_array( $transientApi->getTransient( $controlCode ) ) ? $transientApi->getTransient( $controlCode ) : []; diff --git a/classes/files.php b/classes/files.php index 50019ff0c..cec8cf678 100755 --- a/classes/files.php +++ b/classes/files.php @@ -337,7 +337,12 @@ public static function should_attach( array $field, array $form ){ * @return array */ public static function types(){ - return array( 'advanced_file', 'file' ); + return array( + 'advanced_file', + 'file', + \calderawp\calderaforms\cf2\Fields\FieldTypes\FileFieldType::getCf1Identifier(), + \calderawp\calderaforms\cf2\Fields\FieldTypes\FileFieldType::getType() + ); } } \ No newline at end of file diff --git a/tests/Integration/FileUploadTest.php b/tests/Integration/FileUploadTest.php index fb69b68d4..3bbff6865 100644 --- a/tests/Integration/FileUploadTest.php +++ b/tests/Integration/FileUploadTest.php @@ -22,7 +22,15 @@ public function setUp() /** * @throws \Exception - * @group now + * + * @since 1.8.0 + * + * @covers \calderawp\calderaforms\cf2\Fields\Handlers\FileUpload::processFiles() + * + * @group cf2 + * @group file + * @group field + * @group cf2_file */ public function testProcessFile() { @@ -50,16 +58,56 @@ public function testProcessFile() $field, new Cf1FileUploader() ); - $uploads = $handler->processFiles($files, [md5_file($this->test_file)], 'f1' ); + $uploads = $handler->processFiles($files, [md5_file($this->test_file)] ); $this->assertTrue( is_array( $uploads )); $this->assertEquals( 1, count($uploads ) ); } + protected $filterWasCalled; + /** + * Test that caldera_forms_upload_directory filter is respected + * + * @since 1.8.0 + * + * @covers \calderawp\calderaforms\cf2\Fields\Handlers\FileUpload::processFiles() + * + * @group cf2 + * @group file + * @group field + * @group cf2_file + * + * @throws \Exception + */ public function testFilterDirectoryForUpload(){ - add_filter( 'caldera_forms_upload_directory', function(){ + add_filter( 'caldera_forms_upload_directory', function() { return 'form-uploads'; }); + $formId = 'cf2_file'; + $fieldId = 'cf2_file_2'; + $form = \Caldera_Forms_Forms::get_form( $formId ); + $field = \Caldera_Forms_Field_Util::get_field($fieldId,$form); + $this->assertFalse( \Caldera_Forms_Files::is_private($field) ); + + $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 Cf1FileUploader() + ); + + $uploads = $handler->processFiles($files, [md5_file($this->test_file)] ); + $this->assertTrue( is_array($uploads)); + $this->assertNotFalse( strpos($uploads[0], 'form-uploads'), $uploads[0]); + } } diff --git a/tests/Integration/Handlers/Cf1FileUploaderTest.php b/tests/Integration/Handlers/Cf1FileUploaderTest.php new file mode 100644 index 000000000..439844a5e --- /dev/null +++ b/tests/Integration/Handlers/Cf1FileUploaderTest.php @@ -0,0 +1,80 @@ +test_file = '/tmp/screenshot.jpg'; + copy($orig_file, $this->test_file); + parent::setUp(); + } + + + /** + * + * @since 1.8.0 + * + * @covers \calderawp\calderaforms\cf2\Fields\Handlers\Cf1FileUploader::upload() + * + * @group file + */ + public function testUpload() + { + $file = [ + 'file' => file_get_contents($this->test_file), + 'name' => 'screenshot.jpeg', + 'size' => filesize($this->test_file), + 'tmp_name' => $this->test_file, + ]; + $uploadArgs = [ + 'private' => true, + 'field_id' => 'fld1', + 'form_id' => 'cf1' + ]; + + $uploader = new Cf1FileUploader(); + $uploads = $uploader->upload($file, $uploadArgs); + $this->assertTrue( is_array( $uploads ) ); + $this->assertFalse( is_wp_error( $uploads ) ); + + } + /** + * + * @since 1.8.0 + * + * @covers \calderawp\calderaforms\cf2\Fields\Handlers\Cf1FileUploader::addFilter() + * + * @group file + */ + public function testAddFilter() + { + $uploader = new Cf1FileUploader(); + $uploader->addFilter('f', 'c', true ); + $this->assertTrue( has_filter( 'upload_dir'), array( \Caldera_Forms_Files::class, 'uploads_filter' )); + } + /** + * + * @since 1.8.0 + * + * @covers \calderawp\calderaforms\cf2\Fields\Handlers\Cf1FileUploader::addFilter() + * @covers \calderawp\calderaforms\cf2\Fields\Handlers\Cf1FileUploader::removeFilter() + * + * @group file + */ + public function testRemoveFilter() + { + $uploader = new Cf1FileUploader(); + $uploader->addFilter('f', 'c', true ); + $uploader->removeFilter(); + $this->assertFalse( has_filter( 'upload_dir'), array( \Caldera_Forms_Files::class, 'uploads_filter' )); + } +} diff --git a/tests/Integration/Handlers/screenshot.jpeg b/tests/Integration/Handlers/screenshot.jpeg new file mode 100644 index 000000000..bcf2ea588 Binary files /dev/null and b/tests/Integration/Handlers/screenshot.jpeg differ diff --git a/tests/includes/forms/cf2-file-include.php b/tests/includes/forms/cf2-file-include.php index fb704aae9..9ea5ff977 100644 --- a/tests/includes/forms/cf2-file-include.php +++ b/tests/includes/forms/cf2-file-include.php @@ -71,6 +71,27 @@ function slug_register_caldera_forms_cf2file( $forms ) { 'personally_identifying' => 0, ), ), + 'cf2_file_2' => + array( + 'ID' => 'cf2_file_2', + 'type' => 'cf2_file', + 'label' => 'CF2_File_2', + 'slug' => 'cf2_file_2', + 'conditions' => + array( + 'type' => '', + ), + 'caption' => '', + 'config' => + array( + 'custom_class' => '', + 'multi_upload_text' => '', + 'allowed' => '', + 'email_identifier' => 0, + 'personally_identifying' => 0, + 'media_lib' => true + ), + ), 'test_field_1' => array( 'ID' => 'test_field_1',