Code Duplication    Length = 27-27 lines in 2 locations

tests/forms/uploadfield/UploadFieldTest.php 2 locations

@@ 78-104 (lines=27) @@
75
	/**
76
	 * Test that an object can be uploaded against an object with a has_one relation
77
	 */
78
	public function testUploadHasOneRelation() {
79
		// Unset existing has_one relation before re-uploading
80
		$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
81
		$record->HasOneFileID = null;
82
		$record->write();
83
84
		// Firstly, ensure the file can be uploaded
85
		$tmpFileName = 'testUploadHasOneRelation.txt';
86
		$response = $this->mockFileUpload('HasOneFile', $tmpFileName);
87
		$this->assertFalse($response->isError());
88
		$uploadedFile = DataObject::get_one('File', array(
89
			'"File"."Name"' => $tmpFileName
90
		));
91
		$this->assertTrue(is_object($uploadedFile), 'The file object is created');
92
		$this->assertFileExists(AssetStoreTest_SpyStore::getLocalPath($uploadedFile));
93
94
		// Secondly, ensure that simply uploading an object does not save the file against the relation
95
		$record = DataObject::get_by_id($record->class, $record->ID, false);
96
		$this->assertFalse($record->HasOneFile()->exists());
97
98
		// Thirdly, test submitting the form with the encoded data
99
		$response = $this->mockUploadFileIDs('HasOneFile', array($uploadedFile->ID));
100
		$this->assertEmpty($response['errors']);
101
		$record = DataObject::get_by_id($record->class, $record->ID, false);
102
		$this->assertTrue($record->HasOneFile()->exists());
103
		$this->assertEquals($record->HasOneFile()->Name, $tmpFileName);
104
	}
105
106
	/**
107
	 * Tests that has_one relations work with subclasses of File
@@ 109-135 (lines=27) @@
106
	/**
107
	 * Tests that has_one relations work with subclasses of File
108
	 */
109
	public function testUploadHasOneRelationWithExtendedFile() {
110
		// Unset existing has_one relation before re-uploading
111
		$record = $this->objFromFixture('UploadFieldTest_Record', 'record1');
112
		$record->HasOneExtendedFileID = null;
113
		$record->write();
114
115
		// Test that the file can be safely uploaded
116
		$tmpFileName = 'testUploadHasOneRelationWithExtendedFile.txt';
117
		$response = $this->mockFileUpload('HasOneExtendedFile', $tmpFileName);
118
		$this->assertFalse($response->isError());
119
		$uploadedFile = DataObject::get_one('UploadFieldTest_ExtendedFile', array(
120
			'"File"."Name"' => $tmpFileName
121
		));
122
		$this->assertTrue(is_object($uploadedFile), 'The file object is created');
123
		$this->assertFileExists(AssetStoreTest_SpyStore::getLocalPath($uploadedFile));
124
125
		// Test that the record isn't written to automatically
126
		$record = DataObject::get_by_id($record->class, $record->ID, false);
127
		$this->assertFalse($record->HasOneExtendedFile()->exists());
128
129
		// Test that saving the form writes the record
130
		$response = $this->mockUploadFileIDs('HasOneExtendedFile', array($uploadedFile->ID));
131
		$this->assertEmpty($response['errors']);
132
		$record = DataObject::get_by_id($record->class, $record->ID, false);
133
		$this->assertTrue($record->HasOneExtendedFile()->exists());
134
		$this->assertEquals($record->HasOneExtendedFile()->Name, $tmpFileName);
135
	}
136
137
138
	/**