Permalink
Browse files

fix: missing support for adding and deleting of watchers

  • Loading branch information...
1 parent 974c247 commit 4c27984cb8d6d7b0404a0d660053500f83ff1f65 Sven Krzyzak committed May 14, 2011
View
@@ -10,3 +10,5 @@
end
Redmine::Plugin.find(:redmine_mylyn_connector).requires_redmine(:version_or_higher=>'1.0.0')
+
+require_dependency 'mylyn_connector/hooks/controller_issues_edit_after_save'
@@ -0,0 +1,7 @@
+module MylynConnector
+
+ module Hooks
+
+ end
+
+end
@@ -0,0 +1,35 @@
+module MylynConnector
+
+ module Hooks
+
+ class ControllerIssuesEditAfterSaveHook < Redmine::Hook::Listener
+
+ def controller_issues_edit_after_save(context={})
+
+ if context[:issue] && context[:params][:issue] && context[:params][:issue][:watcher_user_ids]
+
+ issue = context[:issue]
+ watcheruserids = context[:params][:issue][:watcher_user_ids]
+
+ if User.current.allowed_to?(:delete_issue_watchers, issue.project)
+ issue.watchers.each { |w|
+ issue.remove_watcher(User.find(w.user.id)) unless watcheruserids.include?(w.user.id)
+ }
+ end
+
+ if User.current.allowed_to?(:add_issue_watchers, issue.project)
+ watcheruserids.each { |id|
+ user = User.find(id)
+ issue.add_watcher(user) unless issue.watched_by?(user)
+ }
+ end
+
+ end
+
+ end
+
+ end
+
+ end
+
+end
@@ -0,0 +1,4 @@
+module MylynConnector::Test
+
+
+end
@@ -0,0 +1,28 @@
+module MylynConnector
+
+ module Test
+
+ class IntegrationTest < ActionController::IntegrationTest
+
+ include MylynConnector::Version::ClassMethods
+
+ def self.fixtures(*table_names)
+ dir = File.join(File.dirname(__FILE__), "../../../test/fixtures/" + self.rr )
+
+ modified_tables = table_names.reject{|x| !File.exist?(dir + "/" + x.to_s + ".yml") }
+ Fixtures.create_fixtures(dir, modified_tables) unless modified_tables.empty?
+ table_names -= modified_tables
+
+ super(table_names-modified_tables)
+ end
+
+ protected
+
+ def self.rr
+ MylynConnector::Version.redmine_release
+ end
+ end
+
+ end
+
+end
@@ -0,0 +1,66 @@
+require File.dirname(__FILE__) + '/../../test_helper'
+
+class MylynConnector::Hooks::ControllerIssuesEditAfterSaveTest < MylynConnector::Test::IntegrationTest
+
+ self.use_transactional_fixtures = true
+
+ fixtures :users, :roles, :members, :issue_categories, :trackers, :versions, :queries, :projects, :projects_trackers, :custom_fields_trackers, :issues, :journals, :attachments, :custom_fields, :custom_values, :watchers, :time_entries
+
+ include Redmine::Hook::Helper
+
+ context "#controller_issues_edit_before_save" do
+
+ setup do
+ Setting.rest_api_enabled = '1'
+ end
+
+ context "modifing watchers" do
+
+ setup do
+ @headers = {:authorization => credentials('jsmith') }
+ end
+
+ should "add two new watchers and ignore one existing" do
+ issue_id = 3;
+
+ assert_difference('Watcher.count', 2) do
+ put "/issues/" + issue_id.to_s + ".xml", {:issue => {:watcher_user_ids => [1, 2, 3] }}, @headers
+ end
+
+ assert_not_nil(Watcher.find(:first, :conditions => ["watchable_type='Issue' AND watchable_id=? AND user_id=?", issue_id, 1]))
+ assert_not_nil(Watcher.find(:first, :conditions => ["watchable_type='Issue' AND watchable_id=? AND user_id=?", issue_id, 3]))
+ end
+
+ should "add one new watcher and remove one existing" do
+ issue_id = 3;
+
+ assert_difference('Watcher.count', 1) do
+ put "/issues/" + issue_id.to_s + ".xml", {:issue => {:watcher_user_ids => [1, 3] }}, @headers
+ end
+
+ assert_not_nil(Watcher.find(:first, :conditions => ["watchable_type='Issue' AND watchable_id=? AND user_id=?", issue_id, 1]))
+ assert_not_nil(Watcher.find(:first, :conditions => ["watchable_type='Issue' AND watchable_id=? AND user_id=?", issue_id, 3]))
+ assert_nil(Watcher.find(:first, :conditions => ["watchable_type='Issue' AND watchable_id=? AND user_id=?", issue_id, 2]))
+ end
+
+ should "do nothing - insufficient data" do
+
+ assert_difference('Watcher.count', 0) do
+ put "/issues/3.xml", {}, @headers
+ end
+
+ assert_difference('Watcher.count', 0) do
+ put "/issues/3.xml", {:issue => {}}, @headers
+ end
+
+ end
+
+ end
+
+ end
+
+ def credentials(user, password=nil)
+ ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
+ end
+
+end

0 comments on commit 4c27984

Please sign in to comment.