Revision 1297:0a574315af3e .svn/pristine/92

View differences:

.svn/pristine/92/920c665fe00c7af87265d22d1b8ec934bc035540.svn-base
1
# Redmine - project management software
2
# Copyright (C) 2006-2012  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

  
18
require File.expand_path('../../test_helper', __FILE__)
19

  
20
class IssueRelationsControllerTest < ActionController::TestCase
21
  fixtures :projects,
22
           :users,
23
           :roles,
24
           :members,
25
           :member_roles,
26
           :issues,
27
           :issue_statuses,
28
           :issue_relations,
29
           :enabled_modules,
30
           :enumerations,
31
           :trackers
32

  
33
  def setup
34
    User.current = nil
35
    @request.session[:user_id] = 3
36
  end
37

  
38
  def test_create
39
    assert_difference 'IssueRelation.count' do
40
      post :create, :issue_id => 1,
41
                 :relation => {:issue_to_id => '2', :relation_type => 'relates', :delay => ''}
42
    end
43
    relation = IssueRelation.first(:order => 'id DESC')
44
    assert_equal 1, relation.issue_from_id
45
    assert_equal 2, relation.issue_to_id
46
    assert_equal 'relates', relation.relation_type
47
  end
48

  
49
  def test_create_xhr
50
    assert_difference 'IssueRelation.count' do
51
      xhr :post, :create, :issue_id => 3, :relation => {:issue_to_id => '1', :relation_type => 'relates', :delay => ''}
52
      assert_response :success
53
      assert_template 'create'
54
      assert_equal 'text/javascript', response.content_type
55
    end
56
    relation = IssueRelation.first(:order => 'id DESC')
57
    assert_equal 3, relation.issue_from_id
58
    assert_equal 1, relation.issue_to_id
59

  
60
    assert_match /Bug #1/, response.body
61
  end
62

  
63
  def test_create_should_accept_id_with_hash
64
    assert_difference 'IssueRelation.count' do
65
      post :create, :issue_id => 1,
66
                 :relation => {:issue_to_id => '#2', :relation_type => 'relates', :delay => ''}
67
    end
68
    relation = IssueRelation.first(:order => 'id DESC')
69
    assert_equal 2, relation.issue_to_id
70
  end
71

  
72
  def test_create_should_strip_id
73
    assert_difference 'IssueRelation.count' do
74
      post :create, :issue_id => 1,
75
                 :relation => {:issue_to_id => ' 2  ', :relation_type => 'relates', :delay => ''}
76
    end
77
    relation = IssueRelation.first(:order => 'id DESC')
78
    assert_equal 2, relation.issue_to_id
79
  end
80

  
81
  def test_create_should_not_break_with_non_numerical_id
82
    assert_no_difference 'IssueRelation.count' do
83
      assert_nothing_raised do
84
        post :create, :issue_id => 1,
85
                   :relation => {:issue_to_id => 'foo', :relation_type => 'relates', :delay => ''}
86
      end
87
    end
88
  end
89

  
90
  def test_should_create_relations_with_visible_issues_only
91
    Setting.cross_project_issue_relations = '1'
92
    assert_nil Issue.visible(User.find(3)).find_by_id(4)
93

  
94
    assert_no_difference 'IssueRelation.count' do
95
      post :create, :issue_id => 1,
96
                 :relation => {:issue_to_id => '4', :relation_type => 'relates', :delay => ''}
97
    end
98
  end
99

  
100
  should "prevent relation creation when there's a circular dependency"
101

  
102
  def test_create_xhr_with_failure
103
    assert_no_difference 'IssueRelation.count' do
104
      xhr :post, :create, :issue_id => 3, :relation => {:issue_to_id => '999', :relation_type => 'relates', :delay => ''}
105

  
106
      assert_response :success
107
      assert_template 'create'
108
      assert_equal 'text/javascript', response.content_type
109
    end
110

  
111
    assert_match /errorExplanation/, response.body
