]> git.openstreetmap.org Git - rails.git/blob - test/functional/diary_entry_controller_test.rb
Always pass the session ID to the logout page
[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   def test_showing_new_diary_entry
9     @request.cookies["_osm_username"] = users(:normal_user).display_name
10
11     get :new
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
18     #print @response.body
19     
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
24       end
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
29           # that are available
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
36           end
37         end
38       end
39     end
40         
41   end
42   
43   def test_editing_diary_entry
44     @request.cookies["_osm_username"] = users(:normal_user).display_name
45
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
48     get :edit
49     assert_response :redirect
50     assert_redirected_to :controller => :user, :action => "login", :referer => "/diary_entry/edit"
51     
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"
55     
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 
63         end
64       end
65     end
66     
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
74       end
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
86           end
87         end
88       end
89     end
90     
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"
94     new_latitude = "1.1"
95     new_longitude = "2.2"
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
103     
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
111       end
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
123         end
124       end
125     end
126
127     @request.cookies["_osm_username"] = users(:public_user).display_name
128
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
136       end
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
148           end
149         end
150       end
151     end
152     #print @response.body
153     
154   end
155   
156   def test_edit_diary_entry_i18n
157     @request.cookies["_osm_username"] = users(:normal_user).display_name
158
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"
162   end
163   
164   def test_create_diary_entry
165     #post :new
166   end
167   
168   def test_creating_diary_comment
169     
170   end
171   
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
175       get :list
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"
179     
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
185   end
186   
187   def test_rss
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
195       end
196     end
197   end
198   
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"
203   end
204   
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"
208 #  end
209
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"
214   end
215
216   def test_rss_user
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"
220   end
221   
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"
225   end
226
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'
231   end
232 end