cms_controller_test.rb
8.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
require File.dirname(__FILE__) + '/../test_helper'
require 'cms_controller'
# Re-raise errors caught by the controller.
class CmsController; def rescue_action(e) raise e end; end
class CmsControllerTest < Test::Unit::TestCase
fixtures :environments
def setup
@controller = CmsController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@profile = create_user_with_permission('testinguser', 'post_content')
login_as :testinguser
end
attr_reader :profile
should 'list top level documents on index' do
get :index, :profile => profile.identifier
assert_template 'view'
assert_equal profile, assigns(:profile)
assert_nil assigns(:article)
assert_kind_of Array, assigns(:subitems)
end
should 'be able to view a particular document' do
a = profile.articles.build(:name => 'blablabla')
a.save!
get :view, :profile => profile.identifier, :id => a.id
assert_template 'view'
assert_equal a, assigns(:article)
assert_equal [], assigns(:subitems)
assert_kind_of Array, assigns(:subitems)
end
should 'be able to edit a document' do
a = profile.articles.build(:name => 'test')
a.save!
get :edit, :profile => profile.identifier, :id => a.id
assert_template 'edit'
end
should 'be able to create a new document' do
get :new, :profile => profile.identifier
assert_response :success
assert_template 'select_article_type'
# TODO add more types here !!
[ TinyMceArticle, TextileArticle ].each do |item|
assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/cms/new?type=#{item.name}" }
end
end
should 'present edit screen after choosing article type' do
get :new, :profile => profile.identifier, :type => 'TinyMceArticle'
assert_template 'edit'
assert_tag :tag => 'form', :attributes => { :action => "/myprofile/#{profile.identifier}/cms/new", :method => /post/i }, :descendant => { :tag => "input", :attributes => { :type => 'hidden', :value => 'TinyMceArticle' }}
end
should 'be able to save a document' do
assert_difference Article, :count do
post :new, :type => 'TinyMceArticle', :profile => profile.identifier, :article => { :name => 'a test article', :body => 'the text of the article ...' }
end
end
should 'be able to set home page' do
a = profile.articles.build(:name => 'my new home page')
a.save!
assert_not_equal a, profile.home_page
post :set_home_page, :profile => profile.identifier, :id => a.id
assert_redirected_to :action => 'view', :id => a.id
profile.reload
assert_equal a, profile.home_page
end
should 'set last_changed_by when creating article' do
login_as(profile.identifier)
post :new, :type => 'TinyMceArticle', :profile => profile.identifier, :article => { :name => 'changed by me', :body => 'content ...' }
a = profile.articles.find_by_path('changed-by-me')
assert_not_nil a
assert_equal profile, a.last_changed_by
end
should 'set last_changed_by when updating article' do
other_person = create_user('otherperson').person
a = profile.articles.build(:name => 'my article')
a.last_changed_by = other_person
a.save!
login_as(profile.identifier)
post :edit, :profile => profile.identifier, :id => a.id, :article => { :body => 'new content for this article' }
a.reload
assert_equal profile, a.last_changed_by
end
should 'edit by using the correct template to display the editor depending on the mime-type' do
a = profile.articles.build(:name => 'test document')
a.save!
assert_equal 'text/html', a.mime_type
get :edit, :profile => profile.identifier, :id => a.id
assert_response :success
assert_template 'edit'
end
should 'convert mime-types to action names' do
obj = mock
obj.extend(CmsHelper)
assert_equal 'text_html', obj.mime_type_to_action_name('text/html')
assert_equal 'image', obj.mime_type_to_action_name('image')
assert_equal 'application_xnoosferosomething', obj.mime_type_to_action_name('application/x-noosfero-something')
end
should 'be able to remove article' do
a = profile.articles.build(:name => 'my-article')
a.save!
assert_difference Article, :count, -1 do
post :destroy, :profile => profile.identifier, :id => a.id
assert_redirected_to :action => 'index'
end
end
should 'be able to create a RSS feed' do
login_as(profile.identifier)
assert_difference RssFeed, :count do
post :new, :type => RssFeed.name, :profile => profile.identifier, :article => { :name => 'feed', :limit => 15, :include => 'all', :feed_item_description => 'body' }
assert_response :redirect
end
end
should 'be able to update a RSS feed' do
login_as(profile.identifier)
feed = RssFeed.create!(:name => 'myfeed', :limit => 5, :feed_item_description => 'body', :include => 'all', :profile_id => profile.id)
post :edit, :profile => profile.identifier, :id => feed.id, :article => { :limit => 77, :feed_item_description => 'abstract', :include => 'parent_and_children' }
assert_response :redirect
updated = RssFeed.find(feed.id)
assert_equal 77, updated.limit
assert_equal 'abstract', updated.feed_item_description
assert_equal 'parent_and_children', updated.include
end
should 'be able to upload a file' do
assert_difference UploadedFile, :count do
post :new, :type => UploadedFile.name, :profile => profile.identifier, :article => { :uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain')}
end
end
should 'be able to update an uploaded file' do
post :new, :type => UploadedFile.name, :profile => profile.identifier, :article => { :uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain')}
file = profile.articles.find(:first)
assert_equal 'test.txt', file.name
post :edit, :profile => profile.identifier, :id => file.id, :article => { :uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain')}
assert_equal 2, file.versions(true).size
end
should 'be able to upload an image' do
assert_difference UploadedFile, :count do
post :new, :type => UploadedFile.name, :profile => profile.identifier, :article => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}
end
end
should 'offer to create children' do
Article.any_instance.stubs(:allow_children?).returns(true)
article = Article.new(:name => 'test')
article.profile = profile
article.save!
get :view, :profile => profile.identifier, :id => article.id
assert_response :success
assert_template 'view'
assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/cms/new?parent_id=#{article.id}"}
end
should 'not offer to create children if article does not accept them' do
Article.any_instance.stubs(:allow_children?).returns(false)
article = Article.new(:name => 'test')
article.profile = profile
article.save!
get :view, :profile => profile.identifier, :id => article.id
assert_response :success
assert_template 'view'
assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/cms/new?parent_id=#{article.id}"}
end
should 'refuse to create children of non-child articles' do
Article.any_instance.stubs(:allow_children?).returns(false)
article = Article.new(:name => 'test')
article.profile = profile
article.save!
assert_no_difference UploadedFile, :count do
assert_raise ArgumentError do
post :new, :type => UploadedFile.name, :parent_id => article.id, :profile => profile.identifier, :article => { :uploaded_data => fixture_file_upload('/files/test.txt', 'text/plain')}
end
end
end
should 'be able to associate articles with categories' do
env = Environment.default
c1 = env.categories.build(:name => "Test category 1"); c1.save!
c2 = env.categories.build(:name => "Test category 2"); c2.save!
c3 = env.categories.build(:name => "Test Category 3"); c3.save!
# post is in c1 and c3
post :new, :type => TextileArticle.name, :profile => profile.identifier, :article => { :name => 'adding-categories-test', :category_ids => [ c1.id, c3.id] }
saved = TextileArticle.find_by_name('adding-categories-test')
assert_includes saved.categories, c1
assert_not_includes saved.categories, c2
assert_includes saved.categories, c3
end
end