2012-02-03 00:59:40 +01:00
|
|
|
<?php
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
|
|
|
use SilverStripe\Assets\FileNameFilter;
|
|
|
|
use SilverStripe\Control\HTTPResponse;
|
|
|
|
use SilverStripe\Forms\Tests\UploadFieldTest\TestController;
|
|
|
|
use SilverStripe\Forms\Tests\UploadFieldTest\ExtendedFile;
|
|
|
|
use SilverStripe\Forms\Tests\UploadFieldTest\FileExtension;
|
|
|
|
use SilverStripe\Forms\Tests\UploadFieldTest\TestRecord;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Assets\Folder;
|
|
|
|
use SilverStripe\Assets\Filesystem;
|
|
|
|
use SilverStripe\Assets\File;
|
|
|
|
use SilverStripe\Assets\Upload;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Dev\CSSContentParser;
|
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
|
|
|
use SilverStripe\Control\Session;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Forms\UploadField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
class UploadFieldTest extends FunctionalTest {
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'UploadFieldTest.yml';
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
protected $extraDataObjects = [
|
|
|
|
TestRecord::class,
|
|
|
|
ExtendedFile::class,
|
|
|
|
];
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
protected $extraControllers = [
|
|
|
|
TestController::class,
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $requiredExtensions = [
|
|
|
|
File::class => [
|
|
|
|
FileExtension::class
|
|
|
|
]
|
|
|
|
];
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
protected $oldReadingMode = null;
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
$this->logInWithPermission('ADMIN');
|
2016-01-26 04:56:07 +01:00
|
|
|
|
|
|
|
// Save versioned state
|
|
|
|
$this->oldReadingMode = Versioned::get_reading_mode();
|
2016-03-02 06:18:10 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2016-01-26 04:56:07 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Set backend root to /UploadFieldTest
|
2016-10-14 03:30:05 +02:00
|
|
|
TestAssetStore::activate('UploadFieldTest');
|
2015-09-15 04:52:02 +02:00
|
|
|
|
2016-05-09 10:12:29 +02:00
|
|
|
// Set the File Name Filter replacements so files have the expected names
|
2016-10-14 03:30:05 +02:00
|
|
|
Config::inst()->update(FileNameFilter::class, 'default_replacements', array(
|
2016-05-09 10:12:29 +02:00
|
|
|
'/\s/' => '-', // remove whitespace
|
|
|
|
'/_/' => '-', // underscores to dashes
|
|
|
|
'/[^A-Za-z0-9+.\-]+/' => '', // remove non-ASCII chars, only allow alphanumeric plus dash and dot
|
|
|
|
'/[\-]{2,}/' => '-', // remove duplicate dashes
|
|
|
|
'/^[\.\-_]+/' => '', // Remove all leading dots, dashes or underscores
|
|
|
|
));
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Create a test folders for each of the fixture references
|
|
|
|
foreach(Folder::get() as $folder) {
|
2016-10-14 03:30:05 +02:00
|
|
|
$path = TestAssetStore::getLocalPath($folder);
|
2015-10-23 02:51:26 +02:00
|
|
|
Filesystem::makeFolder($path);
|
2015-09-15 04:52:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a test files for each of the fixture references
|
2016-10-14 03:30:05 +02:00
|
|
|
$files = File::get()->exclude('ClassName', Folder::class);
|
2015-09-15 04:52:02 +02:00
|
|
|
foreach($files as $file) {
|
2016-10-14 03:30:05 +02:00
|
|
|
$path = TestAssetStore::getLocalPath($file);
|
2015-10-23 02:51:26 +02:00
|
|
|
Filesystem::makeFolder(dirname($path));
|
2015-09-15 04:52:02 +02:00
|
|
|
$fh = fopen($path, "w+");
|
|
|
|
fwrite($fh, str_repeat('x', 1000000));
|
|
|
|
fclose($fh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
2016-10-14 03:30:05 +02:00
|
|
|
TestAssetStore::reset();
|
2016-01-26 04:56:07 +01:00
|
|
|
if($this->oldReadingMode) {
|
|
|
|
Versioned::set_reading_mode($this->oldReadingMode);
|
|
|
|
}
|
2015-09-15 04:52:02 +02:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test that files can be uploaded against an object with no relation
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testUploadNoRelation() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$tmpFileName = 'testUploadBasic.txt';
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockFileUpload('NoRelationField', $tmpFileName);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFalse($response->isError());
|
2016-10-14 03:30:05 +02:00
|
|
|
$uploadedFile = DataObject::get_one(File::class, array(
|
2013-06-21 00:32:08 +02:00
|
|
|
'"File"."Name"' => $tmpFileName
|
|
|
|
));
|
2016-01-26 04:56:07 +01:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertFileExists(TestAssetStore::getLocalPath($uploadedFile));
|
2012-04-18 04:46:54 +02:00
|
|
|
$this->assertTrue(is_object($uploadedFile), 'The file object is created');
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test that an object can be uploaded against an object with a has_one relation
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testUploadHasOneRelation() {
|
2012-02-03 00:59:40 +01:00
|
|
|
// Unset existing has_one relation before re-uploading
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var TestRecord $record */
|
|
|
|
$record = $this->objFromFixture(TestRecord::class, 'record1');
|
2012-02-03 00:59:40 +01:00
|
|
|
$record->HasOneFileID = null;
|
|
|
|
$record->write();
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Firstly, ensure the file can be uploaded
|
2012-02-03 00:59:40 +01:00
|
|
|
$tmpFileName = 'testUploadHasOneRelation.txt';
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockFileUpload('HasOneFile', $tmpFileName);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFalse($response->isError());
|
2016-10-14 03:30:05 +02:00
|
|
|
$uploadedFile = DataObject::get_one(File::class, array(
|
2013-06-21 00:32:08 +02:00
|
|
|
'"File"."Name"' => $tmpFileName
|
|
|
|
));
|
2012-04-18 04:46:54 +02:00
|
|
|
$this->assertTrue(is_object($uploadedFile), 'The file object is created');
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertFileExists(TestAssetStore::getLocalPath($uploadedFile));
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Secondly, ensure that simply uploading an object does not save the file against the relation
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertFalse($record->HasOneFile()->exists());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Thirdly, test submitting the form with the encoded data
|
|
|
|
$response = $this->mockUploadFileIDs('HasOneFile', array($uploadedFile->ID));
|
|
|
|
$this->assertEmpty($response['errors']);
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertTrue($record->HasOneFile()->exists());
|
|
|
|
$this->assertEquals($record->HasOneFile()->Name, $tmpFileName);
|
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Tests that has_one relations work with subclasses of File
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testUploadHasOneRelationWithExtendedFile() {
|
2012-04-18 05:22:40 +02:00
|
|
|
// Unset existing has_one relation before re-uploading
|
2016-10-14 03:30:05 +02:00
|
|
|
/** @var TestRecord $record */
|
|
|
|
$record = $this->objFromFixture(TestRecord::class, 'record1');
|
2012-04-18 05:22:40 +02:00
|
|
|
$record->HasOneExtendedFileID = null;
|
|
|
|
$record->write();
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Test that the file can be safely uploaded
|
2012-04-18 05:22:40 +02:00
|
|
|
$tmpFileName = 'testUploadHasOneRelationWithExtendedFile.txt';
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockFileUpload('HasOneExtendedFile', $tmpFileName);
|
2012-04-18 05:22:40 +02:00
|
|
|
$this->assertFalse($response->isError());
|
2016-10-14 03:30:05 +02:00
|
|
|
$uploadedFile = DataObject::get_one(ExtendedFile::class, array(
|
2013-06-21 00:32:08 +02:00
|
|
|
'"File"."Name"' => $tmpFileName
|
|
|
|
));
|
2012-04-18 05:22:40 +02:00
|
|
|
$this->assertTrue(is_object($uploadedFile), 'The file object is created');
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertFileExists(TestAssetStore::getLocalPath($uploadedFile));
|
2012-04-18 05:22:40 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Test that the record isn't written to automatically
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertFalse($record->HasOneExtendedFile()->exists());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Test that saving the form writes the record
|
|
|
|
$response = $this->mockUploadFileIDs('HasOneExtendedFile', array($uploadedFile->ID));
|
|
|
|
$this->assertEmpty($response['errors']);
|
2012-04-18 05:22:40 +02:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
2013-05-27 05:22:59 +02:00
|
|
|
$this->assertTrue($record->HasOneExtendedFile()->exists());
|
|
|
|
$this->assertEquals($record->HasOneExtendedFile()->Name, $tmpFileName);
|
2012-04-18 05:22:40 +02:00
|
|
|
}
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test that has_many relations work with files
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testUploadHasManyRelation() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$record = $this->objFromFixture(TestRecord::class, 'record1');
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Test that uploaded files can be posted to a has_many relation
|
2012-02-03 00:59:40 +01:00
|
|
|
$tmpFileName = 'testUploadHasManyRelation.txt';
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockFileUpload('HasManyFiles', $tmpFileName);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFalse($response->isError());
|
2016-10-14 03:30:05 +02:00
|
|
|
$uploadedFile = DataObject::get_one(File::class, array(
|
2013-06-21 00:32:08 +02:00
|
|
|
'"File"."Name"' => $tmpFileName
|
|
|
|
));
|
2012-04-18 04:46:54 +02:00
|
|
|
$this->assertTrue(is_object($uploadedFile), 'The file object is created');
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertFileExists(TestAssetStore::getLocalPath($uploadedFile));
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Test that the record isn't written to automatically
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertEquals(2, $record->HasManyFiles()->Count()); // Existing two files should be retained
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Test that saving the form writes the record
|
|
|
|
$ids = array_merge($record->HasManyFiles()->getIDList(), array($uploadedFile->ID));
|
|
|
|
$response = $this->mockUploadFileIDs('HasManyFiles', $ids);
|
|
|
|
$this->assertEmpty($response['errors']);
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
2013-05-27 05:22:59 +02:00
|
|
|
$this->assertEquals(3, $record->HasManyFiles()->Count()); // New record should appear here now
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test that many_many relationships work with files
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testUploadManyManyRelation() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$record = $this->objFromFixture(TestRecord::class, 'record1');
|
2012-02-03 00:59:40 +01:00
|
|
|
$relationCount = $record->ManyManyFiles()->Count();
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Test that uploaded files can be posted to a many_many relation
|
2012-02-03 00:59:40 +01:00
|
|
|
$tmpFileName = 'testUploadManyManyRelation.txt';
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockFileUpload('ManyManyFiles', $tmpFileName);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFalse($response->isError());
|
2016-10-14 03:30:05 +02:00
|
|
|
$uploadedFile = DataObject::get_one(File::class, array(
|
2013-06-21 00:32:08 +02:00
|
|
|
'"File"."Name"' => $tmpFileName
|
|
|
|
));
|
2012-04-18 04:46:54 +02:00
|
|
|
$this->assertTrue(is_object($uploadedFile), 'The file object is created');
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertFileExists(TestAssetStore::getLocalPath($uploadedFile));
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Test that the record isn't written to automatically
|
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
2013-05-27 05:42:10 +02:00
|
|
|
// Existing file count should be retained
|
|
|
|
$this->assertEquals($relationCount, $record->ManyManyFiles()->Count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Test that saving the form writes the record
|
|
|
|
$ids = array_merge($record->ManyManyFiles()->getIDList(), array($uploadedFile->ID));
|
|
|
|
$response = $this->mockUploadFileIDs('ManyManyFiles', $ids);
|
|
|
|
$this->assertEmpty($response['errors']);
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
2013-05-27 05:22:59 +02:00
|
|
|
$record->flushCache();
|
2013-05-27 05:42:10 +02:00
|
|
|
// New record should appear here now
|
|
|
|
$this->assertEquals($relationCount + 1, $record->ManyManyFiles()->Count());
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2013-07-12 13:16:25 +02:00
|
|
|
/**
|
|
|
|
* Partially covered by {@link UploadTest->testUploadAcceptsAllowedExtension()},
|
|
|
|
* but this test additionally verifies that those constraints are actually enforced
|
|
|
|
* in this controller method.
|
|
|
|
*/
|
|
|
|
public function testAllowedExtensions() {
|
2014-04-09 07:37:25 +02:00
|
|
|
// Test invalid file
|
|
|
|
// Relies on Upload_Validator failing to allow this extension
|
2013-07-12 13:16:25 +02:00
|
|
|
$invalidFile = 'invalid.php';
|
|
|
|
$_FILES = array('AllowedExtensionsField' => $this->getUploadFile($invalidFile));
|
|
|
|
$response = $this->post(
|
2013-07-19 04:18:49 +02:00
|
|
|
'UploadFieldTest_Controller/Form/field/AllowedExtensionsField/upload',
|
2013-07-12 13:16:25 +02:00
|
|
|
array('AllowedExtensionsField' => $this->getUploadFile($invalidFile))
|
|
|
|
);
|
2014-09-20 16:09:48 +02:00
|
|
|
$response = json_decode($response->getBody(), true);
|
|
|
|
$this->assertTrue(array_key_exists('error', $response[0]));
|
|
|
|
$this->assertContains('Extension is not allowed', $response[0]['error']);
|
2013-07-12 13:16:25 +02:00
|
|
|
|
2014-04-09 07:37:25 +02:00
|
|
|
// Test valid file
|
2013-07-19 04:18:49 +02:00
|
|
|
$validFile = 'valid.txt';
|
2013-07-12 13:16:25 +02:00
|
|
|
$_FILES = array('AllowedExtensionsField' => $this->getUploadFile($validFile));
|
|
|
|
$response = $this->post(
|
2013-07-19 04:18:49 +02:00
|
|
|
'UploadFieldTest_Controller/Form/field/AllowedExtensionsField/upload',
|
2013-07-12 13:16:25 +02:00
|
|
|
array('AllowedExtensionsField' => $this->getUploadFile($validFile))
|
|
|
|
);
|
2014-09-20 16:09:48 +02:00
|
|
|
$response = json_decode($response->getBody(), true);
|
|
|
|
$this->assertFalse(array_key_exists('error', $response[0]));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-09 07:37:25 +02:00
|
|
|
// Test that setAllowedExtensions rejects extensions explicitly denied by File.allowed_extensions
|
|
|
|
// Relies on File::validate failing to allow this extension
|
|
|
|
$invalidFile = 'invalid.php';
|
|
|
|
$_FILES = array('AllowedExtensionsField' => $this->getUploadFile($invalidFile));
|
|
|
|
$response = $this->post(
|
|
|
|
'UploadFieldTest_Controller/Form/field/InvalidAllowedExtensionsField/upload',
|
|
|
|
array('InvalidAllowedExtensionsField' => $this->getUploadFile($invalidFile))
|
|
|
|
);
|
2015-06-17 01:24:25 +02:00
|
|
|
$response = json_decode($response->getBody(), true);
|
|
|
|
$this->assertTrue(array_key_exists('error', $response[0]));
|
|
|
|
$this->assertContains('Extension is not allowed', $response[0]['error']);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-07-12 13:16:25 +02:00
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test that has_one relations do not support multiple files
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAllowedMaxFileNumberWithHasOne() {
|
2013-05-27 05:22:59 +02:00
|
|
|
// Get references for each file to upload
|
2016-10-14 03:30:05 +02:00
|
|
|
$file1 = $this->objFromFixture(File::class, 'file1');
|
|
|
|
$file2 = $this->objFromFixture(File::class, 'file2');
|
2013-05-27 05:22:59 +02:00
|
|
|
$fileIDs = array($file1->ID, $file2->ID);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-17 07:12:56 +02:00
|
|
|
// Test each of the three cases - has one with no max filel limit, has one with a limit of
|
|
|
|
// one, has one with a limit of more than one (makes no sense, but should test it anyway).
|
2012-09-19 12:07:39 +02:00
|
|
|
// Each of them should public function in the same way - attaching the first file should work, the
|
2012-04-17 07:12:56 +02:00
|
|
|
// second should cause an error.
|
|
|
|
foreach (array('HasOneFile', 'HasOneFileMaxOne', 'HasOneFileMaxTwo') as $recordName) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-17 07:12:56 +02:00
|
|
|
// Unset existing has_one relation before re-uploading
|
2016-10-14 03:30:05 +02:00
|
|
|
$record = $this->objFromFixture(TestRecord::class, 'record1');
|
2013-05-27 05:22:59 +02:00
|
|
|
$record->{"{$recordName}ID"} = null;
|
2012-04-17 07:12:56 +02:00
|
|
|
$record->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Post form with two files for this field, should result in an error
|
|
|
|
$response = $this->mockUploadFileIDs($recordName, $fileIDs);
|
|
|
|
$isError = !empty($response['errors']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Strictly, a has_one should not allow two files, but this is overridden
|
|
|
|
// by the setAllowedMaxFileNumber(2) call
|
|
|
|
$maxFiles = ($recordName === 'HasOneFileMaxTwo') ? 2 : 1;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Assert that the form fails if the maximum number of files is exceeded
|
|
|
|
$this->assertTrue((count($fileIDs) > $maxFiles) == $isError);
|
2012-04-17 07:12:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-27 05:42:10 +02:00
|
|
|
/**
|
|
|
|
* Test that max number of items on has_many is validated
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAllowedMaxFileNumberWithHasMany() {
|
2012-04-17 07:12:56 +02:00
|
|
|
// The 'HasManyFilesMaxTwo' field has a maximum of two files able to be attached to it.
|
|
|
|
// We want to add files to it until we attempt to add the third. We expect that the first
|
|
|
|
// two should work and the third will fail.
|
2016-10-14 03:30:05 +02:00
|
|
|
$record = $this->objFromFixture(TestRecord::class, 'record1');
|
2012-04-17 07:12:56 +02:00
|
|
|
$record->HasManyFilesMaxTwo()->removeAll();
|
2015-04-02 12:27:33 +02:00
|
|
|
$this->assertCount(0, $record->HasManyFilesMaxTwo());
|
2013-05-27 05:42:10 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Get references for each file to upload
|
2016-10-14 03:30:05 +02:00
|
|
|
$file1 = $this->objFromFixture(File::class, 'file1');
|
|
|
|
$file2 = $this->objFromFixture(File::class, 'file2');
|
|
|
|
$file3 = $this->objFromFixture(File::class, 'file3');
|
2015-04-02 12:27:33 +02:00
|
|
|
$this->assertTrue($file1->exists());
|
|
|
|
$this->assertTrue($file2->exists());
|
|
|
|
$this->assertTrue($file3->exists());
|
2012-04-17 07:12:56 +02:00
|
|
|
|
|
|
|
// Write the first element, should be okay.
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockUploadFileIDs('HasManyFilesMaxTwo', array($file1->ID));
|
|
|
|
$this->assertEmpty($response['errors']);
|
2015-04-02 12:27:33 +02:00
|
|
|
$this->assertCount(1, $record->HasManyFilesMaxTwo());
|
|
|
|
$this->assertContains($file1->ID, $record->HasManyFilesMaxTwo()->getIDList());
|
|
|
|
|
|
|
|
|
|
|
|
$record->HasManyFilesMaxTwo()->removeAll();
|
|
|
|
$this->assertCount(0, $record->HasManyFilesMaxTwo());
|
|
|
|
$this->assertTrue($file1->exists());
|
|
|
|
$this->assertTrue($file2->exists());
|
|
|
|
$this->assertTrue($file3->exists());
|
|
|
|
|
|
|
|
|
2013-05-27 05:42:10 +02:00
|
|
|
|
2012-04-17 07:12:56 +02:00
|
|
|
// Write the second element, should be okay.
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockUploadFileIDs('HasManyFilesMaxTwo', array($file1->ID, $file2->ID));
|
|
|
|
$this->assertEmpty($response['errors']);
|
2015-04-02 12:27:33 +02:00
|
|
|
$this->assertCount(2, $record->HasManyFilesMaxTwo());
|
|
|
|
$this->assertContains($file1->ID, $record->HasManyFilesMaxTwo()->getIDList());
|
|
|
|
$this->assertContains($file2->ID, $record->HasManyFilesMaxTwo()->getIDList());
|
|
|
|
|
|
|
|
$record->HasManyFilesMaxTwo()->removeAll();
|
|
|
|
$this->assertCount(0, $record->HasManyFilesMaxTwo());
|
|
|
|
$this->assertTrue($file1->exists());
|
|
|
|
$this->assertTrue($file2->exists());
|
|
|
|
$this->assertTrue($file3->exists());
|
|
|
|
|
2013-05-27 05:42:10 +02:00
|
|
|
|
2012-04-17 07:12:56 +02:00
|
|
|
// Write the third element, should result in error.
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockUploadFileIDs('HasManyFilesMaxTwo', array($file1->ID, $file2->ID, $file3->ID));
|
|
|
|
$this->assertNotEmpty($response['errors']);
|
2015-04-02 12:27:33 +02:00
|
|
|
$this->assertCount(0, $record->HasManyFilesMaxTwo());
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test that files can be removed from has_one relations
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRemoveFromHasOne() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$record = $this->objFromFixture(TestRecord::class, 'record1');
|
|
|
|
$file1 = $this->objFromFixture(File::class, 'file1');
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Check record exists
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertTrue($record->HasOneFile()->exists());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Remove from record
|
|
|
|
$response = $this->mockUploadFileIDs('HasOneFile', array());
|
|
|
|
$this->assertEmpty($response['errors']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Check file is removed
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertFalse($record->HasOneFile()->exists());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Check file object itself exists
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->assertFileExists(
|
2016-10-14 03:30:05 +02:00
|
|
|
TestAssetStore::getLocalPath($file1),
|
2015-09-15 04:52:02 +02:00
|
|
|
'File is only detached, not deleted from filesystem'
|
|
|
|
);
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test that items can be removed from has_many
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRemoveFromHasMany() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$record = $this->objFromFixture(TestRecord::class, 'record1');
|
|
|
|
$file3 = $this->objFromFixture(File::class, 'file3');
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Check record has two files attached
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertEquals(array('File2', 'File3'), $record->HasManyFiles()->column('Title'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Remove file 2
|
|
|
|
$response = $this->mockUploadFileIDs('HasManyFiles', array($file3->ID));
|
|
|
|
$this->assertEmpty($response['errors']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// check only file 3 is left
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertEquals(array('File3'), $record->HasManyFiles()->column('Title'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Check file 2 object itself exists
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->assertFileExists(
|
2016-10-14 03:30:05 +02:00
|
|
|
TestAssetStore::getLocalPath($file3),
|
2015-09-15 04:52:02 +02:00
|
|
|
'File is only detached, not deleted from filesystem'
|
|
|
|
);
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test that items can be removed from many_many
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRemoveFromManyMany() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$record = $this->objFromFixture(TestRecord::class, 'record1');
|
|
|
|
$file4 = $this->objFromFixture(File::class, 'file4');
|
|
|
|
$file5 = $this->objFromFixture(File::class, 'file5');
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Check that both files are currently set
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertContains('File4', $record->ManyManyFiles()->column('Title'));
|
|
|
|
$this->assertContains('File5', $record->ManyManyFiles()->column('Title'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Remove file 4
|
|
|
|
$response = $this->mockUploadFileIDs('ManyManyFiles', array($file5->ID));
|
|
|
|
$this->assertEmpty($response['errors']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// check only file 5 is left
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertNotContains('File4', $record->ManyManyFiles()->column('Title'));
|
|
|
|
$this->assertContains('File5', $record->ManyManyFiles()->column('Title'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// check file 4 object exists
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->assertFileExists(
|
2016-10-14 03:30:05 +02:00
|
|
|
TestAssetStore::getLocalPath($file4),
|
2015-09-15 04:52:02 +02:00
|
|
|
'File is only detached, not deleted from filesystem'
|
|
|
|
);
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* Test that files can be deleted from has_one
|
2013-05-27 05:22:59 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDeleteFromHasOne() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$record = $this->objFromFixture(TestRecord::class, 'record1');
|
|
|
|
$file1 = $this->objFromFixture(File::class, 'file1');
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Check that file initially exists
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertTrue($record->HasOneFile()->exists());
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertFileExists(TestAssetStore::getLocalPath($file1));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Delete file and update record
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockFileDelete('HasOneFile', $file1->ID);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFalse($response->isError());
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockUploadFileIDs('HasOneFile', array());
|
|
|
|
$this->assertEmpty($response['errors']);
|
|
|
|
|
|
|
|
// Check that file is not set against record
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertFalse($record->HasOneFile()->exists());
|
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
2015-09-15 04:52:02 +02:00
|
|
|
* Test that files can be deleted from has_many
|
2013-05-27 05:22:59 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDeleteFromHasMany() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$record = $this->objFromFixture(TestRecord::class, 'record1');
|
|
|
|
$file2 = $this->objFromFixture(File::class, 'file2');
|
|
|
|
$file3 = $this->objFromFixture(File::class, 'file3');
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Check that files initially exists
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertEquals(array('File2', 'File3'), $record->HasManyFiles()->column('Title'));
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertFileExists(TestAssetStore::getLocalPath($file2));
|
|
|
|
$this->assertFileExists(TestAssetStore::getLocalPath($file3));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Delete dataobject file and update record without file 2
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockFileDelete('HasManyFiles', $file2->ID);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFalse($response->isError());
|
2013-05-27 05:22:59 +02:00
|
|
|
$response = $this->mockUploadFileIDs('HasManyFiles', array($file3->ID));
|
|
|
|
$this->assertEmpty($response['errors']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Test that file is removed from record
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertEquals(array('File3'), $record->HasManyFiles()->column('Title'));
|
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test that files can be deleted from many_many and the filesystem
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDeleteFromManyMany() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$record = $this->objFromFixture(TestRecord::class, 'record1');
|
|
|
|
$file4 = $this->objFromFixture(File::class, 'file4');
|
|
|
|
$file5 = $this->objFromFixture(File::class, 'file5');
|
|
|
|
$fileNoDelete = $this->objFromFixture(File::class, 'file-nodelete');
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Test that files initially exist
|
|
|
|
$setFiles = $record->ManyManyFiles()->column('Title');
|
|
|
|
$this->assertContains('File4', $setFiles);
|
|
|
|
$this->assertContains('File5', $setFiles);
|
|
|
|
$this->assertContains('nodelete.txt', $setFiles);
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertFileExists(TestAssetStore::getLocalPath($file4));
|
|
|
|
$this->assertFileExists(TestAssetStore::getLocalPath($file5));
|
|
|
|
$this->assertFileExists(TestAssetStore::getLocalPath($fileNoDelete));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Delete physical file and update record without file 4
|
|
|
|
$response = $this->mockFileDelete('ManyManyFiles', $file4->ID);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFalse($response->isError());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Check file is removed from record
|
2012-02-03 00:59:40 +01:00
|
|
|
$record = DataObject::get_by_id($record->class, $record->ID, false);
|
|
|
|
$this->assertNotContains('File4', $record->ManyManyFiles()->column('Title'));
|
|
|
|
$this->assertContains('File5', $record->ManyManyFiles()->column('Title'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
// Test record-based permissions
|
2014-12-16 03:43:24 +01:00
|
|
|
$response = $this->mockFileDelete('ManyManyFiles', $fileNoDelete->ID);
|
|
|
|
$this->assertEquals(403, $response->getStatusCode());
|
|
|
|
|
|
|
|
// Test that folders can't be deleted
|
2016-10-14 03:30:05 +02:00
|
|
|
$folder = $this->objFromFixture(Folder::class, 'folder1-subfolder1');
|
2014-12-16 03:43:24 +01:00
|
|
|
$response = $this->mockFileDelete('ManyManyFiles', $folder->ID);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertEquals(403, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Test control output html
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testView() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$file4 = $this->objFromFixture(File::class, 'file4');
|
|
|
|
$file5 = $this->objFromFixture(File::class, 'file5');
|
|
|
|
$fileNoView = $this->objFromFixture(File::class, 'file-noview');
|
|
|
|
$fileNoEdit = $this->objFromFixture(File::class, 'file-noedit');
|
|
|
|
$fileNoDelete = $this->objFromFixture(File::class, 'file-nodelete');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
$response = $this->get('UploadFieldTest_Controller');
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
|
|
|
$parser = new CSSContentParser($response->getBody());
|
2013-05-31 19:59:51 +02:00
|
|
|
$items = $parser->getBySelector(
|
|
|
|
'#UploadFieldTestForm_Form_HasManyNoViewFiles_Holder .ss-uploadfield-files .ss-uploadfield-item'
|
|
|
|
);
|
2012-02-03 00:59:40 +01:00
|
|
|
$ids = array();
|
|
|
|
foreach($items as $item) $ids[] = (int)$item['data-fileid'];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertContains($file4->ID, $ids, 'Views related file');
|
|
|
|
$this->assertContains($file5->ID, $ids, 'Views related file');
|
|
|
|
$this->assertNotContains($fileNoView->ID, $ids, "Doesn't view files without view permissions");
|
|
|
|
$this->assertContains($fileNoEdit->ID, $ids, "Views files without edit permissions");
|
|
|
|
$this->assertContains($fileNoDelete->ID, $ids, "Views files without delete permissions");
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testEdit() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$record = $this->objFromFixture(TestRecord::class, 'record1');
|
|
|
|
$file4 = $this->objFromFixture(File::class, 'file4');
|
|
|
|
$fileNoEdit = $this->objFromFixture(File::class, 'file-noedit');
|
|
|
|
$folder = $this->objFromFixture(Folder::class, 'folder1-subfolder1');
|
2012-02-03 00:59:40 +01:00
|
|
|
|
2014-12-16 03:43:24 +01:00
|
|
|
$response = $this->mockFileEditForm('ManyManyFiles', $file4->ID);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
2014-12-16 03:43:24 +01:00
|
|
|
$response = $this->mockFileEdit('ManyManyFiles', $file4->ID, array('Title' => 'File 4 modified'));
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
|
|
|
$file4 = DataObject::get_by_id($file4->class, $file4->ID, false);
|
|
|
|
$this->assertEquals('File 4 modified', $file4->Title);
|
|
|
|
|
|
|
|
// Test record-based permissions
|
2014-12-16 03:43:24 +01:00
|
|
|
$response = $this->mockFileEditForm('ManyManyFiles', $fileNoEdit->ID);
|
|
|
|
$this->assertEquals(403, $response->getStatusCode());
|
|
|
|
|
|
|
|
$response = $this->mockFileEdit('ManyManyFiles', $fileNoEdit->ID, array());
|
|
|
|
$this->assertEquals(403, $response->getStatusCode());
|
|
|
|
|
|
|
|
// Test folder permissions
|
|
|
|
$response = $this->mockFileEditForm('ManyManyFiles', $folder->ID);
|
|
|
|
$this->assertEquals(403, $response->getStatusCode());
|
|
|
|
|
|
|
|
$response = $this->mockFileEdit('ManyManyFiles', $folder->ID, array());
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertEquals(403, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetRecord() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$record = $this->objFromFixture(TestRecord::class, 'record1');
|
2012-02-03 00:59:40 +01:00
|
|
|
$form = $this->getMockForm();
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('MyField');
|
2012-02-03 00:59:40 +01:00
|
|
|
$field->setForm($form);
|
|
|
|
$this->assertNull($field->getRecord(), 'Returns no record by default');
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('MyField');
|
2012-02-03 00:59:40 +01:00
|
|
|
$field->setForm($form);
|
|
|
|
$form->loadDataFrom($record);
|
|
|
|
$this->assertEquals($record, $field->getRecord(), 'Returns record from form if available');
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('MyField');
|
2012-02-03 00:59:40 +01:00
|
|
|
$field->setForm($form);
|
|
|
|
$field->setRecord($record);
|
|
|
|
$this->assertEquals($record, $field->getRecord(), 'Returns record when set explicitly');
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSetItems() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$record = $this->objFromFixture(TestRecord::class, 'record1');
|
2012-02-03 00:59:40 +01:00
|
|
|
$items = new ArrayList(array(
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->objFromFixture(File::class, 'file1'),
|
|
|
|
$this->objFromFixture(File::class, 'file2')
|
2012-02-03 00:59:40 +01:00
|
|
|
));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// Field with no record attached
|
|
|
|
$field = UploadField::create('DummyField');
|
|
|
|
$field->setItems($items);
|
|
|
|
$this->assertEquals(array('File1', 'File2'), $field->getItems()->column('Title'));
|
2012-02-03 00:59:40 +01:00
|
|
|
|
|
|
|
// Anonymous field
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('MyField');
|
2012-02-03 00:59:40 +01:00
|
|
|
$field->setRecord($record);
|
|
|
|
$field->setItems($items);
|
|
|
|
$this->assertEquals(array('File1', 'File2'), $field->getItems()->column('Title'));
|
|
|
|
|
|
|
|
// Field with has_one auto-detected
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('HasOneFile');
|
2012-02-03 00:59:40 +01:00
|
|
|
$field->setRecord($record);
|
|
|
|
$field->setItems($items);
|
|
|
|
$this->assertEquals(array('File1', 'File2'), $field->getItems()->column('Title'),
|
|
|
|
'Allows overwriting of items even when relationship is detected'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetItems() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$record = $this->objFromFixture(TestRecord::class, 'record1');
|
2012-02-03 00:59:40 +01:00
|
|
|
|
|
|
|
// Anonymous field
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('MyField');
|
|
|
|
$field->setValue(null, $record);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertEquals(array(), $field->getItems()->column('Title'));
|
|
|
|
|
|
|
|
// Field with has_one auto-detected
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('HasOneFile');
|
|
|
|
$field->setValue(null, $record);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertEquals(array('File1'), $field->getItems()->column('Title'));
|
|
|
|
|
|
|
|
// Field with has_many auto-detected
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('HasManyFiles');
|
|
|
|
$field->setValue(null, $record);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertEquals(array('File2', 'File3'), $field->getItems()->column('Title'));
|
|
|
|
|
|
|
|
// Field with many_many auto-detected
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('ManyManyFiles');
|
|
|
|
$field->setValue(null, $record);
|
2012-02-03 00:59:40 +01:00
|
|
|
$this->assertNotContains('File1',$field->getItems()->column('Title'));
|
|
|
|
$this->assertNotContains('File2',$field->getItems()->column('Title'));
|
|
|
|
$this->assertNotContains('File3',$field->getItems()->column('Title'));
|
|
|
|
$this->assertContains('File4',$field->getItems()->column('Title'));
|
|
|
|
$this->assertContains('File5',$field->getItems()->column('Title'));
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testReadonly() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$response = $this->get('UploadFieldTest_Controller');
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
|
|
|
$parser = new CSSContentParser($response->getBody());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$this->assertFalse(
|
2013-05-31 19:59:51 +02:00
|
|
|
(bool)$parser->getBySelector(
|
|
|
|
'#UploadFieldTestForm_Form_ReadonlyField .ss-uploadfield-files .ss-uploadfield-item .ss-ui-button'
|
|
|
|
),
|
2012-09-26 23:34:00 +02:00
|
|
|
'Removes all buttons on items');
|
2013-05-27 05:22:59 +02:00
|
|
|
$this->assertFalse(
|
2013-05-31 19:59:51 +02:00
|
|
|
(bool)$parser->getBySelector('#UploadFieldTestForm_Form_ReadonlyField .ss-uploadfield-dropzone'),
|
2013-05-27 05:22:59 +02:00
|
|
|
'Removes dropzone'
|
|
|
|
);
|
2013-05-31 19:59:51 +02:00
|
|
|
$this->assertFalse(
|
|
|
|
(bool)$parser->getBySelector(
|
|
|
|
'#UploadFieldTestForm_Form_ReadonlyField .ss-uploadfield-addfile'
|
|
|
|
),
|
2013-05-27 05:22:59 +02:00
|
|
|
'Entire "add" area'
|
|
|
|
);
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDisabled() {
|
2012-02-03 00:59:40 +01:00
|
|
|
$response = $this->get('UploadFieldTest_Controller');
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
|
|
|
$parser = new CSSContentParser($response->getBody());
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertFalse(
|
2013-05-31 19:59:51 +02:00
|
|
|
(bool)$parser->getBySelector(
|
|
|
|
'#UploadFieldTestForm_Form_DisabledField .ss-uploadfield-files .ss-uploadfield-item .ss-ui-button'
|
|
|
|
),
|
2012-09-26 23:34:00 +02:00
|
|
|
'Removes all buttons on items');
|
2013-05-31 19:59:51 +02:00
|
|
|
$this->assertFalse((bool)$parser->getBySelector(
|
|
|
|
'#UploadFieldTestForm_Form_DisabledField .ss-uploadfield-dropzone'
|
|
|
|
),
|
2012-09-26 23:34:00 +02:00
|
|
|
'Removes dropzone');
|
|
|
|
$this->assertFalse(
|
2013-05-31 19:59:51 +02:00
|
|
|
(bool)$parser->getBySelector('#UploadFieldTestForm_Form_DisabledField .ss-uploadfield-addfile'),
|
2013-05-27 05:22:59 +02:00
|
|
|
'Entire "add" area'
|
|
|
|
);
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
|
|
|
|
2013-01-09 11:34:02 +01:00
|
|
|
public function testCanUpload() {
|
|
|
|
$response = $this->get('UploadFieldTest_Controller');
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
|
|
|
$parser = new CSSContentParser($response->getBody());
|
2013-05-31 19:59:51 +02:00
|
|
|
$this->assertFalse(
|
|
|
|
(bool)$parser->getBySelector(
|
|
|
|
'#UploadFieldTestForm_Form_CanUploadFalseField_Holder .ss-uploadfield-dropzone'
|
|
|
|
),
|
2013-01-09 11:34:02 +01:00
|
|
|
'Removes dropzone');
|
|
|
|
$this->assertTrue(
|
2014-03-30 08:51:38 +02:00
|
|
|
(bool)$parser->getBySelector(
|
|
|
|
'#UploadFieldTestForm_Form_CanUploadFalseField_Holder .ss-uploadfield-fromfiles'
|
|
|
|
),
|
2013-01-09 11:34:02 +01:00
|
|
|
'Keeps "From files" button'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2013-01-09 11:34:02 +01:00
|
|
|
|
|
|
|
public function testCanUploadWithPermissionCode() {
|
2013-05-27 05:22:59 +02:00
|
|
|
$field = UploadField::create('MyField');
|
2016-01-26 04:56:07 +01:00
|
|
|
Session::clear("loggedInAs");
|
2013-01-09 11:34:02 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$field->setCanUpload(true);
|
2013-01-09 11:34:02 +01:00
|
|
|
$this->assertTrue($field->canUpload());
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$field->setCanUpload(false);
|
2013-01-09 11:34:02 +01:00
|
|
|
$this->assertFalse($field->canUpload());
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->logInWithPermission('ADMIN');
|
2013-01-09 11:34:02 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$field->setCanUpload(false);
|
2013-01-09 11:34:02 +01:00
|
|
|
$this->assertFalse($field->canUpload());
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$field->setCanUpload('ADMIN');
|
2013-01-09 11:34:02 +01:00
|
|
|
$this->assertTrue($field->canUpload());
|
|
|
|
}
|
|
|
|
|
2013-01-11 05:33:06 +01:00
|
|
|
public function testCanAttachExisting() {
|
|
|
|
$response = $this->get('UploadFieldTest_Controller');
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
|
|
|
$parser = new CSSContentParser($response->getBody());
|
|
|
|
$this->assertTrue(
|
2013-05-31 19:59:51 +02:00
|
|
|
(bool)$parser->getBySelector(
|
|
|
|
'#UploadFieldTestForm_Form_CanAttachExistingFalseField_Holder .ss-uploadfield-fromcomputer-fileinput'
|
|
|
|
),
|
2013-01-11 05:33:06 +01:00
|
|
|
'Keeps input file control'
|
|
|
|
);
|
|
|
|
$this->assertFalse(
|
2013-05-31 19:59:51 +02:00
|
|
|
(bool)$parser->getBySelector(
|
|
|
|
'#UploadFieldTestForm_Form_CanAttachExistingFalseField_Holder .ss-uploadfield-fromfiles'
|
|
|
|
),
|
2013-01-11 05:33:06 +01:00
|
|
|
'Removes "From files" button'
|
|
|
|
);
|
2014-10-24 00:31:33 +02:00
|
|
|
|
|
|
|
// Test requests to select files have the correct given permission
|
|
|
|
$response2 = $this->get('UploadFieldTest_Controller/Form/field/CanAttachExistingFalseField/select');
|
|
|
|
$this->assertEquals(403, $response2->getStatusCode());
|
|
|
|
$response3 = $this->get('UploadFieldTest_Controller/Form/field/HasOneFile/select');
|
|
|
|
$this->assertEquals(200, $response3->getStatusCode());
|
2012-06-15 17:44:34 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSelect() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$file4 = $this->objFromFixture(File::class, 'file4');
|
|
|
|
$fileSubfolder = $this->objFromFixture(File::class, 'file-subfolder');
|
2012-02-08 00:58:58 +01:00
|
|
|
|
|
|
|
$response = $this->get('UploadFieldTest_Controller/Form/field/ManyManyFiles/select/');
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
2013-06-05 18:09:33 +02:00
|
|
|
// A bit too much coupling with GridField, but a full template overload would make things too complex
|
|
|
|
$parser = new CSSContentParser($response->getBody());
|
|
|
|
$items = $parser->getBySelector('.ss-gridfield-item');
|
|
|
|
$itemIDs = array_map(create_function('$el', 'return (int)$el["data-id"];'), $items);
|
|
|
|
$this->assertContains($file4->ID, $itemIDs, 'Contains file in assigned folder');
|
|
|
|
$this->assertContains($fileSubfolder->ID, $itemIDs, 'Contains file in subfolder');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSelectWithDisplayFolderName() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$file4 = $this->objFromFixture(File::class, 'file4');
|
|
|
|
$fileSubfolder = $this->objFromFixture(File::class, 'file-subfolder');
|
2013-06-05 18:09:33 +02:00
|
|
|
|
|
|
|
$response = $this->get('UploadFieldTest_Controller/Form/field/HasManyDisplayFolder/select/');
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
|
2012-02-08 00:58:58 +01:00
|
|
|
// A bit too much coupling with GridField, but a full template overload would make things too complex
|
|
|
|
$parser = new CSSContentParser($response->getBody());
|
|
|
|
$items = $parser->getBySelector('.ss-gridfield-item');
|
|
|
|
$itemIDs = array_map(create_function('$el', 'return (int)$el["data-id"];'), $items);
|
|
|
|
$this->assertContains($file4->ID, $itemIDs, 'Contains file in assigned folder');
|
|
|
|
$this->assertNotContains($fileSubfolder->ID, $itemIDs, 'Does not contain file in subfolder');
|
|
|
|
}
|
|
|
|
|
2014-04-16 11:08:25 +02:00
|
|
|
/**
|
|
|
|
* Test that UploadField:overwriteWarning cannot overwrite Upload:replaceFile
|
|
|
|
*/
|
|
|
|
public function testConfigOverwriteWarningCannotRelaceFiles() {
|
|
|
|
Upload::config()->replaceFile = false;
|
|
|
|
UploadField::config()->defaultConfig = array_merge(
|
|
|
|
UploadField::config()->defaultConfig, array('overwriteWarning' => true)
|
|
|
|
);
|
|
|
|
|
|
|
|
$tmpFileName = 'testUploadBasic.txt';
|
|
|
|
$response = $this->mockFileUpload('NoRelationField', $tmpFileName);
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$responseData = Convert::json2array($response->getBody());
|
2016-10-14 03:30:05 +02:00
|
|
|
$uploadedFile = DataObject::get_by_id(File::class, (int) $responseData[0]['id']);
|
2014-04-16 11:08:25 +02:00
|
|
|
$this->assertTrue(is_object($uploadedFile), 'The file object is created');
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertFileExists(TestAssetStore::getLocalPath($uploadedFile));
|
2014-04-16 11:08:25 +02:00
|
|
|
|
|
|
|
$tmpFileName = 'testUploadBasic.txt';
|
|
|
|
$response = $this->mockFileUpload('NoRelationField', $tmpFileName);
|
|
|
|
$this->assertFalse($response->isError());
|
|
|
|
$responseData = Convert::json2array($response->getBody());
|
2016-10-14 03:30:05 +02:00
|
|
|
$uploadedFile2 = DataObject::get_by_id(File::class, (int) $responseData[0]['id']);
|
2014-04-16 11:08:25 +02:00
|
|
|
$this->assertTrue(is_object($uploadedFile2), 'The file object is created');
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertFileExists(TestAssetStore::getLocalPath($uploadedFile2));
|
2014-04-16 11:08:25 +02:00
|
|
|
$this->assertTrue(
|
|
|
|
$uploadedFile->Filename !== $uploadedFile2->Filename,
|
|
|
|
'Filename is not the same'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
|
|
|
$uploadedFile->ID !== $uploadedFile2->ID,
|
|
|
|
'File database record is not the same'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-02-27 00:26:17 +01:00
|
|
|
/**
|
|
|
|
* Tests that UploadField::fileexist works
|
|
|
|
*/
|
|
|
|
public function testFileExists() {
|
|
|
|
// Check that fileexist works on subfolders
|
|
|
|
$nonFile = uniqid().'.txt';
|
|
|
|
$responseEmpty = $this->mockFileExists('NoRelationField', $nonFile);
|
|
|
|
$responseEmptyData = json_decode($responseEmpty->getBody());
|
|
|
|
$this->assertFalse($responseEmpty->isError());
|
|
|
|
$this->assertFalse($responseEmptyData->exists);
|
|
|
|
|
|
|
|
// Check that filexists works on root folder
|
|
|
|
$responseRoot = $this->mockFileExists('RootFolderTest', $nonFile);
|
|
|
|
$responseRootData = json_decode($responseRoot->getBody());
|
|
|
|
$this->assertFalse($responseRoot->isError());
|
|
|
|
$this->assertFalse($responseRootData->exists);
|
|
|
|
|
|
|
|
// Check that uploaded files can be detected in the root
|
|
|
|
$tmpFileName = 'testUploadBasic.txt';
|
|
|
|
$response = $this->mockFileUpload('RootFolderTest', $tmpFileName);
|
|
|
|
$this->assertFalse($response->isError());
|
2016-01-26 04:56:07 +01:00
|
|
|
$this->assertFileExists(ASSETS_PATH . "/UploadFieldTest/.protected/315ae4c3d4/$tmpFileName");
|
2014-02-27 00:26:17 +01:00
|
|
|
$responseExists = $this->mockFileExists('RootFolderTest', $tmpFileName);
|
|
|
|
$responseExistsData = json_decode($responseExists->getBody());
|
|
|
|
$this->assertFalse($responseExists->isError());
|
|
|
|
$this->assertTrue($responseExistsData->exists);
|
|
|
|
|
|
|
|
// Check that uploaded files can be detected
|
|
|
|
$response = $this->mockFileUpload('NoRelationField', $tmpFileName);
|
|
|
|
$this->assertFalse($response->isError());
|
2016-01-26 04:56:07 +01:00
|
|
|
$this->assertFileExists(ASSETS_PATH . "/UploadFieldTest/.protected/UploadedFiles/315ae4c3d4/$tmpFileName");
|
2014-02-27 00:26:17 +01:00
|
|
|
$responseExists = $this->mockFileExists('NoRelationField', $tmpFileName);
|
|
|
|
$responseExistsData = json_decode($responseExists->getBody());
|
|
|
|
$this->assertFalse($responseExists->isError());
|
|
|
|
$this->assertTrue($responseExistsData->exists);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-05-05 04:49:25 +02:00
|
|
|
// Test that files with invalid characters are rewritten safely and both report exists
|
|
|
|
// Check that uploaded files can be detected in the root
|
|
|
|
$tmpFileName = '_test___Upload___Bad.txt';
|
|
|
|
$tmpFileNameExpected = 'test-Upload-Bad.txt';
|
|
|
|
$response = $this->mockFileUpload('NoRelationField', $tmpFileName);
|
|
|
|
$this->assertFalse($response->isError());
|
2016-01-26 04:56:07 +01:00
|
|
|
$this->assertFileExists(ASSETS_PATH . "/UploadFieldTest/.protected/UploadedFiles/315ae4c3d4/$tmpFileNameExpected");
|
2014-05-05 04:49:25 +02:00
|
|
|
// With original file
|
|
|
|
$responseExists = $this->mockFileExists('NoRelationField', $tmpFileName);
|
|
|
|
$responseExistsData = json_decode($responseExists->getBody());
|
|
|
|
$this->assertFalse($responseExists->isError());
|
|
|
|
$this->assertTrue($responseExistsData->exists);
|
|
|
|
// With rewritten file
|
|
|
|
$responseExists = $this->mockFileExists('NoRelationField', $tmpFileNameExpected);
|
|
|
|
$responseExistsData = json_decode($responseExists->getBody());
|
|
|
|
$this->assertFalse($responseExists->isError());
|
|
|
|
$this->assertTrue($responseExistsData->exists);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-05-05 04:49:25 +02:00
|
|
|
// Test that attempts to navigate outside of the directory return false
|
|
|
|
$responseExists = $this->mockFileExists('NoRelationField', "../../../../var/private/$tmpFileName");
|
|
|
|
$this->assertTrue($responseExists->isError());
|
|
|
|
$this->assertContains('File is not a valid upload', $responseExists->getBody());
|
2014-02-27 00:26:17 +01:00
|
|
|
}
|
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
protected function getMockForm() {
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @skipUpgrade */
|
2012-02-03 00:59:40 +01:00
|
|
|
return new Form(new Controller(), 'Form', new FieldList(), new FieldList());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-14 03:30:05 +02:00
|
|
|
* @param string $tmpFileName
|
|
|
|
* @return array Emulating an entry in the $_FILES superglobal
|
2012-02-03 00:59:40 +01:00
|
|
|
*/
|
|
|
|
protected function getUploadFile($tmpFileName = 'UploadFieldTest-testUpload.txt') {
|
|
|
|
$tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
|
|
|
|
$tmpFileContent = '';
|
|
|
|
for($i=0; $i<10000; $i++) $tmpFileContent .= '0';
|
|
|
|
file_put_contents($tmpFilePath, $tmpFileContent);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
// emulates the $_FILES array
|
|
|
|
return array(
|
2013-05-27 05:22:59 +02:00
|
|
|
'name' => array('Uploads' => array($tmpFileName)),
|
|
|
|
'type' => array('Uploads' => array('text/plaintext')),
|
|
|
|
'size' => array('Uploads' => array(filesize($tmpFilePath))),
|
|
|
|
'tmp_name' => array('Uploads' => array($tmpFilePath)),
|
|
|
|
'error' => array('Uploads' => array(UPLOAD_ERR_OK)),
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Simulates a form post to the test controller with the specified file IDs
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param string $fileField Name of field to assign ids to
|
|
|
|
* @param array $ids list of file IDs
|
2016-10-14 03:30:05 +02:00
|
|
|
* @return array Array with key 'errors'
|
2013-05-27 05:22:59 +02:00
|
|
|
*/
|
|
|
|
protected function mockUploadFileIDs($fileField, $ids) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
// collate file ids
|
|
|
|
$files = array();
|
|
|
|
foreach($ids as $id) {
|
|
|
|
$files[$id] = $id;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
$data = array(
|
|
|
|
'action_submit' => 1
|
|
|
|
);
|
|
|
|
if($files) {
|
|
|
|
// Normal post requests can't submit empty array values for fields
|
|
|
|
$data[$fileField] = array('Files' => $files);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$form = new UploadFieldTest\UploadFieldTestForm();
|
2013-05-27 05:22:59 +02:00
|
|
|
$form->loadDataFrom($data, true);
|
2016-01-26 04:56:07 +01:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
if($form->validate()) {
|
|
|
|
$record = $form->getRecord();
|
|
|
|
$form->saveInto($record);
|
|
|
|
$record->write();
|
|
|
|
return array('errors' => null);
|
|
|
|
} else {
|
|
|
|
return array('errors' => $form->getValidator()->getErrors());
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Simulates a file upload
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param string $fileField Name of the field to mock upload for
|
|
|
|
* @param array $tmpFileName Name of temporary file to upload
|
2016-09-09 08:43:05 +02:00
|
|
|
* @return HTTPResponse form response
|
2013-05-27 05:22:59 +02:00
|
|
|
*/
|
|
|
|
protected function mockFileUpload($fileField, $tmpFileName) {
|
|
|
|
$upload = $this->getUploadFile($tmpFileName);
|
|
|
|
$_FILES = array($fileField => $upload);
|
|
|
|
return $this->post(
|
|
|
|
"UploadFieldTest_Controller/Form/field/{$fileField}/upload",
|
|
|
|
array($fileField => $upload)
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-02-27 00:26:17 +01:00
|
|
|
protected function mockFileExists($fileField, $fileName) {
|
|
|
|
return $this->get(
|
|
|
|
"UploadFieldTest_Controller/Form/field/{$fileField}/fileexists?filename=".urlencode($fileName)
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-12-16 03:43:24 +01:00
|
|
|
/**
|
|
|
|
* Gets the edit form for the given file
|
|
|
|
*
|
|
|
|
* @param string $fileField Name of the field
|
|
|
|
* @param integer $fileID ID of the file to delete
|
2016-09-09 08:43:05 +02:00
|
|
|
* @return HTTPResponse form response
|
2014-12-16 03:43:24 +01:00
|
|
|
*/
|
|
|
|
protected function mockFileEditForm($fileField, $fileID) {
|
|
|
|
return $this->get(
|
|
|
|
"UploadFieldTest_Controller/Form/field/{$fileField}/item/{$fileID}/edit"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mocks edit submissions to a file
|
|
|
|
*
|
|
|
|
* @param string $fileField Name of the field
|
|
|
|
* @param integer $fileID ID of the file to delete
|
|
|
|
* @param array $fields Fields to update
|
2016-09-09 08:43:05 +02:00
|
|
|
* @return HTTPResponse form response
|
2014-12-16 03:43:24 +01:00
|
|
|
*/
|
|
|
|
protected function mockFileEdit($fileField, $fileID, $fields = array()) {
|
|
|
|
return $this->post(
|
|
|
|
"UploadFieldTest_Controller/Form/field/{$fileField}/item/{$fileID}/EditForm",
|
|
|
|
$fields
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-05-27 05:22:59 +02:00
|
|
|
/**
|
|
|
|
* Simulates a physical file deletion
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-27 05:22:59 +02:00
|
|
|
* @param string $fileField Name of the field
|
|
|
|
* @param integer $fileID ID of the file to delete
|
2016-09-09 08:43:05 +02:00
|
|
|
* @return HTTPResponse form response
|
2013-05-27 05:22:59 +02:00
|
|
|
*/
|
|
|
|
protected function mockFileDelete($fileField, $fileID) {
|
|
|
|
return $this->post(
|
2014-12-16 03:43:24 +01:00
|
|
|
"UploadFieldTest_Controller/Form/field/{$fileField}/item/{$fileID}/delete",
|
2013-05-27 05:22:59 +02:00
|
|
|
array()
|
2012-02-03 00:59:40 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
public function get($url, $session = null, $headers = null, $cookies = null) {
|
|
|
|
// Inject stage=Stage into the URL, to force working on draft
|
|
|
|
$url = $this->addStageToUrl($url);
|
|
|
|
return parent::get($url, $session, $headers, $cookies);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function post($url, $data, $headers = null, $session = null, $body = null, $cookies = null) {
|
|
|
|
// Inject stage=Stage into the URL, to force working on draft
|
|
|
|
$url = $this->addStageToUrl($url);
|
|
|
|
return parent::post($url, $data, $headers, $session, $body, $cookies);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds ?stage=Stage to url
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function addStageToUrl($url) {
|
|
|
|
if(stripos($url, 'stage=Stage') === false) {
|
|
|
|
if(stripos($url, '?') === false) {
|
|
|
|
$url .= '?stage=Stage';
|
|
|
|
} else {
|
|
|
|
$url .= '&stage=Stage';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|