1 | require File.dirname(__FILE__) + '/abstract_unit' |
---|
2 | require File.dirname(__FILE__) + '/fixtures/entry' |
---|
3 | |
---|
4 | class UrlForFileColumnTest < Test::Unit::TestCase |
---|
5 | include FileColumnHelper |
---|
6 | |
---|
7 | def setup |
---|
8 | Entry.file_column :image |
---|
9 | @request = RequestMock.new |
---|
10 | end |
---|
11 | |
---|
12 | def test_url_for_file_column_with_temp_entry |
---|
13 | @e = Entry.new(:image => upload(f("skanthak.png"))) |
---|
14 | url = url_for_file_column("e", "image") |
---|
15 | assert_match %r{^/entry/image/tmp/\d+(\.\d+)+/skanthak.png$}, url |
---|
16 | end |
---|
17 | |
---|
18 | def test_url_for_file_column_with_saved_entry |
---|
19 | @e = Entry.new(:image => upload(f("skanthak.png"))) |
---|
20 | assert @e.save |
---|
21 | |
---|
22 | url = url_for_file_column("e", "image") |
---|
23 | assert_equal "/entry/image/#{@e.id}/skanthak.png", url |
---|
24 | end |
---|
25 | |
---|
26 | def test_url_for_file_column_works_with_symbol |
---|
27 | @e = Entry.new(:image => upload(f("skanthak.png"))) |
---|
28 | assert @e.save |
---|
29 | |
---|
30 | url = url_for_file_column(:e, :image) |
---|
31 | assert_equal "/entry/image/#{@e.id}/skanthak.png", url |
---|
32 | end |
---|
33 | |
---|
34 | def test_url_for_file_column_works_with_object |
---|
35 | e = Entry.new(:image => upload(f("skanthak.png"))) |
---|
36 | assert e.save |
---|
37 | |
---|
38 | url = url_for_file_column(e, "image") |
---|
39 | assert_equal "/entry/image/#{e.id}/skanthak.png", url |
---|
40 | end |
---|
41 | |
---|
42 | def test_url_for_file_column_should_return_nil_on_no_uploaded_file |
---|
43 | e = Entry.new |
---|
44 | assert_nil url_for_file_column(e, "image") |
---|
45 | end |
---|
46 | |
---|
47 | def test_url_for_file_column_without_extension |
---|
48 | e = Entry.new |
---|
49 | e.image = uploaded_file(file_path("kerb.jpg"), "something/unknown", "local_filename") |
---|
50 | assert e.save |
---|
51 | assert_equal "/entry/image/#{e.id}/local_filename", url_for_file_column(e, "image") |
---|
52 | end |
---|
53 | end |
---|
54 | |
---|
55 | class UrlForFileColumnTest < Test::Unit::TestCase |
---|
56 | include FileColumnHelper |
---|
57 | include ActionView::Helpers::AssetTagHelper |
---|
58 | include ActionView::Helpers::TagHelper |
---|
59 | include ActionView::Helpers::UrlHelper |
---|
60 | |
---|
61 | def setup |
---|
62 | Entry.file_column :image |
---|
63 | |
---|
64 | # mock up some request data structures for AssetTagHelper |
---|
65 | @request = RequestMock.new |
---|
66 | @request.relative_url_root = "/foo/bar" |
---|
67 | @controller = self |
---|
68 | end |
---|
69 | |
---|
70 | def request |
---|
71 | @request |
---|
72 | end |
---|
73 | |
---|
74 | IMAGE_URL = %r{^/foo/bar/entry/image/.+/skanthak.png$} |
---|
75 | def test_with_image_tag |
---|
76 | e = Entry.new(:image => upload(f("skanthak.png"))) |
---|
77 | html = image_tag url_for_file_column(e, "image") |
---|
78 | url = html.scan(/src=\"(.+)\"/).first.first |
---|
79 | |
---|
80 | assert_match IMAGE_URL, url |
---|
81 | end |
---|
82 | |
---|
83 | def test_with_link_to_tag |
---|
84 | e = Entry.new(:image => upload(f("skanthak.png"))) |
---|
85 | html = link_to "Download", url_for_file_column(e, "image", :absolute => true) |
---|
86 | url = html.scan(/href=\"(.+)\"/).first.first |
---|
87 | |
---|
88 | assert_match IMAGE_URL, url |
---|
89 | end |
---|
90 | |
---|
91 | def test_relative_url_root_not_modified |
---|
92 | e = Entry.new(:image => upload(f("skanthak.png"))) |
---|
93 | url_for_file_column(e, "image", :absolute => true) |
---|
94 | |
---|
95 | assert_equal "/foo/bar", @request.relative_url_root |
---|
96 | end |
---|
97 | end |
---|