Révision e33d3026
Ajouté par Julien Enselme il y a presque 10 ans
drupal7/modules/file/tests/file.test | ||
---|---|---|
220 | 220 |
} |
221 | 221 |
} |
222 | 222 |
|
223 |
/** |
|
224 |
* Tests adding a file to a non-node entity. |
|
225 |
*/ |
|
226 |
class FileTaxonomyTermTestCase extends DrupalWebTestCase { |
|
227 |
protected $admin_user; |
|
228 |
|
|
229 |
public static function getInfo() { |
|
230 |
return array( |
|
231 |
'name' => 'Taxonomy term file test', |
|
232 |
'description' => 'Tests adding a file to a non-node entity.', |
|
233 |
'group' => 'File', |
|
234 |
); |
|
235 |
} |
|
236 |
|
|
237 |
public function setUp() { |
|
238 |
$modules[] = 'file'; |
|
239 |
$modules[] = 'taxonomy'; |
|
240 |
parent::setUp($modules); |
|
241 |
$this->admin_user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer taxonomy')); |
|
242 |
$this->drupalLogin($this->admin_user); |
|
243 |
} |
|
244 |
|
|
245 |
/** |
|
246 |
* Creates a file field and attaches it to the "Tags" taxonomy vocabulary. |
|
247 |
* |
|
248 |
* @param $name |
|
249 |
* The field name of the file field to create. |
|
250 |
* @param $uri_scheme |
|
251 |
* The URI scheme to use for the file field (for example, "private" to |
|
252 |
* create a field that stores private files or "public" to create a field |
|
253 |
* that stores public files). |
|
254 |
*/ |
|
255 |
protected function createAttachFileField($name, $uri_scheme) { |
|
256 |
$field = array( |
|
257 |
'field_name' => $name, |
|
258 |
'type' => 'file', |
|
259 |
'settings' => array( |
|
260 |
'uri_scheme' => $uri_scheme, |
|
261 |
), |
|
262 |
'cardinality' => 1, |
|
263 |
); |
|
264 |
field_create_field($field); |
|
265 |
// Attach an instance of it. |
|
266 |
$instance = array( |
|
267 |
'field_name' => $name, |
|
268 |
'label' => 'File', |
|
269 |
'entity_type' => 'taxonomy_term', |
|
270 |
'bundle' => 'tags', |
|
271 |
'required' => FALSE, |
|
272 |
'settings' => array(), |
|
273 |
'widget' => array( |
|
274 |
'type' => 'file_generic', |
|
275 |
'settings' => array(), |
|
276 |
), |
|
277 |
); |
|
278 |
field_create_instance($instance); |
|
279 |
} |
|
280 |
|
|
281 |
/** |
|
282 |
* Tests that a public file can be attached to a taxonomy term. |
|
283 |
* |
|
284 |
* This is a regression test for https://www.drupal.org/node/2305017. |
|
285 |
*/ |
|
286 |
public function testTermFilePublic() { |
|
287 |
$this->_testTermFile('public'); |
|
288 |
} |
|
289 |
|
|
290 |
/** |
|
291 |
* Tests that a private file can be attached to a taxonomy term. |
|
292 |
* |
|
293 |
* This is a regression test for https://www.drupal.org/node/2305017. |
|
294 |
*/ |
|
295 |
public function testTermFilePrivate() { |
|
296 |
$this->_testTermFile('private'); |
|
297 |
} |
|
298 |
|
|
299 |
/** |
|
300 |
* Runs tests for attaching a file field to a taxonomy term. |
|
301 |
* |
|
302 |
* @param $uri_scheme |
|
303 |
* The URI scheme to use for the file field, either "public" or "private". |
|
304 |
*/ |
|
305 |
protected function _testTermFile($uri_scheme) { |
|
306 |
$field_name = strtolower($this->randomName()); |
|
307 |
$this->createAttachFileField($field_name, $uri_scheme); |
|
308 |
// Get a file to upload. |
|
309 |
$file = current($this->drupalGetTestFiles('text')); |
|
310 |
// Add a filesize property to files as would be read by file_load(). |
|
311 |
$file->filesize = filesize($file->uri); |
|
312 |
$langcode = LANGUAGE_NONE; |
|
313 |
$edit = array( |
|
314 |
"name" => $this->randomName(), |
|
315 |
); |
|
316 |
// Attach a file to the term. |
|
317 |
$edit['files[' . $field_name . '_' . $langcode . '_0]'] = drupal_realpath($file->uri); |
|
318 |
$this->drupalPost("admin/structure/taxonomy/tags/add", $edit, t('Save')); |
|
319 |
// Find the term ID we just created. |
|
320 |
$tid = db_query_range('SELECT tid FROM {taxonomy_term_data} ORDER BY tid DESC', 0, 1)->fetchField(); |
|
321 |
$terms = entity_load('taxonomy_term', array($tid)); |
|
322 |
$term = $terms[$tid]; |
|
323 |
$fid = $term->{$field_name}[LANGUAGE_NONE][0]['fid']; |
|
324 |
// Check that the uploaded file is present on the edit form. |
|
325 |
$this->drupalGet("taxonomy/term/$tid/edit"); |
|
326 |
$file_input_name = $field_name . '[' . LANGUAGE_NONE . '][0][fid]'; |
|
327 |
$this->assertFieldByXpath('//input[@type="hidden" and @name="' . $file_input_name . '"]', $fid, 'File is attached on edit form.'); |
|
328 |
// Edit the term and change name without changing the file. |
|
329 |
$edit = array( |
|
330 |
"name" => $this->randomName(), |
|
331 |
); |
|
332 |
$this->drupalPost("taxonomy/term/$tid/edit", $edit, t('Save')); |
|
333 |
// Check that the uploaded file is still present on the edit form. |
|
334 |
$this->drupalGet("taxonomy/term/$tid/edit"); |
|
335 |
$file_input_name = $field_name . '[' . LANGUAGE_NONE . '][0][fid]'; |
|
336 |
$this->assertFieldByXpath('//input[@type="hidden" and @name="' . $file_input_name . '"]', $fid, 'File is attached on edit form.'); |
|
337 |
// Load term while resetting the cache. |
|
338 |
$terms = entity_load('taxonomy_term', array($tid), array(), TRUE); |
|
339 |
$term = $terms[$tid]; |
|
340 |
$this->assertTrue(!empty($term->{$field_name}[LANGUAGE_NONE]), 'Term has attached files.'); |
|
341 |
$this->assertEqual($term->{$field_name}[LANGUAGE_NONE][0]['fid'], $fid, 'Same File ID is attached to the term.'); |
|
342 |
} |
|
343 |
} |
|
344 |
|
|
223 | 345 |
/** |
224 | 346 |
* Tests the 'managed_file' element type. |
225 | 347 |
* |
Formats disponibles : Unified diff
Update drupal to 7.30