##// END OF EJS Templates
Adds functional tests for DocumentsController....
Jean-Philippe Lang -
r7885:afb84b682313
parent child
Show More
@@ -1,100 +1,151
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19 require 'documents_controller'
19 require 'documents_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
22 class DocumentsController; def rescue_action(e) raise e end; end
22 class DocumentsController; def rescue_action(e) raise e end; end
23
23
24 class DocumentsControllerTest < ActionController::TestCase
24 class DocumentsControllerTest < ActionController::TestCase
25 fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, :documents, :enumerations
25 fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, :documents, :enumerations
26
26
27 def setup
27 def setup
28 @controller = DocumentsController.new
28 @controller = DocumentsController.new
29 @request = ActionController::TestRequest.new
29 @request = ActionController::TestRequest.new
30 @response = ActionController::TestResponse.new
30 @response = ActionController::TestResponse.new
31 User.current = nil
31 User.current = nil
32 end
32 end
33
33
34 def test_index
34 def test_index
35 # Sets a default category
35 # Sets a default category
36 e = Enumeration.find_by_name('Technical documentation')
36 e = Enumeration.find_by_name('Technical documentation')
37 e.update_attributes(:is_default => true)
37 e.update_attributes(:is_default => true)
38
38
39 get :index, :project_id => 'ecookbook'
39 get :index, :project_id => 'ecookbook'
40 assert_response :success
40 assert_response :success
41 assert_template 'index'
41 assert_template 'index'
42 assert_not_nil assigns(:grouped)
42 assert_not_nil assigns(:grouped)
43
43
44 # Default category selected in the new document form
44 # Default category selected in the new document form
45 assert_tag :select, :attributes => {:name => 'document[category_id]'},
45 assert_tag :select, :attributes => {:name => 'document[category_id]'},
46 :child => {:tag => 'option', :attributes => {:selected => 'selected'},
46 :child => {:tag => 'option', :attributes => {:selected => 'selected'},
47 :content => 'Technical documentation'}
47 :content => 'Technical documentation'}
48
48
49 assert ! DocumentCategory.find(16).active?
49 assert ! DocumentCategory.find(16).active?
50 assert_no_tag :option, :attributes => {:value => '16'},
50 assert_no_tag :option, :attributes => {:value => '16'},
51 :parent => {:tag => 'select', :attributes => {:id => 'document_category_id'} }
51 :parent => {:tag => 'select', :attributes => {:id => 'document_category_id'} }
52 end
52 end
53
53
54 def test_index_grouped_by_date
55 get :index, :project_id => 'ecookbook', :sort_by => 'date'
56 assert_response :success
57 assert_tag 'h3', :content => '2007-02-12'
58 end
59
60 def test_index_grouped_by_title
61 get :index, :project_id => 'ecookbook', :sort_by => 'title'
62 assert_response :success
63 assert_tag 'h3', :content => 'T'
64 end
65
66 def test_index_grouped_by_author
67 get :index, :project_id => 'ecookbook', :sort_by => 'author'
68 assert_response :success
69 assert_tag 'h3', :content => 'John Smith'
70 end
71
54 def test_index_with_long_description
72 def test_index_with_long_description
55 # adds a long description to the first document
73 # adds a long description to the first document
56 doc = documents(:documents_001)
74 doc = documents(:documents_001)
57 doc.update_attributes(:description => <<LOREM)
75 doc.update_attributes(:description => <<LOREM)
58 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut egestas, mi vehicula varius varius, ipsum massa fermentum orci, eget tristique ante sem vel mi. Nulla facilisi. Donec enim libero, luctus ac sagittis sit amet, vehicula sagittis magna. Duis ultrices molestie ante, eget scelerisque sem iaculis vitae. Etiam fermentum mauris vitae metus pharetra condimentum fermentum est pretium. Proin sollicitudin elementum quam quis pharetra. Aenean facilisis nunc quis elit volutpat mollis. Aenean eleifend varius euismod. Ut dolor est, congue eget dapibus eget, elementum eu odio. Integer et lectus neque, nec scelerisque nisi. EndOfLineHere
76 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut egestas, mi vehicula varius varius, ipsum massa fermentum orci, eget tristique ante sem vel mi. Nulla facilisi. Donec enim libero, luctus ac sagittis sit amet, vehicula sagittis magna. Duis ultrices molestie ante, eget scelerisque sem iaculis vitae. Etiam fermentum mauris vitae metus pharetra condimentum fermentum est pretium. Proin sollicitudin elementum quam quis pharetra. Aenean facilisis nunc quis elit volutpat mollis. Aenean eleifend varius euismod. Ut dolor est, congue eget dapibus eget, elementum eu odio. Integer et lectus neque, nec scelerisque nisi. EndOfLineHere
59
77
60 Vestibulum non velit mi. Aliquam scelerisque libero ut nulla fringilla a sollicitudin magna rhoncus. Praesent a nunc lorem, ac porttitor eros. Sed ac diam nec neque interdum adipiscing quis quis justo. Donec arcu nunc, fringilla eu dictum at, venenatis ac sem. Vestibulum quis elit urna, ac mattis sapien. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
78 Vestibulum non velit mi. Aliquam scelerisque libero ut nulla fringilla a sollicitudin magna rhoncus. Praesent a nunc lorem, ac porttitor eros. Sed ac diam nec neque interdum adipiscing quis quis justo. Donec arcu nunc, fringilla eu dictum at, venenatis ac sem. Vestibulum quis elit urna, ac mattis sapien. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
61 LOREM
79 LOREM
62
80
63 get :index, :project_id => 'ecookbook'
81 get :index, :project_id => 'ecookbook'
64 assert_response :success
82 assert_response :success
65 assert_template 'index'
83 assert_template 'index'
66
84
67 # should only truncate on new lines to avoid breaking wiki formatting
85 # should only truncate on new lines to avoid breaking wiki formatting
68 assert_select '.wiki p', :text => (doc.description.split("\n").first + '...')
86 assert_select '.wiki p', :text => (doc.description.split("\n").first + '...')
69 assert_select '.wiki p', :text => Regexp.new(Regexp.escape("EndOfLineHere..."))
87 assert_select '.wiki p', :text => Regexp.new(Regexp.escape("EndOfLineHere..."))
70 end
88 end
71
89
90 def test_show
91 get :show, :id => 1
92 assert_response :success
93 assert_template 'show'
94 end
95
72 def test_new_with_one_attachment
96 def test_new_with_one_attachment
73 ActionMailer::Base.deliveries.clear
97 ActionMailer::Base.deliveries.clear
74 Setting.notified_events << 'document_added'
98 Setting.notified_events << 'document_added'
75 @request.session[:user_id] = 2
99 @request.session[:user_id] = 2
76 set_tmp_attachments_directory
100 set_tmp_attachments_directory
77
101
78 post :new, :project_id => 'ecookbook',
102 post :new, :project_id => 'ecookbook',
79 :document => { :title => 'DocumentsControllerTest#test_post_new',
103 :document => { :title => 'DocumentsControllerTest#test_post_new',
80 :description => 'This is a new document',
104 :description => 'This is a new document',
81 :category_id => 2},
105 :category_id => 2},
82 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
106 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
83
107
84 assert_redirected_to '/projects/ecookbook/documents'
108 assert_redirected_to '/projects/ecookbook/documents'
85
109
86 document = Document.find_by_title('DocumentsControllerTest#test_post_new')
110 document = Document.find_by_title('DocumentsControllerTest#test_post_new')
87 assert_not_nil document
111 assert_not_nil document
88 assert_equal Enumeration.find(2), document.category
112 assert_equal Enumeration.find(2), document.category
89 assert_equal 1, document.attachments.size
113 assert_equal 1, document.attachments.size
90 assert_equal 'testfile.txt', document.attachments.first.filename
114 assert_equal 'testfile.txt', document.attachments.first.filename
91 assert_equal 1, ActionMailer::Base.deliveries.size
115 assert_equal 1, ActionMailer::Base.deliveries.size
92 end
116 end
93
117
118 def test_edit
119 @request.session[:user_id] = 2
120 get :edit, :id => 1
121 assert_response :success
122 assert_template 'edit'
123 end
124
125 def test_update
126 @request.session[:user_id] = 2
127 post :edit, :id => 1, :document => {:title => 'test_update'}
128 assert_redirected_to '/documents/1'
129 document = Document.find(1)
130 assert_equal 'test_update', document.title
131 end
132
94 def test_destroy
133 def test_destroy
95 @request.session[:user_id] = 2
134 @request.session[:user_id] = 2
96 post :destroy, :id => 1
135 assert_difference 'Document.count', -1 do
136 post :destroy, :id => 1
137 end
97 assert_redirected_to '/projects/ecookbook/documents'
138 assert_redirected_to '/projects/ecookbook/documents'
98 assert_nil Document.find_by_id(1)
139 assert_nil Document.find_by_id(1)
99 end
140 end
141
142 def test_add_attachment
143 @request.session[:user_id] = 2
144 assert_difference 'Attachment.count' do
145 post :add_attachment, :id => 1,
146 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
147 end
148 attachment = Attachment.first(:order => 'id DESC')
149 assert_equal Document.find(1), attachment.container
150 end
100 end
151 end
General Comments 0
You need to be logged in to leave comments. Login now