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 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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 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_visible_scope_for_anonymous
  106. # Anonymous user should see issues of public projects only
  107. journals = Journal.visible(User.anonymous).to_a
  108. assert journals.any?
  109. assert_nil journals.detect {|journal| !journal.issue.project.is_public?}
  110. # Anonymous user should not see issues without permission
  111. Role.anonymous.remove_permission!(:view_issues)
  112. journals = Journal.visible(User.anonymous).to_a
  113. assert journals.empty?
  114. end
  115. def test_visible_scope_for_user
  116. user = User.find(9)
  117. assert user.projects.empty?
  118. # Non member user should see issues of public projects only
  119. journals = Journal.visible(user).to_a
  120. assert journals.any?
  121. assert_nil journals.detect {|journal| !journal.issue.project.is_public?}
  122. # Non member user should not see issues without permission
  123. Role.non_member.remove_permission!(:view_issues)
  124. user.reload
  125. journals = Journal.visible(user).to_a
  126. assert journals.empty?
  127. # User should see issues of projects for which user has view_issues permissions only
  128. Member.create!(:principal => user, :project_id => 1, :role_ids => [1])
  129. user.reload
  130. journals = Journal.visible(user).to_a
  131. assert journals.any?
  132. assert_nil journals.detect {|journal| journal.issue.project_id != 1}
  133. end
  134. def test_visible_scope_for_admin
  135. user = User.find(1)
  136. user.members.each(&:destroy)
  137. assert user.projects.empty?
  138. journals = Journal.visible(user).to_a
  139. assert journals.any?
  140. # Admin should see issues on private projects that admin does not belong to
  141. assert journals.detect {|journal| !journal.issue.project.is_public?}
  142. end
  143. def test_preload_journals_details_custom_fields_should_set_custom_field_instance_variable
  144. d = JournalDetail.new(:property => 'cf', :prop_key => '2')
  145. journals = [Journal.new(:details => [d])]
  146. d.expects(:instance_variable_set).with("@custom_field", CustomField.find(2)).once
  147. Journal.preload_journals_details_custom_fields(journals)
  148. end
  149. def test_preload_journals_details_custom_fields_with_empty_set
  150. assert_nothing_raised do
  151. Journal.preload_journals_details_custom_fields([])
  152. end
  153. end
  154. def test_details_should_normalize_dates
  155. j = JournalDetail.create!(:old_value => Date.parse('2012-11-03'), :value => Date.parse('2013-01-02'))
  156. j.reload
  157. assert_equal '2012-11-03', j.old_value
  158. assert_equal '2013-01-02', j.value
  159. end
  160. def test_details_should_normalize_true_values
  161. j = JournalDetail.create!(:old_value => true, :value => true)
  162. j.reload
  163. assert_equal '1', j.old_value
  164. assert_equal '1', j.value
  165. end
  166. def test_details_should_normalize_false_values
  167. j = JournalDetail.create!(:old_value => false, :value => false)
  168. j.reload
  169. assert_equal '0', j.old_value
  170. assert_equal '0', j.value
  171. end
  172. def test_custom_field_should_return_custom_field_for_cf_detail
  173. d = JournalDetail.new(:property => 'cf', :prop_key => '2')
  174. assert_equal CustomField.find(2), d.custom_field
  175. end
  176. def test_custom_field_should_return_nil_for_non_cf_detail
  177. d = JournalDetail.new(:property => 'subject')
  178. assert_nil d.custom_field
  179. end
  180. def test_visible_details_should_include_relations_to_visible_issues_only
  181. issue = Issue.generate!
  182. visible_issue = Issue.generate!
  183. hidden_issue = Issue.generate!(:is_private => true)
  184. journal = Journal.new
  185. journal.details << JournalDetail.new(:property => 'relation', :prop_key => 'relates', :value => visible_issue.id)
  186. journal.details << JournalDetail.new(:property => 'relation', :prop_key => 'relates', :value => hidden_issue.id)
  187. visible_details = journal.visible_details(User.anonymous)
  188. assert_equal 1, visible_details.size
  189. assert_equal visible_issue.id.to_s, visible_details.first.value.to_s
  190. visible_details = journal.visible_details(User.find(2))
  191. assert_equal 2, visible_details.size
  192. end
  193. def test_attachments
  194. journal = Journal.new
  195. [0, 1].map{ |i| Attachment.generate!(:file => mock_file_with_options(:original_filename => "image#{i}.png")) }.each do |attachment|
  196. journal.details << JournalDetail.new(:property => 'attachment', :prop_key => attachment.id, :value => attachment.filename)
  197. end
  198. attachments = journal.attachments
  199. assert_equal 2, attachments.size
  200. attachments.each_with_index do |attachment, i|
  201. assert_kind_of Attachment, attachment
  202. assert_equal "image#{i}.png", attachment.filename
  203. end
  204. end
  205. end