Chris@1296: # Redmine - project management software Chris@1296: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@1296: # Chris@1296: # This program is free software; you can redistribute it and/or Chris@1296: # modify it under the terms of the GNU General Public License Chris@1296: # as published by the Free Software Foundation; either version 2 Chris@1296: # of the License, or (at your option) any later version. Chris@1296: # Chris@1296: # This program is distributed in the hope that it will be useful, Chris@1296: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1296: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1296: # GNU General Public License for more details. Chris@1296: # Chris@1296: # You should have received a copy of the GNU General Public License Chris@1296: # along with this program; if not, write to the Free Software Chris@1296: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1296: Chris@1296: require File.expand_path('../../test_helper', __FILE__) Chris@1296: Chris@1296: class DocumentsControllerTest < ActionController::TestCase Chris@1296: fixtures :projects, :users, :roles, :members, :member_roles, Chris@1296: :enabled_modules, :documents, :enumerations, Chris@1296: :groups_users, :attachments Chris@1296: Chris@1296: def setup Chris@1296: User.current = nil Chris@1296: end Chris@1296: Chris@1296: def test_index Chris@1296: # Sets a default category Chris@1296: e = Enumeration.find_by_name('Technical documentation') Chris@1296: e.update_attributes(:is_default => true) Chris@1296: Chris@1296: get :index, :project_id => 'ecookbook' Chris@1296: assert_response :success Chris@1296: assert_template 'index' Chris@1296: assert_not_nil assigns(:grouped) Chris@1296: Chris@1296: # Default category selected in the new document form Chris@1296: assert_tag :select, :attributes => {:name => 'document[category_id]'}, Chris@1296: :child => {:tag => 'option', :attributes => {:selected => 'selected'}, Chris@1296: :content => 'Technical documentation'} Chris@1296: Chris@1296: assert ! DocumentCategory.find(16).active? Chris@1296: assert_no_tag :option, :attributes => {:value => '16'}, Chris@1296: :parent => {:tag => 'select', :attributes => {:id => 'document_category_id'} } Chris@1296: end Chris@1296: Chris@1296: def test_index_grouped_by_date Chris@1296: get :index, :project_id => 'ecookbook', :sort_by => 'date' Chris@1296: assert_response :success Chris@1296: assert_tag 'h3', :content => '2007-02-12' Chris@1296: end Chris@1296: Chris@1296: def test_index_grouped_by_title Chris@1296: get :index, :project_id => 'ecookbook', :sort_by => 'title' Chris@1296: assert_response :success Chris@1296: assert_tag 'h3', :content => 'T' Chris@1296: end Chris@1296: Chris@1296: def test_index_grouped_by_author Chris@1296: get :index, :project_id => 'ecookbook', :sort_by => 'author' Chris@1296: assert_response :success Chris@1296: assert_tag 'h3', :content => 'John Smith' Chris@1296: end Chris@1296: Chris@1296: def test_index_with_long_description Chris@1296: # adds a long description to the first document Chris@1296: doc = documents(:documents_001) Chris@1296: doc.update_attributes(:description => < 'ecookbook' Chris@1296: assert_response :success Chris@1296: assert_template 'index' Chris@1296: Chris@1296: # should only truncate on new lines to avoid breaking wiki formatting Chris@1296: assert_select '.wiki p', :text => (doc.description.split("\n").first + '...') Chris@1296: assert_select '.wiki p', :text => Regexp.new(Regexp.escape("EndOfLineHere...")) Chris@1296: end Chris@1296: Chris@1296: def test_show Chris@1296: get :show, :id => 1 Chris@1296: assert_response :success Chris@1296: assert_template 'show' Chris@1296: end Chris@1296: Chris@1296: def test_new Chris@1296: @request.session[:user_id] = 2 Chris@1296: get :new, :project_id => 1 Chris@1296: assert_response :success Chris@1296: assert_template 'new' Chris@1296: end Chris@1296: Chris@1296: def test_create_with_one_attachment Chris@1296: ActionMailer::Base.deliveries.clear Chris@1296: @request.session[:user_id] = 2 Chris@1296: set_tmp_attachments_directory Chris@1296: Chris@1296: with_settings :notified_events => %w(document_added) do Chris@1296: post :create, :project_id => 'ecookbook', Chris@1296: :document => { :title => 'DocumentsControllerTest#test_post_new', Chris@1296: :description => 'This is a new document', Chris@1296: :category_id => 2}, Chris@1296: :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}} Chris@1296: end Chris@1296: assert_redirected_to '/projects/ecookbook/documents' Chris@1296: Chris@1296: document = Document.find_by_title('DocumentsControllerTest#test_post_new') Chris@1296: assert_not_nil document Chris@1296: assert_equal Enumeration.find(2), document.category Chris@1296: assert_equal 1, document.attachments.size Chris@1296: assert_equal 'testfile.txt', document.attachments.first.filename Chris@1296: assert_equal 1, ActionMailer::Base.deliveries.size Chris@1296: end Chris@1296: Chris@1296: def test_create_with_failure Chris@1296: @request.session[:user_id] = 2 Chris@1296: assert_no_difference 'Document.count' do Chris@1296: post :create, :project_id => 'ecookbook', :document => { :title => ''} Chris@1296: end Chris@1296: assert_response :success Chris@1296: assert_template 'new' Chris@1296: end Chris@1296: Chris@1296: def test_create_non_default_category Chris@1296: @request.session[:user_id] = 2 Chris@1296: category2 = Enumeration.find_by_name('User documentation') Chris@1296: category2.update_attributes(:is_default => true) Chris@1296: category1 = Enumeration.find_by_name('Uncategorized') Chris@1296: post :create, Chris@1296: :project_id => 'ecookbook', Chris@1296: :document => { :title => 'no default', Chris@1296: :description => 'This is a new document', Chris@1296: :category_id => category1.id } Chris@1296: assert_redirected_to '/projects/ecookbook/documents' Chris@1296: doc = Document.find_by_title('no default') Chris@1296: assert_not_nil doc Chris@1296: assert_equal category1.id, doc.category_id Chris@1296: assert_equal category1, doc.category Chris@1296: end Chris@1296: Chris@1296: def test_edit Chris@1296: @request.session[:user_id] = 2 Chris@1296: get :edit, :id => 1 Chris@1296: assert_response :success Chris@1296: assert_template 'edit' Chris@1296: end Chris@1296: Chris@1296: def test_update Chris@1296: @request.session[:user_id] = 2 Chris@1296: put :update, :id => 1, :document => {:title => 'test_update'} Chris@1296: assert_redirected_to '/documents/1' Chris@1296: document = Document.find(1) Chris@1296: assert_equal 'test_update', document.title Chris@1296: end Chris@1296: Chris@1296: def test_update_with_failure Chris@1296: @request.session[:user_id] = 2 Chris@1296: put :update, :id => 1, :document => {:title => ''} Chris@1296: assert_response :success Chris@1296: assert_template 'edit' Chris@1296: end Chris@1296: Chris@1296: def test_destroy Chris@1296: @request.session[:user_id] = 2 Chris@1296: assert_difference 'Document.count', -1 do Chris@1296: delete :destroy, :id => 1 Chris@1296: end Chris@1296: assert_redirected_to '/projects/ecookbook/documents' Chris@1296: assert_nil Document.find_by_id(1) Chris@1296: end Chris@1296: Chris@1296: def test_add_attachment Chris@1296: @request.session[:user_id] = 2 Chris@1296: assert_difference 'Attachment.count' do Chris@1296: post :add_attachment, :id => 1, Chris@1296: :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}} Chris@1296: end Chris@1296: attachment = Attachment.first(:order => 'id DESC') Chris@1296: assert_equal Document.find(1), attachment.container Chris@1296: end Chris@1296: end