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