Skip to content

Commit

Permalink
test new upload handlers #2766
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Pollock committed Oct 25, 2018
1 parent 3aba811 commit 8fbf3f8
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 27 deletions.
9 changes: 8 additions & 1 deletion cf2/Fields/Handlers/Cf1FileUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
10 changes: 9 additions & 1 deletion cf2/Fields/Handlers/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


namespace calderawp\calderaforms\cf2\Fields\Handlers;
use calderawp\calderaforms\cf2\Fields\FieldTypes\FileFieldType;
use calderawp\calderaforms\cf2\Transients\TransientApiContract;


Expand All @@ -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;
}


Expand Down
4 changes: 3 additions & 1 deletion cf2/Fields/Handlers/UploaderContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
namespace calderawp\calderaforms\cf2\Fields\Handlers;


class UploaderContract
interface UploaderContract
{

public function upload($file, array $args = array());

}
28 changes: 10 additions & 18 deletions cf2/RestApi/File/CreateFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
]);
Expand All @@ -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;
}



}
51 changes: 50 additions & 1 deletion tests/Integration/FileUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );


}
}
7 changes: 5 additions & 2 deletions tests/Integration/RestApi/CreateFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Binary file added tests/Integration/screenshot.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion tests/Unit/Fields/Handlers/FileUpload/FileUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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 );
}
}
9 changes: 7 additions & 2 deletions tests/Util/Mocks/MockUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

}

0 comments on commit 8fbf3f8

Please sign in to comment.