112
  end
113

  
114
  def test_destroy
115
    assert_difference 'IssueRelation.count', -1 do
116
      delete :destroy, :id => '2'
117
    end
118
  end
119

  
120
  def test_destroy_xhr
121
    IssueRelation.create!(:relation_type => IssueRelation::TYPE_RELATES) do |r|
122
      r.issue_from_id = 3
123
      r.issue_to_id = 1
124
    end
125

  
126
    assert_difference 'IssueRelation.count', -1 do
127
      xhr :delete, :destroy, :id => '2'
128

  
129
      assert_response :success
130
      assert_template 'destroy'
131
      assert_equal 'text/javascript', response.content_type
132
      assert_match /relation-2/, response.body
133
    end
134
  end
135
end
.svn/pristine/92/923ece2f09d9d50ac81cc9fdbc2753d0df52cd4c.svn-base
1
<div id="admin-menu">
2
  <%= render_menu :admin_menu %>
3
</div>
.svn/pristine/92/929002b516dc5d82c7d3b223bcd28e93bb353f29.svn-base
1
require File.expand_path('../../test_helper', __FILE__)
2

  
3
class ProjectEnumerationsControllerTest < ActionController::TestCase
4
  fixtures :projects, :trackers, :issue_statuses, :issues,
5
           :enumerations, :users, :issue_categories,
6
           :projects_trackers,
7
           :roles,
8
           :member_roles,
9
           :members,
10
           :enabled_modules,
11
           :workflows,
12
           :custom_fields, :custom_fields_projects,
13
           :custom_fields_trackers, :custom_values,
14
           :time_entries
15

  
16
  self.use_transactional_fixtures = false
17

  
18
  def setup
19
    @request.session[:user_id] = nil
20
    Setting.default_language = 'en'
21
  end
22

  
23
  def test_update_to_override_system_activities
24
    @request.session[:user_id] = 2 # manager
25
    billable_field = TimeEntryActivityCustomField.find_by_name("Billable")
26

  
27
    put :update, :project_id => 1, :enumerations => {
28
      "9"=> {"parent_id"=>"9", "custom_field_values"=>{"7" => "1"}, "active"=>"0"}, # Design, De-activate
29
      "10"=> {"parent_id"=>"10", "custom_field_values"=>{"7"=>"0"}, "active"=>"1"}, # Development, Change custom value
30
      "14"=>{"parent_id"=>"14", "custom_field_values"=>{"7"=>"1"}, "active"=>"1"}, # Inactive Activity, Activate with custom value
31
      "11"=>{"parent_id"=>"11", "custom_field_values"=>{"7"=>"1"}, "active"=>"1"} # QA, no changes
32
    }
33

  
34
    assert_response :redirect
35
    assert_redirected_to '/projects/ecookbook/settings/activities'
36

  
37
    # Created project specific activities...
38
    project = Project.find('ecookbook')
39

  
40
    # ... Design
41
    design = project.time_entry_activities.find_by_name("Design")
42
    assert design, "Project activity not found"
43

  
44
    assert_equal 9, design.parent_id # Relate to the system activity
45
    assert_not_equal design.parent.id, design.id # Different records
46
    assert_equal design.parent.name, design.name # Same name
47
    assert !design.active?
48

  
49
    # ... Development
50
    development = project.time_entry_activities.find_by_name("Development")
51
    assert development, "Project activity not found"
52

  
53
    assert_equal 10, development.parent_id # Relate to the system activity
54
    assert_not_equal development.parent.id, development.id # Different records
55
    assert_equal development.parent.name, development.name # Same name
56
    assert development.active?
57
    assert_equal "0", development.custom_value_for(billable_field).value
58

  
59
    # ... Inactive Activity
60
    previously_inactive = project.time_entry_activities.find_by_name("Inactive Activity")
61
    assert previously_inactive, "Project activity not found"
62

  
63
    assert_equal 14, previously_inactive.parent_id # Relate to the system activity
