Skip to content

Commit

Permalink
#2766 respect caldera_forms_upload_directory when uploading cf2 file …
Browse files Browse the repository at this point in the history
…field file
  • Loading branch information
Josh Pollock committed Oct 25, 2018
1 parent d215155 commit 757fbce
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 15 deletions.
9 changes: 9 additions & 0 deletions cf2/Fields/Handlers/Cf1FileUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
25 changes: 15 additions & 10 deletions cf2/Fields/Handlers/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] );
Expand All @@ -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 );
}
Expand Down
14 changes: 14 additions & 0 deletions cf2/Fields/Handlers/UploaderContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion cf2/RestApi/File/CreateFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
: [];
Expand Down
7 changes: 6 additions & 1 deletion classes/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}

}
54 changes: 51 additions & 3 deletions tests/Integration/FileUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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]);

}
}
80 changes: 80 additions & 0 deletions tests/Integration/Handlers/Cf1FileUploaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace calderawp\calderaforms\Tests\Integration\Handlers;

use calderawp\calderaforms\cf2\Fields\Handlers\Cf1FileUploader;
use calderawp\calderaforms\Tests\Integration\TestCase;

class Cf1FileUploaderTest 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();
}


/**
*
* @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' ));
}
}
Binary file added tests/Integration/Handlers/screenshot.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions tests/includes/forms/cf2-file-include.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 757fbce

Please sign in to comment.