Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

documents_controller_test.rb 8.8KB

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