64
    assert_not_equal previously_inactive.parent.id, previously_inactive.id # Different records
65
    assert_equal previously_inactive.parent.name, previously_inactive.name # Same name
66
    assert previously_inactive.active?
67
    assert_equal "1", previously_inactive.custom_value_for(billable_field).value
68

  
69
    # ... QA
70
    assert_equal nil, project.time_entry_activities.find_by_name("QA"), "Custom QA activity created when it wasn't modified"
71
  end
72

  
73
  def test_update_will_update_project_specific_activities
74
    @request.session[:user_id] = 2 # manager
75

  
76
    project_activity = TimeEntryActivity.new({
77
                                               :name => 'Project Specific',
78
                                               :parent => TimeEntryActivity.find(:first),
79
                                               :project => Project.find(1),
80
                                               :active => true
81
                                             })
82
    assert project_activity.save
83
    project_activity_two = TimeEntryActivity.new({
84
                                                   :name => 'Project Specific Two',
85
                                                   :parent => TimeEntryActivity.find(:last),
86
                                                   :project => Project.find(1),
87
                                                   :active => true
88
                                                 })
89
    assert project_activity_two.save
90

  
91

  
92
    put :update, :project_id => 1, :enumerations => {
93
      project_activity.id => {"custom_field_values"=>{"7" => "1"}, "active"=>"0"}, # De-activate
94
      project_activity_two.id => {"custom_field_values"=>{"7" => "1"}, "active"=>"0"} # De-activate
95
    }
96

  
97
    assert_response :redirect
98
    assert_redirected_to '/projects/ecookbook/settings/activities'
99

  
100
    # Created project specific activities...
101
    project = Project.find('ecookbook')
102
    assert_equal 2, project.time_entry_activities.count
103

  
104
    activity_one = project.time_entry_activities.find_by_name(project_activity.name)
105
    assert activity_one, "Project activity not found"
106
    assert_equal project_activity.id, activity_one.id
107
    assert !activity_one.active?
108

  
109
    activity_two = project.time_entry_activities.find_by_name(project_activity_two.name)
110
    assert activity_two, "Project activity not found"
111
    assert_equal project_activity_two.id, activity_two.id
112
    assert !activity_two.active?
113
  end
114

  
115
  def test_update_when_creating_new_activities_will_convert_existing_data
116
    assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size
117

  
118
    @request.session[:user_id] = 2 # manager
119
    put :update, :project_id => 1, :enumerations => {
120
      "9"=> {"parent_id"=>"9", "custom_field_values"=>{"7" => "1"}, "active"=>"0"} # Design, De-activate
121
    }
122
    assert_response :redirect
123

  
124
    # No more TimeEntries using the system activity
125
    assert_equal 0, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size, "Time Entries still assigned to system activities"
126
    # All TimeEntries using project activity
127
    project_specific_activity = TimeEntryActivity.find_by_parent_id_and_project_id(9, 1)
128
    assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(project_specific_activity.id, 1).size, "No Time Entries assigned to the project activity"
129
  end
130

  
131
  def test_update_when_creating_new_activities_will_not_convert_existing_data_if_an_exception_is_raised
132
    # TODO: Need to cause an exception on create but these tests
133
    # aren't setup for mocking.  Just create a record now so the
134
    # second one is a dupicate
135
    parent = TimeEntryActivity.find(9)
136
    TimeEntryActivity.create!({:name => parent.name, :project_id => 1, :position => parent.position, :active => true})
137
    TimeEntry.create!({:project_id => 1, :hours => 1.0, :user => User.find(1), :issue_id => 3, :activity_id => 10, :spent_on => '2009-01-01'})
138

  
139
    assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size
140
    assert_equal 1, TimeEntry.find_all_by_activity_id_and_project_id(10, 1).size
141

  
142
    @request.session[:user_id] = 2 # manager
