3 class ApplicationHelperTest < ActionView::TestCase
4 attr_accessor :current_user
15 %w[http://example.com/test ftp://example.com/test https://example.com/test].each do |link|
16 text = "Test #{link} is made into a link"
19 assert_equal false, html.html_safe?
20 assert_dom_equal "Test <a href=\"#{link}\" rel=\"nofollow\">#{link}</a> is made into a link", html
22 html = linkify(text.html_safe)
23 assert_equal true, html.html_safe?
24 assert_dom_equal "Test <a href=\"#{link}\" rel=\"nofollow\">#{link}</a> is made into a link", html
27 %w[test@example.com mailto:test@example.com].each do |link|
28 text = "Test #{link} is not made into a link"
31 assert_equal false, html.html_safe?
32 assert_dom_equal text, html
34 html = linkify(text.html_safe)
35 assert_equal true, html.html_safe?
36 assert_dom_equal text, html
41 link = rss_link_to(:controller => :diary_entry, :action => :rss)
42 assert_dom_equal "<a class=\"rsssmall\" href=\"/diary/rss\"><img alt=\"Rss\" border=\"0\" height=\"16\" src=\"/images/RSS.png\" width=\"16\" /></a>", link
46 link = atom_link_to(:controller => :changeset, :action => :feed)
47 assert_dom_equal "<a class=\"rsssmall\" href=\"/history/feed\"><img alt=\"Rss\" border=\"0\" height=\"16\" src=\"/images/RSS.png\" width=\"16\" /></a>", link
51 self.current_user = nil
54 assert_match /\.hidden /, css
55 assert_match /\.hide_unless_logged_in /, css
56 assert_no_match /\.hide_if_logged_in /, css
57 assert_no_match /\.hide_if_user_/, css
58 assert_no_match /\.show_if_user_/, css
59 assert_match /\.hide_unless_administrator /, css
60 assert_match /\.hide_unless_moderator /, css
62 self.current_user = create(:user)
65 assert_match /\.hidden /, css
66 assert_no_match /\.hide_unless_logged_in /, css
67 assert_match /\.hide_if_logged_in /, css
68 assert_match /\.hide_if_user_#{current_user.id} /, css
69 assert_match /\.show_if_user_#{current_user.id} /, css
70 assert_match /\.hide_unless_administrator /, css
71 assert_match /\.hide_unless_moderator /, css
73 self.current_user = create(:moderator_user)
76 assert_match /\.hidden /, css
77 assert_no_match /\.hide_unless_logged_in /, css
78 assert_match /\.hide_if_logged_in /, css
79 assert_match /\.hide_if_user_#{current_user.id} /, css
80 assert_match /\.show_if_user_#{current_user.id} /, css
81 assert_match /\.hide_unless_administrator /, css
82 assert_no_match /\.hide_unless_moderator /, css
84 self.current_user = create(:administrator_user)
87 assert_match /\.hidden /, css
88 assert_no_match /\.hide_unless_logged_in /, css
89 assert_match /\.hide_if_logged_in /, css
90 assert_match /\.hide_if_user_#{current_user.id} /, css
91 assert_match /\.show_if_user_#{current_user.id} /, css
92 assert_no_match /\.hide_unless_administrator /, css
93 assert_match /\.hide_unless_moderator /, css
97 html = if_logged_in { "Test 1" }
98 assert_dom_equal "<div class=\"hide_unless_logged_in\">Test 1</div>", html
100 html = if_logged_in(:span) { "Test 2" }
101 assert_dom_equal "<span class=\"hide_unless_logged_in\">Test 2</span>", html
104 def test_if_not_logged_in
105 html = if_not_logged_in { "Test 1" }
106 assert_dom_equal "<div class=\"hide_if_logged_in\">Test 1</div>", html
108 html = if_not_logged_in(:span) { "Test 2" }
109 assert_dom_equal "<span class=\"hide_if_logged_in\">Test 2</span>", html
115 html = if_user(user) { "Test 1" }
116 assert_dom_equal "<div class=\"hidden show_if_user_#{user.id}\">Test 1</div>", html
118 html = if_user(user, :span) { "Test 2" }
119 assert_dom_equal "<span class=\"hidden show_if_user_#{user.id}\">Test 2</span>", html
121 html = if_user(nil) { "Test 3" }
124 html = if_user(nil, :span) { "Test 4" }
131 html = unless_user(user) { "Test 1" }
132 assert_dom_equal "<div class=\"hide_if_user_#{user.id}\">Test 1</div>", html
134 html = unless_user(user, :span) { "Test 2" }
135 assert_dom_equal "<span class=\"hide_if_user_#{user.id}\">Test 2</span>", html
137 html = unless_user(nil) { "Test 3" }
138 assert_dom_equal "<div>Test 3</div>", html
140 html = unless_user(nil, :span) { "Test 4" }
141 assert_dom_equal "<span>Test 4</span>", html
144 def test_if_administrator
145 html = if_administrator { "Test 1" }
146 assert_dom_equal "<div class=\"hide_unless_administrator\">Test 1</div>", html
148 html = if_administrator(:span) { "Test 2" }
149 assert_dom_equal "<span class=\"hide_unless_administrator\">Test 2</span>", html
152 def test_richtext_area
153 html = richtext_area(:message, :body, :cols => 40, :rows => 20)
158 assert_equal "ltr", dir
161 assert_equal "rtl", dir
166 assert_equal "rtl", dir
169 assert_equal "ltr", dir
173 def test_friendly_date
174 date = friendly_date(Time.new(2014, 3, 5, 18, 58, 23))
175 assert_match %r{^<span title=" *5 March 2014 at 18:58">.*</span>$}, date
177 date = friendly_date(Time.now - 1.hour)
178 assert_match %r{^<span title=".*">about 1 hour</span>$}, date
180 date = friendly_date(Time.now - 2.days)
181 assert_match %r{^<span title=".*">2 days</span>$}, date
183 date = friendly_date(Time.now - 3.weeks)
184 assert_match %r{^<span title=".*">21 days</span>$}, date
186 date = friendly_date(Time.now - 4.months)
187 assert_match %r{^<span title=".*">4 months</span>$}, date
190 def test_body_class; end
192 def test_current_page_class; end