Skip to content

Commit

Permalink
make file process abstraction more sensible #2766
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Pollock committed Oct 25, 2018
1 parent 8fbf3f8 commit d215155
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 25 deletions.
18 changes: 3 additions & 15 deletions cf2/Fields/Handlers/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
}
14 changes: 12 additions & 2 deletions cf2/RestApi/File/CreateFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
12 changes: 9 additions & 3 deletions tests/Integration/FileUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
});

}
}
1 change: 1 addition & 0 deletions tests/Integration/RestApi/CreateFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public function testInvalidNonce(){
*
* @since 1.8.0
*
* @group now
* @group cf2
* @group file
* @group field
Expand Down
73 changes: 73 additions & 0 deletions tests/Integration/Transients/Cf1TransientsApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace calderawp\calderaforms\Tests\Integration\Transients;

use calderawp\calderaforms\cf2\Transients\Cf1TransientsApi;
use calderawp\calderaforms\Tests\Integration\TestCase;

class Cf1TransientsApiTest extends TestCase
{

/**
* Test that new Transients API wrapper is interoperable with old API's delete method
*
* @since 1.8.0
*
* @group transients
*
* @covers \calderawp\calderaforms\cf2\Transients\Cf1TransientsApi::deleteTransient()
*/
public function testDeleteTransient()
{

$id = rand() . '1';
$data = [
'foo' => '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) );
}

}
9 changes: 5 additions & 4 deletions tests/Unit/Fields/Handlers/FileUpload/FileUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
2 changes: 1 addition & 1 deletion tests/Util/Mocks/MockUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Expand Down

0 comments on commit d215155

Please sign in to comment.