public function FileFieldPathsGeneralTestCase::testSlashes in File (Field) Paths 7
Test File (Field) Paths slashes cleanup functionality.
File
- tests/
filefield_paths.general.test, line 201 - Tests for the File (Field) Paths module.
Class
- FileFieldPathsGeneralTestCase
- Class FileFieldPathsGeneralTestCase
Code
public function testSlashes() {
$langcode = LANGUAGE_NONE;
// Create a File field with 'node/[node:title]' as the File path and
// '[node:title].[file:ffp-extension-original]' as the File name.
$field_name = drupal_strtolower($this
->randomName());
$instance_settings['filefield_paths']['file_path']['value'] = 'node/[node:title]';
$instance_settings['filefield_paths']['file_name']['value'] = '[node:title].[file:ffp-extension-original]';
$this
->createFileField($field_name, $this->content_type, array(), $instance_settings);
// Create a node with a test file.
$test_file = $this
->getTestFile('text');
$title = "{$this->randomName()}/{$this->randomName()}";
$edit['title'] = $title;
$edit["body[{$langcode}][0][value]"] = '';
$edit["files[{$field_name}_{$langcode}_0]"] = drupal_realpath($test_file->uri);
$this
->drupalPost("node/add/{$this->content_type}", $edit, t('Save'));
// Get created Node ID.
$matches = array();
preg_match('/node\\/([0-9]+)/', $this
->getUrl(), $matches);
$nid = $matches[1];
// Ensure slashes are present in file path and name.
$node = node_load($nid);
$this
->assertEqual("public://node/{$title}/{$title}.txt", $node->{$field_name}[$langcode][0]['uri']);
// Remove slashes.
$edit = array(
'instance[settings][filefield_paths][file_path][options][slashes]' => TRUE,
'instance[settings][filefield_paths][file_name][options][slashes]' => TRUE,
'instance[settings][filefield_paths][retroactive_update]' => TRUE,
);
$this
->drupalPost("admin/structure/types/manage/{$this->content_type}/fields/{$field_name}", $edit, t('Save settings'));
// Ensure slashes are not present in file path and name.
$node = node_load($nid, NULL, TRUE);
$title = str_replace('/', '', $title);
$this
->assertEqual("public://node/{$title}/{$title}.txt", $node->{$field_name}[$langcode][0]['uri']);
}