@@ -1,407 +1,409 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
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 |
|
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. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class Project < ActiveRecord::Base |
|
18 | class Project < ActiveRecord::Base | |
19 | # Project statuses |
|
19 | # Project statuses | |
20 | STATUS_ACTIVE = 1 |
|
20 | STATUS_ACTIVE = 1 | |
21 | STATUS_ARCHIVED = 9 |
|
21 | STATUS_ARCHIVED = 9 | |
22 |
|
22 | |||
23 | has_many :members, :include => :user, :conditions => "#{User.table_name}.status=#{User::STATUS_ACTIVE}" |
|
23 | has_many :members, :include => :user, :conditions => "#{User.table_name}.status=#{User::STATUS_ACTIVE}" | |
24 | has_many :users, :through => :members |
|
24 | has_many :users, :through => :members | |
25 | has_many :enabled_modules, :dependent => :delete_all |
|
25 | has_many :enabled_modules, :dependent => :delete_all | |
26 | has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position" |
|
26 | has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position" | |
27 | has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker] |
|
27 | has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker] | |
28 | has_many :issue_changes, :through => :issues, :source => :journals |
|
28 | has_many :issue_changes, :through => :issues, :source => :journals | |
29 | has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC" |
|
29 | has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC" | |
30 | has_many :time_entries, :dependent => :delete_all |
|
30 | has_many :time_entries, :dependent => :delete_all | |
31 | has_many :queries, :dependent => :delete_all |
|
31 | has_many :queries, :dependent => :delete_all | |
32 | has_many :documents, :dependent => :destroy |
|
32 | has_many :documents, :dependent => :destroy | |
33 | has_many :news, :dependent => :delete_all, :include => :author |
|
33 | has_many :news, :dependent => :delete_all, :include => :author | |
34 | has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name" |
|
34 | has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name" | |
35 | has_many :boards, :dependent => :destroy, :order => "position ASC" |
|
35 | has_many :boards, :dependent => :destroy, :order => "position ASC" | |
36 | has_one :repository, :dependent => :destroy |
|
36 | has_one :repository, :dependent => :destroy | |
37 | has_many :changesets, :through => :repository |
|
37 | has_many :changesets, :through => :repository | |
38 | has_one :wiki, :dependent => :destroy |
|
38 | has_one :wiki, :dependent => :destroy | |
39 | # Custom field for the project issues |
|
39 | # Custom field for the project issues | |
40 | has_and_belongs_to_many :issue_custom_fields, |
|
40 | has_and_belongs_to_many :issue_custom_fields, | |
41 | :class_name => 'IssueCustomField', |
|
41 | :class_name => 'IssueCustomField', | |
42 | :order => "#{CustomField.table_name}.position", |
|
42 | :order => "#{CustomField.table_name}.position", | |
43 | :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}", |
|
43 | :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}", | |
44 | :association_foreign_key => 'custom_field_id' |
|
44 | :association_foreign_key => 'custom_field_id' | |
45 |
|
45 | |||
46 | acts_as_nested_set :order => 'name', :dependent => :destroy |
|
46 | acts_as_nested_set :order => 'name', :dependent => :destroy | |
47 | acts_as_attachable :view_permission => :view_files, |
|
47 | acts_as_attachable :view_permission => :view_files, | |
48 | :delete_permission => :manage_files |
|
48 | :delete_permission => :manage_files | |
49 |
|
49 | |||
50 | acts_as_customizable |
|
50 | acts_as_customizable | |
51 | acts_as_searchable :columns => ['name', 'description'], :project_key => 'id', :permission => nil |
|
51 | acts_as_searchable :columns => ['name', 'description'], :project_key => 'id', :permission => nil | |
52 | acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"}, |
|
52 | acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"}, | |
53 | :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}}, |
|
53 | :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}}, | |
54 | :author => nil |
|
54 | :author => nil | |
55 |
|
55 | |||
56 | attr_protected :status, :enabled_module_names |
|
56 | attr_protected :status, :enabled_module_names | |
57 |
|
57 | |||
58 | validates_presence_of :name, :identifier |
|
58 | validates_presence_of :name, :identifier | |
59 | validates_uniqueness_of :name, :identifier |
|
59 | validates_uniqueness_of :name, :identifier | |
60 | validates_associated :repository, :wiki |
|
60 | validates_associated :repository, :wiki | |
61 | validates_length_of :name, :maximum => 30 |
|
61 | validates_length_of :name, :maximum => 30 | |
62 | validates_length_of :homepage, :maximum => 255 |
|
62 | validates_length_of :homepage, :maximum => 255 | |
63 | validates_length_of :identifier, :in => 1..20 |
|
63 | validates_length_of :identifier, :in => 1..20 | |
64 | # donwcase letters, digits, dashes but not digits only |
|
64 | # donwcase letters, digits, dashes but not digits only | |
65 | validates_format_of :identifier, :with => /^(?!\d+$)[a-z0-9\-]*$/, :if => Proc.new { |p| p.identifier_changed? } |
|
65 | validates_format_of :identifier, :with => /^(?!\d+$)[a-z0-9\-]*$/, :if => Proc.new { |p| p.identifier_changed? } | |
66 |
|
66 | # reserved words | ||
|
67 | validates_exclusion_of :identifier, :in => %w( new ) | |||
|
68 | ||||
67 | before_destroy :delete_all_members |
|
69 | before_destroy :delete_all_members | |
68 |
|
70 | |||
69 | named_scope :has_module, lambda { |mod| { :conditions => ["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s] } } |
|
71 | named_scope :has_module, lambda { |mod| { :conditions => ["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s] } } | |
70 | named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"} |
|
72 | named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"} | |
71 | named_scope :public, { :conditions => { :is_public => true } } |
|
73 | named_scope :public, { :conditions => { :is_public => true } } | |
72 | named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } } |
|
74 | named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } } | |
73 |
|
75 | |||
74 | def identifier=(identifier) |
|
76 | def identifier=(identifier) | |
75 | super unless identifier_frozen? |
|
77 | super unless identifier_frozen? | |
76 | end |
|
78 | end | |
77 |
|
79 | |||
78 | def identifier_frozen? |
|
80 | def identifier_frozen? | |
79 | errors[:identifier].nil? && !(new_record? || identifier.blank?) |
|
81 | errors[:identifier].nil? && !(new_record? || identifier.blank?) | |
80 | end |
|
82 | end | |
81 |
|
83 | |||
82 | def issues_with_subprojects(include_subprojects=false) |
|
84 | def issues_with_subprojects(include_subprojects=false) | |
83 | conditions = nil |
|
85 | conditions = nil | |
84 | if include_subprojects |
|
86 | if include_subprojects | |
85 | ids = [id] + descendants.collect(&:id) |
|
87 | ids = [id] + descendants.collect(&:id) | |
86 | conditions = ["#{Project.table_name}.id IN (#{ids.join(',')}) AND #{Project.visible_by}"] |
|
88 | conditions = ["#{Project.table_name}.id IN (#{ids.join(',')}) AND #{Project.visible_by}"] | |
87 | end |
|
89 | end | |
88 | conditions ||= ["#{Project.table_name}.id = ?", id] |
|
90 | conditions ||= ["#{Project.table_name}.id = ?", id] | |
89 | # Quick and dirty fix for Rails 2 compatibility |
|
91 | # Quick and dirty fix for Rails 2 compatibility | |
90 | Issue.send(:with_scope, :find => { :conditions => conditions }) do |
|
92 | Issue.send(:with_scope, :find => { :conditions => conditions }) do | |
91 | Version.send(:with_scope, :find => { :conditions => conditions }) do |
|
93 | Version.send(:with_scope, :find => { :conditions => conditions }) do | |
92 | yield |
|
94 | yield | |
93 | end |
|
95 | end | |
94 | end |
|
96 | end | |
95 | end |
|
97 | end | |
96 |
|
98 | |||
97 | # returns latest created projects |
|
99 | # returns latest created projects | |
98 | # non public projects will be returned only if user is a member of those |
|
100 | # non public projects will be returned only if user is a member of those | |
99 | def self.latest(user=nil, count=5) |
|
101 | def self.latest(user=nil, count=5) | |
100 | find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC") |
|
102 | find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC") | |
101 | end |
|
103 | end | |
102 |
|
104 | |||
103 | # Returns a SQL :conditions string used to find all active projects for the specified user. |
|
105 | # Returns a SQL :conditions string used to find all active projects for the specified user. | |
104 | # |
|
106 | # | |
105 | # Examples: |
|
107 | # Examples: | |
106 | # Projects.visible_by(admin) => "projects.status = 1" |
|
108 | # Projects.visible_by(admin) => "projects.status = 1" | |
107 | # Projects.visible_by(normal_user) => "projects.status = 1 AND projects.is_public = 1" |
|
109 | # Projects.visible_by(normal_user) => "projects.status = 1 AND projects.is_public = 1" | |
108 | def self.visible_by(user=nil) |
|
110 | def self.visible_by(user=nil) | |
109 | user ||= User.current |
|
111 | user ||= User.current | |
110 | if user && user.admin? |
|
112 | if user && user.admin? | |
111 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}" |
|
113 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}" | |
112 | elsif user && user.memberships.any? |
|
114 | elsif user && user.memberships.any? | |
113 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND (#{Project.table_name}.is_public = #{connection.quoted_true} or #{Project.table_name}.id IN (#{user.memberships.collect{|m| m.project_id}.join(',')}))" |
|
115 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND (#{Project.table_name}.is_public = #{connection.quoted_true} or #{Project.table_name}.id IN (#{user.memberships.collect{|m| m.project_id}.join(',')}))" | |
114 | else |
|
116 | else | |
115 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}" |
|
117 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}" | |
116 | end |
|
118 | end | |
117 | end |
|
119 | end | |
118 |
|
120 | |||
119 | def self.allowed_to_condition(user, permission, options={}) |
|
121 | def self.allowed_to_condition(user, permission, options={}) | |
120 | statements = [] |
|
122 | statements = [] | |
121 | base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}" |
|
123 | base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}" | |
122 | if perm = Redmine::AccessControl.permission(permission) |
|
124 | if perm = Redmine::AccessControl.permission(permission) | |
123 | unless perm.project_module.nil? |
|
125 | unless perm.project_module.nil? | |
124 | # If the permission belongs to a project module, make sure the module is enabled |
|
126 | # If the permission belongs to a project module, make sure the module is enabled | |
125 | base_statement << " AND EXISTS (SELECT em.id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}' AND em.project_id=#{Project.table_name}.id)" |
|
127 | base_statement << " AND EXISTS (SELECT em.id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}' AND em.project_id=#{Project.table_name}.id)" | |
126 | end |
|
128 | end | |
127 | end |
|
129 | end | |
128 | if options[:project] |
|
130 | if options[:project] | |
129 | project_statement = "#{Project.table_name}.id = #{options[:project].id}" |
|
131 | project_statement = "#{Project.table_name}.id = #{options[:project].id}" | |
130 | project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects] |
|
132 | project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects] | |
131 | base_statement = "(#{project_statement}) AND (#{base_statement})" |
|
133 | base_statement = "(#{project_statement}) AND (#{base_statement})" | |
132 | end |
|
134 | end | |
133 | if user.admin? |
|
135 | if user.admin? | |
134 | # no restriction |
|
136 | # no restriction | |
135 | else |
|
137 | else | |
136 | statements << "1=0" |
|
138 | statements << "1=0" | |
137 | if user.logged? |
|
139 | if user.logged? | |
138 | statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" if Role.non_member.allowed_to?(permission) |
|
140 | statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" if Role.non_member.allowed_to?(permission) | |
139 | allowed_project_ids = user.memberships.select {|m| m.roles.detect {|role| role.allowed_to?(permission)}}.collect {|m| m.project_id} |
|
141 | allowed_project_ids = user.memberships.select {|m| m.roles.detect {|role| role.allowed_to?(permission)}}.collect {|m| m.project_id} | |
140 | statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any? |
|
142 | statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any? | |
141 | elsif Role.anonymous.allowed_to?(permission) |
|
143 | elsif Role.anonymous.allowed_to?(permission) | |
142 | # anonymous user allowed on public project |
|
144 | # anonymous user allowed on public project | |
143 | statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" |
|
145 | statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" | |
144 | else |
|
146 | else | |
145 | # anonymous user is not authorized |
|
147 | # anonymous user is not authorized | |
146 | end |
|
148 | end | |
147 | end |
|
149 | end | |
148 | statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))" |
|
150 | statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))" | |
149 | end |
|
151 | end | |
150 |
|
152 | |||
151 | # Returns a :conditions SQL string that can be used to find the issues associated with this project. |
|
153 | # Returns a :conditions SQL string that can be used to find the issues associated with this project. | |
152 | # |
|
154 | # | |
153 | # Examples: |
|
155 | # Examples: | |
154 | # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))" |
|
156 | # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))" | |
155 | # project.project_condition(false) => "projects.id = 1" |
|
157 | # project.project_condition(false) => "projects.id = 1" | |
156 | def project_condition(with_subprojects) |
|
158 | def project_condition(with_subprojects) | |
157 | cond = "#{Project.table_name}.id = #{id}" |
|
159 | cond = "#{Project.table_name}.id = #{id}" | |
158 | cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects |
|
160 | cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects | |
159 | cond |
|
161 | cond | |
160 | end |
|
162 | end | |
161 |
|
163 | |||
162 | def self.find(*args) |
|
164 | def self.find(*args) | |
163 | if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/) |
|
165 | if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/) | |
164 | project = find_by_identifier(*args) |
|
166 | project = find_by_identifier(*args) | |
165 | raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil? |
|
167 | raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil? | |
166 | project |
|
168 | project | |
167 | else |
|
169 | else | |
168 | super |
|
170 | super | |
169 | end |
|
171 | end | |
170 | end |
|
172 | end | |
171 |
|
173 | |||
172 | def to_param |
|
174 | def to_param | |
173 | # id is used for projects with a numeric identifier (compatibility) |
|
175 | # id is used for projects with a numeric identifier (compatibility) | |
174 | @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier) |
|
176 | @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier) | |
175 | end |
|
177 | end | |
176 |
|
178 | |||
177 | def active? |
|
179 | def active? | |
178 | self.status == STATUS_ACTIVE |
|
180 | self.status == STATUS_ACTIVE | |
179 | end |
|
181 | end | |
180 |
|
182 | |||
181 | # Archives the project and its descendants recursively |
|
183 | # Archives the project and its descendants recursively | |
182 | def archive |
|
184 | def archive | |
183 | # Archive subprojects if any |
|
185 | # Archive subprojects if any | |
184 | children.each do |subproject| |
|
186 | children.each do |subproject| | |
185 | subproject.archive |
|
187 | subproject.archive | |
186 | end |
|
188 | end | |
187 | update_attribute :status, STATUS_ARCHIVED |
|
189 | update_attribute :status, STATUS_ARCHIVED | |
188 | end |
|
190 | end | |
189 |
|
191 | |||
190 | # Unarchives the project |
|
192 | # Unarchives the project | |
191 | # All its ancestors must be active |
|
193 | # All its ancestors must be active | |
192 | def unarchive |
|
194 | def unarchive | |
193 | return false if ancestors.detect {|a| !a.active?} |
|
195 | return false if ancestors.detect {|a| !a.active?} | |
194 | update_attribute :status, STATUS_ACTIVE |
|
196 | update_attribute :status, STATUS_ACTIVE | |
195 | end |
|
197 | end | |
196 |
|
198 | |||
197 | # Returns an array of projects the project can be moved to |
|
199 | # Returns an array of projects the project can be moved to | |
198 | def possible_parents |
|
200 | def possible_parents | |
199 | @possible_parents ||= (Project.active.find(:all) - self_and_descendants) |
|
201 | @possible_parents ||= (Project.active.find(:all) - self_and_descendants) | |
200 | end |
|
202 | end | |
201 |
|
203 | |||
202 | # Sets the parent of the project |
|
204 | # Sets the parent of the project | |
203 | # Argument can be either a Project, a String, a Fixnum or nil |
|
205 | # Argument can be either a Project, a String, a Fixnum or nil | |
204 | def set_parent!(p) |
|
206 | def set_parent!(p) | |
205 | unless p.nil? || p.is_a?(Project) |
|
207 | unless p.nil? || p.is_a?(Project) | |
206 | if p.to_s.blank? |
|
208 | if p.to_s.blank? | |
207 | p = nil |
|
209 | p = nil | |
208 | else |
|
210 | else | |
209 | p = Project.find_by_id(p) |
|
211 | p = Project.find_by_id(p) | |
210 | return false unless p |
|
212 | return false unless p | |
211 | end |
|
213 | end | |
212 | end |
|
214 | end | |
213 | if p == parent && !p.nil? |
|
215 | if p == parent && !p.nil? | |
214 | # Nothing to do |
|
216 | # Nothing to do | |
215 | true |
|
217 | true | |
216 | elsif p.nil? || (p.active? && move_possible?(p)) |
|
218 | elsif p.nil? || (p.active? && move_possible?(p)) | |
217 | # Insert the project so that target's children or root projects stay alphabetically sorted |
|
219 | # Insert the project so that target's children or root projects stay alphabetically sorted | |
218 | sibs = (p.nil? ? self.class.roots : p.children) |
|
220 | sibs = (p.nil? ? self.class.roots : p.children) | |
219 | to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase } |
|
221 | to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase } | |
220 | if to_be_inserted_before |
|
222 | if to_be_inserted_before | |
221 | move_to_left_of(to_be_inserted_before) |
|
223 | move_to_left_of(to_be_inserted_before) | |
222 | elsif p.nil? |
|
224 | elsif p.nil? | |
223 | if sibs.empty? |
|
225 | if sibs.empty? | |
224 | # move_to_root adds the project in first (ie. left) position |
|
226 | # move_to_root adds the project in first (ie. left) position | |
225 | move_to_root |
|
227 | move_to_root | |
226 | else |
|
228 | else | |
227 | move_to_right_of(sibs.last) unless self == sibs.last |
|
229 | move_to_right_of(sibs.last) unless self == sibs.last | |
228 | end |
|
230 | end | |
229 | else |
|
231 | else | |
230 | # move_to_child_of adds the project in last (ie.right) position |
|
232 | # move_to_child_of adds the project in last (ie.right) position | |
231 | move_to_child_of(p) |
|
233 | move_to_child_of(p) | |
232 | end |
|
234 | end | |
233 | true |
|
235 | true | |
234 | else |
|
236 | else | |
235 | # Can not move to the given target |
|
237 | # Can not move to the given target | |
236 | false |
|
238 | false | |
237 | end |
|
239 | end | |
238 | end |
|
240 | end | |
239 |
|
241 | |||
240 | # Returns an array of the trackers used by the project and its active sub projects |
|
242 | # Returns an array of the trackers used by the project and its active sub projects | |
241 | def rolled_up_trackers |
|
243 | def rolled_up_trackers | |
242 | @rolled_up_trackers ||= |
|
244 | @rolled_up_trackers ||= | |
243 | Tracker.find(:all, :include => :projects, |
|
245 | Tracker.find(:all, :include => :projects, | |
244 | :select => "DISTINCT #{Tracker.table_name}.*", |
|
246 | :select => "DISTINCT #{Tracker.table_name}.*", | |
245 | :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt], |
|
247 | :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt], | |
246 | :order => "#{Tracker.table_name}.position") |
|
248 | :order => "#{Tracker.table_name}.position") | |
247 | end |
|
249 | end | |
248 |
|
250 | |||
249 | # Returns a hash of project users grouped by role |
|
251 | # Returns a hash of project users grouped by role | |
250 | def users_by_role |
|
252 | def users_by_role | |
251 | members.find(:all, :include => [:user, :roles]).inject({}) do |h, m| |
|
253 | members.find(:all, :include => [:user, :roles]).inject({}) do |h, m| | |
252 | m.roles.each do |r| |
|
254 | m.roles.each do |r| | |
253 | h[r] ||= [] |
|
255 | h[r] ||= [] | |
254 | h[r] << m.user |
|
256 | h[r] << m.user | |
255 | end |
|
257 | end | |
256 | h |
|
258 | h | |
257 | end |
|
259 | end | |
258 | end |
|
260 | end | |
259 |
|
261 | |||
260 | # Deletes all project's members |
|
262 | # Deletes all project's members | |
261 | def delete_all_members |
|
263 | def delete_all_members | |
262 | me, mr = Member.table_name, MemberRole.table_name |
|
264 | me, mr = Member.table_name, MemberRole.table_name | |
263 | connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})") |
|
265 | connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})") | |
264 | Member.delete_all(['project_id = ?', id]) |
|
266 | Member.delete_all(['project_id = ?', id]) | |
265 | end |
|
267 | end | |
266 |
|
268 | |||
267 | # Users issues can be assigned to |
|
269 | # Users issues can be assigned to | |
268 | def assignable_users |
|
270 | def assignable_users | |
269 | members.select {|m| m.roles.detect {|role| role.assignable?}}.collect {|m| m.user}.sort |
|
271 | members.select {|m| m.roles.detect {|role| role.assignable?}}.collect {|m| m.user}.sort | |
270 | end |
|
272 | end | |
271 |
|
273 | |||
272 | # Returns the mail adresses of users that should be always notified on project events |
|
274 | # Returns the mail adresses of users that should be always notified on project events | |
273 | def recipients |
|
275 | def recipients | |
274 | members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail} |
|
276 | members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail} | |
275 | end |
|
277 | end | |
276 |
|
278 | |||
277 | # Returns an array of all custom fields enabled for project issues |
|
279 | # Returns an array of all custom fields enabled for project issues | |
278 | # (explictly associated custom fields and custom fields enabled for all projects) |
|
280 | # (explictly associated custom fields and custom fields enabled for all projects) | |
279 | def all_issue_custom_fields |
|
281 | def all_issue_custom_fields | |
280 | @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort |
|
282 | @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort | |
281 | end |
|
283 | end | |
282 |
|
284 | |||
283 | def project |
|
285 | def project | |
284 | self |
|
286 | self | |
285 | end |
|
287 | end | |
286 |
|
288 | |||
287 | def <=>(project) |
|
289 | def <=>(project) | |
288 | name.downcase <=> project.name.downcase |
|
290 | name.downcase <=> project.name.downcase | |
289 | end |
|
291 | end | |
290 |
|
292 | |||
291 | def to_s |
|
293 | def to_s | |
292 | name |
|
294 | name | |
293 | end |
|
295 | end | |
294 |
|
296 | |||
295 | # Returns a short description of the projects (first lines) |
|
297 | # Returns a short description of the projects (first lines) | |
296 | def short_description(length = 255) |
|
298 | def short_description(length = 255) | |
297 | description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description |
|
299 | description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description | |
298 | end |
|
300 | end | |
299 |
|
301 | |||
300 | # Return true if this project is allowed to do the specified action. |
|
302 | # Return true if this project is allowed to do the specified action. | |
301 | # action can be: |
|
303 | # action can be: | |
302 | # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') |
|
304 | # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') | |
303 | # * a permission Symbol (eg. :edit_project) |
|
305 | # * a permission Symbol (eg. :edit_project) | |
304 | def allows_to?(action) |
|
306 | def allows_to?(action) | |
305 | if action.is_a? Hash |
|
307 | if action.is_a? Hash | |
306 | allowed_actions.include? "#{action[:controller]}/#{action[:action]}" |
|
308 | allowed_actions.include? "#{action[:controller]}/#{action[:action]}" | |
307 | else |
|
309 | else | |
308 | allowed_permissions.include? action |
|
310 | allowed_permissions.include? action | |
309 | end |
|
311 | end | |
310 | end |
|
312 | end | |
311 |
|
313 | |||
312 | def module_enabled?(module_name) |
|
314 | def module_enabled?(module_name) | |
313 | module_name = module_name.to_s |
|
315 | module_name = module_name.to_s | |
314 | enabled_modules.detect {|m| m.name == module_name} |
|
316 | enabled_modules.detect {|m| m.name == module_name} | |
315 | end |
|
317 | end | |
316 |
|
318 | |||
317 | def enabled_module_names=(module_names) |
|
319 | def enabled_module_names=(module_names) | |
318 | if module_names && module_names.is_a?(Array) |
|
320 | if module_names && module_names.is_a?(Array) | |
319 | module_names = module_names.collect(&:to_s) |
|
321 | module_names = module_names.collect(&:to_s) | |
320 | # remove disabled modules |
|
322 | # remove disabled modules | |
321 | enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)} |
|
323 | enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)} | |
322 | # add new modules |
|
324 | # add new modules | |
323 | module_names.each {|name| enabled_modules << EnabledModule.new(:name => name)} |
|
325 | module_names.each {|name| enabled_modules << EnabledModule.new(:name => name)} | |
324 | else |
|
326 | else | |
325 | enabled_modules.clear |
|
327 | enabled_modules.clear | |
326 | end |
|
328 | end | |
327 | end |
|
329 | end | |
328 |
|
330 | |||
329 | # Returns an auto-generated project identifier based on the last identifier used |
|
331 | # Returns an auto-generated project identifier based on the last identifier used | |
330 | def self.next_identifier |
|
332 | def self.next_identifier | |
331 | p = Project.find(:first, :order => 'created_on DESC') |
|
333 | p = Project.find(:first, :order => 'created_on DESC') | |
332 | p.nil? ? nil : p.identifier.to_s.succ |
|
334 | p.nil? ? nil : p.identifier.to_s.succ | |
333 | end |
|
335 | end | |
334 |
|
336 | |||
335 | # Copies and saves the Project instance based on the +project+. |
|
337 | # Copies and saves the Project instance based on the +project+. | |
336 | # Will duplicate the source project's: |
|
338 | # Will duplicate the source project's: | |
337 | # * Issues |
|
339 | # * Issues | |
338 | # * Members |
|
340 | # * Members | |
339 | # * Queries |
|
341 | # * Queries | |
340 | def copy(project) |
|
342 | def copy(project) | |
341 | project = project.is_a?(Project) ? project : Project.find(project) |
|
343 | project = project.is_a?(Project) ? project : Project.find(project) | |
342 |
|
344 | |||
343 | Project.transaction do |
|
345 | Project.transaction do | |
344 | # Issues |
|
346 | # Issues | |
345 | project.issues.each do |issue| |
|
347 | project.issues.each do |issue| | |
346 | new_issue = Issue.new |
|
348 | new_issue = Issue.new | |
347 | new_issue.copy_from(issue) |
|
349 | new_issue.copy_from(issue) | |
348 | self.issues << new_issue |
|
350 | self.issues << new_issue | |
349 | end |
|
351 | end | |
350 |
|
352 | |||
351 | # Members |
|
353 | # Members | |
352 | project.members.each do |member| |
|
354 | project.members.each do |member| | |
353 | new_member = Member.new |
|
355 | new_member = Member.new | |
354 | new_member.attributes = member.attributes.dup.except("project_id") |
|
356 | new_member.attributes = member.attributes.dup.except("project_id") | |
355 | new_member.role_ids = member.role_ids.dup |
|
357 | new_member.role_ids = member.role_ids.dup | |
356 | new_member.project = self |
|
358 | new_member.project = self | |
357 | self.members << new_member |
|
359 | self.members << new_member | |
358 | end |
|
360 | end | |
359 |
|
361 | |||
360 | # Queries |
|
362 | # Queries | |
361 | project.queries.each do |query| |
|
363 | project.queries.each do |query| | |
362 | new_query = Query.new |
|
364 | new_query = Query.new | |
363 | new_query.attributes = query.attributes.dup.except("project_id", "sort_criteria") |
|
365 | new_query.attributes = query.attributes.dup.except("project_id", "sort_criteria") | |
364 | new_query.sort_criteria = query.sort_criteria if query.sort_criteria |
|
366 | new_query.sort_criteria = query.sort_criteria if query.sort_criteria | |
365 | new_query.project = self |
|
367 | new_query.project = self | |
366 | self.queries << new_query |
|
368 | self.queries << new_query | |
367 | end |
|
369 | end | |
368 |
|
370 | |||
369 | Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self) |
|
371 | Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self) | |
370 | self.save |
|
372 | self.save | |
371 | end |
|
373 | end | |
372 | end |
|
374 | end | |
373 |
|
375 | |||
374 |
|
376 | |||
375 | # Copies +project+ and returns the new instance. This will not save |
|
377 | # Copies +project+ and returns the new instance. This will not save | |
376 | # the copy |
|
378 | # the copy | |
377 | def self.copy_from(project) |
|
379 | def self.copy_from(project) | |
378 | begin |
|
380 | begin | |
379 | project = project.is_a?(Project) ? project : Project.find(project) |
|
381 | project = project.is_a?(Project) ? project : Project.find(project) | |
380 | if project |
|
382 | if project | |
381 | # clear unique attributes |
|
383 | # clear unique attributes | |
382 | attributes = project.attributes.dup.except('name', 'identifier', 'id', 'status') |
|
384 | attributes = project.attributes.dup.except('name', 'identifier', 'id', 'status') | |
383 | copy = Project.new(attributes) |
|
385 | copy = Project.new(attributes) | |
384 | copy.enabled_modules = project.enabled_modules |
|
386 | copy.enabled_modules = project.enabled_modules | |
385 | copy.trackers = project.trackers |
|
387 | copy.trackers = project.trackers | |
386 | copy.custom_values = project.custom_values.collect {|v| v.clone} |
|
388 | copy.custom_values = project.custom_values.collect {|v| v.clone} | |
387 | return copy |
|
389 | return copy | |
388 | else |
|
390 | else | |
389 | return nil |
|
391 | return nil | |
390 | end |
|
392 | end | |
391 | rescue ActiveRecord::RecordNotFound |
|
393 | rescue ActiveRecord::RecordNotFound | |
392 | return nil |
|
394 | return nil | |
393 | end |
|
395 | end | |
394 | end |
|
396 | end | |
395 |
|
397 | |||
396 | private |
|
398 | private | |
397 | def allowed_permissions |
|
399 | def allowed_permissions | |
398 | @allowed_permissions ||= begin |
|
400 | @allowed_permissions ||= begin | |
399 | module_names = enabled_modules.collect {|m| m.name} |
|
401 | module_names = enabled_modules.collect {|m| m.name} | |
400 | Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name} |
|
402 | Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name} | |
401 | end |
|
403 | end | |
402 | end |
|
404 | end | |
403 |
|
405 | |||
404 | def allowed_actions |
|
406 | def allowed_actions | |
405 | @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten |
|
407 | @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten | |
406 | end |
|
408 | end | |
407 | end |
|
409 | end |
@@ -1,342 +1,343 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
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 |
|
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. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | require File.dirname(__FILE__) + '/../test_helper' |
|
18 | require File.dirname(__FILE__) + '/../test_helper' | |
19 |
|
19 | |||
20 | class ProjectTest < Test::Unit::TestCase |
|
20 | class ProjectTest < Test::Unit::TestCase | |
21 | fixtures :projects, :enabled_modules, |
|
21 | fixtures :projects, :enabled_modules, | |
22 | :issues, :issue_statuses, :journals, :journal_details, |
|
22 | :issues, :issue_statuses, :journals, :journal_details, | |
23 | :users, :members, :member_roles, :roles, :projects_trackers, :trackers, :boards, |
|
23 | :users, :members, :member_roles, :roles, :projects_trackers, :trackers, :boards, | |
24 | :queries |
|
24 | :queries | |
25 |
|
25 | |||
26 | def setup |
|
26 | def setup | |
27 | @ecookbook = Project.find(1) |
|
27 | @ecookbook = Project.find(1) | |
28 | @ecookbook_sub1 = Project.find(3) |
|
28 | @ecookbook_sub1 = Project.find(3) | |
29 | end |
|
29 | end | |
30 |
|
30 | |||
31 | def test_truth |
|
31 | def test_truth | |
32 | assert_kind_of Project, @ecookbook |
|
32 | assert_kind_of Project, @ecookbook | |
33 | assert_equal "eCookbook", @ecookbook.name |
|
33 | assert_equal "eCookbook", @ecookbook.name | |
34 | end |
|
34 | end | |
35 |
|
35 | |||
36 | def test_update |
|
36 | def test_update | |
37 | assert_equal "eCookbook", @ecookbook.name |
|
37 | assert_equal "eCookbook", @ecookbook.name | |
38 | @ecookbook.name = "eCook" |
|
38 | @ecookbook.name = "eCook" | |
39 | assert @ecookbook.save, @ecookbook.errors.full_messages.join("; ") |
|
39 | assert @ecookbook.save, @ecookbook.errors.full_messages.join("; ") | |
40 | @ecookbook.reload |
|
40 | @ecookbook.reload | |
41 | assert_equal "eCook", @ecookbook.name |
|
41 | assert_equal "eCook", @ecookbook.name | |
42 | end |
|
42 | end | |
43 |
|
43 | |||
44 | def test_validate |
|
44 | def test_validate | |
45 | @ecookbook.name = "" |
|
45 | @ecookbook.name = "" | |
46 | assert !@ecookbook.save |
|
46 | assert !@ecookbook.save | |
47 | assert_equal 1, @ecookbook.errors.count |
|
47 | assert_equal 1, @ecookbook.errors.count | |
48 | assert_equal I18n.translate('activerecord.errors.messages.blank'), @ecookbook.errors.on(:name) |
|
48 | assert_equal I18n.translate('activerecord.errors.messages.blank'), @ecookbook.errors.on(:name) | |
49 | end |
|
49 | end | |
50 |
|
50 | |||
51 | def test_validate_identifier |
|
51 | def test_validate_identifier | |
52 | to_test = {"abc" => true, |
|
52 | to_test = {"abc" => true, | |
53 | "ab12" => true, |
|
53 | "ab12" => true, | |
54 | "ab-12" => true, |
|
54 | "ab-12" => true, | |
55 |
"12" => false |
|
55 | "12" => false, | |
|
56 | "new" => false} | |||
56 |
|
57 | |||
57 | to_test.each do |identifier, valid| |
|
58 | to_test.each do |identifier, valid| | |
58 | p = Project.new |
|
59 | p = Project.new | |
59 | p.identifier = identifier |
|
60 | p.identifier = identifier | |
60 | p.valid? |
|
61 | p.valid? | |
61 | assert_equal valid, p.errors.on('identifier').nil? |
|
62 | assert_equal valid, p.errors.on('identifier').nil? | |
62 | end |
|
63 | end | |
63 | end |
|
64 | end | |
64 |
|
65 | |||
65 | def test_archive |
|
66 | def test_archive | |
66 | user = @ecookbook.members.first.user |
|
67 | user = @ecookbook.members.first.user | |
67 | @ecookbook.archive |
|
68 | @ecookbook.archive | |
68 | @ecookbook.reload |
|
69 | @ecookbook.reload | |
69 |
|
70 | |||
70 | assert !@ecookbook.active? |
|
71 | assert !@ecookbook.active? | |
71 | assert !user.projects.include?(@ecookbook) |
|
72 | assert !user.projects.include?(@ecookbook) | |
72 | # Subproject are also archived |
|
73 | # Subproject are also archived | |
73 | assert !@ecookbook.children.empty? |
|
74 | assert !@ecookbook.children.empty? | |
74 | assert @ecookbook.descendants.active.empty? |
|
75 | assert @ecookbook.descendants.active.empty? | |
75 | end |
|
76 | end | |
76 |
|
77 | |||
77 | def test_unarchive |
|
78 | def test_unarchive | |
78 | user = @ecookbook.members.first.user |
|
79 | user = @ecookbook.members.first.user | |
79 | @ecookbook.archive |
|
80 | @ecookbook.archive | |
80 | # A subproject of an archived project can not be unarchived |
|
81 | # A subproject of an archived project can not be unarchived | |
81 | assert !@ecookbook_sub1.unarchive |
|
82 | assert !@ecookbook_sub1.unarchive | |
82 |
|
83 | |||
83 | # Unarchive project |
|
84 | # Unarchive project | |
84 | assert @ecookbook.unarchive |
|
85 | assert @ecookbook.unarchive | |
85 | @ecookbook.reload |
|
86 | @ecookbook.reload | |
86 | assert @ecookbook.active? |
|
87 | assert @ecookbook.active? | |
87 | assert user.projects.include?(@ecookbook) |
|
88 | assert user.projects.include?(@ecookbook) | |
88 | # Subproject can now be unarchived |
|
89 | # Subproject can now be unarchived | |
89 | @ecookbook_sub1.reload |
|
90 | @ecookbook_sub1.reload | |
90 | assert @ecookbook_sub1.unarchive |
|
91 | assert @ecookbook_sub1.unarchive | |
91 | end |
|
92 | end | |
92 |
|
93 | |||
93 | def test_destroy |
|
94 | def test_destroy | |
94 | # 2 active members |
|
95 | # 2 active members | |
95 | assert_equal 2, @ecookbook.members.size |
|
96 | assert_equal 2, @ecookbook.members.size | |
96 | # and 1 is locked |
|
97 | # and 1 is locked | |
97 | assert_equal 3, Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).size |
|
98 | assert_equal 3, Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).size | |
98 | # some boards |
|
99 | # some boards | |
99 | assert @ecookbook.boards.any? |
|
100 | assert @ecookbook.boards.any? | |
100 |
|
101 | |||
101 | @ecookbook.destroy |
|
102 | @ecookbook.destroy | |
102 | # make sure that the project non longer exists |
|
103 | # make sure that the project non longer exists | |
103 | assert_raise(ActiveRecord::RecordNotFound) { Project.find(@ecookbook.id) } |
|
104 | assert_raise(ActiveRecord::RecordNotFound) { Project.find(@ecookbook.id) } | |
104 | # make sure related data was removed |
|
105 | # make sure related data was removed | |
105 | assert Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty? |
|
106 | assert Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty? | |
106 | assert Board.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty? |
|
107 | assert Board.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).empty? | |
107 | end |
|
108 | end | |
108 |
|
109 | |||
109 | def test_move_an_orphan_project_to_a_root_project |
|
110 | def test_move_an_orphan_project_to_a_root_project | |
110 | sub = Project.find(2) |
|
111 | sub = Project.find(2) | |
111 | sub.set_parent! @ecookbook |
|
112 | sub.set_parent! @ecookbook | |
112 | assert_equal @ecookbook.id, sub.parent.id |
|
113 | assert_equal @ecookbook.id, sub.parent.id | |
113 | @ecookbook.reload |
|
114 | @ecookbook.reload | |
114 | assert_equal 4, @ecookbook.children.size |
|
115 | assert_equal 4, @ecookbook.children.size | |
115 | end |
|
116 | end | |
116 |
|
117 | |||
117 | def test_move_an_orphan_project_to_a_subproject |
|
118 | def test_move_an_orphan_project_to_a_subproject | |
118 | sub = Project.find(2) |
|
119 | sub = Project.find(2) | |
119 | assert sub.set_parent!(@ecookbook_sub1) |
|
120 | assert sub.set_parent!(@ecookbook_sub1) | |
120 | end |
|
121 | end | |
121 |
|
122 | |||
122 | def test_move_a_root_project_to_a_project |
|
123 | def test_move_a_root_project_to_a_project | |
123 | sub = @ecookbook |
|
124 | sub = @ecookbook | |
124 | assert sub.set_parent!(Project.find(2)) |
|
125 | assert sub.set_parent!(Project.find(2)) | |
125 | end |
|
126 | end | |
126 |
|
127 | |||
127 | def test_should_not_move_a_project_to_its_children |
|
128 | def test_should_not_move_a_project_to_its_children | |
128 | sub = @ecookbook |
|
129 | sub = @ecookbook | |
129 | assert !(sub.set_parent!(Project.find(3))) |
|
130 | assert !(sub.set_parent!(Project.find(3))) | |
130 | end |
|
131 | end | |
131 |
|
132 | |||
132 | def test_set_parent_should_add_roots_in_alphabetical_order |
|
133 | def test_set_parent_should_add_roots_in_alphabetical_order | |
133 | ProjectCustomField.delete_all |
|
134 | ProjectCustomField.delete_all | |
134 | Project.delete_all |
|
135 | Project.delete_all | |
135 | Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(nil) |
|
136 | Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(nil) | |
136 | Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(nil) |
|
137 | Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(nil) | |
137 | Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(nil) |
|
138 | Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(nil) | |
138 | Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(nil) |
|
139 | Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(nil) | |
139 |
|
140 | |||
140 | assert_equal 4, Project.count |
|
141 | assert_equal 4, Project.count | |
141 | assert_equal Project.all.sort_by(&:name), Project.all.sort_by(&:lft) |
|
142 | assert_equal Project.all.sort_by(&:name), Project.all.sort_by(&:lft) | |
142 | end |
|
143 | end | |
143 |
|
144 | |||
144 | def test_set_parent_should_add_children_in_alphabetical_order |
|
145 | def test_set_parent_should_add_children_in_alphabetical_order | |
145 | ProjectCustomField.delete_all |
|
146 | ProjectCustomField.delete_all | |
146 | parent = Project.create!(:name => 'Parent', :identifier => 'parent') |
|
147 | parent = Project.create!(:name => 'Parent', :identifier => 'parent') | |
147 | Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(parent) |
|
148 | Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(parent) | |
148 | Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(parent) |
|
149 | Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(parent) | |
149 | Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(parent) |
|
150 | Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(parent) | |
150 | Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(parent) |
|
151 | Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(parent) | |
151 |
|
152 | |||
152 | parent.reload |
|
153 | parent.reload | |
153 | assert_equal 4, parent.children.size |
|
154 | assert_equal 4, parent.children.size | |
154 | assert_equal parent.children.sort_by(&:name), parent.children |
|
155 | assert_equal parent.children.sort_by(&:name), parent.children | |
155 | end |
|
156 | end | |
156 |
|
157 | |||
157 | def test_rebuild_should_sort_children_alphabetically |
|
158 | def test_rebuild_should_sort_children_alphabetically | |
158 | ProjectCustomField.delete_all |
|
159 | ProjectCustomField.delete_all | |
159 | parent = Project.create!(:name => 'Parent', :identifier => 'parent') |
|
160 | parent = Project.create!(:name => 'Parent', :identifier => 'parent') | |
160 | Project.create!(:name => 'Project C', :identifier => 'project-c').move_to_child_of(parent) |
|
161 | Project.create!(:name => 'Project C', :identifier => 'project-c').move_to_child_of(parent) | |
161 | Project.create!(:name => 'Project B', :identifier => 'project-b').move_to_child_of(parent) |
|
162 | Project.create!(:name => 'Project B', :identifier => 'project-b').move_to_child_of(parent) | |
162 | Project.create!(:name => 'Project D', :identifier => 'project-d').move_to_child_of(parent) |
|
163 | Project.create!(:name => 'Project D', :identifier => 'project-d').move_to_child_of(parent) | |
163 | Project.create!(:name => 'Project A', :identifier => 'project-a').move_to_child_of(parent) |
|
164 | Project.create!(:name => 'Project A', :identifier => 'project-a').move_to_child_of(parent) | |
164 |
|
165 | |||
165 | Project.update_all("lft = NULL, rgt = NULL") |
|
166 | Project.update_all("lft = NULL, rgt = NULL") | |
166 | Project.rebuild! |
|
167 | Project.rebuild! | |
167 |
|
168 | |||
168 | parent.reload |
|
169 | parent.reload | |
169 | assert_equal 4, parent.children.size |
|
170 | assert_equal 4, parent.children.size | |
170 | assert_equal parent.children.sort_by(&:name), parent.children |
|
171 | assert_equal parent.children.sort_by(&:name), parent.children | |
171 | end |
|
172 | end | |
172 |
|
173 | |||
173 | def test_parent |
|
174 | def test_parent | |
174 | p = Project.find(6).parent |
|
175 | p = Project.find(6).parent | |
175 | assert p.is_a?(Project) |
|
176 | assert p.is_a?(Project) | |
176 | assert_equal 5, p.id |
|
177 | assert_equal 5, p.id | |
177 | end |
|
178 | end | |
178 |
|
179 | |||
179 | def test_ancestors |
|
180 | def test_ancestors | |
180 | a = Project.find(6).ancestors |
|
181 | a = Project.find(6).ancestors | |
181 | assert a.first.is_a?(Project) |
|
182 | assert a.first.is_a?(Project) | |
182 | assert_equal [1, 5], a.collect(&:id) |
|
183 | assert_equal [1, 5], a.collect(&:id) | |
183 | end |
|
184 | end | |
184 |
|
185 | |||
185 | def test_root |
|
186 | def test_root | |
186 | r = Project.find(6).root |
|
187 | r = Project.find(6).root | |
187 | assert r.is_a?(Project) |
|
188 | assert r.is_a?(Project) | |
188 | assert_equal 1, r.id |
|
189 | assert_equal 1, r.id | |
189 | end |
|
190 | end | |
190 |
|
191 | |||
191 | def test_children |
|
192 | def test_children | |
192 | c = Project.find(1).children |
|
193 | c = Project.find(1).children | |
193 | assert c.first.is_a?(Project) |
|
194 | assert c.first.is_a?(Project) | |
194 | assert_equal [5, 3, 4], c.collect(&:id) |
|
195 | assert_equal [5, 3, 4], c.collect(&:id) | |
195 | end |
|
196 | end | |
196 |
|
197 | |||
197 | def test_descendants |
|
198 | def test_descendants | |
198 | d = Project.find(1).descendants |
|
199 | d = Project.find(1).descendants | |
199 | assert d.first.is_a?(Project) |
|
200 | assert d.first.is_a?(Project) | |
200 | assert_equal [5, 6, 3, 4], d.collect(&:id) |
|
201 | assert_equal [5, 6, 3, 4], d.collect(&:id) | |
201 | end |
|
202 | end | |
202 |
|
203 | |||
203 | def test_users_by_role |
|
204 | def test_users_by_role | |
204 | users_by_role = Project.find(1).users_by_role |
|
205 | users_by_role = Project.find(1).users_by_role | |
205 | assert_kind_of Hash, users_by_role |
|
206 | assert_kind_of Hash, users_by_role | |
206 | role = Role.find(1) |
|
207 | role = Role.find(1) | |
207 | assert_kind_of Array, users_by_role[role] |
|
208 | assert_kind_of Array, users_by_role[role] | |
208 | assert users_by_role[role].include?(User.find(2)) |
|
209 | assert users_by_role[role].include?(User.find(2)) | |
209 | end |
|
210 | end | |
210 |
|
211 | |||
211 | def test_rolled_up_trackers |
|
212 | def test_rolled_up_trackers | |
212 | parent = Project.find(1) |
|
213 | parent = Project.find(1) | |
213 | parent.trackers = Tracker.find([1,2]) |
|
214 | parent.trackers = Tracker.find([1,2]) | |
214 | child = parent.children.find(3) |
|
215 | child = parent.children.find(3) | |
215 |
|
216 | |||
216 | assert_equal [1, 2], parent.tracker_ids |
|
217 | assert_equal [1, 2], parent.tracker_ids | |
217 | assert_equal [2, 3], child.trackers.collect(&:id) |
|
218 | assert_equal [2, 3], child.trackers.collect(&:id) | |
218 |
|
219 | |||
219 | assert_kind_of Tracker, parent.rolled_up_trackers.first |
|
220 | assert_kind_of Tracker, parent.rolled_up_trackers.first | |
220 | assert_equal Tracker.find(1), parent.rolled_up_trackers.first |
|
221 | assert_equal Tracker.find(1), parent.rolled_up_trackers.first | |
221 |
|
222 | |||
222 | assert_equal [1, 2, 3], parent.rolled_up_trackers.collect(&:id) |
|
223 | assert_equal [1, 2, 3], parent.rolled_up_trackers.collect(&:id) | |
223 | assert_equal [2, 3], child.rolled_up_trackers.collect(&:id) |
|
224 | assert_equal [2, 3], child.rolled_up_trackers.collect(&:id) | |
224 | end |
|
225 | end | |
225 |
|
226 | |||
226 | def test_rolled_up_trackers_should_ignore_archived_subprojects |
|
227 | def test_rolled_up_trackers_should_ignore_archived_subprojects | |
227 | parent = Project.find(1) |
|
228 | parent = Project.find(1) | |
228 | parent.trackers = Tracker.find([1,2]) |
|
229 | parent.trackers = Tracker.find([1,2]) | |
229 | child = parent.children.find(3) |
|
230 | child = parent.children.find(3) | |
230 | child.trackers = Tracker.find([1,3]) |
|
231 | child.trackers = Tracker.find([1,3]) | |
231 | parent.children.each(&:archive) |
|
232 | parent.children.each(&:archive) | |
232 |
|
233 | |||
233 | assert_equal [1,2], parent.rolled_up_trackers.collect(&:id) |
|
234 | assert_equal [1,2], parent.rolled_up_trackers.collect(&:id) | |
234 | end |
|
235 | end | |
235 |
|
236 | |||
236 | def test_next_identifier |
|
237 | def test_next_identifier | |
237 | ProjectCustomField.delete_all |
|
238 | ProjectCustomField.delete_all | |
238 | Project.create!(:name => 'last', :identifier => 'p2008040') |
|
239 | Project.create!(:name => 'last', :identifier => 'p2008040') | |
239 | assert_equal 'p2008041', Project.next_identifier |
|
240 | assert_equal 'p2008041', Project.next_identifier | |
240 | end |
|
241 | end | |
241 |
|
242 | |||
242 | def test_next_identifier_first_project |
|
243 | def test_next_identifier_first_project | |
243 | Project.delete_all |
|
244 | Project.delete_all | |
244 | assert_nil Project.next_identifier |
|
245 | assert_nil Project.next_identifier | |
245 | end |
|
246 | end | |
246 |
|
247 | |||
247 |
|
248 | |||
248 | def test_enabled_module_names_should_not_recreate_enabled_modules |
|
249 | def test_enabled_module_names_should_not_recreate_enabled_modules | |
249 | project = Project.find(1) |
|
250 | project = Project.find(1) | |
250 | # Remove one module |
|
251 | # Remove one module | |
251 | modules = project.enabled_modules.slice(0..-2) |
|
252 | modules = project.enabled_modules.slice(0..-2) | |
252 | assert modules.any? |
|
253 | assert modules.any? | |
253 | assert_difference 'EnabledModule.count', -1 do |
|
254 | assert_difference 'EnabledModule.count', -1 do | |
254 | project.enabled_module_names = modules.collect(&:name) |
|
255 | project.enabled_module_names = modules.collect(&:name) | |
255 | end |
|
256 | end | |
256 | project.reload |
|
257 | project.reload | |
257 | # Ids should be preserved |
|
258 | # Ids should be preserved | |
258 | assert_equal project.enabled_module_ids.sort, modules.collect(&:id).sort |
|
259 | assert_equal project.enabled_module_ids.sort, modules.collect(&:id).sort | |
259 | end |
|
260 | end | |
260 |
|
261 | |||
261 | def test_copy_from_existing_project |
|
262 | def test_copy_from_existing_project | |
262 | source_project = Project.find(1) |
|
263 | source_project = Project.find(1) | |
263 | copied_project = Project.copy_from(1) |
|
264 | copied_project = Project.copy_from(1) | |
264 |
|
265 | |||
265 | assert copied_project |
|
266 | assert copied_project | |
266 | # Cleared attributes |
|
267 | # Cleared attributes | |
267 | assert copied_project.id.blank? |
|
268 | assert copied_project.id.blank? | |
268 | assert copied_project.name.blank? |
|
269 | assert copied_project.name.blank? | |
269 | assert copied_project.identifier.blank? |
|
270 | assert copied_project.identifier.blank? | |
270 |
|
271 | |||
271 | # Duplicated attributes |
|
272 | # Duplicated attributes | |
272 | assert_equal source_project.description, copied_project.description |
|
273 | assert_equal source_project.description, copied_project.description | |
273 | assert_equal source_project.enabled_modules, copied_project.enabled_modules |
|
274 | assert_equal source_project.enabled_modules, copied_project.enabled_modules | |
274 | assert_equal source_project.trackers, copied_project.trackers |
|
275 | assert_equal source_project.trackers, copied_project.trackers | |
275 |
|
276 | |||
276 | # Default attributes |
|
277 | # Default attributes | |
277 | assert_equal 1, copied_project.status |
|
278 | assert_equal 1, copied_project.status | |
278 | end |
|
279 | end | |
279 |
|
280 | |||
280 | # Context: Project#copy |
|
281 | # Context: Project#copy | |
281 | def test_copy_should_copy_issues |
|
282 | def test_copy_should_copy_issues | |
282 | # Setup |
|
283 | # Setup | |
283 | ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests |
|
284 | ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests | |
284 | source_project = Project.find(2) |
|
285 | source_project = Project.find(2) | |
285 | Project.destroy_all :identifier => "copy-test" |
|
286 | Project.destroy_all :identifier => "copy-test" | |
286 | project = Project.new(:name => 'Copy Test', :identifier => 'copy-test') |
|
287 | project = Project.new(:name => 'Copy Test', :identifier => 'copy-test') | |
287 | project.trackers = source_project.trackers |
|
288 | project.trackers = source_project.trackers | |
288 | assert project.valid? |
|
289 | assert project.valid? | |
289 |
|
290 | |||
290 | assert project.issues.empty? |
|
291 | assert project.issues.empty? | |
291 | assert project.copy(source_project) |
|
292 | assert project.copy(source_project) | |
292 |
|
293 | |||
293 | # Tests |
|
294 | # Tests | |
294 | assert_equal source_project.issues.size, project.issues.size |
|
295 | assert_equal source_project.issues.size, project.issues.size | |
295 | project.issues.each do |issue| |
|
296 | project.issues.each do |issue| | |
296 | assert issue.valid? |
|
297 | assert issue.valid? | |
297 | assert ! issue.assigned_to.blank? |
|
298 | assert ! issue.assigned_to.blank? | |
298 | assert_equal project, issue.project |
|
299 | assert_equal project, issue.project | |
299 | end |
|
300 | end | |
300 | end |
|
301 | end | |
301 |
|
302 | |||
302 | def test_copy_should_copy_members |
|
303 | def test_copy_should_copy_members | |
303 | # Setup |
|
304 | # Setup | |
304 | ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests |
|
305 | ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests | |
305 | source_project = Project.find(2) |
|
306 | source_project = Project.find(2) | |
306 | project = Project.new(:name => 'Copy Test', :identifier => 'copy-test') |
|
307 | project = Project.new(:name => 'Copy Test', :identifier => 'copy-test') | |
307 | project.trackers = source_project.trackers |
|
308 | project.trackers = source_project.trackers | |
308 | project.enabled_modules = source_project.enabled_modules |
|
309 | project.enabled_modules = source_project.enabled_modules | |
309 | assert project.valid? |
|
310 | assert project.valid? | |
310 |
|
311 | |||
311 | assert project.members.empty? |
|
312 | assert project.members.empty? | |
312 | assert project.copy(source_project) |
|
313 | assert project.copy(source_project) | |
313 |
|
314 | |||
314 | # Tests |
|
315 | # Tests | |
315 | assert_equal source_project.members.size, project.members.size |
|
316 | assert_equal source_project.members.size, project.members.size | |
316 | project.members.each do |member| |
|
317 | project.members.each do |member| | |
317 | assert member |
|
318 | assert member | |
318 | assert_equal project, member.project |
|
319 | assert_equal project, member.project | |
319 | end |
|
320 | end | |
320 | end |
|
321 | end | |
321 |
|
322 | |||
322 | def test_copy_should_copy_project_level_queries |
|
323 | def test_copy_should_copy_project_level_queries | |
323 | # Setup |
|
324 | # Setup | |
324 | ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests |
|
325 | ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests | |
325 | source_project = Project.find(2) |
|
326 | source_project = Project.find(2) | |
326 | project = Project.new(:name => 'Copy Test', :identifier => 'copy-test') |
|
327 | project = Project.new(:name => 'Copy Test', :identifier => 'copy-test') | |
327 | project.trackers = source_project.trackers |
|
328 | project.trackers = source_project.trackers | |
328 | project.enabled_modules = source_project.enabled_modules |
|
329 | project.enabled_modules = source_project.enabled_modules | |
329 | assert project.valid? |
|
330 | assert project.valid? | |
330 |
|
331 | |||
331 | assert project.queries.empty? |
|
332 | assert project.queries.empty? | |
332 | assert project.copy(source_project) |
|
333 | assert project.copy(source_project) | |
333 |
|
334 | |||
334 | # Tests |
|
335 | # Tests | |
335 | assert_equal source_project.queries.size, project.queries.size |
|
336 | assert_equal source_project.queries.size, project.queries.size | |
336 | project.queries.each do |query| |
|
337 | project.queries.each do |query| | |
337 | assert query |
|
338 | assert query | |
338 | assert_equal project, query.project |
|
339 | assert_equal project, query.project | |
339 | end |
|
340 | end | |
340 | end |
|
341 | end | |
341 |
|
342 | |||
342 | end |
|
343 | end |
General Comments 0
You need to be logged in to leave comments.
Login now