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
8 def test_showing_new_diary_entry
9 @request.cookies["_osm_username"] = users(:normal_user).display_name
12 assert_response :redirect
13 assert_redirected_to :controller => :user, :action => "login", :referer => "/diary/new"
14 # Now pretend to login by using the session hash, with the
15 # id of the person we want to login as through session(:user)=user.id
16 get(:new, nil, {'user' => users(:normal_user).id})
17 assert_response :success
20 #print @response.to_yaml
21 assert_select "html", :count => 1 do
22 assert_select "head", :count => 1 do
23 assert_select "title", :text => /New Diary Entry/, :count => 1
25 assert_select "body", :count => 1 do
26 assert_select "div#content", :count => 1 do
27 assert_select "h1", "New Diary Entry", :count => 1
28 # We don't care about the layout, we just care about the form fields
30 assert_select "form[action='/diary/new']", :count => 1 do
31 assert_select "input[id=diary_entry_title][name='diary_entry[title]']", :count => 1
32 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :count => 1
33 assert_select "input#latitude[name='diary_entry[latitude]'][type=text]", :count => 1
34 assert_select "input#longitude[name='diary_entry[longitude]'][type=text]", :count => 1
35 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
43 def test_editing_diary_entry
44 @request.cookies["_osm_username"] = users(:normal_user).display_name
46 # Make sure that you are redirected to the login page when you are
47 # not logged in, without and with the id of the entry you want to edit
49 assert_response :redirect
50 assert_redirected_to :controller => :user, :action => "login", :referer => "/diary_entry/edit"
52 get :edit, :id => diary_entries(:normal_user_entry_1).id
53 assert_response :redirect
54 assert_redirected_to :controller => :user, :action => "login", :referer => "/diary_entry/edit"
56 # Verify that you get a not found error, when you don't pass an id
57 get(:edit, nil, {'user' => users(:normal_user).id})
58 assert_response :not_found
59 assert_select "html", :count => 1 do
60 assert_select "body", :count => 1 do
61 assert_select "div#content", :count => 1 do
62 assert_select "h2", :text => "No entry with the id:", :count => 1
67 # Now pass the id, and check that you can edit it, when using the same
68 # user as the person who created the entry
69 get(:edit, {:id => diary_entries(:normal_user_entry_1).id}, {'user' => users(:normal_user).id})
70 assert_response :success
71 assert_select "html", :count => 1 do
72 assert_select "head", :count => 1 do
73 assert_select "title", :text => /Edit diary entry/, :count => 1
75 assert_select "body", :count => 1 do
76 assert_select "div#content", :count => 1 do
77 assert_select "h1", :text => /Edit diary entry/, :count => 1
78 assert_select "form[action='/diary_entry/#{diary_entries(:normal_user_entry_1).id}/edit'][method=post]", :count => 1 do
79 assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{diary_entries(:normal_user_entry_1).title}']", :count => 1
80 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => diary_entries(:normal_user_entry_1).body, :count => 1
81 assert_select "select#diary_entry_language_code", :count => 1
82 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
83 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
84 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
85 assert_select "input", :count => 5
91 # Now lets see if you can edit the diary entry
92 new_title = "New Title"
93 new_body = "This is a new body for the diary entry"
96 new_language_code = "en"
97 post(:edit, {:id => diary_entries(:normal_user_entry_1).id, 'commit' => 'save',
98 'diary_entry'=>{'title' => new_title, 'body' => new_body, 'latitude' => new_latitude,
99 'longitude' => new_longitude, 'language_code' => new_language_code} },
100 {'user' => users(:normal_user).id})
101 assert_response :redirect
102 assert_redirected_to :action => :view, :id => diary_entries(:normal_user_entry_1).id
104 # Now check that the new data is rendered, when logged in
105 get :view, {:id => diary_entries(:normal_user_entry_1).id, :display_name => 'test'}, {'user' => users(:normal_user).id}
106 assert_response :success
107 assert_template 'diary_entry/view'
108 assert_select "html", :count => 1 do
109 assert_select "head", :count => 1 do
110 assert_select "title", :text => /Users' diaries | /, :count => 1
112 assert_select "body", :count => 1 do
113 assert_select "div#content", :count => 1 do
114 assert_select "h2", :text => /#{users(:normal_user).display_name}'s diary/, :count => 1
115 assert_select "b", :text => /#{new_title}/, :count => 1
116 # This next line won't work if the text has been run through the htmlize function
117 # due to formatting that could be introduced
118 assert_select "p", :text => /#{new_body}/, :count => 1
119 assert_select "abbr[class=geo][title=#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}]", :count => 1
120 # As we're not logged in, check that you cannot edit
121 #print @response.body
122 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
127 @request.cookies["_osm_username"] = users(:public_user).display_name
129 # and when not logged in as the user who wrote the entry
130 get :view, {:id => diary_entries(:normal_user_entry_1).id, :display_name => 'test'}, {'user' => users(:public_user).id}
131 assert_response :success
132 assert_template 'diary_entry/view'
133 assert_select "html", :count => 1 do
134 assert_select "head", :count => 1 do
135 assert_select "title", :text => /Users' diaries | /, :count => 1
137 assert_select "body", :count => 1 do
138 assert_select "div#content", :count => 1 do
139 assert_select "h2", :text => /#{users(:normal_user).display_name}'s diary/, :count => 1
140 assert_select "b", :text => /#{new_title}/, :count => 1
141 # This next line won't work if the text has been run through the htmlize function
142 # due to formatting that could be introduced
143 assert_select "p", :text => /#{new_body}/, :count => 1
144 assert_select "abbr[class=geo][title=#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}]", :count => 1
145 # As we're not logged in, check that you cannot edit
146 assert_select "span[class=hidden show_if_user_#{users(:normal_user).id}]", :count => 1 do
147 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
152 #print @response.body
156 def test_edit_diary_entry_i18n
157 @request.cookies["_osm_username"] = users(:normal_user).display_name
159 get(:edit, {:id => diary_entries(:normal_user_entry_1).id}, {'user' => users(:normal_user).id})
160 assert_response :success
161 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
164 def test_create_diary_entry
168 def test_creating_diary_comment
172 # Check that you can get the expected response and template for all available languages
173 # Should test that there are no <span class="translation_missing">
174 def test_listing_diary_entries
176 assert_response :success, "Should be able to list the diary entries in locale"
177 assert_template 'list', "Should use the list template in locale"
178 assert_select "span[class=translation_missing]", false, "Missing translation in list of diary entries"
180 # Now try to find a specific user's diary entry
181 get :list, {:display_name => users(:normal_user).display_name}
182 assert_response :success, "Should be able to list the diary entries for a user in locale"
183 assert_template 'list', "Should use the list template for a user in locale"
184 assert_no_missing_translations
188 get :rss, {:format => :rss}
189 assert_response :success, "Should be able to get a diary RSS"
190 assert_select "rss", :count => 1 do
191 assert_select "channel", :count => 1 do
192 assert_select "channel>title", :count => 1
193 assert_select "image", :count => 1
194 assert_select "channel>item", :count => 2
199 def test_rss_language
200 get :rss, {:language => diary_entries(:normal_user_entry_1).language_code, :format => :rss}
201 assert_response :success, "Should be able to get a specific language diary RSS"
202 assert_select "rss>channel>item", :count => 1 #, "Diary entries should be filtered by language"
205 # def test_rss_nonexisting_language
206 # get :rss, {:language => 'xx', :format => :rss}
207 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
210 def test_rss_language_with_no_entries
211 get :rss, {:language => 'sl', :format => :rss}
212 assert_response :success, "Should be able to get a specific language diary RSS"
213 assert_select "rss>channel>item", :count => 0 #, "Diary entries should be filtered by language"
217 get :rss, {:display_name => users(:normal_user).display_name, :format => :rss}
218 assert_response :success, "Should be able to get a specific users diary RSS"
219 assert_select "rss>channel>item", :count => 2 #, "Diary entries should be filtered by user"
222 def test_rss_nonexisting_user
223 get :rss, {:display_name => 'fakeUsername76543', :format => :rss}
224 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
227 def test_viewing_diary_entry
228 get :view, {:display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id}
229 assert_response :success
230 assert_template 'view'