]> git.openstreetmap.org Git - rails.git/blob - test/functional/diary_entry_controller_test.rb
Convert edit menus to bootstrap dropdown
[rails.git] / test / functional / diary_entry_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 class DiaryEntryControllerTest < ActionController::TestCase
4   fixtures :users, :diary_entries, :diary_comments, :languages
5
6   include ActionView::Helpers::NumberHelper
7
8   ##
9   # test all routes which lead to this controller
10   def test_routes
11     assert_routing(
12       { :path => "/diary", :method => :get },
13       { :controller => "diary_entry", :action => "list" }
14     )
15     assert_routing(
16       { :path => "/diary/language", :method => :get },
17       { :controller => "diary_entry", :action => "list", :language => "language" }
18     )
19     assert_routing(
20       { :path => "/user/username/diary", :method => :get },
21       { :controller => "diary_entry", :action => "list", :display_name => "username" }
22     )
23     assert_routing(
24       { :path => "/diary/friends", :method => :get },
25       { :controller => "diary_entry", :action => "list", :friends => true }
26     )
27     assert_routing(
28       { :path => "/diary/nearby", :method => :get },
29       { :controller => "diary_entry", :action => "list", :nearby => true }
30     )
31
32     assert_routing(
33       { :path => "/diary/rss", :method => :get },
34       { :controller => "diary_entry", :action => "rss", :format => :rss }
35     )
36     assert_routing(
37       { :path => "/diary/language/rss", :method => :get },
38       { :controller => "diary_entry", :action => "rss", :language => "language", :format => :rss }
39     )
40     assert_routing(
41       { :path => "/user/username/diary/rss", :method => :get },
42       { :controller => "diary_entry", :action => "rss", :display_name => "username", :format => :rss }
43     )
44
45     assert_routing(
46       { :path => "/user/username/diary/comments", :method => :get },
47       { :controller => "diary_entry", :action => "comments", :display_name => "username" }
48     )
49     assert_routing(
50       { :path => "/user/username/diary/comments/1", :method => :get },
51       { :controller => "diary_entry", :action => "comments", :display_name => "username", :page => "1" }
52     )
53
54     assert_routing(
55       { :path => "/diary/new", :method => :get },
56       { :controller => "diary_entry", :action => "new" }
57     )
58     assert_routing(
59       { :path => "/diary/new", :method => :post },
60       { :controller => "diary_entry", :action => "new" }
61     )
62     assert_routing(
63       { :path => "/user/username/diary/1", :method => :get },
64       { :controller => "diary_entry", :action => "view", :display_name => "username", :id => "1" }
65     )
66     assert_routing(
67       { :path => "/user/username/diary/1/edit", :method => :get },
68       { :controller => "diary_entry", :action => "edit", :display_name => "username", :id => "1" }
69     )
70     assert_routing(
71       { :path => "/user/username/diary/1/edit", :method => :post },
72       { :controller => "diary_entry", :action => "edit", :display_name => "username", :id => "1" }
73     )
74     assert_routing(
75       { :path => "/user/username/diary/1/newcomment", :method => :post },
76       { :controller => "diary_entry", :action => "comment", :display_name => "username", :id => "1" }
77     )
78     assert_routing(
79       { :path => "/user/username/diary/1/hide", :method => :post },
80       { :controller => "diary_entry", :action => "hide", :display_name => "username", :id => "1" }
81     )
82     assert_routing(
83       { :path => "/user/username/diary/1/hidecomment/2", :method => :post },
84       { :controller => "diary_entry", :action => "hidecomment", :display_name => "username", :id => "1", :comment => "2" }
85     )
86   end
87
88   def test_showing_new_diary_entry
89     @request.cookies["_osm_username"] = users(:normal_user).display_name
90
91     get :new
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
98     #print @response.body
99     
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
104       end
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
109           end
110           assert_select "div#content", :count => 1 do
111             # We don't care about the layout, we just care about the form fields
112             # that are available
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
119             end
120           end
121         end
122       end
123     end
124         
125   end
126   
127   def test_editing_diary_entry
128     @request.cookies["_osm_username"] = users(:normal_user).display_name
129     entry = diary_entries(:normal_user_entry_1)
130
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"
136     
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 
145           end
146         end
147       end
148     end
149     
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
157       end
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
162           end
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
174             end
175           end
176         end
177       end
178     end
179     
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"
183     new_latitude = "1.1"
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
192     
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
200       end
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}&#x27;s diary/, :count => 1
205           end
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
215           end
216         end
217       end
218     end
219
220     @request.cookies["_osm_username"] = users(:public_user).display_name
221
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
229       end
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}&#x27;s diary/, :count => 1
234           end
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
244             end
245           end
246         end
247       end
248     end
249     #print @response.body
250     
251   end
252   
253   def test_edit_diary_entry_i18n
254     @request.cookies["_osm_username"] = users(:normal_user).display_name
255
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"
259   end
260   
261   def test_create_diary_entry
262     @request.cookies["_osm_username"] = users(:normal_user).display_name
263
264     # Make sure that you are redirected to the login page when you
265     # are not logged in
266     get :new
267     assert_response :redirect
268     assert_redirected_to :controller => :user, :action => :login, :referer => "/diary/new"
269
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
276       end
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
281           end
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
293             end
294           end
295         end
296       end
297     end
298
299     # Now try creating a diary entry
300     new_title = "New Title"
301     new_body = "This is a new body for the diary entry"
302     new_latitude = "1.1"
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})
310     end
311     assert_response :redirect
312     assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
313     entry = DiaryEntry.find(4)
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
320   end
321   
322   def test_creating_diary_comment
323     @request.cookies["_osm_username"] = users(:public_user).display_name
324     entry = diary_entries(:normal_user_entry_1)
325
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
329     
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 
338           end
339         end
340       end
341     end
342
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}
347       end
348     end
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(4)
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
361
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 "#comment4", :count => 1 do
367         assert_select "a[href='/user/#{users(:public_user).display_name}']", :text => users(:public_user).display_name, :count => 1
368       end
369       assert_select ".richtext", :text => /New comment/, :count => 1
370     end
371   end
372   
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
376       get :list
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"
380     
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
386   end
387   
388   def test_rss
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
396       end
397     end
398   end
399   
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"
404   end
405   
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"
409 #  end
410
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"
415   end
416
417   def test_rss_user
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"
421   end
422   
423   def test_rss_nonexisting_user
424     get :rss, {:display_name => 'fakeUsername76543', :format => :rss}
425     assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
426   end
427
428   def test_viewing_diary_entry
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'
432   end
433 end