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