You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

documents_controller_test.rb 7.4KB

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