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
89 @request.cookies["_osm_username"] = users(:normal_user).display_name
92 assert_response :redirect
93 assert_redirected_to :controller => :user, :action => "login", :referer => "/diary/new"
94 # Now pretend to login by using the session hash, with the
95 # id of the person we want to login as through session(:user)=user.id
96 get(:new, nil, {'user' => users(:normal_user).id})
97 assert_response :success
100 #print @response.to_yaml
101 assert_select "html", :count => 1 do
102 assert_select "head", :count => 1 do
103 assert_select "title", :text => /New Diary Entry/, :count => 1
105 assert_select "body", :count => 1 do
106 assert_select "div.wrapper", :count => 1 do
107 assert_select "div.content-heading", :count => 1 do
108 assert_select "h1", :text => "New Diary Entry", :count => 1
110 assert_select "div#content", :count => 1 do
111 # We don't care about the layout, we just care about the form fields
113 assert_select "form[action='/diary/new']", :count => 1 do
114 assert_select "input[id=diary_entry_title][name='diary_entry[title]']", :count => 1
115 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :count => 1
116 assert_select "input#latitude[name='diary_entry[latitude]'][type=text]", :count => 1
117 assert_select "input#longitude[name='diary_entry[longitude]'][type=text]", :count => 1
118 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
127 def test_editing_diary_entry
128 @request.cookies["_osm_username"] = users(:normal_user).display_name
129 entry = diary_entries(:normal_user_entry_1)
131 # Make sure that you are redirected to the login page when you are
132 # not logged in, without and with the id of the entry you want to edit
133 get :edit, :display_name => entry.user.display_name, :id => entry.id
134 assert_response :redirect
135 assert_redirected_to :controller => :user, :action => "login", :referer => "/user/#{entry.user.display_name}/diary/#{entry.id}/edit"
137 # Verify that you get a not found error, when you pass a bogus id
138 get(:edit, {:display_name => entry.user.display_name, :id => 9999}, {'user' => entry.user.id})
139 assert_response :not_found
140 assert_select "html", :count => 1 do
141 assert_select "body", :count => 1 do
142 assert_select "div.wrapper", :count => 1 do
143 assert_select "div.content-heading", :count => 1 do
144 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
150 # Now pass the id, and check that you can edit it, when using the same
151 # user as the person who created the entry
152 get(:edit, {:display_name => entry.user.display_name, :id => entry.id}, {'user' => entry.user.id})
153 assert_response :success
154 assert_select "html", :count => 1 do
155 assert_select "head", :count => 1 do
156 assert_select "title", :text => /Edit diary entry/, :count => 1
158 assert_select "body", :count => 1 do
159 assert_select "div.wrapper", :count => 1 do
160 assert_select "div.content-heading", :count => 1 do
161 assert_select "h1", :text => /Edit diary entry/, :count => 1
163 assert_select "div#content", :count => 1 do
164 assert_select "form[action='/user/#{entry.user.display_name}/diary/#{entry.id}/edit'][method=post]", :count => 1 do
165 assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{entry.title}']", :count => 1
166 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => entry.body, :count => 1
167 assert_select "select#diary_entry_language_code", :count => 1
168 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
169 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
170 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
171 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
172 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
173 assert_select "input", :count => 7
180 # Now lets see if you can edit the diary entry
181 new_title = "New Title"
182 new_body = "This is a new body for the diary entry"
184 new_longitude = "2.2"
185 new_language_code = "en"
186 post(:edit, {:display_name => entry.user.display_name, :id => entry.id, 'commit' => 'save',
187 'diary_entry'=>{'title' => new_title, 'body' => new_body, 'latitude' => new_latitude,
188 'longitude' => new_longitude, 'language_code' => new_language_code} },
189 {'user' => entry.user.id})
190 assert_response :redirect
191 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
193 # Now check that the new data is rendered, when logged in
194 get :view, {:display_name => entry.user.display_name, :id => entry.id}, {'user' => entry.user.id}
195 assert_response :success
196 assert_template 'diary_entry/view'
197 assert_select "html", :count => 1 do
198 assert_select "head", :count => 1 do
199 assert_select "title", :text => /Users' diaries | /, :count => 1
201 assert_select "body", :count => 1 do
202 assert_select "div.wrapper", :count => 1 do
203 assert_select "div.content-heading", :count => 1 do
204 assert_select "h2", :text => /#{entry.user.display_name}'s diary/, :count => 1
206 assert_select "div#content", :count => 1 do
207 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
208 # This next line won't work if the text has been run through the htmlize function
209 # due to formatting that could be introduced
210 assert_select "p", :text => /#{new_body}/, :count => 1
211 assert_select "abbr[class=geo][title=#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}]", :count => 1
212 # As we're not logged in, check that you cannot edit
213 #print @response.body
214 assert_select "a[href='/user/#{entry.user.display_name}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
220 @request.cookies["_osm_username"] = users(:public_user).display_name
222 # and when not logged in as the user who wrote the entry
223 get :view, {:display_name => entry.user.display_name, :id => entry.id}, {'user' => entry.user.id}
224 assert_response :success
225 assert_template 'diary_entry/view'
226 assert_select "html", :count => 1 do
227 assert_select "head", :count => 1 do
228 assert_select "title", :text => /Users' diaries | /, :count => 1
230 assert_select "body", :count => 1 do
231 assert_select "div.wrapper", :count => 1 do
232 assert_select "div.content-heading", :count => 1 do
233 assert_select "h2", :text => /#{users(:normal_user).display_name}'s diary/, :count => 1
235 assert_select "div#content", :count => 1 do
236 assert_select "div.post_heading", :text => /#{new_title}/, :count => 1
237 # This next line won't work if the text has been run through the htmlize function
238 # due to formatting that could be introduced
239 assert_select "p", :text => /#{new_body}/, :count => 1
240 assert_select "abbr[class=geo][title=#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}]", :count => 1
241 # As we're not logged in, check that you cannot edit
242 assert_select "li[class=hidden show_if_user_#{entry.user.id}]", :count => 1 do
243 assert_select "a[href='/user/#{entry.user.display_name}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
249 #print @response.body
253 def test_edit_diary_entry_i18n
254 @request.cookies["_osm_username"] = users(:normal_user).display_name
256 get :edit, {:display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id}, {'user' => users(:normal_user).id}
257 assert_response :success
258 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
261 def test_create_diary_entry
262 @request.cookies["_osm_username"] = users(:normal_user).display_name
264 # Make sure that you are redirected to the login page when you
267 assert_response :redirect
268 assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/new"
270 # Now try again when logged in
271 get :new, {}, {:user => users(:normal_user).id}
272 assert_response :success
273 assert_select "html", :count => 1 do
274 assert_select "head", :count => 1 do
275 assert_select "title", :text => /New Diary Entry/, :count => 1
277 assert_select "body", :count => 1 do
278 assert_select "div.wrapper", :count => 1 do
279 assert_select "div.content-heading", :count => 1 do
280 assert_select "h1", :text => /New Diary Entry/, :count => 1
282 assert_select "div#content", :count => 1 do
283 assert_select "form[action='/diary/new'][method=post]", :count => 1 do
284 assert_select "input#diary_entry_title[name='diary_entry[title]']", :count => 1
285 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => "", :count => 1
286 assert_select "select#diary_entry_language_code", :count => 1
287 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
288 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
289 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
290 assert_select "input[name=commit][type=submit][value=Edit]", :count => 1
291 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
292 assert_select "input", :count => 7
299 # Now try creating a diary entry
300 new_title = "New Title"
301 new_body = "This is a new body for the diary entry"
303 new_longitude = "2.2"
304 new_language_code = "en"
305 assert_difference "DiaryEntry.count", 1 do
306 post(:new, {'commit' => 'save',
307 'diary_entry'=>{'title' => new_title, 'body' => new_body, 'latitude' => new_latitude,
308 'longitude' => new_longitude, 'language_code' => new_language_code} },
309 {:user => users(:normal_user).id})
311 assert_response :redirect
312 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
313 entry = DiaryEntry.find(6)
314 assert_equal users(:normal_user).id, entry.user_id
315 assert_equal new_title, entry.title
316 assert_equal new_body, entry.body
317 assert_equal new_latitude.to_f, entry.latitude
318 assert_equal new_longitude.to_f, entry.longitude
319 assert_equal new_language_code, entry.language_code
322 def test_creating_diary_comment
323 @request.cookies["_osm_username"] = users(:public_user).display_name
324 entry = diary_entries(:normal_user_entry_1)
326 # Make sure that you are denied when you are not logged in
327 post :comment, :display_name => entry.user.display_name, :id => entry.id
328 assert_response :forbidden
330 # Verify that you get a not found error, when you pass a bogus id
331 post :comment, {:display_name => entry.user.display_name, :id => 9999}, {:user => users(:public_user).id}
332 assert_response :not_found
333 assert_select "html", :count => 1 do
334 assert_select "body", :count => 1 do
335 assert_select "div.wrapper", :count => 1 do
336 assert_select "div.content-heading", :count => 1 do
337 assert_select "h2", :text => "No entry with the id: 9999", :count => 1
343 # Now try again with the right id
344 assert_difference "ActionMailer::Base.deliveries.size", 1 do
345 assert_difference "DiaryComment.count", 1 do
346 post :comment, {:display_name => entry.user.display_name, :id => entry.id, :diary_comment => {:body => "New comment"}}, {:user => users(:public_user).id}
349 assert_response :redirect
350 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
351 email = ActionMailer::Base.deliveries.first
352 assert_equal [ users(:normal_user).email ], email.to
353 assert_equal "[OpenStreetMap] #{users(:public_user).display_name} commented on your diary entry", email.subject
354 assert_match /New comment/, email.text_part.decoded
355 assert_match /New comment/, email.html_part.decoded
356 ActionMailer::Base.deliveries.clear
357 comment = DiaryComment.find(5)
358 assert_equal entry.id, comment.diary_entry_id
359 assert_equal users(:public_user).id, comment.user_id
360 assert_equal "New comment", comment.body
362 # Now view the diary entry, and check the new comment is present
363 get :view, :display_name => entry.user.display_name, :id => entry.id
364 assert_response :success
365 assert_select ".diary-comment", :count => 1 do
366 assert_select "#comment5", :count => 1 do
367 assert_select "a[href='/user/#{users(:public_user).display_name}']", :text => users(:public_user).display_name, :count => 1
369 assert_select ".richtext", :text => /New comment/, :count => 1
373 # Check that you can get the expected response and template for all available languages
374 # Should test that there are no <span class="translation_missing">
375 def test_listing_diary_entries
377 assert_response :success, "Should be able to list the diary entries in locale"
378 assert_template 'list', "Should use the list template in locale"
379 assert_select "span[class=translation_missing]", false, "Missing translation in list of diary entries"
381 # Now try to find a specific user's diary entry
382 get :list, {:display_name => users(:normal_user).display_name}
383 assert_response :success, "Should be able to list the diary entries for a user in locale"
384 assert_template 'list', "Should use the list template for a user in locale"
385 assert_no_missing_translations
389 get :rss, {:format => :rss}
390 assert_response :success, "Should be able to get a diary RSS"
391 assert_select "rss", :count => 1 do
392 assert_select "channel", :count => 1 do
393 assert_select "channel>title", :count => 1
394 assert_select "image", :count => 1
395 assert_select "channel>item", :count => 2
400 def test_rss_language
401 get :rss, {:language => diary_entries(:normal_user_entry_1).language_code, :format => :rss}
402 assert_response :success, "Should be able to get a specific language diary RSS"
403 assert_select "rss>channel>item", :count => 1 #, "Diary entries should be filtered by language"
406 # def test_rss_nonexisting_language
407 # get :rss, {:language => 'xx', :format => :rss}
408 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
411 def test_rss_language_with_no_entries
412 get :rss, {:language => 'sl', :format => :rss}
413 assert_response :success, "Should be able to get a specific language diary RSS"
414 assert_select "rss>channel>item", :count => 0 #, "Diary entries should be filtered by language"
418 get :rss, {:display_name => users(:normal_user).display_name, :format => :rss}
419 assert_response :success, "Should be able to get a specific users diary RSS"
420 assert_select "rss>channel>item", :count => 2 #, "Diary entries should be filtered by user"
423 def test_rss_nonexisting_user
424 # Try a user that has never existed
425 get :rss, {:display_name => 'fakeUsername76543', :format => :rss}
426 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
428 # Try a suspended user
429 get :rss, {:display_name => users(:suspended_user).display_name, :format => :rss}
430 assert_response :not_found, "Should not be able to get a suspended users diary RSS"
433 get :rss, {:display_name => users(:deleted_user).display_name, :format => :rss}
434 assert_response :not_found, "Should not be able to get a deleted users diary RSS"
437 def test_viewing_diary_entry
438 # Try a normal entry that should work
439 get :view, {:display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id}
440 assert_response :success
441 assert_template :view
443 # Try a deleted entry
444 get :view, {:display_name => users(:normal_user).display_name, :id => diary_entries(:deleted_entry).id}
445 assert_response :not_found
447 # Try an entry by a suspended user
448 get :view, {:display_name => users(:suspended_user).display_name, :id => diary_entries(:entry_by_suspended_user).id}
449 assert_response :not_found
451 # Try an entry by a deleted user
452 get :view, {:display_name => users(:deleted_user).display_name, :id => diary_entries(:entry_by_deleted_user).id}
453 assert_response :not_found
456 def test_viewing_hidden_comments
457 # Get a diary entry that has hidden comments
458 get :view, {:display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_geo_entry).id}
459 assert_response :success
460 assert_template :view
461 assert_select "div.comments" do
462 assert_select "p#comment1", :count => 1 # visible comment
463 assert_select "p#comment2", :count => 0 # comment by suspended user
464 assert_select "p#comment3", :count => 0 # comment by deleted user
465 assert_select "p#comment4", :count => 0 # hidden comment
470 # Try without logging in
471 post :hide, {:display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id}
472 assert_response :forbidden
473 assert_equal true, DiaryEntry.find(diary_entries(:normal_user_entry_1).id).visible
475 @request.cookies["_osm_username"] = users(:normal_user).display_name
477 # Now try as a normal user
478 post :hide, {:display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id}, {:user => users(:normal_user).id}
479 assert_response :redirect
480 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id
481 assert_equal true, DiaryEntry.find(diary_entries(:normal_user_entry_1).id).visible
483 @request.cookies["_osm_username"] = users(:administrator_user).display_name
485 # Finally try as an administrator
486 post :hide, {:display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id}, {:user => users(:administrator_user).id}
487 assert_response :redirect
488 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
489 assert_equal false, DiaryEntry.find(diary_entries(:normal_user_entry_1).id).visible
493 # Try without logging in
494 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}
495 assert_response :forbidden
496 assert_equal true, DiaryComment.find(diary_comments(:comment_for_geo_post).id).visible
498 @request.cookies["_osm_username"] = users(:normal_user).display_name
500 # Now try as a normal user
501 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}
502 assert_response :redirect
503 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_geo_entry).id
504 assert_equal true, DiaryComment.find(diary_comments(:comment_for_geo_post).id).visible
506 @request.cookies["_osm_username"] = users(:administrator_user).display_name
508 # Finally try as an administrator
509 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}
510 assert_response :redirect
511 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_geo_entry).id
512 assert_equal false, DiaryComment.find(diary_comments(:comment_for_geo_post).id).visible
516 # Test a user with no comments
517 get :comments, :display_name => users(:normal_user).display_name
518 assert_response :success
519 assert_template :comments
520 assert_select "table.messages" do
521 assert_select "tr", :count => 1 # header, no comments
524 # Test a user with a comment
525 get :comments, :display_name => users(:public_user).display_name
526 assert_response :success
527 assert_template :comments
528 assert_select "table.messages" do
529 assert_select "tr", :count => 2 # header and one comment
532 # Test a suspended user
533 get :comments, :display_name => users(:suspended_user).display_name
534 assert_response :not_found
536 # Test a deleted user
537 get :comments, :display_name => users(:deleted_user).display_name
538 assert_response :not_found