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