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#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
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
123 def test_editing_diary_entry
124 @request.cookies["_osm_username"] = users(:normal_user).display_name
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
129 assert_response :redirect
130 assert_redirected_to :controller => :user, :action => "login", :referer => "/diary_entry/edit"
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"
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
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
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
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"
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
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
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
207 @request.cookies["_osm_username"] = users(:public_user).display_name
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
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
232 #print @response.body
236 def test_edit_diary_entry_i18n
237 @request.cookies["_osm_username"] = users(:normal_user).display_name
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"
244 def test_create_diary_entry
248 def test_creating_diary_comment
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
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"
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
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
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"
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"
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"
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"
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"
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'