]> git.openstreetmap.org Git - rails.git/blob - test/functional/diary_entry_controller_test.rb
Patching fix to test from branch.
[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
5
6   include ActionView::Helpers::NumberHelper
7
8   def test_showing_new_diary_entry
9     get :new
10     assert_response :redirect
11     assert_redirected_to :controller => :user, :action => "login", :referer => "/diary/new"
12     # Now pretend to login by using the session hash, with the 
13     # id of the person we want to login as through session(:user)=user.id
14     get(:new, nil, {'user' => users(:normal_user).id})
15     assert_response :success
16     #print @response.body
17     
18     #print @response.to_yaml
19     assert_select "html:root", :count => 1 do
20       assert_select "head", :count => 1 do
21         assert_select "title", :text => /New Diary Entry/, :count => 1
22       end
23       assert_select "body", :count => 1 do
24         assert_select "div#content", :count => 1 do
25           assert_select "h1", "New Diary Entry", :count => 1
26           # We don't care about the layout, we just care about the form fields
27           # that are available
28           assert_select "form[action='/diary/new']", :count => 1 do
29             assert_select "input[id=diary_entry_title][name='diary_entry[title]']", :count => 1
30             assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :count => 1
31             assert_select "input#latitude[name='diary_entry[latitude]'][type=text]", :count => 1
32             assert_select "input#longitude[name='diary_entry[longitude]'][type=text]", :count => 1
33             assert_select "input[name=commit][type=submit][value=Save]", :count => 1
34           end
35         end
36       end
37     end
38         
39   end
40   
41   def test_editing_diary_entry
42     # Make sure that you are redirected to the login page when you are 
43     # not logged in, without and with the id of the entry you want to edit
44     get :edit
45     assert_response :redirect
46     assert_redirected_to :controller => :user, :action => "login", :referer => "/diary_entry/edit"
47     
48     get :edit, :id => diary_entries(:normal_user_entry_1).id
49     assert_response :redirect
50     assert_redirected_to :controller => :user, :action => "login", :referer => "/diary_entry/edit"
51     
52     # Verify that you get a not found error, when you don't pass an id
53     get(:edit, nil, {'user' => users(:normal_user).id})
54     assert_response :not_found
55     assert_select "html:root", :count => 1 do
56       assert_select "body", :count => 1 do
57         assert_select "div#content", :count => 1 do
58           assert_select "h2", :text => "No entry with the id:", :count => 1 
59         end
60       end
61     end
62     
63     # Now pass the id, and check that you can edit it, when using the same 
64     # user as the person who created the entry
65     get(:edit, {:id => diary_entries(:normal_user_entry_1).id}, {'user' => users(:normal_user).id})
66     assert_response :success
67     assert_select "html:root", :count => 1 do
68       assert_select "head", :count => 1 do
69         assert_select "title", :text => /Edit diary entry/, :count => 1
70       end
71       assert_select "body", :count => 1 do
72         assert_select "div#content", :count => 1 do 
73           assert_select "h1", :text => /Edit diary entry/, :count => 1
74           assert_select "form[action='/diary_entry/#{diary_entries(:normal_user_entry_1).id}/edit'][method=post]", :count => 1 do
75             assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{diary_entries(:normal_user_entry_1).title}']", :count => 1
76             assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => diary_entries(:normal_user_entry_1).body, :count => 1
77             assert_select "select#diary_entry_language_code", :count => 1
78             assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
79             assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
80             assert_select "input[name=commit][type=submit][value=Save]", :count => 1
81             assert_select "input", :count => 4
82           end
83         end
84       end
85     end
86     
87     # Now lets see if you can edit the diary entry
88     new_title = "New Title"
89     new_body = "This is a new body for the diary entry"
90     new_latitude = "1.1"
91     new_longitude = "2.2"
92     new_language_code = "en"
93     post(:edit, {:id => diary_entries(:normal_user_entry_1).id, 'commit' => 'save', 
94       'diary_entry'=>{'title' => new_title, 'body' => new_body, 'latitude' => new_latitude,
95       'longitude' => new_longitude, 'language_code' => new_language_code} },
96          {'user' => users(:normal_user).id})
97     assert_response :redirect
98     assert_redirected_to :action => :view, :id => diary_entries(:normal_user_entry_1).id
99     
100     # Now check that the new data is rendered, when logged in
101     get :view, {:id => diary_entries(:normal_user_entry_1).id, :display_name => 'test'}, {'user' => users(:normal_user).id}
102     assert_response :success
103     assert_template 'diary_entry/view'
104     assert_select "html:root", :count => 1 do
105       assert_select "head", :count => 1 do
106         assert_select "title", :text => /Users' diaries | /, :count => 1
107       end
108       assert_select "body", :count => 1 do
109         assert_select "div#content", :count => 1 do
110           assert_select "h2", :text => /#{users(:normal_user).display_name}'s diary/, :count => 1
111           assert_select "b", :text => /#{new_title}/, :count => 1
112           # This next line won't work if the text has been run through the htmlize function
113           # due to formatting that could be introduced
114           assert_select "p", :text => /#{new_body}/, :count => 1
115           assert_select "abbr[class=geo][title=#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}]", :count => 1
116           # As we're not logged in, check that you cannot edit
117           #print @response.body
118           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
119         end
120       end
121     end
122     
123     # and when not logged in as the user who wrote the entry
124     get :view, {:id => diary_entries(:normal_user_entry_1).id, :display_name => 'test'}, {'user' => users(:public_user).id}
125     assert_response :success
126     assert_template 'diary_entry/view'
127     assert_select "html:root", :count => 1 do
128       assert_select "head", :count => 1 do
129         assert_select "title", :text => /Users' diaries | /, :count => 1
130       end
131       assert_select "body", :count => 1 do
132         assert_select "div#content", :count => 1 do
133           assert_select "h2", :text => /#{users(:normal_user).display_name}'s diary/, :count => 1
134           assert_select "b", :text => /#{new_title}/, :count => 1
135           # This next line won't work if the text has been run through the htmlize function
136           # due to formatting that could be introduced
137           assert_select "p", :text => /#{new_body}/, :count => 1
138           assert_select "abbr[class=geo][title=#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}]", :count => 1
139           # As we're not logged in, check that you cannot edit
140           assert_select "a[href='/user/#{users(:normal_user).display_name}/diary/#{diary_entries(:normal_user_entry_1).id}/edit']", :text => "Edit this entry", :count => 0
141         end
142       end
143     end
144     #print @response.body
145     
146   end
147   
148   def test_edit_diary_entry_i18n
149     get(:edit, {:id => diary_entries(:normal_user_entry_1).id}, {'user' => users(:normal_user).id})
150     assert_response :success
151     assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
152   end
153   
154   def test_create_diary_entry
155     #post :new
156   end
157   
158   def test_creating_diary_comment
159     
160   end
161   
162   # Check that you can get the expected response and template for all available languages
163   # Should test that there are no <span class="translation_missing">
164   def test_listing_diary_entries
165       get :list
166       assert_response :success, "Should be able to list the diary entries in locale"
167       assert_template 'list', "Should use the list template in locale"
168       assert_select "span[class=translation_missing]", false, "Missing translation in list of diary entries"
169     
170       # Now try to find a specific user's diary entry
171       get :list, {:display_name => users(:normal_user).display_name}
172       assert_response :success, "Should be able to list the diary entries for a user in locale"
173       assert_template 'list', "Should use the list template for a user in locale"
174       assert_no_missing_translations
175   end
176   
177   def test_rss
178     get :rss
179     assert_response :success, "Should be able to get a diary RSS"
180     assert_select "rss:root", :count => 1 do
181       assert_select "channel", :count => 1 do
182         assert_select "channel>title", :count => 1
183         assert_select "image", :count => 1
184         assert_select "channel>item", :count => 2
185       end
186     end
187   end
188   
189   def test_rss_language
190     get :rss, {:language => diary_entries(:normal_user_entry_1).language_code}
191     assert_response :success, "Should be able to get a specific language diary RSS"
192     assert_select "rss>channel>item", :count => 1 #, "Diary entries should be filtered by language"
193   end
194   
195 #  def test_rss_nonexisting_language
196 #    get :rss, {:language => 'xx'}
197 #    assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
198 #  end
199
200   def test_rss_language_with_no_entries
201     get :rss, {:language => 'sl'}
202     assert_response :success, "Should be able to get a specific language diary RSS"
203     assert_select "rss>channel>item", :count => 0 #, "Diary entries should be filtered by language"
204   end
205
206   def test_rss_user
207     get :rss, {:display_name => users(:normal_user).display_name}
208     assert_response :success, "Should be able to get a specific users diary RSS"
209     assert_select "rss>channel>item", :count => 2 #, "Diary entries should be filtered by user"
210   end
211   
212   def test_rss_nonexisting_user
213     get :rss, {:display_name => 'fakeUsername76543'}
214     assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
215   end
216
217   def test_viewing_diary_entry
218     get :view, {:display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id}
219     assert_response :success
220     assert_template 'view'
221   end
222 end