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

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