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.

journal_test.rb 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- 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 JournalTest < ActiveSupport::TestCase
  20. fixtures :projects, :issues, :issue_statuses, :journals, :journal_details,
  21. :issue_relations, :workflows,
  22. :users, :members, :member_roles, :roles, :enabled_modules,
  23. :groups_users, :email_addresses,
  24. :enumerations,
  25. :projects_trackers, :trackers, :custom_fields
  26. def setup
  27. @journal = Journal.find 1
  28. User.current = nil
  29. end
  30. def test_journalized_is_an_issue
  31. issue = @journal.issue
  32. assert_kind_of Issue, issue
  33. assert_equal 1, issue.id
  34. end
  35. def test_new_status
  36. status = @journal.new_status
  37. assert_not_nil status
  38. assert_kind_of IssueStatus, status
  39. assert_equal 2, status.id
  40. end
  41. def test_create_should_send_email_notification
  42. ActionMailer::Base.deliveries.clear
  43. issue = Issue.first
  44. user = User.first
  45. journal = issue.init_journal(user, issue)
  46. assert journal.save
  47. assert_equal 2, ActionMailer::Base.deliveries.size
  48. end
  49. def test_should_not_save_journal_with_blank_notes_and_no_details
  50. journal = Journal.new(:journalized => Issue.first, :user => User.first)
  51. assert_no_difference 'Journal.count' do
  52. assert_equal false, journal.save
  53. end
  54. end
  55. def test_create_should_not_split_non_private_notes
  56. assert_difference 'Journal.count' do
  57. assert_no_difference 'JournalDetail.count' do
  58. journal = Journal.generate!(:notes => 'Notes')
  59. end
  60. end
  61. assert_difference 'Journal.count' do
  62. assert_difference 'JournalDetail.count' do
  63. journal = Journal.generate!(:notes => 'Notes', :details => [JournalDetail.new])
  64. end
  65. end
  66. assert_difference 'Journal.count' do
  67. assert_difference 'JournalDetail.count' do
  68. journal = Journal.generate!(:notes => '', :details => [JournalDetail.new])
  69. end
  70. end
  71. end
  72. def test_create_should_split_private_notes
  73. assert_difference 'Journal.count' do
  74. assert_no_difference 'JournalDetail.count' do
  75. journal = Journal.generate!(:notes => 'Notes', :private_notes => true)
  76. journal.reload
  77. assert_equal true, journal.private_notes
  78. assert_equal 'Notes', journal.notes
  79. end
  80. end
  81. assert_difference 'Journal.count', 2 do
  82. assert_difference 'JournalDetail.count' do
  83. journal = Journal.generate!(:notes => 'Notes', :private_notes => true, :details => [JournalDetail.new])
  84. journal.reload
  85. assert_equal true, journal.private_notes
  86. assert_equal 'Notes', journal.notes
  87. assert_equal 0, journal.details.size
  88. journal_with_changes = Journal.order('id DESC').offset(1).first
  89. assert_equal false, journal_with_changes.private_notes
  90. assert_nil journal_with_changes.notes
  91. assert_equal 1, journal_with_changes.details.size
  92. assert_equal journal.created_on, journal_with_changes.created_on
  93. end
  94. end
  95. assert_difference 'Journal.count' do
  96. assert_difference 'JournalDetail.count' do
  97. journal = Journal.generate!(:notes => '', :private_notes => true, :details => [JournalDetail.new])
  98. journal.reload
  99. assert_equal false, journal.private_notes
  100. assert_equal '', journal.notes
  101. assert_equal 1, journal.details.size
  102. end
  103. end
  104. end
  105. def test_create_should_add_wacher
  106. user = User.first
  107. user.pref.auto_watch_on=['issue_contributed_to']
  108. user.save
  109. journal = Journal.new(:journalized => Issue.first, :notes => 'notes', :user => user)
  110. assert_difference 'Watcher.count', 1 do
  111. assert_equal true, journal.save
  112. end
  113. end
  114. def test_create_should_not_add_watcher
  115. user = User.first
  116. user.pref.auto_watch_on=[]
  117. user.save
  118. journal = Journal.new(:journalized => Issue.first, :notes => 'notes', :user => user)
  119. assert_no_difference 'Watcher.count' do
  120. assert_equal true, journal.save
  121. end
  122. end
  123. def test_create_should_not_add_anonymous_as_watcher
  124. Role.anonymous.add_permission!(:add_issue_watchers)
  125. user = User.anonymous
  126. assert user.pref.auto_watch_on?('issue_contributed_to')
  127. journal = Journal.new(:journalized => Issue.first, :notes => 'notes', :user => user)
  128. assert_no_difference 'Watcher.count' do
  129. assert journal.save
  130. assert journal.valid?
  131. assert journal.journalized.valid?
  132. end
  133. end
  134. def test_visible_scope_for_anonymous
  135. # Anonymous user should see issues of public projects only
  136. journals = Journal.visible(User.anonymous).to_a
  137. assert journals.any?
  138. assert_nil journals.detect {|journal| !journal.issue.project.is_public?}
  139. # Anonymous user should not see issues without permission
  140. Role.anonymous.remove_permission!(:view_issues)
  141. journals = Journal.visible(User.anonymous).to_a
  142. assert journals.empty?
  143. end
  144. def test_visible_scope_for_user
  145. user = User.find(9)
  146. assert user.projects.empty?
  147. # Non member user should see issues of public projects only
  148. journals = Journal.visible(user).to_a
  149. assert journals.any?
  150. assert_nil journals.detect {|journal| !journal.issue.project.is_public?}
  151. # Non member user should not see issues without permission
  152. Role.non_member.remove_permission!(:view_issues)
  153. user.reload
  154. journals = Journal.visible(user).to_a
  155. assert journals.empty?
  156. # User should see issues of projects for which user has view_issues permissions only
  157. Member.create!(:principal => user, :project_id => 1, :role_ids => [1])
  158. user.reload
  159. journals = Journal.visible(user).to_a
  160. assert journals.any?
  161. assert_nil journals.detect {|journal| journal.issue.project_id != 1}
  162. end
  163. def test_visible_scope_for_admin
  164. user = User.find(1)
  165. user.members.each(&:destroy)
  166. assert user.projects.empty?
  167. journals = Journal.visible(user).to_a
  168. assert journals.any?
  169. # Admin should see issues on private projects that admin does not belong to
  170. assert journals.detect {|journal| !journal.issue.project.is_public?}
  171. end
  172. def test_preload_journals_details_custom_fields_should_set_custom_field_instance_variable
  173. d = JournalDetail.new(:property => 'cf', :prop_key => '2')
  174. journals = [Journal.new(:details => [d])]
  175. d.expects(:instance_variable_set).with(:@custom_field, CustomField.find(2)).once
  176. Journal.preload_journals_details_custom_fields(journals)
  177. end
  178. def test_preload_journals_details_custom_fields_with_empty_set
  179. assert_nothing_raised do
  180. Journal.preload_journals_details_custom_fields([])
  181. end
  182. end
  183. def test_details_should_normalize_dates
  184. j = JournalDetail.create!(:old_value => Date.parse('2012-11-03'), :value => Date.parse('2013-01-02'))
  185. j.reload
  186. assert_equal '2012-11-03', j.old_value
  187. assert_equal '2013-01-02', j.value
  188. end
  189. def test_details_should_normalize_true_values
  190. j = JournalDetail.create!(:old_value => true, :value => true)
  191. j.reload
  192. assert_equal '1', j.old_value
  193. assert_equal '1', j.value
  194. end
  195. def test_details_should_normalize_false_values
  196. j = JournalDetail.create!(:old_value => false, :value => false)
  197. j.reload
  198. assert_equal '0', j.old_value
  199. assert_equal '0', j.value
  200. end
  201. def test_custom_field_should_return_custom_field_for_cf_detail
  202. d = JournalDetail.new(:property => 'cf', :prop_key => '2')
  203. assert_equal CustomField.find(2), d.custom_field
  204. end
  205. def test_custom_field_should_return_nil_for_non_cf_detail
  206. d = JournalDetail.new(:property => 'subject')
  207. assert_nil d.custom_field
  208. end
  209. def test_visible_details_should_include_relations_to_visible_issues_only
  210. issue = Issue.generate!
  211. visible_issue = Issue.generate!
  212. hidden_issue = Issue.generate!(:is_private => true)
  213. journal = Journal.new
  214. journal.details << JournalDetail.new(:property => 'relation', :prop_key => 'relates', :value => visible_issue.id)
  215. journal.details << JournalDetail.new(:property => 'relation', :prop_key => 'relates', :value => hidden_issue.id)
  216. visible_details = journal.visible_details(User.anonymous)
  217. assert_equal 1, visible_details.size
  218. assert_equal visible_issue.id.to_s, visible_details.first.value.to_s
  219. visible_details = journal.visible_details(User.find(2))
  220. assert_equal 2, visible_details.size
  221. end
  222. def test_attachments
  223. journal = Journal.new
  224. [0, 1].map{ |i| Attachment.generate!(:file => mock_file_with_options(:original_filename => "image#{i}.png")) }.each do |attachment|
  225. journal.details << JournalDetail.new(:property => 'attachment', :prop_key => attachment.id, :value => attachment.filename)
  226. end
  227. attachments = journal.attachments
  228. assert_equal 2, attachments.size
  229. attachments.each_with_index do |attachment, i|
  230. assert_kind_of Attachment, attachment
  231. assert_equal "image#{i}.png", attachment.filename
  232. end
  233. end
  234. def test_notified_mentions_should_not_include_users_who_cannot_view_private_notes
  235. journal = Journal.generate!(journalized: Issue.find(2), user: User.find(1), private_notes: true, notes: 'Hello @dlopper, @jsmith and @admin.')
  236. # User "dlopper" has "Developer" role on project "eCookbook"
  237. # Role "Developer" does not have the "View private notes" permission
  238. assert_equal [1, 2], journal.notified_mentions.map(&:id).sort
  239. end
  240. end