file.test 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026
  1. <?php
  2. /**
  3. * @file
  4. * Tests for file.module.
  5. */
  6. /**
  7. * Provides methods specifically for testing File module's field handling.
  8. */
  9. class FileFieldTestCase extends DrupalWebTestCase {
  10. protected $admin_user;
  11. function setUp() {
  12. // Since this is a base class for many test cases, support the same
  13. // flexibility that DrupalWebTestCase::setUp() has for the modules to be
  14. // passed in as either an array or a variable number of string arguments.
  15. $modules = func_get_args();
  16. if (isset($modules[0]) && is_array($modules[0])) {
  17. $modules = $modules[0];
  18. }
  19. $modules[] = 'file';
  20. $modules[] = 'file_module_test';
  21. parent::setUp($modules);
  22. $this->admin_user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer users', 'administer permissions', 'administer content types', 'administer nodes', 'bypass node access', 'administer fields'));
  23. $this->drupalLogin($this->admin_user);
  24. }
  25. /**
  26. * Retrieves a sample file of the specified type.
  27. */
  28. function getTestFile($type_name, $size = NULL) {
  29. // Get a file to upload.
  30. $file = current($this->drupalGetTestFiles($type_name, $size));
  31. // Add a filesize property to files as would be read by file_load().
  32. $file->filesize = filesize($file->uri);
  33. return $file;
  34. }
  35. /**
  36. * Retrieves the fid of the last inserted file.
  37. */
  38. function getLastFileId() {
  39. return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField();
  40. }
  41. /**
  42. * Creates a new file field.
  43. *
  44. * @param $name
  45. * The name of the new field (all lowercase), exclude the "field_" prefix.
  46. * @param $type_name
  47. * The node type that this field will be added to.
  48. * @param $field_settings
  49. * A list of field settings that will be added to the defaults.
  50. * @param $instance_settings
  51. * A list of instance settings that will be added to the instance defaults.
  52. * @param $widget_settings
  53. * A list of widget settings that will be added to the widget defaults.
  54. */
  55. function createFileField($name, $type_name, $field_settings = array(), $instance_settings = array(), $widget_settings = array()) {
  56. $field = array(
  57. 'field_name' => $name,
  58. 'type' => 'file',
  59. 'settings' => array(),
  60. 'cardinality' => !empty($field_settings['cardinality']) ? $field_settings['cardinality'] : 1,
  61. );
  62. $field['settings'] = array_merge($field['settings'], $field_settings);
  63. field_create_field($field);
  64. $this->attachFileField($name, 'node', $type_name, $instance_settings, $widget_settings);
  65. }
  66. /**
  67. * Attaches a file field to an entity.
  68. *
  69. * @param $name
  70. * The name of the new field (all lowercase), exclude the "field_" prefix.
  71. * @param $entity_type
  72. * The entity type this field will be added to.
  73. * @param $bundle
  74. * The bundle this field will be added to.
  75. * @param $field_settings
  76. * A list of field settings that will be added to the defaults.
  77. * @param $instance_settings
  78. * A list of instance settings that will be added to the instance defaults.
  79. * @param $widget_settings
  80. * A list of widget settings that will be added to the widget defaults.
  81. */
  82. function attachFileField($name, $entity_type, $bundle, $instance_settings = array(), $widget_settings = array()) {
  83. $instance = array(
  84. 'field_name' => $name,
  85. 'label' => $name,
  86. 'entity_type' => $entity_type,
  87. 'bundle' => $bundle,
  88. 'required' => !empty($instance_settings['required']),
  89. 'settings' => array(),
  90. 'widget' => array(
  91. 'type' => 'file_generic',
  92. 'settings' => array(),
  93. ),
  94. );
  95. $instance['settings'] = array_merge($instance['settings'], $instance_settings);
  96. $instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings);
  97. field_create_instance($instance);
  98. }
  99. /**
  100. * Updates an existing file field with new settings.
  101. */
  102. function updateFileField($name, $type_name, $instance_settings = array(), $widget_settings = array()) {
  103. $instance = field_info_instance('node', $name, $type_name);
  104. $instance['settings'] = array_merge($instance['settings'], $instance_settings);
  105. $instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings);
  106. field_update_instance($instance);
  107. }
  108. /**
  109. * Uploads a file to a node.
  110. */
  111. function uploadNodeFile($file, $field_name, $nid_or_type, $new_revision = TRUE, $extras = array()) {
  112. $langcode = LANGUAGE_NONE;
  113. $edit = array(
  114. "title" => $this->randomName(),
  115. 'revision' => (string) (int) $new_revision,
  116. );
  117. if (is_numeric($nid_or_type)) {
  118. $nid = $nid_or_type;
  119. }
  120. else {
  121. // Add a new node.
  122. $extras['type'] = $nid_or_type;
  123. $node = $this->drupalCreateNode($extras);
  124. $nid = $node->nid;
  125. // Save at least one revision to better simulate a real site.
  126. $this->drupalCreateNode(get_object_vars($node));
  127. $node = node_load($nid, NULL, TRUE);
  128. $this->assertNotEqual($nid, $node->vid, 'Node revision exists.');
  129. }
  130. // Attach a file to the node.
  131. $edit['files[' . $field_name . '_' . $langcode . '_0]'] = drupal_realpath($file->uri);
  132. $this->drupalPost("node/$nid/edit", $edit, t('Save'));
  133. return $nid;
  134. }
  135. /**
  136. * Removes a file from a node.
  137. *
  138. * Note that if replacing a file, it must first be removed then added again.
  139. */
  140. function removeNodeFile($nid, $new_revision = TRUE) {
  141. $edit = array(
  142. 'revision' => (string) (int) $new_revision,
  143. );
  144. $this->drupalPost('node/' . $nid . '/edit', array(), t('Remove'));
  145. $this->drupalPost(NULL, $edit, t('Save'));
  146. }
  147. /**
  148. * Replaces a file within a node.
  149. */
  150. function replaceNodeFile($file, $field_name, $nid, $new_revision = TRUE) {
  151. $edit = array(
  152. 'files[' . $field_name . '_' . LANGUAGE_NONE . '_0]' => drupal_realpath($file->uri),
  153. 'revision' => (string) (int) $new_revision,
  154. );
  155. $this->drupalPost('node/' . $nid . '/edit', array(), t('Remove'));
  156. $this->drupalPost(NULL, $edit, t('Save'));
  157. }
  158. /**
  159. * Asserts that a file exists physically on disk.
  160. */
  161. function assertFileExists($file, $message = NULL) {
  162. $message = isset($message) ? $message : format_string('File %file exists on the disk.', array('%file' => $file->uri));
  163. $this->assertTrue(is_file($file->uri), $message);
  164. }
  165. /**
  166. * Asserts that a file exists in the database.
  167. */
  168. function assertFileEntryExists($file, $message = NULL) {
  169. entity_get_controller('file')->resetCache();
  170. $db_file = file_load($file->fid);
  171. $message = isset($message) ? $message : format_string('File %file exists in database at the correct path.', array('%file' => $file->uri));
  172. $this->assertEqual($db_file->uri, $file->uri, $message);
  173. }
  174. /**
  175. * Asserts that a file does not exist on disk.
  176. */
  177. function assertFileNotExists($file, $message = NULL) {
  178. $message = isset($message) ? $message : format_string('File %file exists on the disk.', array('%file' => $file->uri));
  179. $this->assertFalse(is_file($file->uri), $message);
  180. }
  181. /**
  182. * Asserts that a file does not exist in the database.
  183. */
  184. function assertFileEntryNotExists($file, $message) {
  185. entity_get_controller('file')->resetCache();
  186. $message = isset($message) ? $message : format_string('File %file exists in database at the correct path.', array('%file' => $file->uri));
  187. $this->assertFalse(file_load($file->fid), $message);
  188. }
  189. /**
  190. * Asserts that a file's status is set to permanent in the database.
  191. */
  192. function assertFileIsPermanent($file, $message = NULL) {
  193. $message = isset($message) ? $message : format_string('File %file is permanent.', array('%file' => $file->uri));
  194. $this->assertTrue($file->status == FILE_STATUS_PERMANENT, $message);
  195. }
  196. /**
  197. * Creates a temporary file, for a specific user.
  198. *
  199. * @param string $data
  200. * A string containing the contents of the file.
  201. * @param int $uid
  202. * The user ID of the file owner.
  203. *
  204. * @return object
  205. * A file object, or FALSE on error.
  206. */
  207. function createTemporaryFile($data, $uid = NULL) {
  208. $file = file_save_data($data, NULL, NULL);
  209. if ($file) {
  210. $file->uid = isset($uid) ? $uid : $this->admin_user->uid;
  211. // Change the file status to be temporary.
  212. $file->status = NULL;
  213. return file_save($file);
  214. }
  215. return $file;
  216. }
  217. }
  218. /**
  219. * Tests adding a file to a non-node entity.
  220. */
  221. class FileTaxonomyTermTestCase extends DrupalWebTestCase {
  222. protected $admin_user;
  223. public static function getInfo() {
  224. return array(
  225. 'name' => 'Taxonomy term file test',
  226. 'description' => 'Tests adding a file to a non-node entity.',
  227. 'group' => 'File',
  228. );
  229. }
  230. public function setUp() {
  231. $modules[] = 'file';
  232. $modules[] = 'taxonomy';
  233. parent::setUp($modules);
  234. $this->admin_user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer taxonomy'));
  235. $this->drupalLogin($this->admin_user);
  236. }
  237. /**
  238. * Creates a file field and attaches it to the "Tags" taxonomy vocabulary.
  239. *
  240. * @param $name
  241. * The field name of the file field to create.
  242. * @param $uri_scheme
  243. * The URI scheme to use for the file field (for example, "private" to
  244. * create a field that stores private files or "public" to create a field
  245. * that stores public files).
  246. */
  247. protected function createAttachFileField($name, $uri_scheme) {
  248. $field = array(
  249. 'field_name' => $name,
  250. 'type' => 'file',
  251. 'settings' => array(
  252. 'uri_scheme' => $uri_scheme,
  253. ),
  254. 'cardinality' => 1,
  255. );
  256. field_create_field($field);
  257. // Attach an instance of it.
  258. $instance = array(
  259. 'field_name' => $name,
  260. 'label' => 'File',
  261. 'entity_type' => 'taxonomy_term',
  262. 'bundle' => 'tags',
  263. 'required' => FALSE,
  264. 'settings' => array(),
  265. 'widget' => array(
  266. 'type' => 'file_generic',
  267. 'settings' => array(),
  268. ),
  269. );
  270. field_create_instance($instance);
  271. }
  272. /**
  273. * Tests that a public file can be attached to a taxonomy term.
  274. *
  275. * This is a regression test for https://www.drupal.org/node/2305017.
  276. */
  277. public function testTermFilePublic() {
  278. $this->_testTermFile('public');
  279. }
  280. /**
  281. * Tests that a private file can be attached to a taxonomy term.
  282. *
  283. * This is a regression test for https://www.drupal.org/node/2305017.
  284. */
  285. public function testTermFilePrivate() {
  286. $this->_testTermFile('private');
  287. }
  288. /**
  289. * Runs tests for attaching a file field to a taxonomy term.
  290. *
  291. * @param $uri_scheme
  292. * The URI scheme to use for the file field, either "public" or "private".
  293. */
  294. protected function _testTermFile($uri_scheme) {
  295. $field_name = strtolower($this->randomName());
  296. $this->createAttachFileField($field_name, $uri_scheme);
  297. // Get a file to upload.
  298. $file = current($this->drupalGetTestFiles('text'));
  299. // Add a filesize property to files as would be read by file_load().
  300. $file->filesize = filesize($file->uri);
  301. $langcode = LANGUAGE_NONE;
  302. $edit = array(
  303. "name" => $this->randomName(),
  304. );
  305. // Attach a file to the term.
  306. $edit['files[' . $field_name . '_' . $langcode . '_0]'] = drupal_realpath($file->uri);
  307. $this->drupalPost("admin/structure/taxonomy/tags/add", $edit, t('Save'));
  308. // Find the term ID we just created.
  309. $tid = db_query_range('SELECT tid FROM {taxonomy_term_data} ORDER BY tid DESC', 0, 1)->fetchField();
  310. $terms = entity_load('taxonomy_term', array($tid));
  311. $term = $terms[$tid];
  312. $fid = $term->{$field_name}[LANGUAGE_NONE][0]['fid'];
  313. // Check that the uploaded file is present on the edit form.
  314. $this->drupalGet("taxonomy/term/$tid/edit");
  315. $file_input_name = $field_name . '[' . LANGUAGE_NONE . '][0][fid]';
  316. $this->assertFieldByXpath('//input[@type="hidden" and @name="' . $file_input_name . '"]', $fid, 'File is attached on edit form.');
  317. // Edit the term and change name without changing the file.
  318. $edit = array(
  319. "name" => $this->randomName(),
  320. );
  321. $this->drupalPost("taxonomy/term/$tid/edit", $edit, t('Save'));
  322. // Check that the uploaded file is still present on the edit form.
  323. $this->drupalGet("taxonomy/term/$tid/edit");
  324. $file_input_name = $field_name . '[' . LANGUAGE_NONE . '][0][fid]';
  325. $this->assertFieldByXpath('//input[@type="hidden" and @name="' . $file_input_name . '"]', $fid, 'File is attached on edit form.');
  326. // Load term while resetting the cache.
  327. $terms = entity_load('taxonomy_term', array($tid), array(), TRUE);
  328. $term = $terms[$tid];
  329. $this->assertTrue(!empty($term->{$field_name}[LANGUAGE_NONE]), 'Term has attached files.');
  330. $this->assertEqual($term->{$field_name}[LANGUAGE_NONE][0]['fid'], $fid, 'Same File ID is attached to the term.');
  331. }
  332. }
  333. /**
  334. * Tests the 'managed_file' element type.
  335. *
  336. * @todo Create a FileTestCase base class and move FileFieldTestCase methods
  337. * that aren't related to fields into it.
  338. */
  339. class FileManagedFileElementTestCase extends FileFieldTestCase {
  340. protected $originalDisplayErrorsValue;
  341. public static function getInfo() {
  342. return array(
  343. 'name' => 'Managed file element test',
  344. 'description' => 'Tests the managed_file element type.',
  345. 'group' => 'File',
  346. );
  347. }
  348. public function setUp() {
  349. parent::setUp();
  350. // Disable the displaying of errors, so that the AJAX responses are not
  351. // contaminated with error messages about exceeding the maximum POST size.
  352. $this->originalDisplayErrorsValue = ini_set('display_errors', '0');
  353. }
  354. public function tearDown() {
  355. ini_set('display_errors', $this->originalDisplayErrorsValue);
  356. parent::tearDown();
  357. }
  358. /**
  359. * Tests the managed_file element type.
  360. */
  361. function testManagedFile() {
  362. // Check that $element['#size'] is passed to the child upload element.
  363. $this->drupalGet('file/test');
  364. $this->assertFieldByXpath('//input[@name="files[nested_file]" and @size="13"]', NULL, 'The custom #size attribute is passed to the child upload element.');
  365. // Check that the file fields don't contain duplicate HTML IDs.
  366. $this->assertNoDuplicateIds('There are no duplicate IDs');
  367. // Perform the tests with all permutations of $form['#tree'] and
  368. // $element['#extended'].
  369. foreach (array(0, 1) as $tree) {
  370. foreach (array(0, 1) as $extended) {
  371. $test_file = $this->getTestFile('text');
  372. $path = 'file/test/' . $tree . '/' . $extended;
  373. $input_base_name = $tree ? 'nested_file' : 'file';
  374. // Submit without a file.
  375. $this->drupalPost($path, array(), t('Save'));
  376. $this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), 'Submitted without a file.');
  377. // Submit with a file, but with an invalid form token. Ensure the file
  378. // was not saved.
  379. $last_fid_prior = $this->getLastFileId();
  380. $edit = array(
  381. 'files[' . $input_base_name . ']' => drupal_realpath($test_file->uri),
  382. 'form_token' => 'invalid token',
  383. );
  384. $this->drupalPost($path, $edit, t('Save'));
  385. $this->assertText('The form has become outdated.');
  386. $last_fid = $this->getLastFileId();
  387. $this->assertEqual($last_fid_prior, $last_fid, 'File was not saved when uploaded with an invalid form token.');
  388. // Submit a new file, without using the Upload button.
  389. $last_fid_prior = $this->getLastFileId();
  390. $edit = array('files[' . $input_base_name . ']' => drupal_realpath($test_file->uri));
  391. $this->drupalPost($path, $edit, t('Save'));
  392. $last_fid = $this->getLastFileId();
  393. $this->assertTrue($last_fid > $last_fid_prior, 'New file got saved.');
  394. $this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), 'Submit handler has correct file info.');
  395. // Submit no new input, but with a default file.
  396. $this->drupalPost($path . '/' . $last_fid, array(), t('Save'));
  397. $this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), 'Empty submission did not change an existing file.');
  398. // Now, test the Upload and Remove buttons, with and without Ajax.
  399. foreach (array(FALSE, TRUE) as $ajax) {
  400. // Upload, then Submit.
  401. $last_fid_prior = $this->getLastFileId();
  402. $this->drupalGet($path);
  403. $edit = array('files[' . $input_base_name . ']' => drupal_realpath($test_file->uri));
  404. if ($ajax) {
  405. $this->drupalPostAJAX(NULL, $edit, $input_base_name . '_upload_button');
  406. }
  407. else {
  408. $this->drupalPost(NULL, $edit, t('Upload'));
  409. }
  410. $last_fid = $this->getLastFileId();
  411. $this->assertTrue($last_fid > $last_fid_prior, 'New file got uploaded.');
  412. $this->drupalPost(NULL, array(), t('Save'));
  413. $this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), 'Submit handler has correct file info.');
  414. // Remove, then Submit.
  415. $this->drupalGet($path . '/' . $last_fid);
  416. if ($ajax) {
  417. $this->drupalPostAJAX(NULL, array(), $input_base_name . '_remove_button');
  418. }
  419. else {
  420. $this->drupalPost(NULL, array(), t('Remove'));
  421. }
  422. $this->drupalPost(NULL, array(), t('Save'));
  423. $this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), 'Submission after file removal was successful.');
  424. // Upload, then Remove, then Submit.
  425. $this->drupalGet($path);
  426. $edit = array('files[' . $input_base_name . ']' => drupal_realpath($test_file->uri));
  427. if ($ajax) {
  428. $this->drupalPostAJAX(NULL, $edit, $input_base_name . '_upload_button');
  429. $this->drupalPostAJAX(NULL, array(), $input_base_name . '_remove_button');
  430. }
  431. else {
  432. $this->drupalPost(NULL, $edit, t('Upload'));
  433. $this->drupalPost(NULL, array(), t('Remove'));
  434. }
  435. $this->drupalPost(NULL, array(), t('Save'));
  436. $this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), 'Submission after file upload and removal was successful.');
  437. }
  438. }
  439. }
  440. }
  441. /**
  442. * Tests uploading a file that exceeds the maximum file size.
  443. */
  444. function testManagedFileExceedMaximumFileSize() {
  445. $path = 'file/test/0/0';
  446. $this->drupalGet($path);
  447. // Create a test file that exceeds the maximum POST size with 1 kilobyte.
  448. $post_max_size = $this->_postMaxSizeToInteger(ini_get('post_max_size'));
  449. $filename = 'text-exceeded';
  450. simpletest_generate_file($filename, ceil(($post_max_size + 1024) / 1024), 1024, 'text');
  451. $uri = 'public://' . $filename . '.txt';
  452. $input_base_name = 'file';
  453. $edit = array('files[' . $input_base_name . ']' => drupal_realpath($uri));
  454. $this->drupalPostAJAX(NULL, $edit, $input_base_name . '_upload_button');
  455. $this->assertFieldByXpath('//input[@type="submit"]', t('Upload'), 'After uploading a file that exceeds the maximum file size, the "Upload" button is displayed.');
  456. $this->drupalPost($path, array(), t('Save'));
  457. $this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), 'Submitted without a file.');
  458. }
  459. /**
  460. * Converts php.ini post_max_size value to integer.
  461. *
  462. * @param $string
  463. * The value from php.ini.
  464. *
  465. * @return int
  466. * Converted value.
  467. */
  468. protected function _postMaxSizeToInteger($string) {
  469. sscanf($string, '%u%c', $number, $suffix);
  470. if (isset($suffix)) {
  471. $number = $number * pow(1024, strpos(' KMG', strtoupper($suffix)));
  472. }
  473. return $number;
  474. }
  475. }
  476. /**
  477. * Tests file field widget.
  478. */
  479. class FileFieldWidgetTestCase extends FileFieldTestCase {
  480. public static function getInfo() {
  481. return array(
  482. 'name' => 'File field widget test',
  483. 'description' => 'Tests the file field widget, single and multi-valued, with and without AJAX, with public and private files.',
  484. 'group' => 'File',
  485. );
  486. }
  487. /**
  488. * Tests upload and remove buttons for a single-valued File field.
  489. */
  490. function testSingleValuedWidget() {
  491. // Use 'page' instead of 'article', so that the 'article' image field does
  492. // not conflict with this test. If in the future the 'page' type gets its
  493. // own default file or image field, this test can be made more robust by
  494. // using a custom node type.
  495. $type_name = 'page';
  496. $field_name = strtolower($this->randomName());
  497. $this->createFileField($field_name, $type_name);
  498. $field = field_info_field($field_name);
  499. $instance = field_info_instance('node', $field_name, $type_name);
  500. $test_file = $this->getTestFile('text');
  501. foreach (array('nojs', 'js') as $type) {
  502. // Create a new node with the uploaded file and ensure it got uploaded
  503. // successfully.
  504. // @todo This only tests a 'nojs' submission, because drupalPostAJAX()
  505. // does not yet support file uploads.
  506. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
  507. $node = node_load($nid, NULL, TRUE);
  508. $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  509. $this->assertFileExists($node_file, 'New file saved to disk on node creation.');
  510. // Test that running field_attach_update() leaves the file intact.
  511. $field = new stdClass();
  512. $field->type = $type_name;
  513. $field->nid = $nid;
  514. field_attach_update('node', $field);
  515. $node = node_load($nid);
  516. $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  517. $this->assertFileExists($node_file, 'New file still saved to disk on field update.');
  518. // Ensure the file can be downloaded.
  519. $this->drupalGet(file_create_url($node_file->uri));
  520. $this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
  521. // Ensure the edit page has a remove button instead of an upload button.
  522. $this->drupalGet("node/$nid/edit");
  523. $this->assertNoFieldByXPath('//input[@type="submit"]', t('Upload'), 'Node with file does not display the "Upload" button.');
  524. $this->assertFieldByXpath('//input[@type="submit"]', t('Remove'), 'Node with file displays the "Remove" button.');
  525. // "Click" the remove button (emulating either a nojs or js submission).
  526. switch ($type) {
  527. case 'nojs':
  528. $this->drupalPost(NULL, array(), t('Remove'));
  529. break;
  530. case 'js':
  531. $button = $this->xpath('//input[@type="submit" and @value="' . t('Remove') . '"]');
  532. $this->drupalPostAJAX(NULL, array(), array((string) $button[0]['name'] => (string) $button[0]['value']));
  533. break;
  534. }
  535. // Ensure the page now has an upload button instead of a remove button.
  536. $this->assertNoFieldByXPath('//input[@type="submit"]', t('Remove'), 'After clicking the "Remove" button, it is no longer displayed.');
  537. $this->assertFieldByXpath('//input[@type="submit"]', t('Upload'), 'After clicking the "Remove" button, the "Upload" button is displayed.');
  538. // Save the node and ensure it does not have the file.
  539. $this->drupalPost(NULL, array(), t('Save'));
  540. $node = node_load($nid, NULL, TRUE);
  541. $this->assertTrue(empty($node->{$field_name}[LANGUAGE_NONE][0]['fid']), 'File was successfully removed from the node.');
  542. }
  543. }
  544. /**
  545. * Tests exploiting the temporary file removal of another user using fid.
  546. */
  547. function testTemporaryFileRemovalExploit() {
  548. // Create a victim user.
  549. $victim_user = $this->drupalCreateUser();
  550. // Create an attacker user.
  551. $attacker_user = $this->drupalCreateUser(array(
  552. 'access content',
  553. 'create page content',
  554. 'edit any page content',
  555. ));
  556. // Log in as the attacker user.
  557. $this->drupalLogin($attacker_user);
  558. // Perform tests using the newly created users.
  559. $this->doTestTemporaryFileRemovalExploit($victim_user->uid, $attacker_user->uid);
  560. }
  561. /**
  562. * Tests exploiting the temporary file removal for anonymous users using fid.
  563. */
  564. public function testTemporaryFileRemovalExploitAnonymous() {
  565. // Set up an anonymous victim user.
  566. $victim_uid = 0;
  567. // Set up an anonymous attacker user.
  568. $attacker_uid = 0;
  569. // Set up permissions for anonymous attacker user.
  570. user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
  571. 'access content' => TRUE,
  572. 'create page content' => TRUE,
  573. 'edit any page content' => TRUE,
  574. ));
  575. // In order to simulate being the anonymous attacker user, we need to log
  576. // out here since setUp() has logged in the admin.
  577. $this->drupalLogout();
  578. // Perform tests using the newly set up users.
  579. $this->doTestTemporaryFileRemovalExploit($victim_uid, $attacker_uid);
  580. }
  581. /**
  582. * Tests validation with the Upload button.
  583. */
  584. function testWidgetValidation() {
  585. $type_name = 'article';
  586. $field_name = strtolower($this->randomName());
  587. $this->createFileField($field_name, $type_name);
  588. $this->updateFileField($field_name, $type_name, array('file_extensions' => 'txt'));
  589. foreach (array('nojs', 'js') as $type) {
  590. // Create node and prepare files for upload.
  591. $node = $this->drupalCreateNode(array('type' => 'article'));
  592. $nid = $node->nid;
  593. $this->drupalGet("node/$nid/edit");
  594. $test_file_text = $this->getTestFile('text');
  595. $test_file_image = $this->getTestFile('image');
  596. $field = field_info_field($field_name);
  597. $name = 'files[' . $field_name . '_' . LANGUAGE_NONE . '_0]';
  598. // Upload file with incorrect extension, check for validation error.
  599. $edit[$name] = drupal_realpath($test_file_image->uri);
  600. switch ($type) {
  601. case 'nojs':
  602. $this->drupalPost(NULL, $edit, t('Upload'));
  603. break;
  604. case 'js':
  605. $button = $this->xpath('//input[@type="submit" and @value="' . t('Upload') . '"]');
  606. $this->drupalPostAJAX(NULL, $edit, array((string) $button[0]['name'] => (string) $button[0]['value']));
  607. break;
  608. }
  609. $error_message = t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => 'txt'));
  610. $this->assertRaw($error_message, t('Validation error when file with wrong extension uploaded (JSMode=%type).', array('%type' => $type)));
  611. // Upload file with correct extension, check that error message is removed.
  612. $edit[$name] = drupal_realpath($test_file_text->uri);
  613. switch ($type) {
  614. case 'nojs':
  615. $this->drupalPost(NULL, $edit, t('Upload'));
  616. break;
  617. case 'js':
  618. $button = $this->xpath('//input[@type="submit" and @value="' . t('Upload') . '"]');
  619. $this->drupalPostAJAX(NULL, $edit, array((string) $button[0]['name'] => (string) $button[0]['value']));
  620. break;
  621. }
  622. $this->assertNoRaw($error_message, t('Validation error removed when file with correct extension uploaded (JSMode=%type).', array('%type' => $type)));
  623. }
  624. }
  625. /**
  626. * Helper for testing exploiting the temporary file removal using fid.
  627. *
  628. * @param int $victim_uid
  629. * The victim user ID.
  630. * @param int $attacker_uid
  631. * The attacker user ID.
  632. */
  633. protected function doTestTemporaryFileRemovalExploit($victim_uid, $attacker_uid) {
  634. // Use 'page' instead of 'article', so that the 'article' image field does
  635. // not conflict with this test. If in the future the 'page' type gets its
  636. // own default file or image field, this test can be made more robust by
  637. // using a custom node type.
  638. $type_name = 'page';
  639. $field_name = 'test_file_field';
  640. $this->createFileField($field_name, $type_name);
  641. $test_file = $this->getTestFile('text');
  642. foreach (array('nojs', 'js') as $type) {
  643. // Create a temporary file owned by the anonymous victim user. This will be
  644. // as if they had uploaded the file, but not saved the node they were
  645. // editing or creating.
  646. $victim_tmp_file = $this->createTemporaryFile('some text', $victim_uid);
  647. $victim_tmp_file = file_load($victim_tmp_file->fid);
  648. $this->assertTrue($victim_tmp_file->status != FILE_STATUS_PERMANENT, 'New file saved to disk is temporary.');
  649. $this->assertFalse(empty($victim_tmp_file->fid), 'New file has a fid');
  650. $this->assertEqual($victim_uid, $victim_tmp_file->uid, 'New file belongs to the victim user');
  651. // Have attacker create a new node with a different uploaded file and
  652. // ensure it got uploaded successfully.
  653. // @todo Can we test AJAX? See https://www.drupal.org/node/2538260
  654. $edit = array(
  655. 'title' => $type . '-title',
  656. );
  657. // Attach a file to a node.
  658. $langcode = LANGUAGE_NONE;
  659. $edit['files[' . $field_name . '_' . $langcode . '_0]'] = drupal_realpath($test_file->uri);
  660. $this->drupalPost("node/add/$type_name", $edit, 'Save');
  661. $node = $this->drupalGetNodeByTitle($edit['title']);
  662. $node_file = file_load($node->{$field_name}[$langcode][0]['fid']);
  663. $this->assertFileExists($node_file, 'New file saved to disk on node creation.');
  664. $this->assertEqual($attacker_uid, $node_file->uid, 'New file belongs to the attacker.');
  665. // Ensure the file can be downloaded.
  666. $this->drupalGet(file_create_url($node_file->uri));
  667. $this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
  668. // "Click" the remove button (emulating either a nojs or js submission).
  669. // In this POST request, the attacker "guesses" the fid of the victim's
  670. // temporary file and uses that to remove this file.
  671. $this->drupalGet('node/' . $node->nid . '/edit');
  672. switch ($type) {
  673. case 'nojs':
  674. $this->drupalPost(NULL, array("{$field_name}[$langcode][0][fid]" => (string) $victim_tmp_file->fid), 'Remove');
  675. break;
  676. case 'js':
  677. $button = $this->xpath('//input[@type="submit" and @value="Remove"]');
  678. $this->drupalPostAJAX(NULL, array("{$field_name}[$langcode][0][fid]" => (string) $victim_tmp_file->fid), array((string) $button[0]['name'] => (string) $button[0]['value']));
  679. break;
  680. }
  681. // The victim's temporary file should not be removed by the attacker's
  682. // POST request.
  683. $this->assertFileExists($victim_tmp_file);
  684. }
  685. }
  686. /**
  687. * Tests upload and remove buttons for multiple multi-valued File fields.
  688. */
  689. function testMultiValuedWidget() {
  690. // Use 'page' instead of 'article', so that the 'article' image field does
  691. // not conflict with this test. If in the future the 'page' type gets its
  692. // own default file or image field, this test can be made more robust by
  693. // using a custom node type.
  694. $type_name = 'page';
  695. $field_name = strtolower($this->randomName());
  696. $field_name2 = strtolower($this->randomName());
  697. $this->createFileField($field_name, $type_name, array('cardinality' => 3));
  698. $this->createFileField($field_name2, $type_name, array('cardinality' => 3));
  699. $field = field_info_field($field_name);
  700. $instance = field_info_instance('node', $field_name, $type_name);
  701. $field2 = field_info_field($field_name2);
  702. $instance2 = field_info_instance('node', $field_name2, $type_name);
  703. $test_file = $this->getTestFile('text');
  704. foreach (array('nojs', 'js') as $type) {
  705. // Visit the node creation form, and upload 3 files for each field. Since
  706. // the field has cardinality of 3, ensure the "Upload" button is displayed
  707. // until after the 3rd file, and after that, isn't displayed. Because
  708. // SimpleTest triggers the last button with a given name, so upload to the
  709. // second field first.
  710. // @todo This is only testing a non-Ajax upload, because drupalPostAJAX()
  711. // does not yet emulate jQuery's file upload.
  712. //
  713. $this->drupalGet("node/add/$type_name");
  714. foreach (array($field_name2, $field_name) as $each_field_name) {
  715. for ($delta = 0; $delta < 3; $delta++) {
  716. $edit = array('files[' . $each_field_name . '_' . LANGUAGE_NONE . '_' . $delta . ']' => drupal_realpath($test_file->uri));
  717. // drupalPost() takes a $submit parameter that is the value of the
  718. // button whose click we want to emulate. Since we have multiple
  719. // buttons with the value "Upload", and want to control which one we
  720. // use, we change the value of the other ones to something else.
  721. // Since non-clicked buttons aren't included in the submitted POST
  722. // data, and since drupalPost() will result in $this being updated
  723. // with a newly rebuilt form, this doesn't cause problems. Note that
  724. // $buttons is an array of SimpleXMLElement objects passed by
  725. // reference so modifications to each button will affect
  726. // \DrupalWebTestCase::handleForm().
  727. $buttons = $this->xpath('//input[@type="submit" and @value="Upload"]');
  728. $button_name = $each_field_name . '_' . LANGUAGE_NONE . '_' . $delta . '_upload_button';
  729. foreach ($buttons as $button) {
  730. if ($button['name'] != $button_name) {
  731. $button['value'] = 'DUMMY';
  732. }
  733. }
  734. // If the Upload button doesn't exist, drupalPost() will automatically
  735. // fail with an assertion message.
  736. $this->drupalPost(NULL, $edit, t('Upload'));
  737. }
  738. }
  739. $this->assertNoFieldByXpath('//input[@type="submit"]', t('Upload'), 'After uploading 3 files for each field, the "Upload" button is no longer displayed.');
  740. $num_expected_remove_buttons = 6;
  741. foreach (array($field_name, $field_name2) as $current_field_name) {
  742. // How many uploaded files for the current field are remaining.
  743. $remaining = 3;
  744. // Test clicking each "Remove" button. For extra robustness, test them out
  745. // of sequential order. They are 0-indexed, and get renumbered after each
  746. // iteration, so array(1, 1, 0) means:
  747. // - First remove the 2nd file.
  748. // - Then remove what is then the 2nd file (was originally the 3rd file).
  749. // - Then remove the first file.
  750. foreach (array(1,1,0) as $delta) {
  751. // Ensure we have the expected number of Remove buttons, and that they
  752. // are numbered sequentially.
  753. $buttons = $this->xpath('//input[@type="submit" and @value="Remove"]');
  754. $this->assertTrue(is_array($buttons) && count($buttons) === $num_expected_remove_buttons, format_string('There are %n "Remove" buttons displayed (JSMode=%type).', array('%n' => $num_expected_remove_buttons, '%type' => $type)));
  755. foreach ($buttons as $i => $button) {
  756. $key = $i >= $remaining ? $i - $remaining : $i;
  757. $check_field_name = $field_name2;
  758. if ($current_field_name == $field_name && $i < $remaining) {
  759. $check_field_name = $field_name;
  760. }
  761. $this->assertIdentical((string) $button['name'], $check_field_name . '_' . LANGUAGE_NONE . '_' . $key. '_remove_button');
  762. }
  763. // "Click" the remove button (emulating either a nojs or js submission).
  764. $button_name = $current_field_name . '_' . LANGUAGE_NONE . '_' . $delta . '_remove_button';
  765. switch ($type) {
  766. case 'nojs':
  767. // Same workaround for multiple buttons with the value "Remove" as
  768. // we did for the "Upload" buttons above.
  769. foreach ($buttons as $button) {
  770. if ($button['name'] != $button_name) {
  771. $button['value'] = 'DUMMY';
  772. }
  773. }
  774. $this->drupalPost(NULL, array(), t('Remove'));
  775. break;
  776. case 'js':
  777. // drupalPostAJAX() lets us target the button precisely, so we don't
  778. // require the workaround used above for nojs.
  779. $this->drupalPostAJAX(NULL, array(), array($button_name => t('Remove')));
  780. break;
  781. }
  782. $num_expected_remove_buttons--;
  783. $remaining--;
  784. // Ensure an "Upload" button for the current field is displayed with the
  785. // correct name.
  786. $upload_button_name = $current_field_name . '_' . LANGUAGE_NONE . '_' . $remaining . '_upload_button';
  787. $buttons = $this->xpath('//input[@type="submit" and @value="Upload" and @name=:name]', array(':name' => $upload_button_name));
  788. $this->assertTrue(is_array($buttons) && count($buttons) == 1, format_string('The upload button is displayed with the correct name (JSMode=%type).', array('%type' => $type)));
  789. // Ensure only at most one button per field is displayed.
  790. $buttons = $this->xpath('//input[@type="submit" and @value="Upload"]');
  791. $expected = $current_field_name == $field_name ? 1 : 2;
  792. $this->assertTrue(is_array($buttons) && count($buttons) == $expected, format_string('After removing a file, only one "Upload" button for each possible field is displayed (JSMode=%type).', array('%type' => $type)));
  793. }
  794. }
  795. // Ensure the page now has no Remove buttons.
  796. $this->assertNoFieldByXPath('//input[@type="submit"]', t('Remove'), format_string('After removing all files, there is no "Remove" button displayed (JSMode=%type).', array('%type' => $type)));
  797. // Save the node and ensure it does not have any files.
  798. $this->drupalPost(NULL, array('title' => $this->randomName()), t('Save'));
  799. $matches = array();
  800. preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
  801. $nid = $matches[1];
  802. $node = node_load($nid, NULL, TRUE);
  803. $this->assertTrue(empty($node->{$field_name}[LANGUAGE_NONE][0]['fid']), 'Node was successfully saved without any files.');
  804. }
  805. }
  806. /**
  807. * Tests a file field with a "Private files" upload destination setting.
  808. */
  809. function testPrivateFileSetting() {
  810. // Use 'page' instead of 'article', so that the 'article' image field does
  811. // not conflict with this test. If in the future the 'page' type gets its
  812. // own default file or image field, this test can be made more robust by
  813. // using a custom node type.
  814. $type_name = 'page';
  815. $field_name = strtolower($this->randomName());
  816. $this->createFileField($field_name, $type_name);
  817. $field = field_info_field($field_name);
  818. $instance = field_info_instance('node', $field_name, $type_name);
  819. $test_file = $this->getTestFile('text');
  820. // Change the field setting to make its files private, and upload a file.
  821. $edit = array('field[settings][uri_scheme]' => 'private');
  822. $this->drupalPost("admin/structure/types/manage/$type_name/fields/$field_name", $edit, t('Save settings'));
  823. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
  824. $node = node_load($nid, NULL, TRUE);
  825. $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  826. $this->assertFileExists($node_file, 'New file saved to disk on node creation.');
  827. // Ensure the private file is available to the user who uploaded it.
  828. $this->drupalGet(file_create_url($node_file->uri));
  829. $this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
  830. // Ensure we can't change 'uri_scheme' field settings while there are some
  831. // entities with uploaded files.
  832. $this->drupalGet("admin/structure/types/manage/$type_name/fields/$field_name");
  833. $this->assertFieldByXpath('//input[@id="edit-field-settings-uri-scheme-public" and @disabled="disabled"]', 'public', 'Upload destination setting disabled.');
  834. // Delete node and confirm that setting could be changed.
  835. node_delete($nid);
  836. $this->drupalGet("admin/structure/types/manage/$type_name/fields/$field_name");
  837. $this->assertFieldByXpath('//input[@id="edit-field-settings-uri-scheme-public" and not(@disabled)]', 'public', 'Upload destination setting enabled.');
  838. }
  839. /**
  840. * Tests that download restrictions on private files work on comments.
  841. */
  842. function testPrivateFileComment() {
  843. $user = $this->drupalCreateUser(array('access comments'));
  844. // Remove access comments permission from anon user.
  845. $edit = array(
  846. DRUPAL_ANONYMOUS_RID . '[access comments]' => FALSE,
  847. );
  848. $this->drupalPost('admin/people/permissions', $edit, t('Save permissions'));
  849. // Create a new field.
  850. $edit = array(
  851. 'fields[_add_new_field][label]' => $label = $this->randomName(),
  852. 'fields[_add_new_field][field_name]' => $name = strtolower($this->randomName()),
  853. 'fields[_add_new_field][type]' => 'file',
  854. 'fields[_add_new_field][widget_type]' => 'file_generic',
  855. );
  856. $this->drupalPost('admin/structure/types/manage/article/comment/fields', $edit, t('Save'));
  857. $edit = array('field[settings][uri_scheme]' => 'private');
  858. $this->drupalPost(NULL, $edit, t('Save field settings'));
  859. $this->drupalPost(NULL, array(), t('Save settings'));
  860. // Create node.
  861. $text_file = $this->getTestFile('text');
  862. $edit = array(
  863. 'title' => $this->randomName(),
  864. );
  865. $this->drupalPost('node/add/article', $edit, t('Save'));
  866. $node = $this->drupalGetNodeByTitle($edit['title']);
  867. // Add a comment with a file.
  868. $text_file = $this->getTestFile('text');
  869. $edit = array(
  870. 'files[field_' . $name . '_' . LANGUAGE_NONE . '_' . 0 . ']' => drupal_realpath($text_file->uri),
  871. 'comment_body[' . LANGUAGE_NONE . '][0][value]' => $comment_body = $this->randomName(),
  872. );
  873. $this->drupalPost(NULL, $edit, t('Save'));
  874. // Get the comment ID.
  875. preg_match('/comment-([0-9]+)/', $this->getUrl(), $matches);
  876. $cid = $matches[1];
  877. // Log in as normal user.
  878. $this->drupalLogin($user);
  879. $comment = comment_load($cid);
  880. $comment_file = (object) $comment->{'field_' . $name}[LANGUAGE_NONE][0];
  881. $this->assertFileExists($comment_file, 'New file saved to disk on node creation.');
  882. // Test authenticated file download.
  883. $url = file_create_url($comment_file->uri);
  884. $this->assertNotEqual($url, NULL, 'Confirmed that the URL is valid');
  885. $this->drupalGet(file_create_url($comment_file->uri));
  886. $this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
  887. // Test anonymous file download.
  888. $this->drupalLogout();
  889. $this->drupalGet(file_create_url($comment_file->uri));
  890. $this->assertResponse(403, 'Confirmed that access is denied for the file without the needed permission.');
  891. // Unpublishes node.
  892. $this->drupalLogin($this->admin_user);
  893. $edit = array(
  894. 'status' => FALSE,
  895. );
  896. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  897. // Ensures normal user can no longer download the file.
  898. $this->drupalLogin($user);
  899. $this->drupalGet(file_create_url($comment_file->uri));
  900. $this->assertResponse(403, 'Confirmed that access is denied for the file without the needed permission.');
  901. }
  902. }
  903. /**
  904. * Tests file handling with node revisions.
  905. */
  906. class FileFieldRevisionTestCase extends FileFieldTestCase {
  907. public static function getInfo() {
  908. return array(
  909. 'name' => 'File field revision test',
  910. 'description' => 'Test creating and deleting revisions with files attached.',
  911. 'group' => 'File',
  912. );
  913. }
  914. /**
  915. * Tests creating multiple revisions of a node and managing attached files.
  916. *
  917. * Expected behaviors:
  918. * - Adding a new revision will make another entry in the field table, but
  919. * the original file will not be duplicated.
  920. * - Deleting a revision should not delete the original file if the file
  921. * is in use by another revision.
  922. * - When the last revision that uses a file is deleted, the original file
  923. * should be deleted also.
  924. */
  925. function testRevisions() {
  926. $type_name = 'article';
  927. $field_name = strtolower($this->randomName());
  928. $this->createFileField($field_name, $type_name);
  929. $field = field_info_field($field_name);
  930. $instance = field_info_instance('node', $field_name, $type_name);
  931. // Attach the same fields to users.
  932. $this->attachFileField($field_name, 'user', 'user');
  933. $test_file = $this->getTestFile('text');
  934. // Create a new node with the uploaded file.
  935. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
  936. // Check that the file exists on disk and in the database.
  937. $node = node_load($nid, NULL, TRUE);
  938. $node_file_r1 = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  939. $node_vid_r1 = $node->vid;
  940. $this->assertFileExists($node_file_r1, 'New file saved to disk on node creation.');
  941. $this->assertFileEntryExists($node_file_r1, 'File entry exists in database on node creation.');
  942. $this->assertFileIsPermanent($node_file_r1, 'File is permanent.');
  943. // Upload another file to the same node in a new revision.
  944. $this->replaceNodeFile($test_file, $field_name, $nid);
  945. $node = node_load($nid, NULL, TRUE);
  946. $node_file_r2 = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  947. $node_vid_r2 = $node->vid;
  948. $this->assertFileExists($node_file_r2, 'Replacement file exists on disk after creating new revision.');
  949. $this->assertFileEntryExists($node_file_r2, 'Replacement file entry exists in database after creating new revision.');
  950. $this->assertFileIsPermanent($node_file_r2, 'Replacement file is permanent.');
  951. // Check that the original file is still in place on the first revision.
  952. $node = node_load($nid, $node_vid_r1, TRUE);
  953. $this->assertEqual($node_file_r1, (object) $node->{$field_name}[LANGUAGE_NONE][0], 'Original file still in place after replacing file in new revision.');
  954. $this->assertFileExists($node_file_r1, 'Original file still in place after replacing file in new revision.');
  955. $this->assertFileEntryExists($node_file_r1, 'Original file entry still in place after replacing file in new revision');
  956. $this->assertFileIsPermanent($node_file_r1, 'Original file is still permanent.');
  957. // Save a new version of the node without any changes.
  958. // Check that the file is still the same as the previous revision.
  959. $this->drupalPost('node/' . $nid . '/edit', array('revision' => '1'), t('Save'));
  960. $node = node_load($nid, NULL, TRUE);
  961. $node_file_r3 = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  962. $node_vid_r3 = $node->vid;
  963. $this->assertEqual($node_file_r2, $node_file_r3, 'Previous revision file still in place after creating a new revision without a new file.');
  964. $this->assertFileIsPermanent($node_file_r3, 'New revision file is permanent.');
  965. // Revert to the first revision and check that the original file is active.
  966. $this->drupalPost('node/' . $nid . '/revisions/' . $node_vid_r1 . '/revert', array(), t('Revert'));
  967. $node = node_load($nid, NULL, TRUE);
  968. $node_file_r4 = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  969. $node_vid_r4 = $node->vid;
  970. $this->assertEqual($node_file_r1, $node_file_r4, 'Original revision file still in place after reverting to the original revision.');
  971. $this->assertFileIsPermanent($node_file_r4, 'Original revision file still permanent after reverting to the original revision.');
  972. // Delete the second revision and check that the file is kept (since it is
  973. // still being used by the third revision).
  974. $this->drupalPost('node/' . $nid . '/revisions/' . $node_vid_r2 . '/delete', array(), t('Delete'));
  975. $this->assertFileExists($node_file_r3, 'Second file is still available after deleting second revision, since it is being used by the third revision.');
  976. $this->assertFileEntryExists($node_file_r3, 'Second file entry is still available after deleting second revision, since it is being used by the third revision.');
  977. $this->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting second revision, since it is being used by the third revision.');
  978. // Attach the second file to a user.
  979. $user = $this->drupalCreateUser();
  980. $edit = (array) $user;
  981. $edit[$field_name][LANGUAGE_NONE][0] = (array) $node_file_r3;
  982. user_save($user, $edit);
  983. $this->drupalGet('user/' . $user->uid . '/edit');
  984. // Delete the third revision and check that the file is not deleted yet.
  985. $this->drupalPost('node/' . $nid . '/revisions/' . $node_vid_r3 . '/delete', array(), t('Delete'));
  986. $this->assertFileExists($node_file_r3, 'Second file is still available after deleting third revision, since it is being used by the user.');
  987. $this->assertFileEntryExists($node_file_r3, 'Second file entry is still available after deleting third revision, since it is being used by the user.');
  988. $this->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting third revision, since it is being used by the user.');
  989. // Delete the user and check that the file is also deleted.
  990. user_delete($user->uid);
  991. // TODO: This seems like a bug in File API. Clearing the stat cache should
  992. // not be necessary here. The file really is deleted, but stream wrappers
  993. // doesn't seem to think so unless we clear the PHP file stat() cache.
  994. clearstatcache();
  995. $this->assertFileNotExists($node_file_r3, 'Second file is now deleted after deleting third revision, since it is no longer being used by any other nodes.');
  996. $this->assertFileEntryNotExists($node_file_r3, 'Second file entry is now deleted after deleting third revision, since it is no longer being used by any other nodes.');
  997. // Delete the entire node and check that the original file is deleted.
  998. $this->drupalPost('node/' . $nid . '/delete', array(), t('Delete'));
  999. $this->assertFileNotExists($node_file_r1, 'Original file is deleted after deleting the entire node with two revisions remaining.');
  1000. $this->assertFileEntryNotExists($node_file_r1, 'Original file entry is deleted after deleting the entire node with two revisions remaining.');
  1001. }
  1002. }
  1003. /**
  1004. * Tests that formatters are working properly.
  1005. */
  1006. class FileFieldDisplayTestCase extends FileFieldTestCase {
  1007. public static function getInfo() {
  1008. return array(
  1009. 'name' => 'File field display tests',
  1010. 'description' => 'Test the display of file fields in node and views.',
  1011. 'group' => 'File',
  1012. );
  1013. }
  1014. /**
  1015. * Tests normal formatter display on node display.
  1016. */
  1017. function testNodeDisplay() {
  1018. $field_name = strtolower($this->randomName());
  1019. $type_name = 'article';
  1020. $field_settings = array(
  1021. 'display_field' => '1',
  1022. 'display_default' => '1',
  1023. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  1024. );
  1025. $instance_settings = array(
  1026. 'description_field' => '1',
  1027. );
  1028. $widget_settings = array();
  1029. $this->createFileField($field_name, $type_name, $field_settings, $instance_settings, $widget_settings);
  1030. $field = field_info_field($field_name);
  1031. $instance = field_info_instance('node', $field_name, $type_name);
  1032. // Create a new node *without* the file field set, and check that the field
  1033. // is not shown for each node display.
  1034. $node = $this->drupalCreateNode(array('type' => $type_name));
  1035. $file_formatters = array('file_default', 'file_table', 'file_url_plain', 'hidden');
  1036. foreach ($file_formatters as $formatter) {
  1037. $edit = array(
  1038. "fields[$field_name][type]" => $formatter,
  1039. );
  1040. $this->drupalPost("admin/structure/types/manage/$type_name/display", $edit, t('Save'));
  1041. $this->drupalGet('node/' . $node->nid);
  1042. $this->assertNoText($field_name, format_string('Field label is hidden when no file attached for formatter %formatter', array('%formatter' => $formatter)));
  1043. }
  1044. $test_file = $this->getTestFile('text');
  1045. // Create a new node with the uploaded file.
  1046. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
  1047. $this->drupalGet('node/' . $nid . '/edit');
  1048. // Check that the default formatter is displaying with the file name.
  1049. $node = node_load($nid, NULL, TRUE);
  1050. $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  1051. $default_output = theme('file_link', array('file' => $node_file));
  1052. $this->assertRaw($default_output, 'Default formatter displaying correctly on full node view.');
  1053. // Turn the "display" option off and check that the file is no longer displayed.
  1054. $edit = array($field_name . '[' . LANGUAGE_NONE . '][0][display]' => FALSE);
  1055. $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
  1056. $this->assertNoRaw($default_output, 'Field is hidden when "display" option is unchecked.');
  1057. // Test that fields appear as expected during the preview.
  1058. // Add a second file.
  1059. $name = 'files[' . $field_name . '_' . LANGUAGE_NONE . '_1]';
  1060. $edit[$name] = drupal_realpath($test_file->uri);
  1061. // Uncheck the display checkboxes and go to the preview.
  1062. $edit[$field_name . '[' . LANGUAGE_NONE . '][0][display]'] = FALSE;
  1063. $edit[$field_name . '[' . LANGUAGE_NONE . '][1][display]'] = FALSE;
  1064. $this->drupalPost('node/' . $nid . '/edit', $edit, t('Preview'));
  1065. $this->assertRaw($field_name . '[' . LANGUAGE_NONE . '][0][display]', 'First file appears as expected.');
  1066. $this->assertRaw($field_name . '[' . LANGUAGE_NONE . '][1][display]', 'Second file appears as expected.');
  1067. }
  1068. /**
  1069. * Tests default display of File Field.
  1070. */
  1071. function testDefaultFileFieldDisplay() {
  1072. $field_name = strtolower($this->randomName());
  1073. $type_name = 'article';
  1074. $field_settings = array(
  1075. 'display_field' => '1',
  1076. 'display_default' => '0',
  1077. );
  1078. $instance_settings = array(
  1079. 'description_field' => '1',
  1080. );
  1081. $widget_settings = array();
  1082. $this->createFileField($field_name, $type_name, $field_settings, $instance_settings, $widget_settings);
  1083. $field = field_info_field($field_name);
  1084. $instance = field_info_instance('node', $field_name, $type_name);
  1085. $test_file = $this->getTestFile('text');
  1086. // Create a new node with the uploaded file.
  1087. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
  1088. $this->drupalGet('node/' . $nid . '/edit');
  1089. $this->assertFieldByXPath('//input[@type="checkbox" and @name="' . $field_name . '[und][0][display]"]', NULL, 'Default file display checkbox field exists.');
  1090. $this->assertFieldByXPath('//input[@type="checkbox" and @name="' . $field_name . '[und][0][display]" and not(@checked)]', NULL, 'Default file display is off.');
  1091. }
  1092. }
  1093. /**
  1094. * Tests various validations.
  1095. */
  1096. class FileFieldValidateTestCase extends FileFieldTestCase {
  1097. protected $field;
  1098. protected $node_type;
  1099. public static function getInfo() {
  1100. return array(
  1101. 'name' => 'File field validation tests',
  1102. 'description' => 'Tests validation functions such as file type, max file size, max size per node, and required.',
  1103. 'group' => 'File',
  1104. );
  1105. }
  1106. /**
  1107. * Tests the required property on file fields.
  1108. */
  1109. function testRequired() {
  1110. $type_name = 'article';
  1111. $field_name = strtolower($this->randomName());
  1112. $this->createFileField($field_name, $type_name, array(), array('required' => '1'));
  1113. $field = field_info_field($field_name);
  1114. $instance = field_info_instance('node', $field_name, $type_name);
  1115. $test_file = $this->getTestFile('text');
  1116. // Try to post a new node without uploading a file.
  1117. $langcode = LANGUAGE_NONE;
  1118. $edit = array("title" => $this->randomName());
  1119. $this->drupalPost('node/add/' . $type_name, $edit, t('Save'));
  1120. $this->assertRaw(t('!title field is required.', array('!title' => $instance['label'])), 'Node save failed when required file field was empty.');
  1121. // Create a new node with the uploaded file.
  1122. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
  1123. $this->assertTrue($nid !== FALSE, format_string('uploadNodeFile(@test_file, @field_name, @type_name) succeeded', array('@test_file' => $test_file->uri, '@field_name' => $field_name, '@type_name' => $type_name)));
  1124. $node = node_load($nid, NULL, TRUE);
  1125. $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  1126. $this->assertFileExists($node_file, 'File exists after uploading to the required field.');
  1127. $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required field.');
  1128. // Try again with a multiple value field.
  1129. field_delete_field($field_name);
  1130. $this->createFileField($field_name, $type_name, array('cardinality' => FIELD_CARDINALITY_UNLIMITED), array('required' => '1'));
  1131. // Try to post a new node without uploading a file in the multivalue field.
  1132. $edit = array('title' => $this->randomName());
  1133. $this->drupalPost('node/add/' . $type_name, $edit, t('Save'));
  1134. $this->assertRaw(t('!title field is required.', array('!title' => $instance['label'])), 'Node save failed when required multiple value file field was empty.');
  1135. // Create a new node with the uploaded file into the multivalue field.
  1136. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
  1137. $node = node_load($nid, NULL, TRUE);
  1138. $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  1139. $this->assertFileExists($node_file, 'File exists after uploading to the required multiple value field.');
  1140. $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required multipel value field.');
  1141. // Remove our file field.
  1142. field_delete_field($field_name);
  1143. }
  1144. /**
  1145. * Tests the max file size validator.
  1146. */
  1147. function testFileMaxSize() {
  1148. $type_name = 'article';
  1149. $field_name = strtolower($this->randomName());
  1150. $this->createFileField($field_name, $type_name, array(), array('required' => '1'));
  1151. $field = field_info_field($field_name);
  1152. $instance = field_info_instance('node', $field_name, $type_name);
  1153. $small_file = $this->getTestFile('text', 131072); // 128KB.
  1154. $large_file = $this->getTestFile('text', 1310720); // 1.2MB
  1155. // Test uploading both a large and small file with different increments.
  1156. $sizes = array(
  1157. '1M' => 1048576,
  1158. '1024K' => 1048576,
  1159. '1048576' => 1048576,
  1160. );
  1161. foreach ($sizes as $max_filesize => $file_limit) {
  1162. // Set the max file upload size.
  1163. $this->updateFileField($field_name, $type_name, array('max_filesize' => $max_filesize));
  1164. $instance = field_info_instance('node', $field_name, $type_name);
  1165. // Create a new node with the small file, which should pass.
  1166. $nid = $this->uploadNodeFile($small_file, $field_name, $type_name);
  1167. $node = node_load($nid, NULL, TRUE);
  1168. $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  1169. $this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) under the max limit (%maxsize).', array('%filesize' => format_size($small_file->filesize), '%maxsize' => $max_filesize)));
  1170. $this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) under the max limit (%maxsize).', array('%filesize' => format_size($small_file->filesize), '%maxsize' => $max_filesize)));
  1171. // Check that uploading the large file fails (1M limit).
  1172. $nid = $this->uploadNodeFile($large_file, $field_name, $type_name);
  1173. $error_message = t('The file is %filesize exceeding the maximum file size of %maxsize.', array('%filesize' => format_size($large_file->filesize), '%maxsize' => format_size($file_limit)));
  1174. $this->assertRaw($error_message, format_string('Node save failed when file (%filesize) exceeded the max upload size (%maxsize).', array('%filesize' => format_size($large_file->filesize), '%maxsize' => $max_filesize)));
  1175. }
  1176. // Turn off the max filesize.
  1177. $this->updateFileField($field_name, $type_name, array('max_filesize' => ''));
  1178. // Upload the big file successfully.
  1179. $nid = $this->uploadNodeFile($large_file, $field_name, $type_name);
  1180. $node = node_load($nid, NULL, TRUE);
  1181. $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  1182. $this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) with no max limit.', array('%filesize' => format_size($large_file->filesize))));
  1183. $this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) with no max limit.', array('%filesize' => format_size($large_file->filesize))));
  1184. // Remove our file field.
  1185. field_delete_field($field_name);
  1186. }
  1187. /**
  1188. * Tests file extension checking.
  1189. */
  1190. function testFileExtension() {
  1191. $type_name = 'article';
  1192. $field_name = strtolower($this->randomName());
  1193. $this->createFileField($field_name, $type_name);
  1194. $field = field_info_field($field_name);
  1195. $instance = field_info_instance('node', $field_name, $type_name);
  1196. $test_file = $this->getTestFile('image');
  1197. list(, $test_file_extension) = explode('.', $test_file->filename);
  1198. // Disable extension checking.
  1199. $this->updateFileField($field_name, $type_name, array('file_extensions' => ''));
  1200. // Check that the file can be uploaded with no extension checking.
  1201. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
  1202. $node = node_load($nid, NULL, TRUE);
  1203. $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  1204. $this->assertFileExists($node_file, 'File exists after uploading a file with no extension checking.');
  1205. $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.');
  1206. // Enable extension checking for text files.
  1207. $this->updateFileField($field_name, $type_name, array('file_extensions' => 'txt'));
  1208. // Check that the file with the wrong extension cannot be uploaded.
  1209. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
  1210. $error_message = t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => 'txt'));
  1211. $this->assertRaw($error_message, 'Node save failed when file uploaded with the wrong extension.');
  1212. // Enable extension checking for text and image files.
  1213. $this->updateFileField($field_name, $type_name, array('file_extensions' => "txt $test_file_extension"));
  1214. // Check that the file can be uploaded with extension checking.
  1215. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
  1216. $node = node_load($nid, NULL, TRUE);
  1217. $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  1218. $this->assertFileExists($node_file, 'File exists after uploading a file with extension checking.');
  1219. $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with extension checking.');
  1220. // Remove our file field.
  1221. field_delete_field($field_name);
  1222. }
  1223. }
  1224. /**
  1225. * Tests that files are uploaded to proper locations.
  1226. */
  1227. class FileFieldPathTestCase extends FileFieldTestCase {
  1228. public static function getInfo() {
  1229. return array(
  1230. 'name' => 'File field file path tests',
  1231. 'description' => 'Test that files are uploaded to the proper location with token support.',
  1232. 'group' => 'File',
  1233. );
  1234. }
  1235. /**
  1236. * Tests the normal formatter display on node display.
  1237. */
  1238. function testUploadPath() {
  1239. $field_name = strtolower($this->randomName());
  1240. $type_name = 'article';
  1241. $field = $this->createFileField($field_name, $type_name);
  1242. $test_file = $this->getTestFile('text');
  1243. // Create a new node.
  1244. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
  1245. // Check that the file was uploaded to the correct location.
  1246. $node = node_load($nid, NULL, TRUE);
  1247. $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  1248. $expected_path = 'public://' . date('Y', REQUEST_TIME) . '-' . date('m', REQUEST_TIME) . '/' . $test_file->filename;
  1249. $this->assertPathMatch($expected_path, $node_file->uri, format_string('The file %file was uploaded to the correct path.', array('%file' => $node_file->uri)));
  1250. // Change the path to contain multiple subdirectories.
  1251. $field = $this->updateFileField($field_name, $type_name, array('file_directory' => 'foo/bar/baz'));
  1252. // Upload a new file into the subdirectories.
  1253. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
  1254. // Check that the file was uploaded into the subdirectory.
  1255. $node = node_load($nid, NULL, TRUE);
  1256. $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  1257. $this->assertPathMatch('public://foo/bar/baz/' . $test_file->filename, $node_file->uri, format_string('The file %file was uploaded to the correct path.', array('%file' => $node_file->uri)));
  1258. // Check the path when used with tokens.
  1259. // Change the path to contain multiple token directories.
  1260. $field = $this->updateFileField($field_name, $type_name, array('file_directory' => '[current-user:uid]/[current-user:name]'));
  1261. // Upload a new file into the token subdirectories.
  1262. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
  1263. // Check that the file was uploaded into the subdirectory.
  1264. $node = node_load($nid, NULL, TRUE);
  1265. $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  1266. // Do token replacement using the same user which uploaded the file, not
  1267. // the user running the test case.
  1268. $data = array('user' => $this->admin_user);
  1269. $subdirectory = token_replace('[user:uid]/[user:name]', $data);
  1270. $this->assertPathMatch('public://' . $subdirectory . '/' . $test_file->filename, $node_file->uri, format_string('The file %file was uploaded to the correct path with token replacements.', array('%file' => $node_file->uri)));
  1271. }
  1272. /**
  1273. * Asserts that a file is uploaded to the right location.
  1274. *
  1275. * @param $expected_path
  1276. * The location where the file is expected to be uploaded. Duplicate file
  1277. * names to not need to be taken into account.
  1278. * @param $actual_path
  1279. * Where the file was actually uploaded.
  1280. * @param $message
  1281. * The message to display with this assertion.
  1282. */
  1283. function assertPathMatch($expected_path, $actual_path, $message) {
  1284. // Strip off the extension of the expected path to allow for _0, _1, etc.
  1285. // suffixes when the file hits a duplicate name.
  1286. $pos = strrpos($expected_path, '.');
  1287. $base_path = substr($expected_path, 0, $pos);
  1288. $extension = substr($expected_path, $pos + 1);
  1289. $result = preg_match('/' . preg_quote($base_path, '/') . '(_[0-9]+)?\.' . preg_quote($extension, '/') . '/', $actual_path);
  1290. $this->assertTrue($result, $message);
  1291. }
  1292. }
  1293. /**
  1294. * Tests the file token replacement in strings.
  1295. */
  1296. class FileTokenReplaceTestCase extends FileFieldTestCase {
  1297. public static function getInfo() {
  1298. return array(
  1299. 'name' => 'File token replacement',
  1300. 'description' => 'Generates text using placeholders for dummy content to check file token replacement.',
  1301. 'group' => 'File',
  1302. );
  1303. }
  1304. /**
  1305. * Creates a file, then tests the tokens generated from it.
  1306. */
  1307. function testFileTokenReplacement() {
  1308. global $language;
  1309. $url_options = array(
  1310. 'absolute' => TRUE,
  1311. 'language' => $language,
  1312. );
  1313. // Create file field.
  1314. $type_name = 'article';
  1315. $field_name = 'field_' . strtolower($this->randomName());
  1316. $this->createFileField($field_name, $type_name);
  1317. $field = field_info_field($field_name);
  1318. $instance = field_info_instance('node', $field_name, $type_name);
  1319. $test_file = $this->getTestFile('text');
  1320. // Coping a file to test uploads with non-latin filenames.
  1321. $filename = drupal_dirname($test_file->uri) . '/текстовый файл.txt';
  1322. $test_file = file_copy($test_file, $filename);
  1323. // Create a new node with the uploaded file.
  1324. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
  1325. // Load the node and the file.
  1326. $node = node_load($nid, NULL, TRUE);
  1327. $file = file_load($node->{$field_name}[LANGUAGE_NONE][0]['fid']);
  1328. // Generate and test sanitized tokens.
  1329. $tests = array();
  1330. $tests['[file:fid]'] = $file->fid;
  1331. $tests['[file:name]'] = check_plain($file->filename);
  1332. $tests['[file:path]'] = check_plain($file->uri);
  1333. $tests['[file:mime]'] = check_plain($file->filemime);
  1334. $tests['[file:size]'] = format_size($file->filesize);
  1335. $tests['[file:url]'] = check_plain(file_create_url($file->uri));
  1336. $tests['[file:timestamp]'] = format_date($file->timestamp, 'medium', '', NULL, $language->language);
  1337. $tests['[file:timestamp:short]'] = format_date($file->timestamp, 'short', '', NULL, $language->language);
  1338. $tests['[file:owner]'] = check_plain(format_username($this->admin_user));
  1339. $tests['[file:owner:uid]'] = $file->uid;
  1340. // Test to make sure that we generated something for each token.
  1341. $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
  1342. foreach ($tests as $input => $expected) {
  1343. $output = token_replace($input, array('file' => $file), array('language' => $language));
  1344. $this->assertEqual($output, $expected, format_string('Sanitized file token %token replaced.', array('%token' => $input)));
  1345. }
  1346. // Generate and test unsanitized tokens.
  1347. $tests['[file:name]'] = $file->filename;
  1348. $tests['[file:path]'] = $file->uri;
  1349. $tests['[file:mime]'] = $file->filemime;
  1350. $tests['[file:size]'] = format_size($file->filesize);
  1351. foreach ($tests as $input => $expected) {
  1352. $output = token_replace($input, array('file' => $file), array('language' => $language, 'sanitize' => FALSE));
  1353. $this->assertEqual($output, $expected, format_string('Unsanitized file token %token replaced.', array('%token' => $input)));
  1354. }
  1355. }
  1356. }
  1357. /**
  1358. * Tests file access on private nodes.
  1359. */
  1360. class FilePrivateTestCase extends FileFieldTestCase {
  1361. public static function getInfo() {
  1362. return array(
  1363. 'name' => 'Private file test',
  1364. 'description' => 'Uploads a test to a private node and checks access.',
  1365. 'group' => 'File',
  1366. );
  1367. }
  1368. function setUp() {
  1369. parent::setUp(array('node_access_test', 'field_test'));
  1370. node_access_rebuild();
  1371. variable_set('node_access_test_private', TRUE);
  1372. }
  1373. /**
  1374. * Tests file access for file uploaded to a private node.
  1375. */
  1376. function testPrivateFile() {
  1377. // Use 'page' instead of 'article', so that the 'article' image field does
  1378. // not conflict with this test. If in the future the 'page' type gets its
  1379. // own default file or image field, this test can be made more robust by
  1380. // using a custom node type.
  1381. $type_name = 'page';
  1382. $field_name = strtolower($this->randomName());
  1383. $this->createFileField($field_name, $type_name, array('uri_scheme' => 'private'));
  1384. // Create a field with no view access - see field_test_field_access().
  1385. $no_access_field_name = 'field_no_view_access';
  1386. $this->createFileField($no_access_field_name, $type_name, array('uri_scheme' => 'private'));
  1387. $test_file = $this->getTestFile('text');
  1388. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name, TRUE, array('private' => TRUE));
  1389. $node = node_load($nid, NULL, TRUE);
  1390. $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  1391. // Ensure the file can be downloaded.
  1392. $this->drupalGet(file_create_url($node_file->uri));
  1393. $this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
  1394. $this->drupalLogOut();
  1395. $this->drupalGet(file_create_url($node_file->uri));
  1396. $this->assertResponse(403, 'Confirmed that access is denied for the file without the needed permission.');
  1397. // Test with the field that should deny access through field access.
  1398. $this->drupalLogin($this->admin_user);
  1399. $nid = $this->uploadNodeFile($test_file, $no_access_field_name, $type_name, TRUE, array('private' => TRUE));
  1400. $node = node_load($nid, NULL, TRUE);
  1401. $node_file = (object) $node->{$no_access_field_name}[LANGUAGE_NONE][0];
  1402. // Ensure the file cannot be downloaded.
  1403. $this->drupalGet(file_create_url($node_file->uri));
  1404. $this->assertResponse(403, 'Confirmed that access is denied for the file without view field access permission.');
  1405. // Attempt to reuse the existing file when creating a new node, and confirm
  1406. // that access is still denied.
  1407. $edit = array();
  1408. $edit['title'] = $this->randomName(8);
  1409. $edit[$field_name . '[' . LANGUAGE_NONE . '][0][fid]'] = $node_file->fid;
  1410. $this->drupalPost('node/add/page', $edit, t('Save'));
  1411. $new_node = $this->drupalGetNodeByTitle($edit['title']);
  1412. $this->assertTrue(!empty($new_node), 'Node was created.');
  1413. $this->assertUrl('node/' . $new_node->nid);
  1414. $this->assertNoRaw($node_file->filename, 'File without view field access permission does not appear after attempting to attach it to a new node.');
  1415. $this->drupalGet(file_create_url($node_file->uri));
  1416. $this->assertResponse(403, 'Confirmed that access is denied for the file without view field access permission after attempting to attach it to a new node.');
  1417. // As an anonymous user, create a temporary file with no references and
  1418. // confirm that only the session that uploaded it may view it.
  1419. $this->drupalLogout();
  1420. user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array(
  1421. "create $type_name content",
  1422. 'access content',
  1423. ));
  1424. $test_file = $this->getTestFile('text');
  1425. $this->drupalGet('node/add/' . $type_name);
  1426. $edit = array('files[' . $field_name . '_' . LANGUAGE_NONE . '_0]' => drupal_realpath($test_file->uri));
  1427. $this->drupalPost(NULL, $edit, t('Upload'));
  1428. $files = file_load_multiple(array(), array('uid' => 0));
  1429. $this->assertEqual(1, count($files), 'Loaded one anonymous file.');
  1430. $file = end($files);
  1431. $this->assertNotEqual($file->status, FILE_STATUS_PERMANENT, 'File is temporary.');
  1432. $usage = file_usage_list($file);
  1433. $this->assertFalse($usage, 'No file usage found.');
  1434. $file_url = file_create_url($file->uri);
  1435. $this->drupalGet($file_url);
  1436. $this->assertResponse(200, 'Confirmed that the anonymous uploader has access to the temporary file.');
  1437. // Close the prior connection and remove the session cookie.
  1438. $this->curlClose();
  1439. $this->cookies = array();
  1440. $this->drupalGet($file_url);
  1441. $this->assertResponse(403, 'Confirmed that another anonymous user cannot access the temporary file.');
  1442. // As an anonymous user, create a permanent file that is referenced by a
  1443. // published node and confirm that all anonymous users may view it.
  1444. $test_file = $this->getTestFile('text');
  1445. $this->drupalGet('node/add/' . $type_name);
  1446. $edit = array();
  1447. $edit['title'] = $this->randomName();
  1448. $edit['files[' . $field_name . '_' . LANGUAGE_NONE . '_0]'] = drupal_realpath($test_file->uri);
  1449. $this->drupalPost(NULL, $edit, t('Save'));
  1450. $new_node = $this->drupalGetNodeByTitle($edit['title']);
  1451. $file = file_load($new_node->{$field_name}[LANGUAGE_NONE][0]['fid']);
  1452. $this->assertEqual($file->status, FILE_STATUS_PERMANENT, 'File is permanent.');
  1453. $usage = file_usage_list($file);
  1454. $this->assertTrue($usage, 'File usage found.');
  1455. $file_url = file_create_url($file->uri);
  1456. $this->drupalGet($file_url);
  1457. $this->assertResponse(200, 'Confirmed that the anonymous uploader has access to the permanent file that is referenced by a published node.');
  1458. // Close the prior connection and remove the session cookie.
  1459. $this->curlClose();
  1460. $this->cookies = array();
  1461. $this->drupalGet($file_url);
  1462. $this->assertResponse(200, 'Confirmed that another anonymous user also has access to the permanent file that is referenced by a published node.');
  1463. // As an anonymous user, create a permanent file that is referenced by an
  1464. // unpublished node and confirm that no anonymous users may view it (even
  1465. // the session that uploaded the file) because they cannot view the
  1466. // unpublished node.
  1467. $test_file = $this->getTestFile('text');
  1468. $this->drupalGet('node/add/' . $type_name);
  1469. $edit = array();
  1470. $edit['title'] = $this->randomName();
  1471. $edit['files[' . $field_name . '_' . LANGUAGE_NONE . '_0]'] = drupal_realpath($test_file->uri);
  1472. $this->drupalPost(NULL, $edit, t('Save'));
  1473. $new_node = $this->drupalGetNodeByTitle($edit['title']);
  1474. $new_node->status = NODE_NOT_PUBLISHED;
  1475. node_save($new_node);
  1476. $file = file_load($new_node->{$field_name}[LANGUAGE_NONE][0]['fid']);
  1477. $this->assertEqual($file->status, FILE_STATUS_PERMANENT, 'File is permanent.');
  1478. $usage = file_usage_list($file);
  1479. $this->assertTrue($usage, 'File usage found.');
  1480. $file_url = file_create_url($file->uri);
  1481. $this->drupalGet($file_url);
  1482. $this->assertResponse(403, 'Confirmed that the anonymous uploader cannot access the permanent file when it is referenced by an unpublished node.');
  1483. // Close the prior connection and remove the session cookie.
  1484. $this->curlClose();
  1485. $this->cookies = array();
  1486. $this->drupalGet($file_url);
  1487. $this->assertResponse(403, 'Confirmed that another anonymous user cannot access the permanent file when it is referenced by an unpublished node.');
  1488. }
  1489. /**
  1490. * Tests file access for private nodes when file download access is granted.
  1491. */
  1492. function testPrivateFileDownloadAccessGranted() {
  1493. // Tell file_module_test to attempt to grant access to all private files,
  1494. // and ensure that it is doing so correctly.
  1495. $test_file = $this->getTestFile('text');
  1496. $uri = file_unmanaged_move($test_file->uri, 'private://');
  1497. $file_url = file_create_url($uri);
  1498. $this->drupalGet($file_url);
  1499. $this->assertResponse(403, 'Access is not granted to an arbitrary private file by default.');
  1500. variable_set('file_module_test_grant_download_access', TRUE);
  1501. $this->drupalGet($file_url);
  1502. $this->assertResponse(200, 'Access is granted to an arbitrary private file after a module grants access to all private files in hook_file_download().');
  1503. // Create a public node with a file attached.
  1504. $type_name = 'page';
  1505. $field_name = strtolower($this->randomName());
  1506. $this->createFileField($field_name, $type_name, array('uri_scheme' => 'private'));
  1507. $test_file = $this->getTestFile('text');
  1508. $nid = $this->uploadNodeFile($test_file, $field_name, $type_name, TRUE, array('private' => FALSE));
  1509. $node = node_load($nid, NULL, TRUE);
  1510. $file_url = file_create_url($node->{$field_name}[LANGUAGE_NONE][0]['uri']);
  1511. // Unpublish the node and ensure that only administrators (not anonymous
  1512. // users) can access the node and download the file; the expectation is
  1513. // that the File module's hook_file_download() implementation will deny
  1514. // access and thereby override the file_module_test module's access grant.
  1515. $node->status = NODE_NOT_PUBLISHED;
  1516. node_save($node);
  1517. $this->drupalLogin($this->admin_user);
  1518. $this->drupalGet("node/$nid");
  1519. $this->assertResponse(200, 'Administrator can access the unpublished node.');
  1520. $this->drupalGet($file_url);
  1521. $this->assertResponse(200, 'Administrator can download the file attached to the unpublished node.');
  1522. $this->drupalLogOut();
  1523. $this->drupalGet("node/$nid");
  1524. $this->assertResponse(403, 'Anonymous user cannot access the unpublished node.');
  1525. $this->drupalGet($file_url);
  1526. $this->assertResponse(403, 'Anonymous user cannot download the file attached to the unpublished node.');
  1527. // Re-publish the node and ensure that the node and file can be accessed by
  1528. // everyone.
  1529. $node->status = NODE_PUBLISHED;
  1530. node_save($node);
  1531. $this->drupalLogin($this->admin_user);
  1532. $this->drupalGet("node/$nid");
  1533. $this->assertResponse(200, 'Administrator can access the published node.');
  1534. $this->drupalGet($file_url);
  1535. $this->assertResponse(200, 'Administrator can download the file attached to the published node.');
  1536. $this->drupalLogOut();
  1537. $this->drupalGet("node/$nid");
  1538. $this->assertResponse(200, 'Anonymous user can access the published node.');
  1539. $this->drupalGet($file_url);
  1540. $this->assertResponse(200, 'Anonymous user can download the file attached to the published node.');
  1541. // Make the node private via the node access system and test that only
  1542. // administrators (not anonymous users) can access the node and download
  1543. // the file.
  1544. $node->private = TRUE;
  1545. node_save($node);
  1546. $this->drupalLogin($this->admin_user);
  1547. $this->drupalGet("node/$nid");
  1548. $this->assertResponse(200, 'Administrator can access the private node.');
  1549. $this->drupalGet($file_url);
  1550. $this->assertResponse(200, 'Administrator can download the file attached to the private node.');
  1551. $this->drupalLogOut();
  1552. $this->drupalGet("node/$nid");
  1553. $this->assertResponse(403, 'Anonymous user cannot access the private node.');
  1554. $this->drupalGet($file_url);
  1555. $this->assertResponse(403, 'Anonymous user cannot download the file attached to the private node.');
  1556. }
  1557. }
  1558. /**
  1559. * Confirm that file field submissions work correctly for anonymous visitors.
  1560. */
  1561. class FileFieldAnonymousSubmission extends FileFieldTestCase {
  1562. public static function getInfo() {
  1563. return array(
  1564. 'name' => 'File form anonymous submission',
  1565. 'description' => 'Test anonymous form submission.',
  1566. 'group' => 'File',
  1567. );
  1568. }
  1569. function setUp() {
  1570. parent::setUp();
  1571. // Allow node submissions by anonymous users.
  1572. user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array(
  1573. 'create article content',
  1574. 'access content',
  1575. ));
  1576. }
  1577. /**
  1578. * Tests the basic node submission for an anonymous visitor.
  1579. */
  1580. function testAnonymousNode() {
  1581. $bundle_label = 'Article';
  1582. $node_title = 'Test page';
  1583. // Load the node form.
  1584. $this->drupalGet('node/add/article');
  1585. $this->assertResponse(200, 'Loaded the article node form.');
  1586. $this->assertText(strip_tags(t('Create @name', array('@name' => $bundle_label))));
  1587. $edit = array(
  1588. 'title' => $node_title,
  1589. 'body[und][0][value]' => 'Test article',
  1590. 'body[und][0][format]' => 'filtered_html',
  1591. );
  1592. $this->drupalPost(NULL, $edit, t('Save'));
  1593. $this->assertResponse(200);
  1594. $t_args = array('@type' => $bundle_label, '%title' => $node_title);
  1595. $this->assertText(strip_tags(t('@type %title has been created.', $t_args)), 'The node was created.');
  1596. $matches = array();
  1597. if (preg_match('@node/(\d+)$@', $this->getUrl(), $matches)) {
  1598. $nid = end($matches);
  1599. $this->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.');
  1600. $node = node_load($nid);
  1601. $this->assertNotEqual($node, NULL, 'The node was loaded successfully.');
  1602. }
  1603. }
  1604. /**
  1605. * Tests file submission for an anonymous visitor.
  1606. */
  1607. function testAnonymousNodeWithFile() {
  1608. $bundle_label = 'Article';
  1609. $node_title = 'Test page';
  1610. // Load the node form.
  1611. $this->drupalGet('node/add/article');
  1612. $this->assertResponse(200, 'Loaded the article node form.');
  1613. $this->assertText(strip_tags(t('Create @name', array('@name' => $bundle_label))));
  1614. // Generate an image file.
  1615. $image = $this->getTestImage();
  1616. // Submit the form.
  1617. $edit = array(
  1618. 'title' => $node_title,
  1619. 'body[und][0][value]' => 'Test article',
  1620. 'body[und][0][format]' => 'filtered_html',
  1621. 'files[field_image_und_0]' => drupal_realpath($image->uri),
  1622. );
  1623. $this->drupalPost(NULL, $edit, t('Save'));
  1624. $this->assertResponse(200);
  1625. $t_args = array('@type' => $bundle_label, '%title' => $node_title);
  1626. $this->assertText(strip_tags(t('@type %title has been created.', $t_args)), 'The node was created.');
  1627. $matches = array();
  1628. if (preg_match('@node/(\d+)$@', $this->getUrl(), $matches)) {
  1629. $nid = end($matches);
  1630. $this->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.');
  1631. $node = node_load($nid);
  1632. $this->assertNotEqual($node, NULL, 'The node was loaded successfully.');
  1633. $this->assertEqual($node->field_image[LANGUAGE_NONE][0]['filename'], $image->filename, 'The image was uploaded successfully.');
  1634. }
  1635. }
  1636. /**
  1637. * Tests file submission for an anonymous visitor with a missing node title.
  1638. */
  1639. function testAnonymousNodeWithFileWithoutTitle() {
  1640. $this->drupalLogout();
  1641. $this->_testNodeWithFileWithoutTitle();
  1642. }
  1643. /**
  1644. * Tests file submission for an authenticated user with a missing node title.
  1645. */
  1646. function testAuthenticatedNodeWithFileWithoutTitle() {
  1647. $admin_user = $this->drupalCreateUser(array(
  1648. 'bypass node access',
  1649. 'access content overview',
  1650. 'administer nodes',
  1651. ));
  1652. $this->drupalLogin($admin_user);
  1653. $this->_testNodeWithFileWithoutTitle();
  1654. }
  1655. /**
  1656. * Helper method to test file submissions with missing node titles.
  1657. */
  1658. protected function _testNodeWithFileWithoutTitle() {
  1659. $bundle_label = 'Article';
  1660. $node_title = 'Test page';
  1661. // Load the node form.
  1662. $this->drupalGet('node/add/article');
  1663. $this->assertResponse(200, 'Loaded the article node form.');
  1664. $this->assertText(strip_tags(t('Create @name', array('@name' => $bundle_label))));
  1665. // Generate an image file.
  1666. $image = $this->getTestImage();
  1667. // Submit the form but exclude the title field.
  1668. $edit = array(
  1669. 'body[und][0][value]' => 'Test article',
  1670. 'body[und][0][format]' => 'filtered_html',
  1671. 'files[field_image_und_0]' => drupal_realpath($image->uri),
  1672. );
  1673. $this->drupalPost(NULL, $edit, t('Save'));
  1674. $this->assertResponse(200);
  1675. $t_args = array('@type' => $bundle_label, '%title' => $node_title);
  1676. $this->assertNoText(strip_tags(t('@type %title has been created.', $t_args)), 'The node was created.');
  1677. $this->assertText(t('!name field is required.', array('!name' => t('Title'))));
  1678. // Submit the form again but this time with the missing title field. This
  1679. // should still work.
  1680. $edit = array(
  1681. 'title' => $node_title,
  1682. );
  1683. $this->drupalPost(NULL, $edit, t('Save'));
  1684. // Confirm the final submission actually worked.
  1685. $t_args = array('@type' => $bundle_label, '%title' => $node_title);
  1686. $this->assertText(strip_tags(t('@type %title has been created.', $t_args)), 'The node was created.');
  1687. $matches = array();
  1688. if (preg_match('@node/(\d+)$@', $this->getUrl(), $matches)) {
  1689. $nid = end($matches);
  1690. $this->assertNotEqual($nid, 0, 'The node ID was extracted from the URL.');
  1691. $node = node_load($nid);
  1692. $this->assertNotEqual($node, NULL, 'The node was loaded successfully.');
  1693. $this->assertEqual($node->field_image[LANGUAGE_NONE][0]['filename'], $image->filename, 'The image was uploaded successfully.');
  1694. }
  1695. }
  1696. /**
  1697. * Generates a test image.
  1698. *
  1699. * @return stdClass
  1700. * A file object.
  1701. */
  1702. function getTestImage() {
  1703. // Get a file to upload.
  1704. $file = current($this->drupalGetTestFiles('image'));
  1705. // Add a filesize property to files as would be read by file_load().
  1706. $file->filesize = filesize($file->uri);
  1707. return $file;
  1708. }
  1709. }
  1710. /**
  1711. * Tests the file_scan_directory() function.
  1712. */
  1713. class FileScanDirectory extends FileFieldTestCase {
  1714. /**
  1715. * @var string
  1716. */
  1717. protected $path;
  1718. /**
  1719. * {@inheritdoc}
  1720. */
  1721. public static function getInfo() {
  1722. return array(
  1723. 'name' => 'File ScanDirectory',
  1724. 'description' => 'Tests the file_scan_directory() function.',
  1725. 'group' => 'File',
  1726. );
  1727. }
  1728. /**
  1729. * {@inheritdoc}
  1730. */
  1731. function setUp() {
  1732. parent::setUp();
  1733. $this->path = 'modules/file/tests/fixtures/file_scan_ignore';
  1734. }
  1735. /**
  1736. * Tests file_scan_directory() obeys 'file_scan_ignore_directories' setting.
  1737. * If nomask is not passed as argument, it should use the default settings.
  1738. * If nomask is passed as argument, it should obey this rule.
  1739. */
  1740. public function testNoMask() {
  1741. $files = file_scan_directory($this->path, '/\.txt$/');
  1742. $this->assertEqual(3, count($files), '3 text files found when not ignoring directories.');
  1743. global $conf;
  1744. $conf['file_scan_ignore_directories'] = array('frontend_framework');
  1745. $files = file_scan_directory($this->path, '/\.txt$/');
  1746. $this->assertEqual(1, count($files), '1 text files found when ignoring directories called "frontend_framework".');
  1747. // Make that directories specified by default still work when a new nomask is provided.
  1748. $files = file_scan_directory($this->path, '/\.txt$/', array('nomask' => '/^c.txt/'));
  1749. $this->assertEqual(2, count($files), '2 text files found when an "nomask" option is passed in.');
  1750. // Ensure that the directories in file_scan_ignore_directories are escaped using preg_quote.
  1751. $conf['file_scan_ignore_directories'] = array('frontend.*');
  1752. $files = file_scan_directory($this->path, '/\.txt$/');
  1753. $this->assertEqual(3, count($files), '2 text files found when ignoring a directory that is not there.');
  1754. }
  1755. }
  1756. /**
  1757. * Test theme implementations declared in file_theme().
  1758. */
  1759. class FileThemeImplementationsTestCase extends DrupalUnitTestCase {
  1760. public static function getInfo() {
  1761. return array(
  1762. 'name' => 'Theme implementations declared in file_theme()',
  1763. 'description' => 'Unit tests theme functions in the file module.',
  1764. 'group' => 'File',
  1765. );
  1766. }
  1767. function testThemeFileUploadHelp() {
  1768. $variables = array(
  1769. 'description' => NULL,
  1770. 'upload_validators' => NULL,
  1771. );
  1772. $this->assertEqual('', theme_file_upload_help($variables), 'Empty string returned by theme_file_upload_help() with NULL inputs.');
  1773. }
  1774. }