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.

activity_test.rb 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 ActivityTest < ActiveSupport::TestCase
  20. fixtures :projects, :versions, :attachments, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details,
  21. :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages, :time_entries,
  22. :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions
  23. def setup
  24. User.current = nil
  25. @project = Project.find(1)
  26. end
  27. def test_activity_without_subprojects
  28. events = find_events(User.anonymous, :project => @project)
  29. assert_not_nil events
  30. assert events.include?(Issue.find(1))
  31. assert !events.include?(Issue.find(4))
  32. # subproject issue
  33. assert !events.include?(Issue.find(5))
  34. end
  35. def test_activity_with_subprojects
  36. events = find_events(User.anonymous, :project => @project, :with_subprojects => 1)
  37. assert_not_nil events
  38. assert events.include?(Issue.find(1))
  39. # subproject issue
  40. assert events.include?(Issue.find(5))
  41. end
  42. def test_global_activity_anonymous
  43. events = find_events(User.anonymous)
  44. assert_not_nil events
  45. assert events.include?(Issue.find(1))
  46. assert events.include?(Message.find(5))
  47. # Issue of a private project
  48. assert !events.include?(Issue.find(4))
  49. # Private issue and comment
  50. assert !events.include?(Issue.find(14))
  51. assert !events.include?(Journal.find(5))
  52. end
  53. def test_global_activity_logged_user
  54. events = find_events(User.find(2)) # manager
  55. assert_not_nil events
  56. assert events.include?(Issue.find(1))
  57. # Issue of a private project the user belongs to
  58. assert events.include?(Issue.find(4))
  59. end
  60. def test_user_activity
  61. user = User.find(2)
  62. events = Redmine::Activity::Fetcher.new(User.anonymous, :author => user).events(nil, nil, :limit => 10)
  63. assert(events.size > 0)
  64. assert(events.size <= 10)
  65. assert_nil(events.detect {|e| e.event_author != user})
  66. end
  67. def test_journal_with_notes_and_changes_should_be_returned_once
  68. f = Redmine::Activity::Fetcher.new(User.anonymous, :project => Project.find(1))
  69. f.scope = ['issues']
  70. events = f.events
  71. assert_equal events, events.uniq
  72. end
  73. def test_files_activity
  74. f = Redmine::Activity::Fetcher.new(User.anonymous, :project => Project.find(1))
  75. f.scope = ['files']
  76. events = f.events
  77. assert_kind_of Array, events
  78. assert events.include?(Attachment.find_by_container_type_and_container_id('Project', 1))
  79. assert events.include?(Attachment.find_by_container_type_and_container_id('Version', 1))
  80. assert_equal [Attachment], events.collect(&:class).uniq
  81. assert_equal %w(Project Version), events.collect(&:container_type).uniq.sort
  82. end
  83. def test_event_group_for_issue
  84. issue = Issue.find(1)
  85. assert_equal issue, issue.event_group
  86. end
  87. def test_event_group_for_journal
  88. issue = Issue.find(1)
  89. journal = issue.journals.first
  90. assert_equal issue, journal.event_group
  91. end
  92. def test_event_group_for_issue_time_entry
  93. time = TimeEntry.where(:issue_id => 1).first
  94. assert_equal time.issue, time.event_group
  95. end
  96. def test_event_group_for_project_time_entry
  97. time = TimeEntry.where(:issue_id => nil).first
  98. assert_equal time, time.event_group
  99. end
  100. def test_event_group_for_message
  101. message = Message.find(1)
  102. reply = message.children.first
  103. assert_equal message, message.event_group
  104. assert_equal message, reply.event_group
  105. end
  106. def test_event_group_for_wiki_content_version
  107. content = WikiContentVersion.find(1)
  108. assert_equal content.page, content.event_group
  109. end
  110. class TestActivityProviderWithPermission
  111. def self.activity_provider_options
  112. {'test' => {:permission => :custom_permission}}
  113. end
  114. end
  115. class TestActivityProviderWithNilPermission
  116. def self.activity_provider_options
  117. {'test' => {:permission => nil}}
  118. end
  119. end
  120. class TestActivityProviderWithoutPermission
  121. def self.activity_provider_options
  122. {'test' => {}}
  123. end
  124. end
  125. class MockUser
  126. def initialize(*permissions)
  127. @permissions = permissions
  128. end
  129. def allowed_to?(permission, *args)
  130. @permissions.include?(permission)
  131. end
  132. end
  133. def test_event_types_should_consider_activity_provider_permission
  134. Redmine::Activity.register 'test', :class_name => 'ActivityTest::TestActivityProviderWithPermission'
  135. user = MockUser.new(:custom_permission)
  136. f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1))
  137. assert_include 'test', f.event_types
  138. ensure
  139. Redmine::Activity.delete 'test'
  140. end
  141. def test_event_types_should_include_activity_provider_with_nil_permission
  142. Redmine::Activity.register 'test', :class_name => 'ActivityTest::TestActivityProviderWithNilPermission'
  143. user = MockUser.new
  144. f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1))
  145. assert_include 'test', f.event_types
  146. ensure
  147. Redmine::Activity.delete 'test'
  148. end
  149. def test_event_types_should_use_default_permission_for_activity_provider_without_permission
  150. Redmine::Activity.register 'test', :class_name => 'ActivityTest::TestActivityProviderWithoutPermission'
  151. user = MockUser.new
  152. f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1))
  153. assert_not_include 'test', f.event_types
  154. user = MockUser.new(:view_test)
  155. f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1))
  156. assert_include 'test', f.event_types
  157. ensure
  158. Redmine::Activity.delete 'test'
  159. end
  160. private
  161. def find_events(user, options={})
  162. Redmine::Activity::Fetcher.new(user, options).events(Date.today - 30, Date.today + 1)
  163. end
  164. end