@@ -0,0 +1,76 | |||
|
1 | # encoding: utf-8 | |
|
2 | # | |
|
3 | # Redmine - project management software | |
|
4 | # Copyright (C) 2006-2015 Jean-Philippe Lang | |
|
5 | # | |
|
6 | # This program is free software; you can redistribute it and/or | |
|
7 | # modify it under the terms of the GNU General Public License | |
|
8 | # as published by the Free Software Foundation; either version 2 | |
|
9 | # of the License, or (at your option) any later version. | |
|
10 | # | |
|
11 | # This program is distributed in the hope that it will be useful, | |
|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
|
14 | # GNU General Public License for more details. | |
|
15 | # | |
|
16 | # You should have received a copy of the GNU General Public License | |
|
17 | # along with this program; if not, write to the Free Software | |
|
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
|
19 | ||
|
20 | require File.expand_path('../../test_helper', __FILE__) | |
|
21 | ||
|
22 | class AttachmentTest < ActiveSupport::TestCase | |
|
23 | fixtures :users, :email_addresses, :projects, :roles, :members, :member_roles, | |
|
24 | :enabled_modules, :issues, :trackers, :attachments | |
|
25 | ||
|
26 | self.use_transactional_fixtures = false | |
|
27 | ||
|
28 | def setup | |
|
29 | set_tmp_attachments_directory | |
|
30 | end | |
|
31 | ||
|
32 | def test_rollback_after_create_should_remove_file_from_disk | |
|
33 | diskfile = nil | |
|
34 | ||
|
35 | Attachment.transaction do | |
|
36 | a = Attachment.new(:container => Issue.find(1), | |
|
37 | :file => uploaded_test_file("testfile.txt", "text/plain"), | |
|
38 | :author => User.find(1)) | |
|
39 | a.save! | |
|
40 | diskfile = a.diskfile | |
|
41 | assert File.exist?(diskfile) | |
|
42 | raise ActiveRecord::Rollback | |
|
43 | end | |
|
44 | assert !File.exist?(diskfile) | |
|
45 | end | |
|
46 | ||
|
47 | def test_destroy_should_remove_file_from_disk_after_commit | |
|
48 | a = Attachment.new(:container => Issue.find(1), | |
|
49 | :file => uploaded_test_file("testfile.txt", "text/plain"), | |
|
50 | :author => User.find(1)) | |
|
51 | a.save! | |
|
52 | diskfile = a.diskfile | |
|
53 | assert File.exist?(diskfile) | |
|
54 | ||
|
55 | Attachment.transaction do | |
|
56 | a.destroy | |
|
57 | assert File.exist?(diskfile) | |
|
58 | end | |
|
59 | assert !File.exist?(diskfile) | |
|
60 | end | |
|
61 | ||
|
62 | def test_rollback_after_destroy_should_not_remove_file_from_disk | |
|
63 | a = Attachment.new(:container => Issue.find(1), | |
|
64 | :file => uploaded_test_file("testfile.txt", "text/plain"), | |
|
65 | :author => User.find(1)) | |
|
66 | a.save! | |
|
67 | diskfile = a.diskfile | |
|
68 | assert File.exist?(diskfile) | |
|
69 | ||
|
70 | Attachment.transaction do | |
|
71 | a.destroy | |
|
72 | raise ActiveRecord::Rollback | |
|
73 | end | |
|
74 | assert File.exist?(diskfile) | |
|
75 | end | |
|
76 | end |
@@ -53,6 +53,7 class Attachment < ActiveRecord::Base | |||
|
53 | 53 | @@thumbnails_storage_path = File.join(Rails.root, "tmp", "thumbnails") |
|
54 | 54 | |
|
55 | 55 | before_create :files_to_final_location |
|
56 | after_rollback :delete_from_disk, :on => :create | |
|
56 | 57 | after_commit :delete_from_disk, :on => :destroy |
|
57 | 58 | |
|
58 | 59 | # Returns an unsaved copy of the attachment |
@@ -20,7 +20,7 | |||
|
20 | 20 | require File.expand_path('../../test_helper', __FILE__) |
|
21 | 21 | |
|
22 | 22 | class AttachmentTest < ActiveSupport::TestCase |
|
23 | fixtures :users, :projects, :roles, :members, :member_roles, | |
|
23 | fixtures :users, :email_addresses, :projects, :roles, :members, :member_roles, | |
|
24 | 24 | :enabled_modules, :issues, :trackers, :attachments |
|
25 | 25 | |
|
26 | 26 | # TODO: remove this with Rails 5 that supports after_commit callbacks |
General Comments 0
You need to be logged in to leave comments.
Login now