]> git.openstreetmap.org Git - rails.git/blob - vendor/plugins/file_column/lib/test_case.rb
Merge 14394:14533 from trunk.
[rails.git] / vendor / plugins / file_column / lib / test_case.rb
1 require 'test/unit'
2
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 
8   # file fields.
9   #
10   # === Example:
11   #
12   #    def test_file_column_post
13   #      entry = { :title => 'foo', :file => upload('/tmp/foo.txt')}
14   #      post :upload, :entry => entry
15   #  
16   #      # ...
17   #    end
18   #
19   # === Parameters
20   #
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
26       case path
27       when /\.jpg$/ then content_type = "image/jpeg"
28       when /\.png$/ then content_type = "image/png"
29       else content_type = nil
30       end
31     end
32     uploaded_file(path, content_type, File.basename(path), type)
33   end
34   
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.
40   #
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.
44   #
45   # For example, the directory structure could look like this:
46   #
47   #   test/fixtures/file_column/
48   #   `-- container
49   #       |-- first_image
50   #       |   |-- 1
51   #       |   |   `-- image1.jpg
52   #       |   `-- tmp
53   #       `-- second_image
54   #           |-- 1
55   #           |   `-- image2.jpg
56   #           `-- tmp
57   #
58   # Your fixture file for this one "container" class fixture could look like this:
59   #
60   #   first:
61   #     id:           1
62   #     first_image:  image1.jpg
63   #     second_image: image1.jpg
64   #
65   # A usage example:
66   #
67   #  def setup
68   #    setup_fixture_files
69   #
70   #    # ...
71   #  end
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", "*")
75     
76     FileUtils.mkdir_p tmp_path unless File.exists?(tmp_path)
77     FileUtils.cp_r file_fixtures, tmp_path
78   end
79   
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+
82   # method.
83   #
84   # A usage example:
85   #
86   #  def teardown
87   #    teardown_fixture_files
88   #
89   #    # ...
90   #  end
91   def teardown_fixture_files
92     FileUtils.rm_rf File.join(RAILS_ROOT, "test", "tmp", "file_column")
93   end
94   
95   private
96   
97   def uploaded_file(path, content_type, filename, type=:tempfile) # :nodoc:
98     if type == :tempfile
99       t = Tempfile.new(File.basename(filename))
100       FileUtils.copy_file(path, t.path)
101     else
102       if path
103         t = StringIO.new(IO.read(path))
104       else
105         t = StringIO.new
106       end
107     end
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}
113     end
114     return t
115   end
116 end
117
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")
124 end