143
    put :update, :project_id => 1, :enumerations => {
144
      "9"=> {"parent_id"=>"9", "custom_field_values"=>{"7" => "1"}, "active"=>"0"}, # Design
145
      "10"=> {"parent_id"=>"10", "custom_field_values"=>{"7"=>"0"}, "active"=>"1"} # Development, Change custom value
146
    }
147
    assert_response :redirect
148

  
149
    # TimeEntries shouldn't have been reassigned on the failed record
150
    assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size, "Time Entries are not assigned to system activities"
151
    # TimeEntries shouldn't have been reassigned on the saved record either
152
    assert_equal 1, TimeEntry.find_all_by_activity_id_and_project_id(10, 1).size, "Time Entries are not assigned to system activities"
153
  end
154

  
155
  def test_destroy
156
    @request.session[:user_id] = 2 # manager
157
    project_activity = TimeEntryActivity.new({
158
                                               :name => 'Project Specific',
159
                                               :parent => TimeEntryActivity.find(:first),
160
                                               :project => Project.find(1),
161
                                               :active => true
162
                                             })
163
    assert project_activity.save
164
    project_activity_two = TimeEntryActivity.new({
165
                                                   :name => 'Project Specific Two',
166
                                                   :parent => TimeEntryActivity.find(:last),
167
                                                   :project => Project.find(1),
168
                                                   :active => true
169
                                                 })
170
    assert project_activity_two.save
171

  
172
    delete :destroy, :project_id => 1
173
    assert_response :redirect
174
    assert_redirected_to '/projects/ecookbook/settings/activities'
175

  
176
    assert_nil TimeEntryActivity.find_by_id(project_activity.id)
177
    assert_nil TimeEntryActivity.find_by_id(project_activity_two.id)
178
  end
179

  
180
  def test_destroy_should_reassign_time_entries_back_to_the_system_activity
181
    @request.session[:user_id] = 2 # manager
182
    project_activity = TimeEntryActivity.new({
183
                                               :name => 'Project Specific Design',
184
                                               :parent => TimeEntryActivity.find(9),
185
                                               :project => Project.find(1),
186
                                               :active => true
187
                                             })
188
    assert project_activity.save
189
    assert TimeEntry.update_all("activity_id = '#{project_activity.id}'", ["project_id = ? AND activity_id = ?", 1, 9])
190
    assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(project_activity.id, 1).size
191

  
192
    delete :destroy, :project_id => 1
193
    assert_response :redirect
194
    assert_redirected_to '/projects/ecookbook/settings/activities'
195

  
196
    assert_nil TimeEntryActivity.find_by_id(project_activity.id)
197
    assert_equal 0, TimeEntry.find_all_by_activity_id_and_project_id(project_activity.id, 1).size, "TimeEntries still assigned to project specific activity"
198
    assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size, "TimeEntries still assigned to project specific activity"
199
  end
200

  
201
end
.svn/pristine/92/92d58d10b91b7c7dfd6de735e3b6cd28edd15f9e.svn-base
1
# Redmine - project management software
2
# Copyright (C) 2006-2012  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

  
18
require 'zlib'
19

  
20
class WikiContent < ActiveRecord::Base
21
  self.locking_column = 'version'
22
  belongs_to :page, :class_name => 'WikiPage', :foreign_key => 'page_id'
23
  belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
24
  validates_presence_of :text
25
  validates_length_of :comments, :maximum => 255, :allow_nil => true
26

  
27
  acts_as_versioned
28

  
29
  def visible?(user=User.current)
30
    page.visible?(user)
31
  end
32

  
33
  def project
34
    page.project
35
  end
36

  
37
  def attachments
38
    page.nil? ? [] : page.attachments
39
  end
40

  
41
  # Returns the mail adresses of users that should be notified
42
  def recipients
43
    notified = project.notified_users
44
    notified.reject! {|user| !visible?(user)}
45
    notified.collect(&:mail)
46
  end
47

  
48
  # Return true if the content is the current page content
49
  def current_version?
50
    true
51
  end
