]> git.openstreetmap.org Git - rails.git/blob - test/functional/diary_entry_controller_test.rb
Merge branch 'master' into openstreetbugs
[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#content", :count => 1 do
107           assert_select "h1", "New Diary Entry", :count => 1
108           # We don't care about the layout, we just care about the form fields
109           # that are available
110           assert_select "form[action='/diary/new']", :count => 1 do
111             assert_select "input[id=diary_entry_title][name='diary_entry[title]']", :count => 1
112             assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :count => 1
113             assert_select "input#latitude[name='diary_entry[latitude]'][type=text]", :count => 1
114             assert_select "input#longitude[name='diary_entry[longitude]'][type=text]", :count => 1
115             assert_select "input[name=commit][type=submit][value=Save]", :count => 1
116           end
117         end
118       end
119     end
120         
121   end
122   
123   def test_editing_diary_entry
124     @request.cookies["_osm_username"] = users(:normal_user).display_name
125     entry = diary_entries(:normal_user_entry_1)
126
127     # Make sure that you are redirected to the login page when you are 
128     # not logged in, without and with the id of the entry you want to edit
129     get :edit, :display_name => entry.user.display_name, :id => entry.id
130     assert_response :redirect
131     assert_redirected_to :controller => :user, :action => "login", :referer => "/user/#{entry.user.display_name}/diary/#{entry.id}/edit"
132     
133     # Verify that you get a not found error, when you pass a bogus id
134     get(:edit, {:display_name => entry.user.display_name, :id => 9999}, {'user' => entry.user.id})
135     assert_response :not_found
136     assert_select "html", :count => 1 do
137       assert_select "body", :count => 1 do
138         assert_select "div#content", :count => 1 do
139           assert_select "h2", :text => "No entry with the id: 9999", :count => 1 
140         end
141       end
142     end
143     
144     # Now pass the id, and check that you can edit it, when using the same 
145     # user as the person who created the entry
146     get(:edit, {:display_name => entry.user.display_name, :id => entry.id}, {'user' => entry.user.id})
147     assert_response :success
148     assert_select "html", :count => 1 do
149       assert_select "head", :count => 1 do
150         assert_select "title", :text => /Edit diary entry/, :count => 1
151       end
152       assert_select "body", :count => 1 do
153         assert_select "div#content", :count => 1 do 
154           assert_select "h1", :text => /Edit diary entry/, :count => 1
155           assert_select "form[action='/user/#{entry.user.display_name}/diary/#{entry.id}/edit'][method=post]", :count => 1 do
156             assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{entry.title}']", :count => 1
157             assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => entry.body, :count => 1
158             assert_select "select#diary_entry_language_code", :count => 1
159             assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
160             assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
161             assert_select "input[name=commit][type=submit][value=Save]", :count => 1
162             assert_select "input", :count => 5
163           end
164         end
165       end
166     end
167     
168     # Now lets see if you can edit the diary entry
169     new_title = "New Title"
170     new_body = "This is a new body for the diary entry"
171     new_latitude = "1.1"
172     new_longitude = "2.2"
173     new_language_code = "en"
174     post(:edit, {:display_name => entry.user.display_name, :id => entry.id, 'commit' => 'save', 
175       'diary_entry'=>{'title' => new_title, 'body' => new_body, 'latitude' => new_latitude,
176       'longitude' => new_longitude, 'language_code' => new_language_code} },
177          {'user' => entry.user.id})
178     assert_response :redirect
179     assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
180     
181     # Now check that the new data is rendered, when logged in
182     get :view, {:display_name => entry.user.display_name, :id => entry.id}, {'user' => entry.user.id}
183     assert_response :success
184     assert_template 'diary_entry/view'
185     assert_select "html", :count => 1 do
186       assert_select "head", :count => 1 do
187         assert_select "title", :text => /Users' diaries | /, :count => 1
188       end
189       assert_select "body", :count => 1 do
190         assert_select "div#content", :count => 1 do
191           assert_select "h2", :text => /#{entry.user.display_name}'s diary/, :count => 1
192           assert_select "b", :text => /#{new_title}/, :count => 1
193           # This next line won't work if the text has been run through the htmlize function
194           # due to formatting that could be introduced
195           assert_select "p", :text => /#{new_body}/, :count => 1
196           assert_select "abbr[class=geo][title=#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}]", :count => 1
197           # As we're not logged in, check that you cannot edit
198           #print @response.body
199           assert_select "a[href='/user/#{entry.user.display_name}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
200         end
201       end
202     end
203
204     @request.cookies["_osm_username"] = users(:public_user).display_name
205
206     # and when not logged in as the user who wrote the entry
207     get :view, {:display_name => entry.user.display_name, :id => entry.id}, {'user' => entry.user.id}
208     assert_response :success
209     assert_template 'diary_entry/view'
210     assert_select "html", :count => 1 do
211       assert_select "head", :count => 1 do
212         assert_select "title", :text => /Users' diaries | /, :count => 1
213       end
214       assert_select "body", :count => 1 do
215         assert_select "div#content", :count => 1 do
216           assert_select "h2", :text => /#{users(:normal_user).display_name}'s diary/, :count => 1
217           assert_select "b", :text => /#{new_title}/, :count => 1
218           # This next line won't work if the text has been run through the htmlize function
219           # due to formatting that could be introduced
220           assert_select "p", :text => /#{new_body}/, :count => 1
221           assert_select "abbr[class=geo][title=#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}]", :count => 1
222           # As we're not logged in, check that you cannot edit
223           assert_select "span[class=hidden show_if_user_#{entry.user.id}]", :count => 1 do
224             assert_select "a[href='/user/#{entry.user.display_name}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
225           end
226         end
227       end
228     end
229     #print @response.body
230     
231   end
232   
233   def test_edit_diary_entry_i18n
234     @request.cookies["_osm_username"] = users(:normal_user).display_name
235
236     get(:edit, {:id => diary_entries(:normal_user_entry_1).id}, {'user' => users(:normal_user).id})
237     assert_response :success
238     assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
239   end
240   
241   def test_create_diary_entry
242     #post :new
243   end
244   
245   def test_creating_diary_comment
246     
247   end
248   
249   # Check that you can get the expected response and template for all available languages
250   # Should test that there are no <span class="translation_missing">
251   def test_listing_diary_entries
252       get :list
253       assert_response :success, "Should be able to list the diary entries in locale"
254       assert_template 'list', "Should use the list template in locale"
255       assert_select "span[class=translation_missing]", false, "Missing translation in list of diary entries"
256     
257       # Now try to find a specific user's diary entry
258       get :list, {:display_name => users(:normal_user).display_name}
259       assert_response :success, "Should be able to list the diary entries for a user in locale"
260       assert_template 'list', "Should use the list template for a user in locale"
261       assert_no_missing_translations
262   end
263   
264   def test_rss
265     get :rss, {:format => :rss}
266     assert_response :success, "Should be able to get a diary RSS"
267     assert_select "rss", :count => 1 do
268       assert_select "channel", :count => 1 do
269         assert_select "channel>title", :count => 1
270         assert_select "image", :count => 1
271         assert_select "channel>item", :count => 2
272       end
273     end
274   end
275   
276   def test_rss_language
277     get :rss, {:language => diary_entries(:normal_user_entry_1).language_code, :format => :rss}
278     assert_response :success, "Should be able to get a specific language diary RSS"
279     assert_select "rss>channel>item", :count => 1 #, "Diary entries should be filtered by language"
280   end
281   
282 #  def test_rss_nonexisting_language
283 #    get :rss, {:language => 'xx', :format => :rss}
284 #    assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
285 #  end
286
287   def test_rss_language_with_no_entries
288     get :rss, {:language => 'sl', :format => :rss}
289     assert_response :success, "Should be able to get a specific language diary RSS"
290     assert_select "rss>channel>item", :count => 0 #, "Diary entries should be filtered by language"
291   end
292
293   def test_rss_user
294     get :rss, {:display_name => users(:normal_user).display_name, :format => :rss}
295     assert_response :success, "Should be able to get a specific users diary RSS"
296     assert_select "rss>channel>item", :count => 2 #, "Diary entries should be filtered by user"
297   end
298   
299   def test_rss_nonexisting_user
300     get :rss, {:display_name => 'fakeUsername76543', :format => :rss}
301     assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
302   end
303
304   def test_viewing_diary_entry
305     get :view, {:display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id}
306     assert_response :success
307     assert_template 'view'
308   end
309 end