You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

attachment_transaction_test.rb 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require_relative '../test_helper'
  19. class AttachmentTest < ActiveSupport::TestCase
  20. fixtures :users, :email_addresses, :projects, :roles, :members, :member_roles,
  21. :enabled_modules, :issues, :trackers, :attachments
  22. self.use_transactional_tests = false
  23. def setup
  24. User.current = nil
  25. set_tmp_attachments_directory
  26. end
  27. def test_rollback_after_create_should_remove_file_from_disk
  28. diskfile = nil
  29. Attachment.transaction do
  30. a = Attachment.new(:container => Issue.find(1),
  31. :file => uploaded_test_file("testfile.txt", "text/plain"),
  32. :author => User.find(1))
  33. a.save!
  34. diskfile = a.diskfile
  35. assert File.exist?(diskfile)
  36. raise ActiveRecord::Rollback
  37. end
  38. assert !File.exist?(diskfile)
  39. end
  40. def test_destroy_should_remove_file_from_disk_after_commit
  41. a = Attachment.new(:container => Issue.find(1),
  42. :file => uploaded_test_file("testfile.txt", "text/plain"),
  43. :author => User.find(1))
  44. a.save!
  45. diskfile = a.diskfile
  46. assert File.exist?(diskfile)
  47. Attachment.transaction do
  48. a.destroy
  49. assert File.exist?(diskfile)
  50. end
  51. assert !File.exist?(diskfile)
  52. end
  53. def test_rollback_after_destroy_should_not_remove_file_from_disk
  54. a = Attachment.new(:container => Issue.find(1),
  55. :file => uploaded_test_file("testfile.txt", "text/plain"),
  56. :author => User.find(1))
  57. a.save!
  58. diskfile = a.diskfile
  59. assert File.exist?(diskfile)
  60. Attachment.transaction do
  61. a.destroy
  62. raise ActiveRecord::Rollback
  63. end
  64. assert File.exist?(diskfile)
  65. end
  66. end