comparison test/unit/attachment_test.rb @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents bb32da3bea34 622f24f53b42
children
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
1 # encoding: utf-8 1 # encoding: utf-8
2 # 2 #
3 # Redmine - project management software 3 # Redmine - project management software
4 # Copyright (C) 2006-2012 Jean-Philippe Lang 4 # Copyright (C) 2006-2013 Jean-Philippe Lang
5 # 5 #
6 # This program is free software; you can redistribute it and/or 6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License 7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2 8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version. 9 # of the License, or (at your option) any later version.
50 assert_equal 'testfile.txt', a.filename 50 assert_equal 'testfile.txt', a.filename
51 assert_equal 59, a.filesize 51 assert_equal 59, a.filesize
52 assert_equal 'text/plain', a.content_type 52 assert_equal 'text/plain', a.content_type
53 assert_equal 0, a.downloads 53 assert_equal 0, a.downloads
54 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest 54 assert_equal '1478adae0d4eb06d35897518540e25d6', a.digest
55
56 assert a.disk_directory
57 assert_match %r{\A\d{4}/\d{2}\z}, a.disk_directory
58
55 assert File.exist?(a.diskfile) 59 assert File.exist?(a.diskfile)
56 assert_equal 59, File.size(a.diskfile) 60 assert_equal 59, File.size(a.diskfile)
61 end
62
63 def test_copy_should_preserve_attributes
64 a = Attachment.find(1)
65 copy = a.copy
66
67 assert_save copy
68 copy = Attachment.order('id DESC').first
69 %w(filename filesize content_type author_id created_on description digest disk_filename disk_directory diskfile).each do |attribute|
70 assert_equal a.send(attribute), copy.send(attribute), "#{attribute} was different"
71 end
57 end 72 end
58 73
59 def test_size_should_be_validated_for_new_file 74 def test_size_should_be_validated_for_new_file
60 with_settings :attachment_max_size => 0 do 75 with_settings :attachment_max_size => 0 do
61 a = Attachment.new(:container => Issue.find(1), 76 a = Attachment.new(:container => Issue.find(1),
166 assert_difference 'Attachment.count', -2 do 181 assert_difference 'Attachment.count', -2 do
167 Attachment.prune 182 Attachment.prune
168 end 183 end
169 end 184 end
170 185
171 context "Attachmnet.attach_files" do 186 def test_move_from_root_to_target_directory_should_move_root_files
172 should "attach the file" do 187 a = Attachment.find(20)
173 issue = Issue.first 188 assert a.disk_directory.blank?
174 assert_difference 'Attachment.count' do 189 # Create a real file for this fixture
175 Attachment.attach_files(issue, 190 File.open(a.diskfile, "w") do |f|
176 '1' => { 191 f.write "test file at the root of files directory"
177 'file' => uploaded_test_file('testfile.txt', 'text/plain'), 192 end
178 'description' => 'test' 193 assert a.readable?
179 }) 194 Attachment.move_from_root_to_target_directory
180 end 195
181 196 a.reload
182 attachment = Attachment.first(:order => 'id DESC') 197 assert_equal '2012/05', a.disk_directory
183 assert_equal issue, attachment.container 198 assert a.readable?
184 assert_equal 'testfile.txt', attachment.filename 199 end
185 assert_equal 59, attachment.filesize 200
186 assert_equal 'test', attachment.description 201 test "Attachmnet.attach_files should attach the file" do
187 assert_equal 'text/plain', attachment.content_type 202 issue = Issue.first
188 assert File.exists?(attachment.diskfile) 203 assert_difference 'Attachment.count' do
189 assert_equal 59, File.size(attachment.diskfile) 204 Attachment.attach_files(issue,
190 end 205 '1' => {
191 206 'file' => uploaded_test_file('testfile.txt', 'text/plain'),
192 should "add unsaved files to the object as unsaved attachments" do 207 'description' => 'test'
193 # Max size of 0 to force Attachment creation failures 208 })
194 with_settings(:attachment_max_size => 0) do 209 end
195 @project = Project.find(1) 210
196 response = Attachment.attach_files(@project, { 211 attachment = Attachment.first(:order => 'id DESC')
197 '1' => {'file' => mock_file, 'description' => 'test'}, 212 assert_equal issue, attachment.container
198 '2' => {'file' => mock_file, 'description' => 'test'} 213 assert_equal 'testfile.txt', attachment.filename
199 }) 214 assert_equal 59, attachment.filesize
200 215 assert_equal 'test', attachment.description
201 assert response[:unsaved].present? 216 assert_equal 'text/plain', attachment.content_type
202 assert_equal 2, response[:unsaved].length 217 assert File.exists?(attachment.diskfile)
203 assert response[:unsaved].first.new_record? 218 assert_equal 59, File.size(attachment.diskfile)
204 assert response[:unsaved].second.new_record? 219 end
205 assert_equal response[:unsaved], @project.unsaved_attachments 220
206 end 221 test "Attachmnet.attach_files should add unsaved files to the object as unsaved attachments" do
222 # Max size of 0 to force Attachment creation failures
223 with_settings(:attachment_max_size => 0) do
224 @project = Project.find(1)
225 response = Attachment.attach_files(@project, {
226 '1' => {'file' => mock_file, 'description' => 'test'},
227 '2' => {'file' => mock_file, 'description' => 'test'}
228 })
229
230 assert response[:unsaved].present?
231 assert_equal 2, response[:unsaved].length
232 assert response[:unsaved].first.new_record?
233 assert response[:unsaved].second.new_record?
234 assert_equal response[:unsaved], @project.unsaved_attachments
207 end 235 end
208 end 236 end
209 237
210 def test_latest_attach 238 def test_latest_attach
211 set_fixtures_attachments_directory 239 set_fixtures_attachments_directory