content_controller_spec.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:14k
源码类别:

Ajax

开发平台:

Others

  1. require File.dirname(__FILE__) + '/../../spec_helper'
  2. require 'http_mock'
  3. describe Admin::ContentController do
  4.   integrate_views
  5.   # Like it's a shared, need call everywhere
  6.   describe 'index action', :shared => true do
  7.     it 'should render template index' do
  8.       get 'index'
  9.       response.should render_template('index')
  10.     end
  11.     it 'should see all published in index' do
  12.       get :index, :search => {:published => '0', :published_at => '2008-08', :user_id => '2'}
  13.       response.should render_template('index')
  14.       response.should be_success
  15.     end
  16.     it 'should restrict only by searchstring' do
  17.       get :index, :search => {:searchstring => 'originally'}
  18.       assigns(:articles).should == [contents(:xmltest)]
  19.       response.should render_template('index')
  20.       response.should be_success
  21.     end
  22.     it 'should restrict by searchstring and published_at' do
  23.       get :index, :search => {:searchstring => 'originally', :published_at => '2008-08'}
  24.       assigns(:articles).should be_empty
  25.       response.should render_template('index')
  26.       response.should be_success
  27.     end
  28.   end
  29.   describe 'autosave action', :shared => true do
  30.     it 'should save new article with draft status and link to other article if first autosave' do
  31.       lambda do
  32.       lambda do
  33.         post :autosave, :article => {:allow_comments => '1',
  34.           :body_and_extended => 'my draft in autosave',
  35.           :keywords => 'mientag',
  36.           :permalink => 'big-post',
  37.           :title => 'big post',
  38.           :text_filter => 'none',
  39.           :published => '1',
  40.           :published_at => 'December 23, 2009 03:20 PM'}
  41.       end.should change(Article, :count)
  42.       end.should change(Tag, :count)
  43.       result = Article.last
  44.       result.body.should == 'my draft in autosave'
  45.       result.title.should == 'big post'
  46.       result.permalink.should == 'big-post'
  47.       result.parent_id.should be_nil
  48.     end
  49.     describe "for a published article" do
  50.       before :each do
  51.         @article = contents(:article1)
  52.         @data = {:allow_comments => @article.allow_comments,
  53.           :body_and_extended => 'my draft in autosave',
  54.           :keywords => '',
  55.           :permalink => @article.permalink,
  56.           :title => @article.title,
  57.           :text_filter => @article.text_filter,
  58.           :published => '1',
  59.           :published_at => 'December 23, 2009 03:20 PM'}
  60.       end
  61.       it 'should create a draft article with proper attributes and existing article as a parent' do
  62.         lambda do
  63.           post :autosave, :id => @article.id, :article => @data
  64.         end.should change(Article, :count)
  65.         result = Article.last
  66.         result.body.should == 'my draft in autosave'
  67.         result.title.should == @article.title
  68.         result.permalink.should == @article.permalink
  69.         result.parent_id.should == @article.id
  70.       end
  71.       it 'should not create another draft article with parent_id if article has already a draft associated' do
  72.         draft = Article.create!(@article.attributes.merge(:guid => nil, :state => 'draft', :parent_id => @article.id))
  73.         lambda do
  74.           post :autosave, :id => @article.id, :article => @data
  75.         end.should_not change(Article, :count)
  76.         Article.last.parent_id.should == @article.id
  77.       end
  78.       it 'should create a draft with the same permalink even if the title has changed' do
  79.         @data[:title] = @article.title + " more stuff"
  80.         lambda do
  81.           post :autosave, :id => @article.id, :article => @data
  82.         end.should change(Article, :count)
  83.         result = Article.last
  84.         result.parent_id.should == @article.id
  85.         result.permalink.should == @article.permalink
  86.       end
  87.     end
  88.   end
  89.   describe 'insert_editor action' do
  90.     before do
  91.       @user = users(:tobi)
  92.       request.session = { :user => @user.id }
  93.     end
  94.     it 'should render _simple_editor' do
  95.       get(:insert_editor, :editor => 'simple')
  96.       response.should render_template('_simple_editor')
  97.     end
  98.     it 'should render _visual_editor' do
  99.       get(:insert_editor, :editor => 'visual')
  100.       response.should render_template('_visual_editor')
  101.     end
  102.   end
  103.   describe 'new action', :shared => true do
  104.     it 'should render new with get' do
  105.       get :new
  106.       response.should render_template('new')
  107.       assert_template_has 'article'
  108.     end
  109.     def base_article(options={})
  110.       { :title => "posted via tests!",
  111.         :body => "A good body",
  112.         :keywords => "tagged",
  113.         :allow_comments => '1',
  114.         :allow_pings => '1' }.merge(options)
  115.     end
  116.     it 'should create article with no comments' do
  117.       post(:new, 'article' => base_article({:allow_comments => '0'}),
  118.                  'categories' => [categories(:software).id])
  119.       assigns(:article).should_not be_allow_comments
  120.       assigns(:article).should be_allow_pings
  121.       assigns(:article).should be_published
  122.     end
  123.     it 'should create article with no pings' do
  124.       post(:new, 'article' => {:allow_pings => '0'},
  125.                  'categories' => [categories(:software).id])
  126.       assigns(:article).should be_allow_comments
  127.       assigns(:article).should_not be_allow_pings
  128.       assigns(:article).should be_published
  129.     end
  130.     it 'should create' do
  131.       begin
  132.         u = users(:randomuser)
  133.         u.notify_via_email = true
  134.         u.notify_on_new_articles = true
  135.         u.save!
  136.         ActionMailer::Base.perform_deliveries = true
  137.         ActionMailer::Base.deliveries = []
  138.         category = Factory(:category)
  139.         emails = ActionMailer::Base.deliveries
  140.         assert_difference 'Article.count_published_articles' do
  141.           tags = ['foo', 'bar', 'baz bliz', 'gorp gack gar']
  142.           post :new,
  143.             'article' => base_article(:keywords => tags) ,
  144.             'categories' => [category.id]
  145.           assert_response :redirect, :action => 'show'
  146.         end
  147.         new_article = Article.last
  148.         assert_equal @user, new_article.user
  149.         assert_equal 1, new_article.categories.size
  150.         assert_equal [category], new_article.categories
  151.         assert_equal 4, new_article.tags.size
  152.         assert_equal(1, emails.size)
  153.         assert_equal('randomuser@example.com', emails.first.to[0])
  154.       ensure
  155.         ActionMailer::Base.perform_deliveries = false
  156.       end
  157.     end
  158.     it 'should create article in future' do
  159.       assert_no_difference 'Article.count_published_articles' do
  160.         post(:new,
  161.              :article =>  base_article(:published_at => Time.now + 1.hour) )
  162.         assert_response :redirect, :action => 'show'
  163.         assigns(:article).should_not be_published
  164.       end
  165.       assert_equal 1, Trigger.count
  166.     end
  167.     it 'should create a filtered article' do
  168.       body = "body via *markdown*"
  169.       extended="*foo*"
  170.       post :new, 'article' => { :title => "another test", :body => body, :extended => extended}
  171.       assert_response :redirect, :action => 'show'
  172.       new_article = Article.find(:first, :order => "created_at DESC")
  173.       assert_equal body, new_article.body
  174.       assert_equal extended, new_article.extended
  175.       assert_equal "markdown", new_article.text_filter.name
  176.       assert_equal "<p>body via <em>markdown</em></p>", new_article.html(:body)
  177.       assert_equal "<p><em>foo</em></p>", new_article.html(:extended)
  178.     end
  179.   end
  180.   describe 'destroy action', :shared => true do
  181.     it 'should_not destroy article by get' do
  182.       assert_no_difference 'Article.count' do
  183.         art_id = @article.id
  184.         assert_not_nil Article.find(art_id)
  185.         get :destroy, 'id' => art_id
  186.         response.should be_success
  187.       end
  188.     end
  189.     it 'should destroy article by post' do
  190.       assert_difference 'Article.count', -1 do
  191.         art_id = @article.id
  192.         post :destroy, 'id' => art_id
  193.         response.should redirect_to(:action => 'index')
  194.         lambda{
  195.           article = Article.find(art_id)
  196.         }.should raise_error(ActiveRecord::RecordNotFound)
  197.       end
  198.     end
  199.   end
  200.   describe 'with admin connection' do
  201.     before do
  202.       @user = users(:tobi)
  203.       @article = contents(:article1)
  204.       request.session = { :user => @user.id }
  205.     end
  206.     it_should_behave_like 'index action'
  207.     it_should_behave_like 'new action'
  208.     it_should_behave_like 'destroy action'
  209.     it_should_behave_like 'autosave action'
  210.     describe 'edit action' do
  211.       it 'should edit article' do
  212.         get :edit, 'id' => contents(:article1).id
  213.         response.should render_template('new')
  214.         assert_template_has 'article'
  215.         assigns(:article).should be_valid
  216.         response.should have_text(/body/)
  217.         response.should have_text(/extended content/)
  218.       end
  219.       it 'should update article by edit action' do
  220.         begin
  221.           ActionMailer::Base.perform_deliveries = true
  222.           emails = ActionMailer::Base.deliveries
  223.           emails.clear
  224.           art_id = contents(:article1).id
  225.           body = "another *textile* test"
  226.           post :edit, 'id' => art_id, 'article' => {:body => body, :text_filter => 'textile'}
  227.           assert_response :redirect, :action => 'show', :id => art_id
  228.           article = contents(:article1).reload
  229.           article.text_filter.name.should == "textile"
  230.           body.should == article.body
  231.           emails.size.should == 0
  232.         ensure
  233.           ActionMailer::Base.perform_deliveries = false
  234.         end
  235.       end
  236.       it 'should allow updating body_and_extended' do
  237.         article = contents(:article1)
  238.         post :edit, 'id' => article.id, 'article' => {
  239.           'body_and_extended' => 'foo<!--more-->bar<!--more-->baz'
  240.         }
  241.         assert_response :redirect
  242.         article.reload
  243.         article.body.should == 'foo'
  244.         article.extended.should == 'bar<!--more-->baz'
  245.       end
  246.       it 'should delete draft about this article if update' do
  247.         article = contents(:article1)
  248.         draft = Article.create!(article.attributes.merge(:state => 'draft', :parent_id => article.id, :guid => nil))
  249.         lambda do
  250.           post :edit, 'id' => article.id, 'article' => { 'title' => 'new'}
  251.         end.should change(Article, :count).by(-1)
  252.         Article.should_not be_exists({:id => draft.id})
  253.       end
  254.       it 'should delete all draft about this article if update not happen but why not' do
  255.         article = contents(:article1)
  256.         draft = Article.create!(article.attributes.merge(:state => 'draft', :parent_id => article.id, :guid => nil))
  257.         draft_2 = Article.create!(article.attributes.merge(:state => 'draft', :parent_id => article.id, :guid => nil))
  258.         lambda do
  259.           post :edit, 'id' => article.id, 'article' => { 'title' => 'new'}
  260.         end.should change(Article, :count).by(-2)
  261.         Article.should_not be_exists({:id => draft.id})
  262.         Article.should_not be_exists({:id => draft_2.id})
  263.       end
  264.     end
  265.     describe 'resource_add action' do
  266.       it 'should add resource' do
  267.         art_id = contents(:article1).id
  268.         get :resource_add, :id => art_id, :resource_id => resources(:resource1).id
  269.         response.should render_template('_show_resources')
  270.         assigns(:article).should be_valid
  271.         assigns(:resource).should be_valid
  272.         assert Article.find(art_id).resources.include?(resources(:resource1))
  273.         assert_not_nil assigns(:article)
  274.         assert_not_nil assigns(:resource)
  275.         assert_not_nil assigns(:resources)
  276.       end
  277.     end
  278.     describe 'resource_remove action' do
  279.       it 'should remove resource' do
  280.         art_id = contents(:article1).id
  281.         get :resource_remove, :id => art_id, :resource_id => resources(:resource1).id
  282.         response.should render_template('_show_resources')
  283.         assert assigns(:article).valid?
  284.         assert assigns(:resource).valid?
  285.         assert !Article.find(art_id).resources.include?(resources(:resource1))
  286.         assert_not_nil assigns(:article)
  287.         assert_not_nil assigns(:resource)
  288.         assert_not_nil assigns(:resources)
  289.       end
  290.     end
  291.     describe 'auto_complete_for_article_keywords action' do
  292.       it 'should return foo for keywords fo' do
  293.         get :auto_complete_for_article_keywords, :article => {:keywords => 'fo'}
  294.         response.should be_success
  295.         response.body.should == '<ul><li>foo</li></ul>'
  296.       end
  297.       it 'should return nothing for hello' do
  298.         get :auto_complete_for_article_keywords, :article => {:keywords => 'hello'}
  299.         response.should be_success
  300.         response.body.should == '<ul></ul>'
  301.       end
  302.       it 'should return bar and baz for ba keyword' do
  303.         get :auto_complete_for_article_keywords, :article => {:keywords => 'ba'}
  304.         response.should be_success
  305.         response.body.should == '<ul><li>bar</li><li>bazz</li></ul>'
  306.       end
  307.     end
  308.   end
  309.   describe 'with publisher connection' do
  310.     before :each do
  311.       @user = users(:user_publisher)
  312.       @article = contents(:publisher_article)
  313.       request.session = {:user => @user.id}
  314.     end
  315.     it_should_behave_like 'index action'
  316.     it_should_behave_like 'new action'
  317.     it_should_behave_like 'destroy action'
  318.     describe 'edit action' do
  319.       it "should redirect if edit article doesn't his" do
  320.         get :edit, :id => contents(:article1).id
  321.         response.should redirect_to(:action => 'index')
  322.       end
  323.       it 'should edit article' do
  324.         get :edit, 'id' => contents(:publisher_article).id
  325.         response.should render_template('new')
  326.         assert_template_has 'article'
  327.         assigns(:article).should be_valid
  328.       end
  329.       it 'should update article by edit action' do
  330.         begin
  331.           ActionMailer::Base.perform_deliveries = true
  332.           emails = ActionMailer::Base.deliveries
  333.           emails.clear
  334.           art_id = contents(:publisher_article).id
  335.           body = "another *textile* test"
  336.           post :edit, 'id' => art_id, 'article' => {:body => body, :text_filter => 'textile'}
  337.           response.should redirect_to(:action => 'index')
  338.           article = contents(:publisher_article).reload
  339.           article.text_filter.name.should == "textile"
  340.           body.should == article.body
  341.           emails.size.should == 0
  342.         ensure
  343.           ActionMailer::Base.perform_deliveries = false
  344.         end
  345.       end
  346.     end
  347.     describe 'destroy action can be access' do
  348.       it 'should redirect when want destroy article' do
  349.         assert_no_difference 'Article.count' do
  350.           get :destroy, :id => contents(:article1)
  351.           response.should redirect_to(:action => 'index')
  352.         end
  353.       end
  354.       it 'should redirect when want destroy article' do
  355.         assert_no_difference 'Article.count' do
  356.           post :destroy, :id => contents(:article1)
  357.           response.should redirect_to(:action => 'index')
  358.         end
  359.       end
  360.     end
  361.   end
  362. end