1.20.x file.test FileUploadTransliterationTest::testTransliteration()

File

modules/simpletest/tests/file.test, line 1014
This provides SimpleTests for the core file handling functionality. These include FileValidateTest and FileSaveTest.

Class

FileUploadTransliterationTest
Tests transliterating files on upload and subsequent bulk updating.

Code

function testTransliteration() {
  // Make a particularly troublesome file name, starting with an umlaut and
  // containing upper-case variant characters.
  $original_filename = 'üФайл Ä fàü.txt';
  $file = $this->createFile($original_filename, 'Sample text');
  $config = config('system.core');

  $upload_options = array(
    'file_test_replace' => FILE_EXISTS_RENAME,
    'extensions' => 'txt',
    'is_image_file' => FALSE,
    'files[file_test_upload]' => backdrop_realpath($file->uri),
  );
  $this->backdropPost('file-test/upload', $upload_options, t('Submit'));
  $first_fid = db_query("SELECT MAX(fid) FROM {file_managed}")->fetchField();
  $first_file = file_load($first_fid);

  // Check that characters were transliterated and the file exists.
  debug($first_file);
  $this->assertIdentical($first_file->filename, $original_filename, 'File name not transliterated or modified to lowercase.');
  $this->assertIdentical($first_file->uri, 'temporary://ufail_a_fau.txt', 'File name has been transliterated upon upload.');
  $this->assertTrue(file_exists($first_file->uri), 'Transliterated file exists at expected path.');

  // Delete the file to prevent conflicts.
  $first_file->delete();

  // Change transliteration settings and upload again.
  $config->set('file_transliterate_lowercase', 1);
  $config->set('file_transliterate_uploads_display_name', 1);
  $config->save();

  $this->backdropPost('file-test/upload', $upload_options, t('Submit'));
  $second_fid = db_query("SELECT MAX(fid) FROM {file_managed}")->fetchField();
  $second_file = file_load($second_fid);

  debug($second_file);
  $this->assertIdentical($second_file->filename, 'ufail_a_fau.txt', 'File display name transliterated and converted to lowercase.');
  $this->assertIdentical($second_file->uri, 'temporary://ufail_a_fau.txt', 'File name has been transliterated and lowercased.');
  $this->assertTrue(file_exists($second_file->uri), 'Transliterated file exists at expected path.');

  // Delete the file to prevent conflicts.
  $second_file->delete();

  // Disable all transliteration.
  $config->set('file_transliterate_uploads', 0);
  $config->save();

  $this->backdropPost('file-test/upload', $upload_options, t('Submit'));
  $non_transliterated_fid = db_query("SELECT MAX(fid) FROM {file_managed}")->fetchField();
  $non_transliterated_file = file_load($non_transliterated_fid);

  $this->assertIdentical($non_transliterated_file->filename, $original_filename, 'File display name not transliterated or modified to lowercase.');
  $this->assertIdentical($non_transliterated_file->uri, 'temporary://' . $original_filename, 'File name has not been modified in any way.');
  $this->assertTrue(file_exists($non_transliterated_file->uri), 'Non-transliterated file exists at expected path.');

  // Now that we have a non-transliterated file. Use the bulk conversion
  // form to transliterate all of the existing files.
  $this->backdropGet('admin/config/media/file-system/transliteration');
  // Inaccessible initially because we've disabled transliteration.
  $this->assertText('File transliteration is not yet enabled.');

  // Enable again, but disable lowercase and display name.
  $config->set('file_transliterate_uploads', 1);
  $config->set('file_transliterate_lowercase', 0);
  $config->set('file_transliterate_uploads_display_name', 0);
  $config->save();

  // Now available, one table row should be available.
  $this->backdropGet('admin/config/media/file-system/transliteration');
  $table_rows = $this->xpath('//*[@id="system-transliteration-retroactive"]//tbody//tr');
  $this->assertEqual(count($table_rows), 2, 'Two files available for transliteration.');
  $this->backdropPost(NULL, array(), t('Transliterate'));
  $this->assertText('2 file names have been successfully transliterated.');

  entity_get_controller('file')->resetCache();
  $now_transliterated_file = file_load($non_transliterated_fid);
  $this->assertIdentical($now_transliterated_file->filename, $non_transliterated_file->filename, 'File display name not transliterated or modified to lowercase.');
  $this->assertIdentical($now_transliterated_file->uri, 'temporary://uFail_A_fau.txt', 'File name transliterated after bulk updating.');

  // Revisit the page. No files should be available for transliteration.
  $this->backdropGet('admin/config/media/file-system/transliteration');
  $this->assertText('Transliteration is not required.');

  // Lowercase existing file names and bulk update again.
  $config->set('file_transliterate_lowercase', 1);
  $config->set('file_transliterate_uploads_display_name', 1);
  $config->save();
  $this->backdropGet('admin/config/media/file-system/transliteration');
  $table_rows = $this->xpath('//*[@id="system-transliteration-retroactive"]//tbody//tr');
  // Transliterated but not lowercase yet.
  $this->assertLink('uFail_A_fau.txt');
  $this->assertEqual(count($table_rows), 2, 'Two files available for transliteration.');
  $this->backdropPost(NULL, array(), t('Transliterate'));
  $this->assertText('2 file names have been successfully transliterated.');

  // Reload and re-check the transliterated file is now lowercase.
  entity_get_controller('file')->resetCache();
  $now_transliterated_file = file_load($non_transliterated_fid);
  $this->assertIdentical($now_transliterated_file->filename, $non_transliterated_file->filename, 'File display name not transliterated or modified to lowercase.');
  $this->assertIdentical($now_transliterated_file->uri, 'temporary://ufail_a_fau.txt', 'File name transliterated and lower case after bulk updating.');

  // The file should not show up anymore as available for retroactive
  // transliteration. This ensures that case sensitive comparison has been
  // successful.
  $this->backdropGet('admin/config/media/file-system/transliteration');
  $this->assertText('Transliteration is not required.');
  $this->assertNoLink('ufail_a_fau.txt');
}