<?php
namespace Drupal\file\Tests;
class FileTokenReplaceTest extends FileFieldTestBase {
public static function getInfo() {
return array(
'name' => 'File token replacement',
'description' => 'Generates text using placeholders for dummy content to check file token replacement.',
'group' => 'File',
);
}
function testFileTokenReplacement() {
$language_interface = language(LANGUAGE_TYPE_INTERFACE);
$url_options = array(
'absolute' => TRUE,
'language' => $language_interface,
);
$type_name = 'article';
$field_name = 'field_' . strtolower($this
->randomName());
$this
->createFileField($field_name, $type_name);
$field = field_info_field($field_name);
$instance = field_info_instance('node', $field_name, $type_name);
$test_file = $this
->getTestFile('text');
$filename = drupal_dirname($test_file->uri) . '/текстовый файл.txt';
$test_file = file_copy($test_file, $filename);
$nid = $this
->uploadNodeFile($test_file, $field_name, $type_name);
$node = node_load($nid, TRUE);
$file = file_load($node->{$field_name}[LANGUAGE_NOT_SPECIFIED][0]['fid']);
$tests = array();
$tests['[file:fid]'] = $file->fid;
$tests['[file:name]'] = check_plain($file->filename);
$tests['[file:path]'] = check_plain($file->uri);
$tests['[file:mime]'] = check_plain($file->filemime);
$tests['[file:size]'] = format_size($file->filesize);
$tests['[file:url]'] = check_plain(file_create_url($file->uri));
$tests['[file:timestamp]'] = format_date($file->timestamp, 'medium', '', NULL, $language_interface->langcode);
$tests['[file:timestamp:short]'] = format_date($file->timestamp, 'short', '', NULL, $language_interface->langcode);
$tests['[file:owner]'] = check_plain(user_format_name($this->admin_user));
$tests['[file:owner:uid]'] = $file->uid;
$this
->assertFalse(in_array(0, array_map('strlen', $tests)), t('No empty tokens generated.'));
foreach ($tests as $input => $expected) {
$output = token_replace($input, array(
'file' => $file,
), array(
'langcode' => $language_interface->langcode,
));
$this
->assertEqual($output, $expected, t('Sanitized file token %token replaced.', array(
'%token' => $input,
)));
}
$tests['[file:name]'] = $file->filename;
$tests['[file:path]'] = $file->uri;
$tests['[file:mime]'] = $file->filemime;
$tests['[file:size]'] = format_size($file->filesize);
foreach ($tests as $input => $expected) {
$output = token_replace($input, array(
'file' => $file,
), array(
'langcode' => $language_interface->langcode,
'sanitize' => FALSE,
));
$this
->assertEqual($output, $expected, t('Unsanitized file token %token replaced.', array(
'%token' => $input,
)));
}
}
}