##// END OF EJS Templates
Additional tests for DocumentsController....
Jean-Philippe Lang -
r8144:92e4708ee368
parent child
Show More
@@ -1,151 +1,167
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
19
20 class DocumentsControllerTest < ActionController::TestCase
20 class DocumentsControllerTest < ActionController::TestCase
21 fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, :documents, :enumerations
21 fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, :documents, :enumerations
22
22
23 def setup
23 def setup
24 User.current = nil
24 User.current = nil
25 end
25 end
26
26
27 def test_index
27 def test_index
28 # Sets a default category
28 # Sets a default category
29 e = Enumeration.find_by_name('Technical documentation')
29 e = Enumeration.find_by_name('Technical documentation')
30 e.update_attributes(:is_default => true)
30 e.update_attributes(:is_default => true)
31
31
32 get :index, :project_id => 'ecookbook'
32 get :index, :project_id => 'ecookbook'
33 assert_response :success
33 assert_response :success
34 assert_template 'index'
34 assert_template 'index'
35 assert_not_nil assigns(:grouped)
35 assert_not_nil assigns(:grouped)
36
36
37 # Default category selected in the new document form
37 # Default category selected in the new document form
38 assert_tag :select, :attributes => {:name => 'document[category_id]'},
38 assert_tag :select, :attributes => {:name => 'document[category_id]'},
39 :child => {:tag => 'option', :attributes => {:selected => 'selected'},
39 :child => {:tag => 'option', :attributes => {:selected => 'selected'},
40 :content => 'Technical documentation'}
40 :content => 'Technical documentation'}
41
41
42 assert ! DocumentCategory.find(16).active?
42 assert ! DocumentCategory.find(16).active?
43 assert_no_tag :option, :attributes => {:value => '16'},
43 assert_no_tag :option, :attributes => {:value => '16'},
44 :parent => {:tag => 'select', :attributes => {:id => 'document_category_id'} }
44 :parent => {:tag => 'select', :attributes => {:id => 'document_category_id'} }
45 end
45 end
46
46
47 def test_index_grouped_by_date
47 def test_index_grouped_by_date
48 get :index, :project_id => 'ecookbook', :sort_by => 'date'
48 get :index, :project_id => 'ecookbook', :sort_by => 'date'
49 assert_response :success
49 assert_response :success
50 assert_tag 'h3', :content => '2007-02-12'
50 assert_tag 'h3', :content => '2007-02-12'
51 end
51 end
52
52
53 def test_index_grouped_by_title
53 def test_index_grouped_by_title
54 get :index, :project_id => 'ecookbook', :sort_by => 'title'
54 get :index, :project_id => 'ecookbook', :sort_by => 'title'
55 assert_response :success
55 assert_response :success
56 assert_tag 'h3', :content => 'T'
56 assert_tag 'h3', :content => 'T'
57 end
57 end
58
58
59 def test_index_grouped_by_author
59 def test_index_grouped_by_author
60 get :index, :project_id => 'ecookbook', :sort_by => 'author'
60 get :index, :project_id => 'ecookbook', :sort_by => 'author'
61 assert_response :success
61 assert_response :success
62 assert_tag 'h3', :content => 'John Smith'
62 assert_tag 'h3', :content => 'John Smith'
63 end
63 end
64
64
65 def test_index_with_long_description
65 def test_index_with_long_description
66 # adds a long description to the first document
66 # adds a long description to the first document
67 doc = documents(:documents_001)
67 doc = documents(:documents_001)
68 doc.update_attributes(:description => <<LOREM)
68 doc.update_attributes(:description => <<LOREM)
69 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
69 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
70
70
71 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.
71 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.
72 LOREM
72 LOREM
73
73
74 get :index, :project_id => 'ecookbook'
74 get :index, :project_id => 'ecookbook'
75 assert_response :success
75 assert_response :success
76 assert_template 'index'
76 assert_template 'index'
77
77
78 # should only truncate on new lines to avoid breaking wiki formatting
78 # should only truncate on new lines to avoid breaking wiki formatting
79 assert_select '.wiki p', :text => (doc.description.split("\n").first + '...')
79 assert_select '.wiki p', :text => (doc.description.split("\n").first + '...')
80 assert_select '.wiki p', :text => Regexp.new(Regexp.escape("EndOfLineHere..."))
80 assert_select '.wiki p', :text => Regexp.new(Regexp.escape("EndOfLineHere..."))
81 end
81 end
82
82
83 def test_show
83 def test_show
84 get :show, :id => 1
84 get :show, :id => 1
85 assert_response :success
85 assert_response :success
86 assert_template 'show'
86 assert_template 'show'
87 end
87 end
88
88
89 def test_new
89 def test_new
90 @request.session[:user_id] = 2
90 @request.session[:user_id] = 2
91 get :new, :project_id => 1
91 get :new, :project_id => 1
92 assert_response :success
92 assert_response :success
93 assert_template 'new'
93 assert_template 'new'
94 end
94 end
95
95
96 def test_create_with_one_attachment
96 def test_create_with_one_attachment
97 ActionMailer::Base.deliveries.clear
97 ActionMailer::Base.deliveries.clear
98 Setting.notified_events << 'document_added'
98 Setting.notified_events << 'document_added'
99 @request.session[:user_id] = 2
99 @request.session[:user_id] = 2
100 set_tmp_attachments_directory
100 set_tmp_attachments_directory
101
101
102 post :create, :project_id => 'ecookbook',
102 post :create, :project_id => 'ecookbook',
103 :document => { :title => 'DocumentsControllerTest#test_post_new',
103 :document => { :title => 'DocumentsControllerTest#test_post_new',
104 :description => 'This is a new document',
104 :description => 'This is a new document',
105 :category_id => 2},
105 :category_id => 2},
106 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
106 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
107
107
108 assert_redirected_to '/projects/ecookbook/documents'
108 assert_redirected_to '/projects/ecookbook/documents'
109
109
110 document = Document.find_by_title('DocumentsControllerTest#test_post_new')
110 document = Document.find_by_title('DocumentsControllerTest#test_post_new')
111 assert_not_nil document
111 assert_not_nil document
112 assert_equal Enumeration.find(2), document.category
112 assert_equal Enumeration.find(2), document.category
113 assert_equal 1, document.attachments.size
113 assert_equal 1, document.attachments.size
114 assert_equal 'testfile.txt', document.attachments.first.filename
114 assert_equal 'testfile.txt', document.attachments.first.filename
115 assert_equal 1, ActionMailer::Base.deliveries.size
115 assert_equal 1, ActionMailer::Base.deliveries.size
116 end
116 end
117
117
118 def test_create_with_failure
119 @request.session[:user_id] = 2
120 assert_no_difference 'Document.count' do
121 post :create, :project_id => 'ecookbook', :document => { :title => ''}
122 end
123 assert_response :success
124 assert_template 'new'
125 end
126
118 def test_edit
127 def test_edit
119 @request.session[:user_id] = 2
128 @request.session[:user_id] = 2
120 get :edit, :id => 1
129 get :edit, :id => 1
121 assert_response :success
130 assert_response :success
122 assert_template 'edit'
131 assert_template 'edit'
123 end
132 end
124
133
125 def test_update
134 def test_update
126 @request.session[:user_id] = 2
135 @request.session[:user_id] = 2
127 put :update, :id => 1, :document => {:title => 'test_update'}
136 put :update, :id => 1, :document => {:title => 'test_update'}
128 assert_redirected_to '/documents/1'
137 assert_redirected_to '/documents/1'
129 document = Document.find(1)
138 document = Document.find(1)
130 assert_equal 'test_update', document.title
139 assert_equal 'test_update', document.title
131 end
140 end
132
141
142 def test_update_with_failure
143 @request.session[:user_id] = 2
144 put :update, :id => 1, :document => {:title => ''}
145 assert_response :success
146 assert_template 'edit'
147 end
148
133 def test_destroy
149 def test_destroy
134 @request.session[:user_id] = 2
150 @request.session[:user_id] = 2
135 assert_difference 'Document.count', -1 do
151 assert_difference 'Document.count', -1 do
136 delete :destroy, :id => 1
152 delete :destroy, :id => 1
137 end
153 end
138 assert_redirected_to '/projects/ecookbook/documents'
154 assert_redirected_to '/projects/ecookbook/documents'
139 assert_nil Document.find_by_id(1)
155 assert_nil Document.find_by_id(1)
140 end
156 end
141
157
142 def test_add_attachment
158 def test_add_attachment
143 @request.session[:user_id] = 2
159 @request.session[:user_id] = 2
144 assert_difference 'Attachment.count' do
160 assert_difference 'Attachment.count' do
145 post :add_attachment, :id => 1,
161 post :add_attachment, :id => 1,
146 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
162 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
147 end
163 end
148 attachment = Attachment.first(:order => 'id DESC')
164 attachment = Attachment.first(:order => 'id DESC')
149 assert_equal Document.find(1), attachment.container
165 assert_equal Document.find(1), attachment.container
150 end
166 end
151 end
167 end
General Comments 0
You need to be logged in to leave comments. Login now