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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2015 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 < ActionController::TestCase
  19. fixtures :projects, :users, :email_addresses, :roles, :members, :member_roles,
  20. :enabled_modules, :documents, :enumerations,
  21. :groups_users, :attachments
  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, :project_id => 'ecookbook'
  30. assert_response :success
  31. assert_template 'index'
  32. assert_not_nil assigns(:grouped)
  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, :project_id => 'ecookbook', :sort_by => 'date'
  42. assert_response :success
  43. assert_select 'h3', :text => '2007-02-12'
  44. end
  45. def test_index_grouped_by_title
  46. get :index, :project_id => 'ecookbook', :sort_by => 'title'
  47. assert_response :success
  48. assert_select 'h3', :text => 'T'
  49. end
  50. def test_index_grouped_by_author
  51. get :index, :project_id => 'ecookbook', :sort_by => 'author'
  52. assert_response :success
  53. assert_select 'h3', :text => 'John Smith'
  54. end
  55. def test_index_with_long_description
  56. # adds a long description to the first document
  57. doc = documents(:documents_001)
  58. doc.update_attributes(:description => <<LOREM)
  59. 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
  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.
  61. LOREM
  62. get :index, :project_id => 'ecookbook'
  63. assert_response :success
  64. assert_template 'index'
  65. # should only truncate on new lines to avoid breaking wiki formatting
  66. assert_select '.wiki p', :text => (doc.description.split("\n").first + '...')
  67. assert_select '.wiki p', :text => Regexp.new(Regexp.escape("EndOfLineHere..."))
  68. end
  69. def test_show
  70. get :show, :id => 1
  71. assert_response :success
  72. assert_template 'show'
  73. end
  74. def test_new
  75. @request.session[:user_id] = 2
  76. get :new, :project_id => 1
  77. assert_response :success
  78. assert_template 'new'
  79. end
  80. def test_create_with_one_attachment
  81. ActionMailer::Base.deliveries.clear
  82. @request.session[:user_id] = 2
  83. set_tmp_attachments_directory
  84. with_settings :notified_events => %w(document_added) do
  85. post :create, :project_id => 'ecookbook',
  86. :document => { :title => 'DocumentsControllerTest#test_post_new',
  87. :description => 'This is a new document',
  88. :category_id => 2},
  89. :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
  90. end
  91. assert_redirected_to '/projects/ecookbook/documents'
  92. document = Document.find_by_title('DocumentsControllerTest#test_post_new')
  93. assert_not_nil document
  94. assert_equal Enumeration.find(2), document.category
  95. assert_equal 1, document.attachments.size
  96. assert_equal 'testfile.txt', document.attachments.first.filename
  97. assert_equal 1, ActionMailer::Base.deliveries.size
  98. end
  99. def test_create_with_failure
  100. @request.session[:user_id] = 2
  101. assert_no_difference 'Document.count' do
  102. post :create, :project_id => 'ecookbook', :document => { :title => ''}
  103. end
  104. assert_response :success
  105. assert_template 'new'
  106. end
  107. def test_create_non_default_category
  108. @request.session[:user_id] = 2
  109. category2 = Enumeration.find_by_name('User documentation')
  110. category2.update_attributes(:is_default => true)
  111. category1 = Enumeration.find_by_name('Uncategorized')
  112. post :create,
  113. :project_id => 'ecookbook',
  114. :document => { :title => 'no default',
  115. :description => 'This is a new document',
  116. :category_id => category1.id }
  117. assert_redirected_to '/projects/ecookbook/documents'
  118. doc = Document.find_by_title('no default')
  119. assert_not_nil doc
  120. assert_equal category1.id, doc.category_id
  121. assert_equal category1, doc.category
  122. end
  123. def test_edit
  124. @request.session[:user_id] = 2
  125. get :edit, :id => 1
  126. assert_response :success
  127. assert_template 'edit'
  128. end
  129. def test_update
  130. @request.session[:user_id] = 2
  131. put :update, :id => 1, :document => {:title => 'test_update'}
  132. assert_redirected_to '/documents/1'
  133. document = Document.find(1)
  134. assert_equal 'test_update', document.title
  135. end
  136. def test_update_with_failure
  137. @request.session[:user_id] = 2
  138. put :update, :id => 1, :document => {:title => ''}
  139. assert_response :success
  140. assert_template 'edit'
  141. end
  142. def test_destroy
  143. @request.session[:user_id] = 2
  144. assert_difference 'Document.count', -1 do
  145. delete :destroy, :id => 1
  146. end
  147. assert_redirected_to '/projects/ecookbook/documents'
  148. assert_nil Document.find_by_id(1)
  149. end
  150. def test_add_attachment
  151. @request.session[:user_id] = 2
  152. assert_difference 'Attachment.count' do
  153. post :add_attachment, :id => 1,
  154. :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
  155. end
  156. attachment = Attachment.order('id DESC').first
  157. assert_equal Document.find(1), attachment.container
  158. end
  159. end