public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
technoweenie (author)
Mon Sep 04 00:28:36 -0700 2006
commit  697616519d9e38ac1ae7a367340217eb39d73a0d
tree    aa8d796e0796043ca98fa817d2ec04fe6b05546c
parent  26e849d40a5b40cb207afb0809c97062831f6840
mephisto / test / functional / admin / articles_controller_test.rb
100644 360 lines (309 sloc) 14.576 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
require File.dirname(__FILE__) + '/../../test_helper'
require 'admin/articles_controller'
 
# Re-raise errors caught by the controller.
class Admin::ArticlesController; def rescue_action(e) raise e end; end
 
class Admin::ArticlesControllerTest < Test::Unit::TestCase
  fixtures :contents, :content_versions, :sections, :assigned_sections, :users, :sites, :tags, :taggings
 
  def setup
    @controller = Admin::ArticlesController.new
    @request = ActionController::TestRequest.new
    @response = ActionController::TestResponse.new
    login_as :quentin
    FileUtils.mkdir_p ASSET_PATH
  end
 
  def test_should_require_login
    login_as nil
    get :index
    assert_redirected_to :controller => 'account', :action => 'login'
  end
 
  def test_should_accept_cookie_login
    login_with_cookie_as :quentin
    get :index
    assert_response :success
  end
  
  def test_should_show_articles
    get :index
    assert_equal 6, assigns(:articles).length
  end
  
  def test_should_show_articles_with_empty_seartest_should_show_checked_sectionsch
    get :index, :q => '', :filter => 'title', :section => '0'
    assert_equal 6, assigns(:articles).length
  end
 
  def test_should_search_article_titles
    get :index, :q => 'future', :filter => 'title'
    assert_response :success
    assert_models_equal [contents(:future)], assigns(:articles)
  end
 
  def test_should_search_article_tags
    get :index, :q => 'rails', :filter => 'tags'
    assert_response :success
    assert_models_equal [contents(:another)], assigns(:articles)
  end
 
  def test_should_search_article_body
    get :index, :q => 'welcome', :filter => 'body'
    assert_response :success
    assert_models_equal [contents(:welcome), contents(:another)], assigns(:articles)
  end
 
  def test_should_search_article_by_section
    get :index, :filter => 'section', :section => '2'
    assert_response :success
    assert_models_equal [contents(:welcome), contents(:about), contents(:site_map)], assigns(:articles)
  end
 
  def test_should_search_article_by_section_and_title
    get :index, :filter => 'title', :q => 'welcome', :section => '2'
    assert_response :success
    assert_models_equal [contents(:welcome)], assigns(:articles)
  end
 
  def test_should_show_home_section_first
    get :new
    assert_no_tag :tag => 'input', :attributes => { :id => 'draft' }
    assert_equal sections(:home), assigns(:sections).first
  end
 
  def test_should_show_timezone_published_date
    Time.mock! Time.local(2005, 1, 1, 10, 0, 0) do
      get :new
      assert_no_tag 'select', :attributes => { :name => 'article[expire_comments_at(1i)]' }
      assert_response :success
      assert_tag 'option', :content => '11', :attributes => { :selected => 'selected' },
        :ancestor => { :tag => 'select', :attributes => { :name => 'article[published_at(4i)]' } }
    end
  end
 
  def test_should_create_article
    Time.mock! Time.local(2005, 1, 1, 12, 0, 0) do
      assert_difference Article, :count do
        post :create, :article => { :title => "My Red Hot Car", :excerpt => "Blah Blah", :body => "Blah Blah",
          'published_at(1i)' => '2005', 'published_at(2i)' => '1', 'published_at(3i)' => '1', 'published_at(4i)' => '10' }, :submit => :save
        assert_redirected_to :action => 'index'
        assert assigns(:article).published?
        assert_equal Time.local(2005, 1, 1, 9, 0, 0).utc, assigns(:article).published_at
        assert !assigns(:article).new_record?
        assert_equal users(:quentin), assigns(:article).updater
      end
    end
  end
  
  def test_should_create_publish_event
    assert_event_created 'publish' do
      post :create, :article => { :title => "My Red Hot Car", :excerpt => "Blah Blah", :body => "Blah Blah", :published_at => Time.now }, :submit => :save
      assigns(:article).events.first
    end
  end
  
  def test_should_show_validation_error_on_invalid_create
    assert_no_difference Article, :count do
      post :create, :article => { :excerpt => "Blah Blah", :body => "Blah Blah" }, :submit => :save
      assert_response :success
      assert assigns(:article).new_record?
      assert assigns(:article).errors.on(:title)
      assert !assigns(:article).published?
    end
  end
 
  def test_should_show_correct_sections_on_invalid_create
    assert_no_difference Article, :count do
      post :create, :article => {:excerpt => "Blah Blah", :body => "Blah Blah", :section_ids => {'2' => "true", '1' => ''}}, :submit => :save
      assert_response :success
      assert assigns(:article).new_record?
      assert_tag :tag => 'input', :attributes => {:id => "article_section_ids_#{sections(:about).id.to_s}", :checked => 'checked'}
    end
  end
 
  def test_should_show_default_checked_sections
    get :new
    assert_response :success
    assert_tag 'form', :attributes => { :action => '/admin/articles/create' }
    assert_tag 'input', :attributes => { :id => "article_section_ids_#{sections(:home).id.to_s}" }
    assert_no_tag 'input', :attributes => { :id => "article_section_ids_#{sections(:about).id.to_s}", :checked => 'checked' }
  end
  
  def test_should_show_title
    get :edit, :id => contents(:welcome).id
    assert_response :success
    assert_tag 'input', :attributes => { :id => 'article_title', :value => contents(:welcome).title }
  end
 
  def test_should_edit_article_version
    get :edit, :id => contents(:welcome).id, :version => '1'
    assert_tag 'input', :attributes => { :id => 'article_title', :value => contents(:welcome).title + '!!!!!!' }
  end
 
  def test_should_show_checked_sections
    get :edit, :id => contents(:welcome).id
    assert_response :success
    assert_tag 'input', :attributes => { :id => "article_section_ids_#{sections(:home).id.to_s}" }
    assert_tag 'input', :attributes => { :id => "article_section_ids_#{sections(:about).id.to_s}" }
  
    get :edit, :id => contents(:another).id
    assert_response :success
    assert_tag 'input', :attributes => { :id => "article_section_ids_#{sections(:home).id.to_s}", :checked => 'checked' }
  end
  
  def test_should_show_published_date_selector
    get :edit, :id => contents(:welcome).id
    local_time = assigns(:article).published_at
    assert_tag 'select', :attributes => { :name => "article[#{:published_at}(1i)]" }
    [ :year, :month, :day, :hour, :min ].each_with_index do |attr, i|
      value = local_time.send(attr)
      assert_tag 'option', :attributes => { :selected => 'selected', :value =>
        (i > 2 ? local_time.send(attr).to_s.rjust(2, '0') : value.to_s) },
        :ancestor => { :tag => 'select', :attributes => { :name => "article[#{:published_at}(#{i+1}i)]" } }
    end
  end
  
  def test_edit_form_should_have_correct_post_action
    get :edit, :id => contents(:welcome).id
    assert_response :success
    assert_tag :tag => 'form', :attributes => { :action => "/admin/articles/update/#{contents(:welcome).id}" }
  end
 
  def test_show_action_previews_article
    get :show, :id => contents(:welcome).id
    assert_response :success
  end
 
  def test_should_update_article_with_correct_time
    Time.mock! Time.local(2005, 1, 1, 12, 0, 0) do
      post :update, :id => contents(:welcome).id, :article => { 'published_at(1i)' => '2005', 'published_at(2i)' => '1', 'published_at(3i)' => '1', 'published_at(4i)' => '10' }
      assert_redirected_to :action => 'index'
      assert assigns(:article).published?
      assert_equal Time.local(2005, 1, 1, 9, 0, 0).utc, assigns(:article).published_at
    end
  end
 
  def test_should_create_article_with_given_sections
    post :create, :article => { :title => "My Red Hot Car", :excerpt => "Blah Blah", :body => "Blah Blah", :section_ids => [sections(:home).id.to_s] }, :submit => :save
    assert_redirected_to :action => 'index'
    assert_equal [sections(:home)], assigns(:article).sections
  end
  
  def test_should_update_article_with_no_sections
    post :update, :id => contents(:welcome).id, :article => { :title => "My Red Hot Car", :excerpt => "Blah Blah", :body => "Blah Blah", :section_ids => [] }, :submit => :save
    assert_redirected_to :action => 'index'
    assert_equal [], assigns(:article).sections
  end
 
  def test_should_update_article_with_the_same_sections
    post :update, :id => contents(:welcome).id, :article => { :title => "My Red Hot Car", :excerpt => "Blah Blah", :body => "Blah Blah",
      :section_ids => [sections(:home), sections(:about)].map { |s| s.id.to_s } }, :submit => :save
    assert_redirected_to :action => 'index'
    assert_equal [sections(:about), sections(:home)], assigns(:article).sections
  end
 
  def test_should_create_edit_event
    assert_event_created_for :welcome, 'edit' do |article|
      post :update, :id => article.id, :article_published => true, :article => { :title => "My Red Hot Car", :published_at => 5.days.ago }, :submit => :save
      assert_redirected_to :action => 'index'
      assert !assigns(:article).new_record?
      assert assigns(:article).published?
    end
  end
  
  def test_should_update_article_with_given_sections
    login_as :arthur
    assert_difference AssignedSection, :count, -1 do
      post :update, :id => contents(:welcome).id, :article => { :title => "My Red Hot Car", :excerpt => "Blah Blah", :body => "Blah Blah", :section_ids => [sections(:home).id] }, :submit => :save
      assert_redirected_to :action => 'index'
      assert_equal [sections(:home)], assigns(:article).sections
      assert_equal users(:arthur), assigns(:article).updater
    end
  end
 
  def test_should_update_and_show_notice_for_save_and_keep_editing
    xhr :post, :update, :id => contents(:welcome).id, :article => {
      :title => "My Red Hot Car", :excerpt => "Blah Blah", :body => "Blah Blah",
      :section_ids => [sections(:home), sections(:about)].map { |s| s.id.to_s } }, :commit => 'Apply changes and keep editing'
 
    assert @response.body.grep(/Flash\.notice/)
  end
 
  def test_should_create_new_article_with_default_comment_age
    [:first, :hostess, :garden].each do |site|
      login_as :quentin do
        host! sites(site).host
        get :new
        assert_response :success
        assert_equal sites(site).comment_age, assigns(:article).comment_age, "error on #{sites(site).title}"
      end
    end
  end
 
  def test_should_show_draft_checkbox_for_new_articles
    get :new
    assert_response :success
    assert_draft_check_box
    assert_publish_date_select :hidden
  end
 
  def test_should_not_show_draft_checkbox_for_published_articles
    get :edit, :id => contents(:about)
    assert_response :success
    assert_draft_check_box
    assert_publish_date_select :hidden
  end
 
  def test_should_show_draft_checkbox_for_unpublished_articles
    get :edit, :id => contents(:draft)
    assert_response :success
    assert_draft_check_box
    assert_publish_date_select
  end
 
  def test_should_create_article_draft
    assert_difference Article, :count do
      post :create, :article => { :title => "My Red Hot Car", :excerpt => "Blah Blah", :body => "Blah Blah", :published_at => 5.days.ago }, :draft => '1'
      assert_nil @controller.params['published_at']
      assert_redirected_to :action => 'index'
      assert !assigns(:article).new_record?
      assert !assigns(:article).published?
      assert_nil assigns(:article).published_at
    end
  end
 
  def test_should_change_article_to_draft
    post :update, :id => contents(:welcome).id, :draft => '1'
      assert !assigns(:article).published?
      assert_nil assigns(:article).published_at
  end
 
  def test_should_save_article_without_revision
    assert_no_difference Article::Version, :count do
      post :update, :id => contents(:welcome).id, :article => { :title => 'Foo' }, :commit => 'Save without Revision'
    end
  end
 
  def test_should_upload_asset
    assert_difference Asset, :count, 3 do
      post :upload, :asset => { :uploaded_data => fixture_file_upload('assets/logo.png', 'image/png') }
      assert_response :success
      assert_template 'new'
    end
  end
 
  def test_should_upload_asset_and_redirect_to_article
    assert_difference Asset, :count, 3 do
      post :upload, :id => contents(:welcome).id, :asset => { :uploaded_data => fixture_file_upload('assets/logo.png', 'image/png') }
      assert_response :success
      assert_template 'edit'
      assert_equal contents(:welcome), assigns(:article)
    end
  end
 
  def test_should_not_error_on_new_article_asset_upload
    assert_no_difference Asset, :count do
      post :upload
      assert_response :success
      assert_template 'new'
    end
  end
 
  def test_should_not_error_on_article_asset_upload
    assert_no_difference Asset, :count do
      post :upload, :id => contents(:welcome).id
      assert_response :success
      assert_template 'edit'
      assert_equal contents(:welcome), assigns(:article)
    end
  end
 
  def test_should_not_create_article_when_uploading_asset
    Time.mock! Time.local(2005, 1, 1, 12, 0, 0) do
      assert_no_difference Article, :count do
        post :upload, :asset => { :uploaded_data => fixture_file_upload('assets/logo.png', 'image/png') },
          :article => { :title => "My Red Hot Car", :excerpt => "Blah Blah", :body => "Blah Blah",
          'published_at(1i)' => '2005', 'published_at(2i)' => '1', 'published_at(3i)' => '1', 'published_at(4i)' => '10' }, :submit => :save
        assert_response :success
        assert_template 'new'
        assert_valid assigns(:article)
        assert assigns(:article).new_record?
        assert_equal Time.local(2005, 1, 1, 9, 0, 0).utc, assigns(:article).published_at
        assert_equal users(:quentin), assigns(:article).updater
      end
    end
  end
 
  def teardown
    FileUtils.rm_rf ASSET_PATH
  end
 
  protected
    def assert_draft_check_box(visibility = true)
      assert_tag_visibility visibility, 'label', :attributes => { :for => 'article-draft' }
      assert_tag_visibility visibility, 'input', :attributes => { :type => 'checkbox', :id => 'article-draft', :name => 'draft', :value => '1' }
    end
 
    def assert_publish_date_select(visibility = true)
      assert_tag 'dt', :attributes => { :id => 'publish-date-lbl' }
      assert_tag 'dd', :attributes => { :id => 'publish-date' }
      assert_tag_visibility visibility, 'dt', :attributes => { :id => 'publish-date-lbl', :style => 'display:none' }
      assert_tag_visibility visibility, 'dd', :attributes => { :id => 'publish-date', :style => 'display:none' }
    end
    
    def assert_tag_visibility(visibility, *args)
      send *(args.unshift(visibility != :hidden ? :assert_tag : :assert_no_tag))
    end
end