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_entry/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_entry/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", :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"
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' => new_language} },
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_create_diary_entry
152 def test_creating_diary_comment
156 # Check that you can get the expected response and template for all available languages
157 # Should test that there are no <span class="translation_missing">
158 def test_listing_diary_entries
159 I18n.available_locales.each do |locale|
163 assert_response :success, "Should be able to list the diary entries in #{locale}"
164 assert_template 'list', "Should use the list template in #{locale}"
166 # Now try to find a specific user's diary entry
167 get :list, {:display_name => users(:normal_user).display_name}
168 assert_response :success, "Should be able to list the diary entries for a user in #{locale}"
169 assert_template 'list', "Should use the list template for a user in #{locale}"
175 assert_response :success
178 def test_viewing_diary_entry
179 get :view, {:display_name => users(:normal_user).display_name, :id => diary_entries(:normal_user_entry_1).id}
180 assert_response :success
181 assert_template 'view'