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 "input#latitude[name='diary_entry[latitude]']", :count => 1
76 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1
77 assert_select "input[name=commit][type=submit][value=Save]", :count => 1
78 assert_select "input", :count => 4
84 # Now lets see if you can edit the diary entry
85 new_title = "New Title"
86 new_body = "This is a new body for the diary entry"
89 post(:edit, {:id => diary_entries(:normal_user_entry_1).id, 'commit' => 'save',
90 'diary_entry'=>{'title' => new_title, 'body' => new_body, 'latitude' => new_latitude, 'longitude' => new_longitude} },
91 {'user' => users(:normal_user).id})
92 assert_response :redirect
93 assert_redirected_to :action => :view, :id => diary_entries(:normal_user_entry_1).id
95 # Now check that the new data is rendered, when logged in
96 get :view, {:id => diary_entries(:normal_user_entry_1).id, :display_name => 'test'}, {'user' => users(:normal_user).id}
97 assert_response :success
98 assert_template 'diary_entry/view'
99 assert_select "html:root", :count => 1 do
100 assert_select "head", :count => 1 do
101 assert_select "title", :text => /Users' diaries | /, :count => 1
103 assert_select "body", :count => 1 do
104 assert_select "div#content", :count => 1 do
105 assert_select "h2", :text => /#{users(:normal_user).display_name}'s diary/, :count => 1
106 assert_select "b", :text => /#{new_title}/, :count => 1
107 # This next line won't work if the text has been run through the htmlize function
108 # due to formatting that could be introduced
109 assert_select "p", :text => /#{new_body}/, :count => 1
110 assert_select "span.latitude", :text => new_latitude, :count => 1
111 assert_select "span.longitude", :text => new_longitude, :count => 1
112 # As we're not logged in, check that you cannot edit
113 #print @response.body
114 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 # and when not logged in as the user who wrote the entry
120 get :view, {:id => diary_entries(:normal_user_entry_1).id, :display_name => 'test'}, {'user' => users(:second_user).id}
121 assert_response :success
122 assert_template 'diary_entry/view'
123 assert_select "html:root", :count => 1 do
124 assert_select "head", :count => 1 do
125 assert_select "title", :text => /Users' diaries | /, :count => 1
127 assert_select "body", :count => 1 do
128 assert_select "div#content", :count => 1 do
129 assert_select "h2", :text => /#{users(:normal_user).display_name}'s diary/, :count => 1
130 assert_select "b", :text => /#{new_title}/, :count => 1
131 # This next line won't work if the text has been run through the htmlize function
132 # due to formatting that could be introduced
133 assert_select "p", :text => /#{new_body}/, :count => 1
134 assert_select "span.latitude", :text => new_latitude, :count => 1
135 assert_select "span.longitude", :text => new_longitude, :count => 1
136 # As we're not logged in, check that you cannot edit
137 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 #print @response.body
145 def test_editing_creating_diary_comment
149 def test_listing_diary_entries
159 def test_viewing_diary_entry