1 require File.dirname(__FILE__) + '/../test_helper'
3 class DiaryEntryControllerTest < ActionController::TestCase
4 fixtures :users, :diary_entries, :diary_comments
6 def test_showing_new_diary_entry
8 assert_response :redirect
9 assert_redirected_to :controller => :user, :action => "login", :referer => "/diary/new"
10 # Now pretend to login by using the session hash, with the
11 # id of the person we want to login as through session(:user)=user.id
12 get(:new, nil, {'user' => users(:normal_user).id})
13 assert_response :success
16 #print @response.to_yaml
17 assert_select "html:root", :count => 1 do
18 assert_select "head", :count => 1 do
19 assert_select "title", :text => /New Diary Entry/, :count => 1
21 assert_select "body", :count => 1 do
22 assert_select "div#content", :count => 1 do
23 assert_select "h1", "New Diary Entry", :count => 1
24 # We don't care about the layout, we just care about the form fields
26 assert_select "form[action='/diary/new']", :count => 1 do
27 assert_select "input[id=diary_entry_title][name='diary_entry[title]']", :count => 1
28 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :count => 1
29 assert_select "input#latitude[name='diary_entry[latitude]'][type=text]", :count => 1
30 assert_select "input#longitude[name='diary_entry[longitude]'][type=text]", :count => 1
31 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
39 def test_editing_diary_entry
40 # Make sure that you are redirected to the login page when you are
41 # not logged in, without and with the id of the entry you want to edit
43 assert_response :redirect
44 assert_redirected_to :controller => :user, :action => "login", :referer => "/diary_entry/edit"
46 get :edit, :id => diary_entries(:normal_user_entry_1).id
47 assert_response :redirect
48 assert_redirected_to :controller => :user, :action => "login", :referer => "/diary_entry/edit"
50 # Verify that you get a not found error, when you don't pass an id
51 get(:edit, nil, {'user' => users(:normal_user).id})
52 assert_response :not_found
53 assert_select "html:root", :count => 1 do
54 assert_select "body", :count => 1 do
55 assert_select "div#content", :count => 1 do
56 assert_select "h2", :text => "No entry with the id:", :count => 1
61 # Now pass the id, and check that you can edit it, when using the same
62 # user as the person who created the entry
63 get(:edit, {:id => diary_entries(:normal_user_entry_1).id}, {'user' => users(:normal_user).id})
64 assert_response :success
65 assert_select "html:root", :count => 1 do
66 assert_select "head", :count => 1 do
67 assert_select "title", :text => /Edit diary entry/, :count => 1
69 assert_select "body", :count => 1 do
70 assert_select "div#content", :count => 1 do
71 assert_select "h1", :text => /Edit diary entry/, :count => 1
72 assert_select "form[action='/diary_entry/#{diary_entries(:normal_user_entry_1).id}/edit'][method=post]", :count => 1 do
73 assert_select "input#diary_entry_title[name='diary_entry[title]'][value='#{diary_entries(:normal_user_entry_1).title}']", :count => 1
74 assert_select "textarea#diary_entry_body[name='diary_entry[body]']", :text => diary_entries(:normal_user_entry_1).body, :count => 1
75 assert_select "select#diary_entry_language_code", :count => 1
76 assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1
77 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
78 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
79 assert_select "input", :count => 4
85 # Now lets see if you can edit the diary entry
86 new_title = "New Title"
87 new_body = "This is a new body for the diary entry"
90 new_language_code = "en"
91 post(:edit, {:id => diary_entries(:normal_user_entry_1).id, 'commit' => 'save',
92 'diary_entry'=>{'title' => new_title, 'body' => new_body, 'latitude' => new_latitude,
93 'longitude' => new_longitude, 'language_code' => new_language_code} },
94 {'user' => users(:normal_user).id})
95 assert_response :redirect
96 assert_redirected_to :action => :view, :id => diary_entries(:normal_user_entry_1).id
98 # Now check that the new data is rendered, when logged in
99 get :view, {:id => diary_entries(:normal_user_entry_1).id, :display_name => 'test'}, {'user' => users(:normal_user).id}
100 assert_response :success
101 assert_template 'diary_entry/view'
102 assert_select "html:root", :count => 1 do
103 assert_select "head", :count => 1 do
104 assert_select "title", :text => /Users' diaries | /, :count => 1
106 assert_select "body", :count => 1 do
107 assert_select "div#content", :count => 1 do
108 assert_select "h2", :text => /#{users(:normal_user).display_name}'s diary/, :count => 1
109 assert_select "b", :text => /#{new_title}/, :count => 1
110 # This next line won't work if the text has been run through the htmlize function
111 # due to formatting that could be introduced
112 assert_select "p", :text => /#{new_body}/, :count => 1
113 assert_select "span.latitude", :text => new_latitude, :count => 1
114 assert_select "span.longitude", :text => new_longitude, :count => 1
115 # As we're not logged in, check that you cannot edit
116 #print @response.body
117 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
122 # and when not logged in as the user who wrote the entry
123 get :view, {:id => diary_entries(:normal_user_entry_1).id, :display_name => 'test'}, {'user' => users(:public_user).id}
124 assert_response :success
125 assert_template 'diary_entry/view'
126 assert_select "html:root", :count => 1 do
127 assert_select "head", :count => 1 do
128 assert_select "title", :text => /Users' diaries | /, :count => 1
130 assert_select "body", :count => 1 do
131 assert_select "div#content", :count => 1 do
132 assert_select "h2", :text => /#{users(:normal_user).display_name}'s diary/, :count => 1
133 assert_select "b", :text => /#{new_title}/, :count => 1
134 # This next line won't work if the text has been run through the htmlize function
135 # due to formatting that could be introduced
136 assert_select "p", :text => /#{new_body}/, :count => 1
137 assert_select "span.latitude", :text => new_latitude, :count => 1
138 assert_select "span.longitude", :text => new_longitude, :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
144 #print @response.body
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"
154 def test_create_diary_entry
158 def test_creating_diary_comment
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
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"
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
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
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"
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"
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"
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"
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"
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'