52

  
53
  class Version
54
    belongs_to :page, :class_name => '::WikiPage', :foreign_key => 'page_id'
55
    belongs_to :author, :class_name => '::User', :foreign_key => 'author_id'
56
    attr_protected :data
57

  
58
    acts_as_event :title => Proc.new {|o| "#{l(:label_wiki_edit)}: #{o.page.title} (##{o.version})"},
59
                  :description => :comments,
60
                  :datetime => :updated_on,
61
                  :type => 'wiki-page',
62
                  :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.page.wiki.project, :id => o.page.title, :version => o.version}}
63

  
64
    acts_as_activity_provider :type => 'wiki_edits',
65
                              :timestamp => "#{WikiContent.versioned_table_name}.updated_on",
66
                              :author_key => "#{WikiContent.versioned_table_name}.author_id",
67
                              :permission => :view_wiki_edits,
68
                              :find_options => {:select => "#{WikiContent.versioned_table_name}.updated_on, #{WikiContent.versioned_table_name}.comments, " +
69
                                                           "#{WikiContent.versioned_table_name}.#{WikiContent.version_column}, #{WikiPage.table_name}.title, " +
70
                                                           "#{WikiContent.versioned_table_name}.page_id, #{WikiContent.versioned_table_name}.author_id, " +
71
                                                           "#{WikiContent.versioned_table_name}.id",
72
                                                :joins => "LEFT JOIN #{WikiPage.table_name} ON #{WikiPage.table_name}.id = #{WikiContent.versioned_table_name}.page_id " +
73
                                                          "LEFT JOIN #{Wiki.table_name} ON #{Wiki.table_name}.id = #{WikiPage.table_name}.wiki_id " +
74
                                                          "LEFT JOIN #{Project.table_name} ON #{Project.table_name}.id = #{Wiki.table_name}.project_id"}
75

  
76
    after_destroy :page_update_after_destroy
77

  
78
    def text=(plain)
79
      case Setting.wiki_compression
80
      when 'gzip'
81
      begin
82
        self.data = Zlib::Deflate.deflate(plain, Zlib::BEST_COMPRESSION)
83
        self.compression = 'gzip'
84
      rescue
85
        self.data = plain
86
        self.compression = ''
87
      end
88
      else
89
        self.data = plain
90
        self.compression = ''
91
      end
92
      plain
93
    end
94

  
95
    def text
96
      @text ||= begin
97
        str = case compression
98
              when 'gzip'
99
                Zlib::Inflate.inflate(data)
100
              else
101
                # uncompressed data
102
                data
103
              end
104
        str.force_encoding("UTF-8") if str.respond_to?(:force_encoding)
105
        str
106
      end
107
    end
108

  
109
    def project
110
      page.project
111
    end
112

  
113
    # Return true if the content is the current page content
114
    def current_version?
115
      page.content.version == self.version
116
    end
117

  
118
    # Returns the previous version or nil
119
    def previous
120
      @previous ||= WikiContent::Version.
121
        reorder('version DESC').
122
        includes(:author).
123
        where("wiki_content_id = ? AND version < ?", wiki_content_id, version).first
124
    end
125

  
126
    # Returns the next version or nil
127
    def next
128
      @next ||= WikiContent::Version.
129
        reorder('version ASC').
130
        includes(:author).
131
        where("wiki_content_id = ? AND version > ?", wiki_content_id, version).first
132
    end
133

  
134
    private
135

  
136
    # Updates page's content if the latest version is removed
137
    # or destroys the page if it was the only version
138
    def page_update_after_destroy
139
      latest = page.content.versions.reorder("#{self.class.table_name}.version DESC").first
140
      if latest && page.content.version != latest.version
141
        raise ActiveRecord::Rollback unless page.content.revert_to!(latest)
142
      elsif latest.nil?
143
        raise ActiveRecord::Rollback unless page.destroy
144
      end
145
    end
146
  end
147
end

Also available in: Unified diff