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
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
18 #print @response.to_yaml
19 assert_select "html", :count => 1 do
20 assert_select "head", :count => 1 do
21 assert_select "title", :text => /New Diary Entry/, :count => 1
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
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
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
45 assert_response :redirect
46 assert_redirected_to :controller => :user, :action => "login", :referer => "/diary_entry/edit"
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"
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", :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
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", :count => 1 do
68 assert_select "head", :count => 1 do
69 assert_select "title", :text => /Edit diary entry/, :count => 1
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 => 5
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"
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
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", :count => 1 do
105 assert_select "head", :count => 1 do
106 assert_select "title", :text => /Users' diaries | /, :count => 1
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
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", :count => 1 do
128 assert_select "head", :count => 1 do
129 assert_select "title", :text => /Users' diaries | /, :count => 1
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 "span[class=hidden show_if_user_#{users(:normal_user).id}]", :count => 1 do
141 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
146 #print @response.body
150 def test_edit_diary_entry_i18n
151 get(:edit, {:id => diary_entries(:normal_user_entry_1).id}, {'user' => users(:normal_user).id})
152 assert_response :success
153 assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
156 def test_create_diary_entry
160 def test_creating_diary_comment
164 # Check that you can get the expected response and template for all available languages
165 # Should test that there are no <span class="translation_missing">
166 def test_listing_diary_entries
168 assert_response :success, "Should be able to list the diary entries in locale"
169 assert_template 'list', "Should use the list template in locale"
170 assert_select "span[class=translation_missing]", false, "Missing translation in list of diary entries"
172 # Now try to find a specific user's diary entry
173 get :list, {:display_name => users(:normal_user).display_name}
174 assert_response :success, "Should be able to list the diary entries for a user in locale"
175 assert_template 'list', "Should use the list template for a user in locale"
176 assert_no_missing_translations
180 get :rss, {:format => :rss}
181 assert_response :success, "Should be able to get a diary RSS"
182 assert_select "rss", :count => 1 do
183 assert_select "channel", :count => 1 do
184 assert_select "channel>title", :count => 1
185 assert_select "image", :count => 1
186 assert_select "channel>item", :count => 2
191 def test_rss_language
192 get :rss, {:language => diary_entries(:normal_user_entry_1).language_code, :format => :rss}
193 assert_response :success, "Should be able to get a specific language diary RSS"
194 assert_select "rss>channel>item", :count => 1 #, "Diary entries should be filtered by language"
197 # def test_rss_nonexisting_language
198 # get :rss, {:language => 'xx', :format => :rss}
199 # assert_response :not_found, "Should not be able to get a nonexisting language diary RSS"
202 def test_rss_language_with_no_entries
203 get :rss, {:language => 'sl', :format => :rss}
204 assert_response :success, "Should be able to get a specific language diary RSS"
205 assert_select "rss>channel>item", :count => 0 #, "Diary entries should be filtered by language"
209 get :rss, {:display_name => users(:normal_user).display_name, :format => :rss}
210 assert_response :success, "Should be able to get a specific users diary RSS"
211 assert_select "rss>channel>item", :count => 2 #, "Diary entries should be filtered by user"
214 def test_rss_nonexisting_user
215 get :rss, {:display_name => 'fakeUsername76543', :format => :rss}
216 assert_response :not_found, "Should not be able to get a nonexisting users diary RSS"
219 def test_viewing_diary_entry
220 get :view, {:display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id}
221 assert_response :success
222 assert_template 'view'