Skip to content

Instantly share code, notes, and snippets.

@Shelob9 Shelob9/1.php
Last active Nov 29, 2018

Embed
What would you like to do?
class Caldera_Forms_Files{
//...
public static function get_max_upload_size( array $field){}
public static function is_file_too_large( array $field, $path ){}
//...
}
<?php
namespace calderawp\calderaforms\Tests\Integration;
use Caldera_Forms_Files;
use calderawp\calderaforms\Tests\Util\Traits\TestsImages;
class Caldera_Forms_FilesTest extends TestCase
{
use TestsImages;
protected $field = [
'ID' => 'fld123',
'type' => 'file',
'config' => [
],
];
/** @inheritdoc */
public function tearDown()
{
$this->deleteTestCatFile();
parent::tearDown();
}
//...
/**
*
* @group now
*
* @since 1.8.0
*
* @covers Caldera_Forms_Files::get_max_upload_size()
*/
public function testGetMaxUploadSize()
{
$field = array_merge($this->field, [
'config' => [ 'max_upload' => 42 ],
]);
//returns saved value
$this->assertEquals( 42, Caldera_Forms_Files::get_max_upload_size($field ) );
$field = array_merge($this->field, [
'config' => [ 'max_upload' => '42' ],
]);
//always a string
$this->assertEquals( 42, Caldera_Forms_Files::get_max_upload_size($field ) );
//0 by default
$this->assertEquals( 0, Caldera_Forms_Files::get_max_upload_size($this->field ) );
}
/**
*
* @group now
*
* @since 1.8.0
*
* @covers Caldera_Forms_Files::is_file_too_large()
* @covers Caldera_Forms_Files::get_max_upload_size()
*/
public function testIfFileIsTooLarge(){
$field = array_merge($this->field, [
'config' => [ 'max_upload' => 42 ],
]);
//This file is larger than 42 bytes
$this->assertTrue(
Caldera_Forms_Files::is_file_too_large($field, $this->getSmallCatPath() )
);
$field = array_merge($this->field, [
'config' => [ 'max_upload' => 42000000 ],
]);
//This file is smaller than 42000000 bytes
$this->assertFalse(
Caldera_Forms_Files::is_file_too_large($field, $this->getSmallCatPath() )
);
//No limits, all files are the right size.
$this->assertFalse(
Caldera_Forms_Files::is_file_too_large($this->field, $this->getSmallCatPath() )
);
}
}
class Caldera_Forms_Files {
//...
/**
* Get max size for file field
*
* @since 1.8.0
*
* @param array $field Field config
*
* @return int
*/
public static function get_max_upload_size( array $field)
{
return ! empty( $field[ 'config']['max_upload'] ) ? absint( $field[ 'config']['max_upload']) : 0;
}
/**
* Check if a file is too large to upload
*
* @since 1.8.0
*
* Returns false if no max upload size set.
* Returns true if max upload size option is set and file is larger than limit
*
* @param array $field Field config
* @param string $path_to_file Path to check against
*/
public static function is_file_too_large( array $field, $path_to_file )
{
return 0 !== self::get_max_upload_size($field) && self::get_max_upload_size($field) < filesize($path_to_file );
}
}
<?php
namespace calderawp\calderaforms\Tests\Integration\Handlers;
use calderawp\calderaforms\cf2\Exception;
use calderawp\calderaforms\cf2\Fields\Handlers\Cf1FileUploader;
use calderawp\calderaforms\cf2\Fields\Handlers\FileFieldHandler;
use calderawp\calderaforms\cf2\Fields\Handlers\FileUpload;
use calderawp\calderaforms\Tests\Integration\TestCase;
use calderawp\calderaforms\Tests\Util\Traits\TestsImages;
class FileFieldHandlerTest extends TestCase
{
use TestsImages;
//...
/**
* @since 1.8.0
*
* @cover \calderawp\calderaforms\cf2\Fields\Handlers\FileUpload::getAllowedTypes()
*/
public function testKnowsFileIsTooLarge()
{
$field = [ 'ID' => 'fld1', 'config' => [ 'max_upload' => 42 ] ];
$form = [ 'ID' => 'cd1' ];
$uploader = new Cf1FileUploader();
$handler = new FileUpload($field, $form, $uploader);
$this->assertTrue( $handler->isFileTooLarge($this->getSmallCatPath() ) );
}
/**
* @since 1.8.0
*
* @cover \calderawp\calderaforms\cf2\Fields\Handlers\FileUpload::processFiles()
* @cover \calderawp\calderaforms\cf2\Fields\Handlers\FileUpload::isFileTooLarge()
*/
public function testExceptionWhenFileIsTooLarge()
{
$this->expectException(Exception::class);
$field = [ 'ID' => 'fld1', 'config' => [ 'max_upload' => 42 ] ];
$form = [ 'ID' => 'cd1' ];
$uploader = new Cf1FileUploader();
$handler = new FileUpload($field, $form, $uploader);
$handler->processFiles([ $this->getSmallCatPath() ], []);
}
/**
*
* @since 1.8.0
*
* @cover \calderawp\calderaforms\cf2\Fields\Handlers\FileUpload::getAllowedTypes()
*/
public function testAllowsFileSize()
{
$field = [ 'ID' => 'fld1', 'config' => [ 'max_upload' => 0 ] ];
$form = [ 'ID' => 'cd1' ];
$uploader = new Cf1FileUploader();
$handler = new FileUpload($field, $form, $uploader);
$this->assertFalse( $handler->isFileTooLarge($this->getSmallCatPath() ) );
$field = [ 'ID' => 'fld1', 'config' => [ 'max_upload' => 70000000 ] ];
$form = [ 'ID' => 'cd1' ];
$uploader = new Cf1FileUploader();
$handler = new FileUpload($field, $form, $uploader);
$this->assertFalse( $handler->isFileTooLarge($this->getSmallCatPath() ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.