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.

issues_helper_test.rb 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 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 File.expand_path('../../test_helper', __FILE__)
  19. class IssuesHelperTest < Redmine::HelperTest
  20. include IssuesHelper
  21. include CustomFieldsHelper
  22. include ERB::Util
  23. include Rails.application.routes.url_helpers
  24. fixtures :projects, :trackers, :issue_statuses, :issues,
  25. :enumerations, :users, :issue_categories,
  26. :projects_trackers,
  27. :roles,
  28. :member_roles,
  29. :members,
  30. :enabled_modules,
  31. :custom_fields,
  32. :attachments,
  33. :versions, :workflows
  34. def test_issue_heading
  35. assert_equal "Bug #1", issue_heading(Issue.find(1))
  36. end
  37. def test_issues_destroy_confirmation_message_with_one_root_issue
  38. assert_equal l(:text_issues_destroy_confirmation),
  39. issues_destroy_confirmation_message(Issue.find(1))
  40. end
  41. def test_issues_destroy_confirmation_message_with_an_arrayt_of_root_issues
  42. assert_equal l(:text_issues_destroy_confirmation),
  43. issues_destroy_confirmation_message(Issue.find([1, 2]))
  44. end
  45. def test_issues_destroy_confirmation_message_with_one_parent_issue
  46. Issue.find(2).update! :parent_issue_id => 1
  47. assert_equal l(:text_issues_destroy_confirmation) + "\n" +
  48. l(:text_issues_destroy_descendants_confirmation, :count => 1),
  49. issues_destroy_confirmation_message(Issue.find(1))
  50. end
  51. def test_issues_destroy_confirmation_message_with_one_parent_issue_and_its_child
  52. Issue.find(2).update! :parent_issue_id => 1
  53. assert_equal l(:text_issues_destroy_confirmation),
  54. issues_destroy_confirmation_message(Issue.find([1, 2]))
  55. end
  56. def test_issues_destroy_confirmation_message_with_issues_that_share_descendants
  57. root = Issue.generate!
  58. child = Issue.generate!(:parent_issue_id => root.id)
  59. Issue.generate!(:parent_issue_id => child.id)
  60. assert_equal l(:text_issues_destroy_confirmation) + "\n" +
  61. l(:text_issues_destroy_descendants_confirmation, :count => 1),
  62. issues_destroy_confirmation_message([root.reload, child.reload])
  63. end
  64. test 'show_detail with no_html should show a changing attribute' do
  65. detail = JournalDetail.new(:property => 'attr', :old_value => '40',
  66. :value => '100', :prop_key => 'done_ratio')
  67. assert_equal "% Done changed from 40 to 100", show_detail(detail, true)
  68. end
  69. test 'show_detail with no_html should show a new attribute' do
  70. detail = JournalDetail.new(:property => 'attr', :old_value => nil,
  71. :value => '100', :prop_key => 'done_ratio')
  72. assert_equal "% Done set to 100", show_detail(detail, true)
  73. end
  74. test 'show_detail with no_html should show a deleted attribute' do
  75. detail = JournalDetail.new(:property => 'attr', :old_value => '50',
  76. :value => nil, :prop_key => 'done_ratio')
  77. assert_equal "% Done deleted (50)", show_detail(detail, true)
  78. end
  79. test 'show_detail with html should show a changing attribute with HTML highlights' do
  80. detail = JournalDetail.new(:property => 'attr', :old_value => '40',
  81. :value => '100', :prop_key => 'done_ratio')
  82. html = show_detail(detail, false)
  83. assert_include '<strong>% Done</strong>', html
  84. assert_include '<i>40</i>', html
  85. assert_include '<i>100</i>', html
  86. end
  87. test 'show_detail with html should show a new attribute with HTML highlights' do
  88. detail = JournalDetail.new(:property => 'attr', :old_value => nil,
  89. :value => '100', :prop_key => 'done_ratio')
  90. html = show_detail(detail, false)
  91. assert_include '<strong>% Done</strong>', html
  92. assert_include '<i>100</i>', html
  93. end
  94. test 'show_detail with html should show a deleted attribute with HTML highlights' do
  95. detail = JournalDetail.new(:property => 'attr', :old_value => '50',
  96. :value => nil, :prop_key => 'done_ratio')
  97. html = show_detail(detail, false)
  98. assert_include '<strong>% Done</strong>', html
  99. assert_include '<del><i>50</i></del>', html
  100. end
  101. test 'show_detail with a start_date attribute should format the dates' do
  102. detail =
  103. JournalDetail.new(
  104. :property => 'attr',
  105. :old_value => '2010-01-01',
  106. :value => '2010-01-31',
  107. :prop_key => 'start_date'
  108. )
  109. with_settings :date_format => '%m/%d/%Y' do
  110. assert_match "01/31/2010", show_detail(detail, true)
  111. assert_match "01/01/2010", show_detail(detail, true)
  112. end
  113. end
  114. test 'show_detail with a due_date attribute should format the dates' do
  115. detail =
  116. JournalDetail.new(
  117. :property => 'attr',
  118. :old_value => '2010-01-01',
  119. :value => '2010-01-31',
  120. :prop_key => 'due_date'
  121. )
  122. with_settings :date_format => '%m/%d/%Y' do
  123. assert_match "01/31/2010", show_detail(detail, true)
  124. assert_match "01/01/2010", show_detail(detail, true)
  125. end
  126. end
  127. test 'show_detail should show old and new values with a project attribute' do
  128. User.current = User.find(2)
  129. detail = JournalDetail.new(:property => 'attr', :prop_key => 'project_id',
  130. :old_value => 1, :value => 2)
  131. assert_match 'eCookbook', show_detail(detail, true)
  132. assert_match 'OnlineStore', show_detail(detail, true)
  133. end
  134. test 'show_detail with a project attribute should show project ID if project is not visible' do
  135. detail = JournalDetail.new(:property => 'attr', :prop_key => 'project_id',
  136. :old_value => 1, :value => 2)
  137. assert_match 'eCookbook', show_detail(detail, true)
  138. assert_match '2', show_detail(detail, true)
  139. end
  140. test 'show_detail should show old and new values with a issue status attribute' do
  141. detail = JournalDetail.new(:property => 'attr', :prop_key => 'status_id',
  142. :old_value => 1, :value => 2)
  143. assert_match 'New', show_detail(detail, true)
  144. assert_match 'Assigned', show_detail(detail, true)
  145. end
  146. test 'show_detail should show old and new values with a tracker attribute' do
  147. detail = JournalDetail.new(:property => 'attr', :prop_key => 'tracker_id',
  148. :old_value => 1, :value => 2)
  149. assert_match 'Bug', show_detail(detail, true)
  150. assert_match 'Feature request', show_detail(detail, true)
  151. end
  152. test 'show_detail should show old and new values with a assigned to attribute' do
  153. detail = JournalDetail.new(:property => 'attr', :prop_key => 'assigned_to_id',
  154. :old_value => 1, :value => 2)
  155. assert_match 'Redmine Admin', show_detail(detail, true)
  156. assert_match 'John Smith', show_detail(detail, true)
  157. end
  158. test 'show_detail should show old and new values with a priority attribute' do
  159. detail = JournalDetail.new(:property => 'attr', :prop_key => 'priority_id',
  160. :old_value => 4, :value => 5)
  161. assert_match 'Low', show_detail(detail, true)
  162. assert_match 'Normal', show_detail(detail, true)
  163. end
  164. test 'show_detail should show old and new values with a category attribute' do
  165. detail = JournalDetail.new(:property => 'attr', :prop_key => 'category_id',
  166. :old_value => 1, :value => 2)
  167. assert_match 'Printing', show_detail(detail, true)
  168. assert_match 'Recipes', show_detail(detail, true)
  169. end
  170. test 'show_detail should show old and new values with a fixed version attribute' do
  171. detail = JournalDetail.new(:property => 'attr', :prop_key => 'fixed_version_id',
  172. :old_value => 1, :value => 2)
  173. assert_match '0.1', show_detail(detail, true)
  174. assert_match '1.0', show_detail(detail, true)
  175. end
  176. test 'show_detail should show old and new values with a estimated hours attribute' do
  177. detail = JournalDetail.new(:property => 'attr', :prop_key => 'estimated_hours',
  178. :old_value => '5', :value => '6.3')
  179. assert_match '5:00', show_detail(detail, true)
  180. assert_match '6:18', show_detail(detail, true)
  181. end
  182. test 'show_detail should not show values with a description attribute' do
  183. detail = JournalDetail.new(:property => 'attr', :prop_key => 'description',
  184. :old_value => 'Foo', :value => 'Bar')
  185. assert_equal 'Description updated', show_detail(detail, true)
  186. end
  187. test 'show_detail should show old and new values with a custom field' do
  188. detail = JournalDetail.new(:property => 'cf', :prop_key => '1',
  189. :old_value => 'MySQL', :value => 'PostgreSQL')
  190. assert_equal 'Database changed from MySQL to PostgreSQL', show_detail(detail, true)
  191. end
  192. test 'show_detail should not show values with a long text custom field' do
  193. field = IssueCustomField.create!(:name => "Long field", :field_format => 'text')
  194. detail = JournalDetail.new(:property => 'cf', :prop_key => field.id,
  195. :old_value => 'Foo', :value => 'Bar')
  196. assert_equal 'Long field updated', show_detail(detail, true)
  197. end
  198. test 'show_detail should show added file' do
  199. detail = JournalDetail.new(:property => 'attachment', :prop_key => '1',
  200. :old_value => nil, :value => 'error281.txt')
  201. assert_match 'error281.txt', show_detail(detail, true)
  202. end
  203. test 'show_detail should show removed file' do
  204. detail = JournalDetail.new(:property => 'attachment', :prop_key => '1',
  205. :old_value => 'error281.txt', :value => nil)
  206. assert_match 'error281.txt', show_detail(detail, true)
  207. end
  208. def test_show_detail_relation_added
  209. detail = JournalDetail.new(:property => 'relation',
  210. :prop_key => 'precedes',
  211. :value => 1)
  212. assert_equal "Precedes Bug #1: Cannot print recipes added", show_detail(detail, true)
  213. str = link_to("Bug #1", "/issues/1", :class => Issue.find(1).css_classes)
  214. assert_equal "<strong>Precedes</strong> <i>#{str}: Cannot print recipes</i> added",
  215. show_detail(detail, false)
  216. end
  217. def test_show_detail_relation_added_with_inexistant_issue
  218. inexistant_issue_number = 9999
  219. assert_nil Issue.find_by_id(inexistant_issue_number)
  220. detail = JournalDetail.new(:property => 'relation',
  221. :prop_key => 'precedes',
  222. :value => inexistant_issue_number)
  223. assert_equal "Precedes Issue ##{inexistant_issue_number} added", show_detail(detail, true)
  224. assert_equal "<strong>Precedes</strong> <i>Issue ##{inexistant_issue_number}</i> added", show_detail(detail, false)
  225. end
  226. def test_show_detail_relation_added_should_not_disclose_issue_that_is_not_visible
  227. issue = Issue.generate!(:is_private => true)
  228. detail = JournalDetail.new(:property => 'relation',
  229. :prop_key => 'precedes',
  230. :value => issue.id)
  231. assert_equal "Precedes Issue ##{issue.id} added", show_detail(detail, true)
  232. assert_equal "<strong>Precedes</strong> <i>Issue ##{issue.id}</i> added", show_detail(detail, false)
  233. end
  234. def test_show_detail_relation_deleted
  235. detail = JournalDetail.new(:property => 'relation',
  236. :prop_key => 'precedes',
  237. :old_value => 1)
  238. assert_equal "Precedes deleted (Bug #1: Cannot print recipes)", show_detail(detail, true)
  239. str = link_to("Bug #1",
  240. "/issues/1",
  241. :class => Issue.find(1).css_classes)
  242. assert_equal "<strong>Precedes</strong> deleted (<i>#{str}: Cannot print recipes</i>)",
  243. show_detail(detail, false)
  244. end
  245. def test_show_detail_relation_deleted_with_inexistant_issue
  246. inexistant_issue_number = 9999
  247. assert_nil Issue.find_by_id(inexistant_issue_number)
  248. detail = JournalDetail.new(:property => 'relation',
  249. :prop_key => 'precedes',
  250. :old_value => inexistant_issue_number)
  251. assert_equal "Precedes deleted (Issue #9999)", show_detail(detail, true)
  252. assert_equal "<strong>Precedes</strong> deleted (<i>Issue #9999</i>)", show_detail(detail, false)
  253. end
  254. def test_show_detail_relation_deleted_should_not_disclose_issue_that_is_not_visible
  255. issue = Issue.generate!(:is_private => true)
  256. detail = JournalDetail.new(:property => 'relation',
  257. :prop_key => 'precedes',
  258. :old_value => issue.id)
  259. assert_equal "Precedes deleted (Issue ##{issue.id})", show_detail(detail, true)
  260. assert_equal "<strong>Precedes</strong> deleted (<i>Issue ##{issue.id}</i>)", show_detail(detail, false)
  261. end
  262. def test_details_to_strings_with_multiple_values_removed_from_custom_field
  263. field = IssueCustomField.generate!(:name => 'User', :field_format => 'user', :multiple => true)
  264. details = []
  265. details << JournalDetail.new(:property => 'cf', :prop_key => field.id.to_s, :old_value => '1', :value => nil)
  266. details << JournalDetail.new(:property => 'cf', :prop_key => field.id.to_s, :old_value => '3', :value => nil)
  267. assert_equal ["User deleted (Dave Lopper, Redmine Admin)"], details_to_strings(details, true)
  268. assert_equal ["<strong>User</strong> deleted (<del><i>Dave Lopper, Redmine Admin</i></del>)"], details_to_strings(details, false)
  269. end
  270. def test_details_to_strings_with_multiple_values_added_to_custom_field
  271. field = IssueCustomField.generate!(:name => 'User', :field_format => 'user', :multiple => true)
  272. details = []
  273. details << JournalDetail.new(:property => 'cf', :prop_key => field.id.to_s, :old_value => nil, :value => '1')
  274. details << JournalDetail.new(:property => 'cf', :prop_key => field.id.to_s, :old_value => nil, :value => '3')
  275. assert_equal ["User Dave Lopper, Redmine Admin added"], details_to_strings(details, true)
  276. assert_equal ["<strong>User</strong> <i>Dave Lopper, Redmine Admin</i> added"], details_to_strings(details, false)
  277. end
  278. def test_details_to_strings_with_multiple_values_added_and_removed_from_custom_field
  279. field = IssueCustomField.generate!(:name => 'User', :field_format => 'user', :multiple => true)
  280. details = []
  281. details << JournalDetail.new(:property => 'cf', :prop_key => field.id.to_s, :old_value => nil, :value => '1')
  282. details << JournalDetail.new(:property => 'cf', :prop_key => field.id.to_s, :old_value => '2', :value => nil)
  283. details << JournalDetail.new(:property => 'cf', :prop_key => field.id.to_s, :old_value => '3', :value => nil)
  284. assert_equal(
  285. [
  286. "User Redmine Admin added",
  287. "User deleted (Dave Lopper, John Smith)"
  288. ],
  289. details_to_strings(details, true)
  290. )
  291. assert_equal(
  292. [
  293. "<strong>User</strong> <i>Redmine Admin</i> added",
  294. "<strong>User</strong> deleted (<del><i>Dave Lopper, John Smith</i></del>)"
  295. ],
  296. details_to_strings(details, false)
  297. )
  298. end
  299. def test_find_name_by_reflection_should_return_nil_for_missing_record
  300. assert_nil find_name_by_reflection('status', 99)
  301. end
  302. def test_issue_due_date_details
  303. travel_to Time.parse('2019-06-01 23:00:00 UTC') do
  304. User.current = User.first
  305. User.current.pref.update_attribute :time_zone, 'UTC'
  306. issue = Issue.generate!
  307. # due date is not set
  308. assert_nil issue_due_date_details(issue)
  309. # due date is set
  310. issue.due_date = User.current.today + 5
  311. issue.save!
  312. assert_equal '06/06/2019 (Due in 5 days)', issue_due_date_details(issue)
  313. # Don't show "Due in X days" if the issue is closed
  314. issue.status = IssueStatus.find_by_is_closed(true)
  315. issue.save!
  316. assert_equal '06/06/2019', issue_due_date_details(issue)
  317. end
  318. end
  319. def test_url_for_new_subtask
  320. issue = Issue.find(1)
  321. params = {:issue => {:parent_issue_id => issue.id, :tracker_id => issue.tracker.id}}
  322. assert_equal new_project_issue_path(issue.project, params),
  323. url_for_new_subtask(issue)
  324. end
  325. def test_issue_spent_hours_details_should_link_to_project_time_entries_depending_on_cross_project_setting
  326. %w(descendants).each do |setting|
  327. with_settings :cross_project_subtasks => setting do
  328. TimeEntry.generate!(:issue => Issue.generate!(:parent_issue_id => 1), :hours => 3)
  329. TimeEntry.generate!(:issue => Issue.generate!(:parent_issue_id => 1), :hours => 4)
  330. assert_match "href=\"/projects/ecookbook/time_entries?issue_id=~1\"", issue_spent_hours_details(Issue.find(1))
  331. end
  332. end
  333. end
  334. def test_issue_spent_hours_details_should_link_to_global_time_entries_depending_on_cross_project_setting
  335. %w(system tree hierarchy).each do |setting|
  336. with_settings :cross_project_subtasks => setting do
  337. TimeEntry.generate!(:issue => Issue.generate!(:parent_issue_id => 1), :hours => 3)
  338. TimeEntry.generate!(:issue => Issue.generate!(:parent_issue_id => 1), :hours => 4)
  339. assert_match "href=\"/time_entries?issue_id=~1\"", issue_spent_hours_details(Issue.find(1))
  340. end
  341. end
  342. end
  343. def test_render_issues_stats
  344. html = render_issues_stats(1, 1, {:issue_id => '15,16'})
  345. assert_include '<a href="/issues?issue_id=15%2C16&amp;set_filter=true&amp;status_id=%2A">2</a>', html
  346. assert_include '<a href="/issues?issue_id=15%2C16&amp;set_filter=true&amp;status_id=o">1 open</a>', html
  347. assert_include '<a href="/issues?issue_id=15%2C16&amp;set_filter=true&amp;status_id=c">1 closed</a>', html
  348. end
  349. def test_render_descendants_stats
  350. parent = Issue.generate!(:status_id => 1)
  351. child = Issue.generate!(:parent_issue_id => parent.id, :status_id => 1)
  352. Issue.generate!(:parent_issue_id => child.id, :status_id => 5)
  353. parent.reload
  354. html = render_descendants_stats(parent)
  355. assert_include "<a href=\"/issues?parent_id=~#{parent.id}&amp;set_filter=true&amp;status_id=%2A\">2</a>", html
  356. assert_include "<a href=\"/issues?parent_id=~#{parent.id}&amp;set_filter=true&amp;status_id=o\">1 open</a>", html
  357. assert_include "<a href=\"/issues?parent_id=~#{parent.id}&amp;set_filter=true&amp;status_id=c\">1 closed</a>", html
  358. end
  359. def test_render_relations_stats
  360. issue = Issue.generate!(:status_id => 1)
  361. relations = []
  362. open_issue = Issue.generate!(:status_id => 1)
  363. relations << IssueRelation.create!(:issue_from => open_issue,
  364. :issue_to => issue,
  365. :relation_type => IssueRelation::TYPE_RELATES)
  366. closed_issue = Issue.generate!(:status_id => 5)
  367. relations << IssueRelation.create!(:issue_from => closed_issue,
  368. :issue_to => issue,
  369. :relation_type => IssueRelation::TYPE_FOLLOWS)
  370. html = render_relations_stats(issue, relations)
  371. assert_include "<a href=\"/issues?issue_id=#{open_issue.id}%2C#{closed_issue.id}&amp;set_filter=true&amp;status_id=%2A\">2</a></span>", html
  372. assert_include "<a href=\"/issues?issue_id=#{open_issue.id}%2C#{closed_issue.id}&amp;set_filter=true&amp;status_id=o\">1 open</a>", html
  373. assert_include "<a href=\"/issues?issue_id=#{open_issue.id}%2C#{closed_issue.id}&amp;set_filter=true&amp;status_id=c\">1 closed</a>", html
  374. end
  375. end