|
@@ 38-66 (lines=29) @@
|
| 35 |
|
/** |
| 36 |
|
* Test that an object can be uploaded against an object with a has_one relation |
| 37 |
|
*/ |
| 38 |
|
public function testUploadHasOneRelation() { |
| 39 |
|
$this->loginWithPermission('ADMIN'); |
| 40 |
|
|
| 41 |
|
// Unset existing has_one relation before re-uploading |
| 42 |
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1'); |
| 43 |
|
$record->HasOneFileID = null; |
| 44 |
|
$record->write(); |
| 45 |
|
|
| 46 |
|
// Firstly, ensure the file can be uploaded |
| 47 |
|
$tmpFileName = 'testUploadHasOneRelation.txt'; |
| 48 |
|
$response = $this->mockFileUpload('HasOneFile', $tmpFileName); |
| 49 |
|
$this->assertFalse($response->isError()); |
| 50 |
|
$this->assertFileExists(ASSETS_PATH . "/UploadFieldTest/$tmpFileName"); |
| 51 |
|
$uploadedFile = DataObject::get_one('File', array( |
| 52 |
|
'"File"."Name"' => $tmpFileName |
| 53 |
|
)); |
| 54 |
|
$this->assertTrue(is_object($uploadedFile), 'The file object is created'); |
| 55 |
|
|
| 56 |
|
// Secondly, ensure that simply uploading an object does not save the file against the relation |
| 57 |
|
$record = DataObject::get_by_id($record->class, $record->ID, false); |
| 58 |
|
$this->assertFalse($record->HasOneFile()->exists()); |
| 59 |
|
|
| 60 |
|
// Thirdly, test submitting the form with the encoded data |
| 61 |
|
$response = $this->mockUploadFileIDs('HasOneFile', array($uploadedFile->ID)); |
| 62 |
|
$this->assertEmpty($response['errors']); |
| 63 |
|
$record = DataObject::get_by_id($record->class, $record->ID, false); |
| 64 |
|
$this->assertTrue($record->HasOneFile()->exists()); |
| 65 |
|
$this->assertEquals($record->HasOneFile()->Name, $tmpFileName); |
| 66 |
|
} |
| 67 |
|
|
| 68 |
|
/** |
| 69 |
|
* Tests that has_one relations work with subclasses of File |
|
@@ 71-99 (lines=29) @@
|
| 68 |
|
/** |
| 69 |
|
* Tests that has_one relations work with subclasses of File |
| 70 |
|
*/ |
| 71 |
|
public function testUploadHasOneRelationWithExtendedFile() { |
| 72 |
|
$this->loginWithPermission('ADMIN'); |
| 73 |
|
|
| 74 |
|
// Unset existing has_one relation before re-uploading |
| 75 |
|
$record = $this->objFromFixture('UploadFieldTest_Record', 'record1'); |
| 76 |
|
$record->HasOneExtendedFileID = null; |
| 77 |
|
$record->write(); |
| 78 |
|
|
| 79 |
|
// Test that the file can be safely uploaded |
| 80 |
|
$tmpFileName = 'testUploadHasOneRelationWithExtendedFile.txt'; |
| 81 |
|
$response = $this->mockFileUpload('HasOneExtendedFile', $tmpFileName); |
| 82 |
|
$this->assertFalse($response->isError()); |
| 83 |
|
$this->assertFileExists(ASSETS_PATH . "/UploadFieldTest/$tmpFileName"); |
| 84 |
|
$uploadedFile = DataObject::get_one('UploadFieldTest_ExtendedFile', array( |
| 85 |
|
'"File"."Name"' => $tmpFileName |
| 86 |
|
)); |
| 87 |
|
$this->assertTrue(is_object($uploadedFile), 'The file object is created'); |
| 88 |
|
|
| 89 |
|
// Test that the record isn't written to automatically |
| 90 |
|
$record = DataObject::get_by_id($record->class, $record->ID, false); |
| 91 |
|
$this->assertFalse($record->HasOneExtendedFile()->exists()); |
| 92 |
|
|
| 93 |
|
// Test that saving the form writes the record |
| 94 |
|
$response = $this->mockUploadFileIDs('HasOneExtendedFile', array($uploadedFile->ID)); |
| 95 |
|
$this->assertEmpty($response['errors']); |
| 96 |
|
$record = DataObject::get_by_id($record->class, $record->ID, false); |
| 97 |
|
$this->assertTrue($record->HasOneExtendedFile()->exists()); |
| 98 |
|
$this->assertEquals($record->HasOneExtendedFile()->Name, $tmpFileName); |
| 99 |
|
} |
| 100 |
|
|
| 101 |
|
|
| 102 |
|
/** |