3 class DiaryEntryControllerTest < ActionController::TestCase
4 fixtures :users, :diary_entries, :diary_comments, :languages
6 include ActionView::Helpers::NumberHelper
9 # test all routes which lead to this controller
12 { :path => "/diary", :method => :get },
13 { :controller => "diary_entry", :action => "list" }
16 { :path => "/diary/language", :method => :get },
17 { :controller => "diary_entry", :action => "list", :language => "language" }
20 { :path => "/user/username/diary", :method => :get },
21 { :controller => "diary_entry", :action => "list", :display_name => "username" }
24 { :path => "/diary/friends", :method => :get },
25 { :controller => "diary_entry", :action => "list", :friends => true }
28 { :path => "/diary/nearby", :method => :get },
29 { :controller => "diary_entry", :action => "list", :nearby => true }
33 { :path => "/diary/rss", :method => :get },
34 { :controller => "diary_entry", :action => "rss", :format => :rss }
37 { :path => "/diary/language/rss", :method => :get },
38 { :controller => "diary_entry", :action => "rss", :language => "language", :format => :rss }
41 { :path => "/user/username/diary/rss", :method => :get },
42 { :controller => "diary_entry", :action => "rss", :display_name => "username", :format => :rss }
46 { :path => "/user/username/diary/comments", :method => :get },
47 { :controller => "diary_entry", :action => "comments", :display_name => "username" }
50 { :path => "/user/username/diary/comments/1", :method => :get },
51 { :controller => "diary_entry", :action => "comments", :display_name => "username", :page => "1" }
55 { :path => "/diary/new", :method => :get },
56 { :controller => "diary_entry", :action => "new" }
59 { :path => "/diary/new", :method => :post },
60 { :controller => "diary_entry", :action => "new" }
63 { :path => "/user/username/diary/1", :method => :get },
64 { :controller => "diary_entry", :action => "view", :display_name => "username", :id => "1" }
67 { :path => "/user/username/diary/1/edit", :method => :get },
68 { :controller => "diary_entry", :action => "edit", :display_name => "username", :id => "1" }
71 { :path => "/user/username/diary/1/edit", :method => :post },
72 { :controller => "diary_entry", :action => "edit", :display_name => "username", :id => "1" }
75 { :path => "/user/username/diary/1/newcomment", :method => :post },
76 { :controller => "diary_entry", :action => "comment", :display_name => "username", :id => "1" }
79 { :path => "/user/username/diary/1/hide", :method => :post },
80 { :controller => "diary_entry", :action => "hide", :display_name => "username", :id => "1" }
83 { :path => "/user/username/diary/1/hidecomment/2", :method => :post },
84 { :controller => "diary_entry", :action => "hidecomment", :display_name => "username", :id => "1", :comment => "2" }
88 def test_showing_new_diary_entry
90 assert_response :redirect
91 assert_redirected_to :controller => :user, :action => "login", :referer => "/diary/new"
92 # Now pretend to login by using the session hash, with the
93 # id of the person we want to login as through session(:user)=user.id
94 get(:new, nil, "user" => users(:normal_user).id)
95 assert_response :success
96 # print @response.body
98 # print @response.to_yaml
99 assert_select "title", :text => /New Diary Entry/, :count => 1
100 assert_select "div.content-heading", :count => 1 do
101 assert_select "h1", :text => "New Diary Entry", :count => 1
103 assert_select "div#content", :count => 1 do
104 # We don't care about the layout, we just care about the form fields
106 assert_select "form[action='/diary/new']", :count => 1 do
107 assert_select "input[id=diary_entry_title][name='diary_entry[title]']", :count => 1
108 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :count => 1
109 assert_select "input#latitude[name='diary_entry[latitude]'][type=text]", :count => 1
110 assert_select "input#longitude[name='diary_entry[longitude]'][type=text]", :count => 1
111 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
116 def test_editing_diary_entry
117 entry = diary_entries(:normal_user_entry_1)
119 # Make sure that you are redirected to the login page when you are
120 # not logged in, without and with the id of the entry you want to edit
121 get :edit, :display_name => entry.user.display_name, :id => entry.id
122 assert_response :redirect
123 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/#{entry.user.display_name}/diary/#{entry.id}/edit"
125 # Verify that you get a not found error, when you pass a bogus id
126 get(:edit, { :display_name => entry.user.display_name, :id => 9999 }, { "user" => entry.user.id })
127 assert_response :not_found
128 assert_select "div.content-heading", :count => 1 do
129 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
132 # Now pass the id, and check that you can edit it, when using the same
133 # user as the person who created the entry
134 get(:edit, { :display_name => entry.user.display_name, :id => entry.id }, { "user" => entry.user.id })
135 assert_response :success
136 assert_select "title", :text => /Edit diary entry/, :count => 1
137 assert_select "div.content-heading", :count => 1 do
138 assert_select "h1", :text => /Edit diary entry/, :count => 1
140 assert_select "div#content", :count => 1 do
141 assert_select "form[action='/user/#{entry.user.display_name}/diary/#{entry.id}/edit'][method=post]", :count => 1 do
142 assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{entry.title}']", :count => 1
143 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => entry.body, :count => 1
144 assert_select "select#diary_entry_language_code", :count => 1
145 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
146 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
147 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
148 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
149 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
150 assert_select "input", :count => 7
154 # Now lets see if you can edit the diary entry
155 new_title = "New Title"
156 new_body = "This is a new body for the diary entry"
158 new_longitude = "2.2"
159 new_language_code = "en"
160 post(:edit, { :display_name => entry.user.display_name, :id => entry.id, "commit" => "save",
161 "diary_entry" => { "title" => new_title, "body" => new_body, "latitude" => new_latitude,
162 "longitude" => new_longitude, "language_code" => new_language_code } },
163 { "user" => entry.user.id })
164 assert_response :redirect
165 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
167 # Now check that the new data is rendered, when logged in
168 get :view, { :display_name => entry.user.display_name, :id => entry.id }, { "user" => entry.user.id }
169 assert_response :success
170 assert_template "diary_entry/view"
171 assert_select "title", :text => /Users' diaries | /, :count => 1
172 assert_select "div.content-heading", :count => 1 do
173 assert_select "h2", :text => /#{entry.user.display_name}'s diary/, :count => 1
175 assert_select "div#content", :count => 1 do
176 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
177 # This next line won't work if the text has been run through the htmlize function
178 # due to formatting that could be introduced
179 assert_select "p", :text => /#{new_body}/, :count => 1
180 assert_select "abbr[class='geo'][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
181 # As we're not logged in, check that you cannot edit
182 # print @response.body
183 assert_select "a[href='/user/#{entry.user.display_name}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
186 # and when not logged in as the user who wrote the entry
187 get :view, { :display_name => entry.user.display_name, :id => entry.id }, { "user" => entry.user.id }
188 assert_response :success
189 assert_template "diary_entry/view"
190 assert_select "title", :text => /Users' diaries | /, :count => 1
191 assert_select "div.content-heading", :count => 1 do
192 assert_select "h2", :text => /#{users(:normal_user).display_name}'s diary/, :count => 1
194 assert_select "div#content", :count => 1 do
195 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
196 # This next line won't work if the text has been run through the htmlize function
197 # due to formatting that could be introduced
198 assert_select "p", :text => /#{new_body}/, :count => 1
199 assert_select "abbr[class=geo][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1
200 # As we're not logged in, check that you cannot edit
201 assert_select "li[class='hidden show_if_user_#{entry.user.id}']", :count => 1 do
202 assert_select "a[href='/user/#{entry.user.display_name}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
207 def test_edit_diary_entry_i18n
208 get :edit, { :display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id }, { "user" => users(:normal_user).id }
209 assert_response :success
210 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
213 def test_create_diary_entry
214 # Make sure that you are redirected to the login page when you
217 assert_response :redirect
218 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/new"
220 # Now try again when logged in
221 get :new, {}, { :user => users(:normal_user).id }
222 assert_response :success
223 assert_select "title", :text => /New Diary Entry/, :count => 1
224 assert_select "div.content-heading", :count => 1 do
225 assert_select "h1", :text => /New Diary Entry/, :count => 1
227 assert_select "div#content", :count => 1 do
228 assert_select "form[action='/diary/new'][method=post]", :count => 1 do
229 assert_select "input#diary_entry_title[name='diary_entry[title]']", :count => 1
230 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => "", :count => 1
231 assert_select "select#diary_entry_language_code", :count => 1
232 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
233 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
234 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
235 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
236 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
237 assert_select "input", :count => 7
241 # Now try creating a diary entry
242 new_title = "New Title"
243 new_body = "This is a new body for the diary entry"
245 new_longitude = "2.2"
246 new_language_code = "en"
247 assert_difference "DiaryEntry.count", 1 do
248 post(:new, { "commit" => "save",
249 "diary_entry" => { "title" => new_title, "body" => new_body, "latitude" => new_latitude,
250 "longitude" => new_longitude, "language_code" => new_language_code } },
251 { :user => users(:normal_user).id })
253 assert_response :redirect
254 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
255 entry = DiaryEntry.find(6)
256 assert_equal users(:normal_user).id, entry.user_id
257 assert_equal new_title, entry.title
258 assert_equal new_body, entry.body
259 assert_equal new_latitude.to_f, entry.latitude
260 assert_equal new_longitude.to_f, entry.longitude
261 assert_equal new_language_code, entry.language_code
264 def test_creating_diary_comment
265 entry = diary_entries(:normal_user_entry_1)
267 # Make sure that you are denied when you are not logged in
268 post :comment, :display_name => entry.user.display_name, :id => entry.id
269 assert_response :forbidden
271 # Verify that you get a not found error, when you pass a bogus id
272 post :comment, { :display_name => entry.user.display_name, :id => 9999 }, { :user => users(:public_user).id }
273 assert_response :not_found
274 assert_select "div.content-heading", :count => 1 do
275 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
278 # Now try again with the right id
279 assert_difference "ActionMailer::Base.deliveries.size", 1 do
280 assert_difference "DiaryComment.count", 1 do
281 post :comment, { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "New comment" } }, { :user => users(:public_user).id }
284 assert_response :redirect
285 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
286 email = ActionMailer::Base.deliveries.first
287 assert_equal [users(:normal_user).email], email.to
288 assert_equal "[OpenStreetMap] #{users(:public_user).display_name} commented on your diary entry", email.subject
289 assert_match /New comment/, email.text_part.decoded
290 assert_match /New comment/, email.html_part.decoded
291 ActionMailer::Base.deliveries.clear
292 comment = DiaryComment.find(5)
293 assert_equal entry.id, comment.diary_entry_id
294 assert_equal users(:public_user).id, comment.user_id
295 assert_equal "New comment", comment.body
297 # Now view the diary entry, and check the new comment is present
298 get :view, :display_name => entry.user.display_name, :id => entry.id
299 assert_response :success
300 assert_select ".diary-comment", :count => 1 do
301 assert_select "#comment5", :count => 1 do
302 assert_select "a[href='/user/#{users(:public_user).display_name}']", :text => users(:public_user).display_name, :count => 1
304 assert_select ".richtext", :text => /New comment/, :count => 1
308 # Check that you can get the expected response and template for all available languages
309 # Should test that there are no <span class="translation_missing">
310 def test_listing_diary_entries
312 assert_response :success, "Should be able to list the diary entries in locale"
313 assert_template "list", "Should use the list template in locale"
314 assert_select "span[class=translation_missing]", false, "Missing translation in list of diary entries"
316 # Now try to find a specific user's diary entry
317 get :list, :display_name => users(:normal_user).display_name
318 assert_response :success, "Should be able to list the diary entries for a user in locale"
319 assert_template "list", "Should use the list template for a user in locale"
320 assert_no_missing_translations
324 get :rss, :format => :rss
325 assert_response :success, "Should be able to get a diary RSS"
326 assert_select "rss", :count => 1 do
327 assert_select "channel", :count => 1 do
328 assert_select "channel>title", :count => 1
329 assert_select "image", :count => 1
330 assert_select "channel>item", :count => 2
335 def test_rss_language
336 get :rss, :language => diary_entries(:normal_user_entry_1).language_code, :format => :rss
337 assert_response :success, "Should be able to get a specific language diary RSS"
338 assert_select "rss>channel>item", :count => 1 # , "Diary entries should be filtered by language"
341 # def test_rss_nonexisting_language
342 # get :rss, {:language => 'xx', :format => :rss}
343 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
346 def test_rss_language_with_no_entries
347 get :rss, :language => "sl", :format => :rss
348 assert_response :success, "Should be able to get a specific language diary RSS"
349 assert_select "rss>channel>item", :count => 0 # , "Diary entries should be filtered by language"
353 get :rss, :display_name => users(:normal_user).display_name, :format => :rss
354 assert_response :success, "Should be able to get a specific users diary RSS"
355 assert_select "rss>channel>item", :count => 2 # , "Diary entries should be filtered by user"
358 def test_rss_nonexisting_user
359 # Try a user that has never existed
360 get :rss, :display_name => "fakeUsername76543", :format => :rss
361 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
363 # Try a suspended user
364 get :rss, :display_name => users(:suspended_user).display_name, :format => :rss
365 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
368 get :rss, :display_name => users(:deleted_user).display_name, :format => :rss
369 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
372 def test_viewing_diary_entry
373 # Try a normal entry that should work
374 get :view, :display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id
375 assert_response :success
376 assert_template :view
378 # Try a deleted entry
379 get :view, :display_name => users(:normal_user).display_name, :id => diary_entries(:deleted_entry).id
380 assert_response :not_found
382 # Try an entry by a suspended user
383 get :view, :display_name => users(:suspended_user).display_name, :id => diary_entries(:entry_by_suspended_user).id
384 assert_response :not_found
386 # Try an entry by a deleted user
387 get :view, :display_name => users(:deleted_user).display_name, :id => diary_entries(:entry_by_deleted_user).id
388 assert_response :not_found
391 def test_viewing_hidden_comments
392 # Get a diary entry that has hidden comments
393 get :view, :display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_geo_entry).id
394 assert_response :success
395 assert_template :view
396 assert_select "div.comments" do
397 assert_select "p#comment1", :count => 1 # visible comment
398 assert_select "p#comment2", :count => 0 # comment by suspended user
399 assert_select "p#comment3", :count => 0 # comment by deleted user
400 assert_select "p#comment4", :count => 0 # hidden comment
405 # Try without logging in
406 post :hide, :display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id
407 assert_response :forbidden
408 assert_equal true, DiaryEntry.find(diary_entries(:normal_user_entry_1).id).visible
410 # Now try as a normal user
411 post :hide, { :display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id }, { :user => users(:normal_user).id }
412 assert_response :redirect
413 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id
414 assert_equal true, DiaryEntry.find(diary_entries(:normal_user_entry_1).id).visible
416 # Finally try as an administrator
417 post :hide, { :display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id }, { :user => users(:administrator_user).id }
418 assert_response :redirect
419 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
420 assert_equal false, DiaryEntry.find(diary_entries(:normal_user_entry_1).id).visible
424 # Try without logging in
425 post :hidecomment, :display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_geo_entry).id, :comment => diary_comments(:comment_for_geo_post).id
426 assert_response :forbidden
427 assert_equal true, DiaryComment.find(diary_comments(:comment_for_geo_post).id).visible
429 # Now try as a normal user
430 post :hidecomment, { :display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_geo_entry).id, :comment => diary_comments(:comment_for_geo_post).id }, { :user => users(:normal_user).id }
431 assert_response :redirect
432 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_geo_entry).id
433 assert_equal true, DiaryComment.find(diary_comments(:comment_for_geo_post).id).visible
435 # Finally try as an administrator
436 post :hidecomment, { :display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_geo_entry).id, :comment => diary_comments(:comment_for_geo_post).id }, { :user => users(:administrator_user).id }
437 assert_response :redirect
438 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_geo_entry).id
439 assert_equal false, DiaryComment.find(diary_comments(:comment_for_geo_post).id).visible
443 # Test a user with no comments
444 get :comments, :display_name => users(:normal_user).display_name
445 assert_response :success
446 assert_template :comments
447 assert_select "table.messages" do
448 assert_select "tr", :count => 1 # header, no comments
451 # Test a user with a comment
452 get :comments, :display_name => users(:public_user).display_name
453 assert_response :success
454 assert_template :comments
455 assert_select "table.messages" do
456 assert_select "tr", :count => 2 # header and one comment
459 # Test a suspended user
460 get :comments, :display_name => users(:suspended_user).display_name
461 assert_response :not_found
463 # Test a deleted user
464 get :comments, :display_name => users(:deleted_user).display_name
465 assert_response :not_found