3 # Add the methods +upload+, the <tt>setup_file_fixtures</tt> and
4 # <tt>teardown_file_fixtures</tt> to the class Test::Unit::TestCase.
5 class Test::Unit::TestCase
6 # Returns a +Tempfile+ object as it would have been generated on file upload.
7 # Use this method to create the parameters when emulating form posts with
12 # def test_file_column_post
13 # entry = { :title => 'foo', :file => upload('/tmp/foo.txt')}
14 # post :upload, :entry => entry
21 # * <tt>path</tt> The path to the file to upload.
22 # * <tt>content_type</tt> The MIME type of the file. If it is <tt>:guess</tt>,
23 # the method will try to guess it.
24 def upload(path, content_type=:guess, type=:tempfile)
25 if content_type == :guess
27 when /\.jpg$/ then content_type = "image/jpeg"
28 when /\.png$/ then content_type = "image/png"
29 else content_type = nil
32 uploaded_file(path, content_type, File.basename(path), type)
35 # Copies the fixture files from "Rails.root/test/fixtures/file_column" into
36 # the temporary storage directory used for testing
37 # ("Rails.root/test/tmp/file_column"). Call this method in your
38 # <tt>setup</tt> methods to get the file fixtures (images, for example) into
39 # the directory used by file_column in testing.
41 # Note that the files and directories in the "fixtures/file_column" directory
42 # must have the same structure as you would expect in your "/public" directory
43 # after uploading with FileColumn.
45 # For example, the directory structure could look like this:
47 # test/fixtures/file_column/
58 # Your fixture file for this one "container" class fixture could look like this:
62 # first_image: image1.jpg
63 # second_image: image1.jpg
72 def setup_fixture_files
73 tmp_path = File.join(Rails.root, "test", "tmp", "file_column")
74 file_fixtures = Dir.glob File.join(Rails.root, "test", "fixtures", "file_column", "*")
76 FileUtils.mkdir_p tmp_path unless File.exists?(tmp_path)
77 FileUtils.cp_r file_fixtures, tmp_path
80 # Removes the directory "Rails.root/test/tmp/file_column/" so the files
81 # copied on test startup are removed. Call this in your unit test's +teardown+
87 # teardown_fixture_files
91 def teardown_fixture_files
92 FileUtils.rm_rf File.join(Rails.root, "test", "tmp", "file_column")
97 def uploaded_file(path, content_type, filename, type=:tempfile) # :nodoc:
99 t = Tempfile.new(File.basename(filename))
100 FileUtils.copy_file(path, t.path)
103 t = StringIO.new(IO.read(path))
108 (class << t; self; end).class_eval do
109 alias local_path path if type == :tempfile
110 define_method(:local_path) { "" } if type == :stringio
111 define_method(:original_filename) {filename}
112 define_method(:content_type) {content_type}
118 # If we are running in the "test" environment, we overwrite the default
119 # settings for FileColumn so that files are not uploaded into "/public/"
120 # in tests but rather into the directory "/test/tmp/file_column".
121 if Rails.env == "test"
122 FileColumn::ClassMethods::DEFAULT_OPTIONS[:root_path] =
123 File.join(Rails.root, "test", "tmp", "file_column")