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
125 entry = diary_entries(:normal_user_entry_1)
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"
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
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
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[name=commit][type=submit][value=Edit]", :count => 1
163 assert_select "input[name=commit][type=submit][value=Preview]", :count => 1
164 assert_select "input", :count => 7
170 # Now lets see if you can edit the diary entry
171 new_title = "New Title"
172 new_body = "This is a new body for the diary entry"
174 new_longitude = "2.2"
175 new_language_code = "en"
176 post(:edit, {:display_name => entry.user.display_name, :id => entry.id, 'commit' => 'save',
177 'diary_entry'=>{'title' => new_title, 'body' => new_body, 'latitude' => new_latitude,
178 'longitude' => new_longitude, 'language_code' => new_language_code} },
179 {'user' => entry.user.id})
180 assert_response :redirect
181 assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
183 # Now check that the new data is rendered, when logged in
184 get :view, {:display_name => entry.user.display_name, :id => entry.id}, {'user' => entry.user.id}
185 assert_response :success
186 assert_template 'diary_entry/view'
187 assert_select "html", :count => 1 do
188 assert_select "head", :count => 1 do
189 assert_select "title", :text => /Users' diaries | /, :count => 1
191 assert_select "body", :count => 1 do
192 assert_select "div#content", :count => 1 do
193 assert_select "h2", :text => /#{entry.user.display_name}'s diary/, :count => 1
194 assert_select "b", :text => /#{new_title}/, :count => 1
195 # This next line won't work if the text has been run through the htmlize function
196 # due to formatting that could be introduced
197 assert_select "p", :text => /#{new_body}/, :count => 1
198 assert_select "abbr[class=geo][title=#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}]", :count => 1
199 # As we're not logged in, check that you cannot edit
200 #print @response.body
201 assert_select "a[href='/user/#{entry.user.display_name}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
206 @request.cookies["_osm_username"] = users(:public_user).display_name
208 # and when not logged in as the user who wrote the entry
209 get :view, {:display_name => entry.user.display_name, :id => entry.id}, {'user' => entry.user.id}
210 assert_response :success
211 assert_template 'diary_entry/view'
212 assert_select "html", :count => 1 do
213 assert_select "head", :count => 1 do
214 assert_select "title", :text => /Users' diaries | /, :count => 1
216 assert_select "body", :count => 1 do
217 assert_select "div#content", :count => 1 do
218 assert_select "h2", :text => /#{users(:normal_user).display_name}'s diary/, :count => 1
219 assert_select "b", :text => /#{new_title}/, :count => 1
220 # This next line won't work if the text has been run through the htmlize function
221 # due to formatting that could be introduced
222 assert_select "p", :text => /#{new_body}/, :count => 1
223 assert_select "abbr[class=geo][title=#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}]", :count => 1
224 # As we're not logged in, check that you cannot edit
225 assert_select "span[class=hidden show_if_user_#{entry.user.id}]", :count => 1 do
226 assert_select "a[href='/user/#{entry.user.display_name}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1
231 #print @response.body
235 def test_edit_diary_entry_i18n
236 @request.cookies["_osm_username"] = users(:normal_user).display_name
238 get(:edit, {:id => diary_entries(:normal_user_entry_1).id}, {'user' => users(:normal_user).id})
239 assert_response :success
240 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
243 def test_create_diary_entry
247 def test_creating_diary_comment
251 # Check that you can get the expected response and template for all available languages
252 # Should test that there are no <span class="translation_missing">
253 def test_listing_diary_entries
255 assert_response :success, "Should be able to list the diary entries in locale"
256 assert_template 'list', "Should use the list template in locale"
257 assert_select "span[class=translation_missing]", false, "Missing translation in list of diary entries"
259 # Now try to find a specific user's diary entry
260 get :list, {:display_name => users(:normal_user).display_name}
261 assert_response :success, "Should be able to list the diary entries for a user in locale"
262 assert_template 'list', "Should use the list template for a user in locale"
263 assert_no_missing_translations
267 get :rss, {:format => :rss}
268 assert_response :success, "Should be able to get a diary RSS"
269 assert_select "rss", :count => 1 do
270 assert_select "channel", :count => 1 do
271 assert_select "channel>title", :count => 1
272 assert_select "image", :count => 1
273 assert_select "channel>item", :count => 2
278 def test_rss_language
279 get :rss, {:language => diary_entries(:normal_user_entry_1).language_code, :format => :rss}
280 assert_response :success, "Should be able to get a specific language diary RSS"
281 assert_select "rss>channel>item", :count => 1 #, "Diary entries should be filtered by language"
284 # def test_rss_nonexisting_language
285 # get :rss, {:language => 'xx', :format => :rss}
286 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
289 def test_rss_language_with_no_entries
290 get :rss, {:language => 'sl', :format => :rss}
291 assert_response :success, "Should be able to get a specific language diary RSS"
292 assert_select "rss>channel>item", :count => 0 #, "Diary entries should be filtered by language"
296 get :rss, {:display_name => users(:normal_user).display_name, :format => :rss}
297 assert_response :success, "Should be able to get a specific users diary RSS"
298 assert_select "rss>channel>item", :count => 2 #, "Diary entries should be filtered by user"
301 def test_rss_nonexisting_user
302 get :rss, {:display_name => 'fakeUsername76543', :format => :rss}
303 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
306 def test_viewing_diary_entry
307 get :view, {:display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id}
308 assert_response :success
309 assert_template 'view'