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 "span[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
265 def test_creating_diary_comment
269 # Check that you can get the expected response and template for all available languages
270 # Should test that there are no <span class="translation_missing">
271 def test_listing_diary_entries
273 assert_response :success, "Should be able to list the diary entries in locale"
274 assert_template 'list', "Should use the list template in locale"
275 assert_select "span[class=translation_missing]", false, "Missing translation in list of diary entries"
277 # Now try to find a specific user's diary entry
278 get :list, {:display_name => users(:normal_user).display_name}
279 assert_response :success, "Should be able to list the diary entries for a user in locale"
280 assert_template 'list', "Should use the list template for a user in locale"
281 assert_no_missing_translations
285 get :rss, {:format => :rss}
286 assert_response :success, "Should be able to get a diary RSS"
287 assert_select "rss", :count => 1 do
288 assert_select "channel", :count => 1 do
289 assert_select "channel>title", :count => 1
290 assert_select "image", :count => 1
291 assert_select "channel>item", :count => 2
296 def test_rss_language
297 get :rss, {:language => diary_entries(:normal_user_entry_1).language_code, :format => :rss}
298 assert_response :success, "Should be able to get a specific language diary RSS"
299 assert_select "rss>channel>item", :count => 1 #, "Diary entries should be filtered by language"
302 # def test_rss_nonexisting_language
303 # get :rss, {:language => 'xx', :format => :rss}
304 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
307 def test_rss_language_with_no_entries
308 get :rss, {:language => 'sl', :format => :rss}
309 assert_response :success, "Should be able to get a specific language diary RSS"
310 assert_select "rss>channel>item", :count => 0 #, "Diary entries should be filtered by language"
314 get :rss, {:display_name => users(:normal_user).display_name, :format => :rss}
315 assert_response :success, "Should be able to get a specific users diary RSS"
316 assert_select "rss>channel>item", :count => 2 #, "Diary entries should be filtered by user"
319 def test_rss_nonexisting_user
320 get :rss, {:display_name => 'fakeUsername76543', :format => :rss}
321 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
324 def test_viewing_diary_entry
325 get :view, {:display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id}
326 assert_response :success
327 assert_template 'view'