1 require File.dirname(__FILE__) + '/../test_helper'
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
98 #print @response.to_yaml
99 assert_select "html", :count => 1 do
100 assert_select "head", :count => 1 do
101 assert_select "title", :text => /New Diary Entry/, :count => 1
103 assert_select "body", :count => 1 do
104 assert_select "div.wrapper", :count => 1 do
105 assert_select "div.content-heading", :count => 1 do
106 assert_select "h1", :text => "New Diary Entry", :count => 1
108 assert_select "div#content", :count => 1 do
109 # We don't care about the layout, we just care about the form fields
111 assert_select "form[action='/diary/new']", :count => 1 do
112 assert_select "input[id=diary_entry_title][name='diary_entry[title]']", :count => 1
113 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :count => 1
114 assert_select "input#latitude[name='diary_entry[latitude]'][type=text]", :count => 1
115 assert_select "input#longitude[name='diary_entry[longitude]'][type=text]", :count => 1
116 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
125 def test_editing_diary_entry
126 entry = diary_entries(:normal_user_entry_1)
128 # Make sure that you are redirected to the login page when you are
129 # not logged in, without and with the id of the entry you want to edit
130 get :edit, :display_name => entry.user.display_name, :id => entry.id
131 assert_response :redirect
132 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/#{entry.user.display_name}/diary/#{entry.id}/edit"
134 # Verify that you get a not found error, when you pass a bogus id
135 get(:edit, {:display_name => entry.user.display_name, :id => 9999}, {'user' => entry.user.id})
136 assert_response :not_found
137 assert_select "html", :count => 1 do
138 assert_select "body", :count => 1 do
139 assert_select "div.wrapper", :count => 1 do
140 assert_select "div.content-heading", :count => 1 do
141 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
147 # Now pass the id, and check that you can edit it, when using the same
148 # user as the person who created the entry
149 get(:edit, {:display_name => entry.user.display_name, :id => entry.id}, {'user' => entry.user.id})
150 assert_response :success
151 assert_select "html", :count => 1 do
152 assert_select "head", :count => 1 do
153 assert_select "title", :text => /Edit diary entry/, :count => 1
155 assert_select "body", :count => 1 do
156 assert_select "div.wrapper", :count => 1 do
157 assert_select "div.content-heading", :count => 1 do
158 assert_select "h1", :text => /Edit diary entry/, :count => 1
160 assert_select "div#content", :count => 1 do
161 assert_select "form[action='/user/#{entry.user.display_name}/diary/#{entry.id}/edit'][method=post]", :count => 1 do
162 assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{entry.title}']", :count => 1
163 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => entry.body, :count => 1
164 assert_select "select#diary_entry_language_code", :count => 1
165 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
166 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
167 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
168 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
169 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
170 assert_select "input", :count => 7
177 # Now lets see if you can edit the diary entry
178 new_title = "New Title"
179 new_body = "This is a new body for the diary entry"
181 new_longitude = "2.2"
182 new_language_code = "en"
183 post(:edit, {:display_name => entry.user.display_name, :id => entry.id, 'commit' => 'save',
184 'diary_entry'=>{'title' => new_title, 'body' => new_body, 'latitude' => new_latitude,
185 'longitude' => new_longitude, 'language_code' => new_language_code} },
186 {'user' => entry.user.id})
187 assert_response :redirect
188 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
190 # Now check that the new data is rendered, when logged in
191 get :view, {:display_name => entry.user.display_name, :id => entry.id}, {'user' => entry.user.id}
192 assert_response :success
193 assert_template 'diary_entry/view'
194 assert_select "html", :count => 1 do
195 assert_select "head", :count => 1 do
196 assert_select "title", :text => /Users' diaries | /, :count => 1
198 assert_select "body", :count => 1 do
199 assert_select "div.wrapper", :count => 1 do
200 assert_select "div.content-heading", :count => 1 do
201 assert_select "h2", :text => /#{entry.user.display_name}'s diary/, :count => 1
203 assert_select "div#content", :count => 1 do
204 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
205 # This next line won't work if the text has been run through the htmlize function
206 # due to formatting that could be introduced
207 assert_select "p", :text => /#{new_body}/, :count => 1
208 assert_select "abbr[class=geo][title=#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}]", :count => 1
209 # As we're not logged in, check that you cannot edit
210 #print @response.body
211 assert_select "a[href='/user/#{entry.user.display_name}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
217 # and when not logged in as the user who wrote the entry
218 get :view, {:display_name => entry.user.display_name, :id => entry.id}, {'user' => entry.user.id}
219 assert_response :success
220 assert_template 'diary_entry/view'
221 assert_select "html", :count => 1 do
222 assert_select "head", :count => 1 do
223 assert_select "title", :text => /Users' diaries | /, :count => 1
225 assert_select "body", :count => 1 do
226 assert_select "div.wrapper", :count => 1 do
227 assert_select "div.content-heading", :count => 1 do
228 assert_select "h2", :text => /#{users(:normal_user).display_name}'s diary/, :count => 1
230 assert_select "div#content", :count => 1 do
231 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
232 # This next line won't work if the text has been run through the htmlize function
233 # due to formatting that could be introduced
234 assert_select "p", :text => /#{new_body}/, :count => 1
235 assert_select "abbr[class=geo][title=#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}]", :count => 1
236 # As we're not logged in, check that you cannot edit
237 assert_select "li[class=hidden show_if_user_#{entry.user.id}]", :count => 1 do
238 assert_select "a[href='/user/#{entry.user.display_name}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
244 #print @response.body
248 def test_edit_diary_entry_i18n
249 get :edit, {:display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id}, {'user' => users(:normal_user).id}
250 assert_response :success
251 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
254 def test_create_diary_entry
255 # Make sure that you are redirected to the login page when you
258 assert_response :redirect
259 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/new"
261 # Now try again when logged in
262 get :new, {}, {:user => users(:normal_user).id}
263 assert_response :success
264 assert_select "html", :count => 1 do
265 assert_select "head", :count => 1 do
266 assert_select "title", :text => /New Diary Entry/, :count => 1
268 assert_select "body", :count => 1 do
269 assert_select "div.wrapper", :count => 1 do
270 assert_select "div.content-heading", :count => 1 do
271 assert_select "h1", :text => /New Diary Entry/, :count => 1
273 assert_select "div#content", :count => 1 do
274 assert_select "form[action='/diary/new'][method=post]", :count => 1 do
275 assert_select "input#diary_entry_title[name='diary_entry[title]']", :count => 1
276 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => "", :count => 1
277 assert_select "select#diary_entry_language_code", :count => 1
278 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
279 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
280 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
281 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
282 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
283 assert_select "input", :count => 7
290 # Now try creating a diary entry
291 new_title = "New Title"
292 new_body = "This is a new body for the diary entry"
294 new_longitude = "2.2"
295 new_language_code = "en"
296 assert_difference "DiaryEntry.count", 1 do
297 post(:new, {'commit' => 'save',
298 'diary_entry'=>{'title' => new_title, 'body' => new_body, 'latitude' => new_latitude,
299 'longitude' => new_longitude, 'language_code' => new_language_code} },
300 {:user => users(:normal_user).id})
302 assert_response :redirect
303 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
304 entry = DiaryEntry.find(6)
305 assert_equal users(:normal_user).id, entry.user_id
306 assert_equal new_title, entry.title
307 assert_equal new_body, entry.body
308 assert_equal new_latitude.to_f, entry.latitude
309 assert_equal new_longitude.to_f, entry.longitude
310 assert_equal new_language_code, entry.language_code
313 def test_creating_diary_comment
314 entry = diary_entries(:normal_user_entry_1)
316 # Make sure that you are denied when you are not logged in
317 post :comment, :display_name => entry.user.display_name, :id => entry.id
318 assert_response :forbidden
320 # Verify that you get a not found error, when you pass a bogus id
321 post :comment, {:display_name => entry.user.display_name, :id => 9999}, {:user => users(:public_user).id}
322 assert_response :not_found
323 assert_select "html", :count => 1 do
324 assert_select "body", :count => 1 do
325 assert_select "div.wrapper", :count => 1 do
326 assert_select "div.content-heading", :count => 1 do
327 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
333 # Now try again with the right id
334 assert_difference "ActionMailer::Base.deliveries.size", 1 do
335 assert_difference "DiaryComment.count", 1 do
336 post :comment, {:display_name => entry.user.display_name, :id => entry.id, :diary_comment => {:body => "New comment"}}, {:user => users(:public_user).id}
339 assert_response :redirect
340 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
341 email = ActionMailer::Base.deliveries.first
342 assert_equal [ users(:normal_user).email ], email.to
343 assert_equal "[OpenStreetMap] #{users(:public_user).display_name} commented on your diary entry", email.subject
344 assert_match /New comment/, email.text_part.decoded
345 assert_match /New comment/, email.html_part.decoded
346 ActionMailer::Base.deliveries.clear
347 comment = DiaryComment.find(5)
348 assert_equal entry.id, comment.diary_entry_id
349 assert_equal users(:public_user).id, comment.user_id
350 assert_equal "New comment", comment.body
352 # Now view the diary entry, and check the new comment is present
353 get :view, :display_name => entry.user.display_name, :id => entry.id
354 assert_response :success
355 assert_select ".diary-comment", :count => 1 do
356 assert_select "#comment5", :count => 1 do
357 assert_select "a[href='/user/#{users(:public_user).display_name}']", :text => users(:public_user).display_name, :count => 1
359 assert_select ".richtext", :text => /New comment/, :count => 1
363 # Check that you can get the expected response and template for all available languages
364 # Should test that there are no <span class="translation_missing">
365 def test_listing_diary_entries
367 assert_response :success, "Should be able to list the diary entries in locale"
368 assert_template 'list', "Should use the list template in locale"
369 assert_select "span[class=translation_missing]", false, "Missing translation in list of diary entries"
371 # Now try to find a specific user's diary entry
372 get :list, {:display_name => users(:normal_user).display_name}
373 assert_response :success, "Should be able to list the diary entries for a user in locale"
374 assert_template 'list', "Should use the list template for a user in locale"
375 assert_no_missing_translations
379 get :rss, {:format => :rss}
380 assert_response :success, "Should be able to get a diary RSS"
381 assert_select "rss", :count => 1 do
382 assert_select "channel", :count => 1 do
383 assert_select "channel>title", :count => 1
384 assert_select "image", :count => 1
385 assert_select "channel>item", :count => 2
390 def test_rss_language
391 get :rss, {:language => diary_entries(:normal_user_entry_1).language_code, :format => :rss}
392 assert_response :success, "Should be able to get a specific language diary RSS"
393 assert_select "rss>channel>item", :count => 1 #, "Diary entries should be filtered by language"
396 # def test_rss_nonexisting_language
397 # get :rss, {:language => 'xx', :format => :rss}
398 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
401 def test_rss_language_with_no_entries
402 get :rss, {:language => 'sl', :format => :rss}
403 assert_response :success, "Should be able to get a specific language diary RSS"
404 assert_select "rss>channel>item", :count => 0 #, "Diary entries should be filtered by language"
408 get :rss, {:display_name => users(:normal_user).display_name, :format => :rss}
409 assert_response :success, "Should be able to get a specific users diary RSS"
410 assert_select "rss>channel>item", :count => 2 #, "Diary entries should be filtered by user"
413 def test_rss_nonexisting_user
414 # Try a user that has never existed
415 get :rss, {:display_name => 'fakeUsername76543', :format => :rss}
416 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
418 # Try a suspended user
419 get :rss, {:display_name => users(:suspended_user).display_name, :format => :rss}
420 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
423 get :rss, {:display_name => users(:deleted_user).display_name, :format => :rss}
424 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
427 def test_viewing_diary_entry
428 # Try a normal entry that should work
429 get :view, {:display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id}
430 assert_response :success
431 assert_template :view
433 # Try a deleted entry
434 get :view, {:display_name => users(:normal_user).display_name, :id => diary_entries(:deleted_entry).id}
435 assert_response :not_found
437 # Try an entry by a suspended user
438 get :view, {:display_name => users(:suspended_user).display_name, :id => diary_entries(:entry_by_suspended_user).id}
439 assert_response :not_found
441 # Try an entry by a deleted user
442 get :view, {:display_name => users(:deleted_user).display_name, :id => diary_entries(:entry_by_deleted_user).id}
443 assert_response :not_found
446 def test_viewing_hidden_comments
447 # Get a diary entry that has hidden comments
448 get :view, {:display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_geo_entry).id}
449 assert_response :success
450 assert_template :view
451 assert_select "div.comments" do
452 assert_select "p#comment1", :count => 1 # visible comment
453 assert_select "p#comment2", :count => 0 # comment by suspended user
454 assert_select "p#comment3", :count => 0 # comment by deleted user
455 assert_select "p#comment4", :count => 0 # hidden comment
460 # Try without logging in
461 post :hide, {:display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id}
462 assert_response :forbidden
463 assert_equal true, DiaryEntry.find(diary_entries(:normal_user_entry_1).id).visible
465 # Now try as a normal user
466 post :hide, {:display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id}, {:user => users(:normal_user).id}
467 assert_response :redirect
468 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id
469 assert_equal true, DiaryEntry.find(diary_entries(:normal_user_entry_1).id).visible
471 # Finally try as an administrator
472 post :hide, {:display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id}, {:user => users(:administrator_user).id}
473 assert_response :redirect
474 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
475 assert_equal false, DiaryEntry.find(diary_entries(:normal_user_entry_1).id).visible
479 # Try without logging in
480 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}
481 assert_response :forbidden
482 assert_equal true, DiaryComment.find(diary_comments(:comment_for_geo_post).id).visible
484 # Now try as a normal user
485 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}
486 assert_response :redirect
487 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_geo_entry).id
488 assert_equal true, DiaryComment.find(diary_comments(:comment_for_geo_post).id).visible
490 # Finally try as an administrator
491 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}
492 assert_response :redirect
493 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_geo_entry).id
494 assert_equal false, DiaryComment.find(diary_comments(:comment_for_geo_post).id).visible
498 # Test a user with no comments
499 get :comments, :display_name => users(:normal_user).display_name
500 assert_response :success
501 assert_template :comments
502 assert_select "table.messages" do
503 assert_select "tr", :count => 1 # header, no comments
506 # Test a user with a comment
507 get :comments, :display_name => users(:public_user).display_name
508 assert_response :success
509 assert_template :comments
510 assert_select "table.messages" do
511 assert_select "tr", :count => 2 # header and one comment
514 # Test a suspended user
515 get :comments, :display_name => users(:suspended_user).display_name
516 assert_response :not_found
518 # Test a deleted user
519 get :comments, :display_name => users(:deleted_user).display_name
520 assert_response :not_found