##// END OF EJS Templates
Rails4: replace deprecated Relation#first with finder options at DocumentsControllerTest...
Toshi MARUYAMA -
r12232:848edf35faea
parent child
Show More
@@ -1,186 +1,186
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class DocumentsControllerTest < ActionController::TestCase
21 21 fixtures :projects, :users, :roles, :members, :member_roles,
22 22 :enabled_modules, :documents, :enumerations,
23 23 :groups_users, :attachments
24 24
25 25 def setup
26 26 User.current = nil
27 27 end
28 28
29 29 def test_index
30 30 # Sets a default category
31 31 e = Enumeration.find_by_name('Technical documentation')
32 32 e.update_attributes(:is_default => true)
33 33
34 34 get :index, :project_id => 'ecookbook'
35 35 assert_response :success
36 36 assert_template 'index'
37 37 assert_not_nil assigns(:grouped)
38 38
39 39 # Default category selected in the new document form
40 40 assert_tag :select, :attributes => {:name => 'document[category_id]'},
41 41 :child => {:tag => 'option', :attributes => {:selected => 'selected'},
42 42 :content => 'Technical documentation'}
43 43
44 44 assert ! DocumentCategory.find(16).active?
45 45 assert_no_tag :option, :attributes => {:value => '16'},
46 46 :parent => {:tag => 'select', :attributes => {:id => 'document_category_id'} }
47 47 end
48 48
49 49 def test_index_grouped_by_date
50 50 get :index, :project_id => 'ecookbook', :sort_by => 'date'
51 51 assert_response :success
52 52 assert_tag 'h3', :content => '2007-02-12'
53 53 end
54 54
55 55 def test_index_grouped_by_title
56 56 get :index, :project_id => 'ecookbook', :sort_by => 'title'
57 57 assert_response :success
58 58 assert_tag 'h3', :content => 'T'
59 59 end
60 60
61 61 def test_index_grouped_by_author
62 62 get :index, :project_id => 'ecookbook', :sort_by => 'author'
63 63 assert_response :success
64 64 assert_tag 'h3', :content => 'John Smith'
65 65 end
66 66
67 67 def test_index_with_long_description
68 68 # adds a long description to the first document
69 69 doc = documents(:documents_001)
70 70 doc.update_attributes(:description => <<LOREM)
71 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 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 74 LOREM
75 75
76 76 get :index, :project_id => 'ecookbook'
77 77 assert_response :success
78 78 assert_template 'index'
79 79
80 80 # should only truncate on new lines to avoid breaking wiki formatting
81 81 assert_select '.wiki p', :text => (doc.description.split("\n").first + '...')
82 82 assert_select '.wiki p', :text => Regexp.new(Regexp.escape("EndOfLineHere..."))
83 83 end
84 84
85 85 def test_show
86 86 get :show, :id => 1
87 87 assert_response :success
88 88 assert_template 'show'
89 89 end
90 90
91 91 def test_new
92 92 @request.session[:user_id] = 2
93 93 get :new, :project_id => 1
94 94 assert_response :success
95 95 assert_template 'new'
96 96 end
97 97
98 98 def test_create_with_one_attachment
99 99 ActionMailer::Base.deliveries.clear
100 100 @request.session[:user_id] = 2
101 101 set_tmp_attachments_directory
102 102
103 103 with_settings :notified_events => %w(document_added) do
104 104 post :create, :project_id => 'ecookbook',
105 105 :document => { :title => 'DocumentsControllerTest#test_post_new',
106 106 :description => 'This is a new document',
107 107 :category_id => 2},
108 108 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
109 109 end
110 110 assert_redirected_to '/projects/ecookbook/documents'
111 111
112 112 document = Document.find_by_title('DocumentsControllerTest#test_post_new')
113 113 assert_not_nil document
114 114 assert_equal Enumeration.find(2), document.category
115 115 assert_equal 1, document.attachments.size
116 116 assert_equal 'testfile.txt', document.attachments.first.filename
117 117 assert_equal 1, ActionMailer::Base.deliveries.size
118 118 end
119 119
120 120 def test_create_with_failure
121 121 @request.session[:user_id] = 2
122 122 assert_no_difference 'Document.count' do
123 123 post :create, :project_id => 'ecookbook', :document => { :title => ''}
124 124 end
125 125 assert_response :success
126 126 assert_template 'new'
127 127 end
128 128
129 129 def test_create_non_default_category
130 130 @request.session[:user_id] = 2
131 131 category2 = Enumeration.find_by_name('User documentation')
132 132 category2.update_attributes(:is_default => true)
133 133 category1 = Enumeration.find_by_name('Uncategorized')
134 134 post :create,
135 135 :project_id => 'ecookbook',
136 136 :document => { :title => 'no default',
137 137 :description => 'This is a new document',
138 138 :category_id => category1.id }
139 139 assert_redirected_to '/projects/ecookbook/documents'
140 140 doc = Document.find_by_title('no default')
141 141 assert_not_nil doc
142 142 assert_equal category1.id, doc.category_id
143 143 assert_equal category1, doc.category
144 144 end
145 145
146 146 def test_edit
147 147 @request.session[:user_id] = 2
148 148 get :edit, :id => 1
149 149 assert_response :success
150 150 assert_template 'edit'
151 151 end
152 152
153 153 def test_update
154 154 @request.session[:user_id] = 2
155 155 put :update, :id => 1, :document => {:title => 'test_update'}
156 156 assert_redirected_to '/documents/1'
157 157 document = Document.find(1)
158 158 assert_equal 'test_update', document.title
159 159 end
160 160
161 161 def test_update_with_failure
162 162 @request.session[:user_id] = 2
163 163 put :update, :id => 1, :document => {:title => ''}
164 164 assert_response :success
165 165 assert_template 'edit'
166 166 end
167 167
168 168 def test_destroy
169 169 @request.session[:user_id] = 2
170 170 assert_difference 'Document.count', -1 do
171 171 delete :destroy, :id => 1
172 172 end
173 173 assert_redirected_to '/projects/ecookbook/documents'
174 174 assert_nil Document.find_by_id(1)
175 175 end
176 176
177 177 def test_add_attachment
178 178 @request.session[:user_id] = 2
179 179 assert_difference 'Attachment.count' do
180 180 post :add_attachment, :id => 1,
181 181 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
182 182 end
183 attachment = Attachment.first(:order => 'id DESC')
183 attachment = Attachment.order('id DESC').first
184 184 assert_equal Document.find(1), attachment.container
185 185 end
186 186 end
General Comments 0
You need to be logged in to leave comments. Login now