Mercurial > hg > soundsoftware-site
comparison .svn/pristine/c7/c7d848a2c73159ca5a8ab924fb977b51dbc69f9f.svn-base @ 1295:622f24f53b42 redmine-2.3
Update to Redmine SVN revision 11972 on 2.3-stable branch
author | Chris Cannam |
---|---|
date | Fri, 14 Jun 2013 09:02:21 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1294:3e4c3460b6ca | 1295:622f24f53b42 |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2006-2013 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 ProjectTest < ActiveSupport::TestCase | |
21 fixtures :projects, :trackers, :issue_statuses, :issues, | |
22 :journals, :journal_details, | |
23 :enumerations, :users, :issue_categories, | |
24 :projects_trackers, | |
25 :custom_fields, | |
26 :custom_fields_projects, | |
27 :custom_fields_trackers, | |
28 :custom_values, | |
29 :roles, | |
30 :member_roles, | |
31 :members, | |
32 :enabled_modules, | |
33 :versions, | |
34 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions, | |
35 :groups_users, | |
36 :boards, :messages, | |
37 :repositories, | |
38 :news, :comments, | |
39 :documents | |
40 | |
41 def setup | |
42 @ecookbook = Project.find(1) | |
43 @ecookbook_sub1 = Project.find(3) | |
44 set_tmp_attachments_directory | |
45 User.current = nil | |
46 end | |
47 | |
48 def test_truth | |
49 assert_kind_of Project, @ecookbook | |
50 assert_equal "eCookbook", @ecookbook.name | |
51 end | |
52 | |
53 def test_default_attributes | |
54 with_settings :default_projects_public => '1' do | |
55 assert_equal true, Project.new.is_public | |
56 assert_equal false, Project.new(:is_public => false).is_public | |
57 end | |
58 | |
59 with_settings :default_projects_public => '0' do | |
60 assert_equal false, Project.new.is_public | |
61 assert_equal true, Project.new(:is_public => true).is_public | |
62 end | |
63 | |
64 with_settings :sequential_project_identifiers => '1' do | |
65 assert !Project.new.identifier.blank? | |
66 assert Project.new(:identifier => '').identifier.blank? | |
67 end | |
68 | |
69 with_settings :sequential_project_identifiers => '0' do | |
70 assert Project.new.identifier.blank? | |
71 assert !Project.new(:identifier => 'test').blank? | |
72 end | |
73 | |
74 with_settings :default_projects_modules => ['issue_tracking', 'repository'] do | |
75 assert_equal ['issue_tracking', 'repository'], Project.new.enabled_module_names | |
76 end | |
77 end | |
78 | |
79 def test_default_trackers_should_match_default_tracker_ids_setting | |
80 with_settings :default_projects_tracker_ids => ['1', '3'] do | |
81 assert_equal Tracker.find(1, 3).sort, Project.new.trackers.sort | |
82 end | |
83 end | |
84 | |
85 def test_default_trackers_should_be_all_trackers_with_blank_setting | |
86 with_settings :default_projects_tracker_ids => nil do | |
87 assert_equal Tracker.all.sort, Project.new.trackers.sort | |
88 end | |
89 end | |
90 | |
91 def test_default_trackers_should_be_empty_with_empty_setting | |
92 with_settings :default_projects_tracker_ids => [] do | |
93 assert_equal [], Project.new.trackers | |
94 end | |
95 end | |
96 | |
97 def test_default_trackers_should_not_replace_initialized_trackers | |
98 with_settings :default_projects_tracker_ids => ['1', '3'] do | |
99 assert_equal Tracker.find(1, 2).sort, Project.new(:tracker_ids => [1, 2]).trackers.sort | |
100 end | |
101 end | |
102 | |
103 def test_update | |
104 assert_equal "eCookbook", @ecookbook.name | |
105 @ecookbook.name = "eCook" | |
106 assert @ecookbook.save, @ecookbook.errors.full_messages.join("; ") | |
107 @ecookbook.reload | |
108 assert_equal "eCook", @ecookbook.name | |
109 end | |
110 | |
111 def test_validate_identifier | |
112 to_test = {"abc" => true, | |
113 "ab12" => true, | |
114 "ab-12" => true, | |
115 "ab_12" => true, | |
116 "12" => false, | |
117 "new" => false} | |
118 | |
119 to_test.each do |identifier, valid| | |
120 p = Project.new | |
121 p.identifier = identifier | |
122 p.valid? | |
123 if valid | |
124 assert p.errors['identifier'].blank?, "identifier #{identifier} was not valid" | |
125 else | |
126 assert p.errors['identifier'].present?, "identifier #{identifier} was valid" | |
127 end | |
128 end | |
129 end | |
130 | |
131 def test_identifier_should_not_be_frozen_for_a_new_project | |
132 assert_equal false, Project.new.identifier_frozen? | |
133 end | |
134 | |
135 def test_identifier_should_not_be_frozen_for_a_saved_project_with_blank_identifier | |
136 Project.update_all(["identifier = ''"], "id = 1") | |
137 | |
138 assert_equal false, Project.find(1).identifier_frozen? | |
139 end | |
140 | |
141 def test_identifier_should_be_frozen_for_a_saved_project_with_valid_identifier | |
142 assert_equal true, Project.find(1).identifier_frozen? | |
143 end | |
144 | |
145 def test_members_should_be_active_users | |
146 Project.all.each do |project| | |
147 assert_nil project.members.detect {|m| !(m.user.is_a?(User) && m.user.active?) } | |
148 end | |
149 end | |
150 | |
151 def test_users_should_be_active_users | |
152 Project.all.each do |project| | |
153 assert_nil project.users.detect {|u| !(u.is_a?(User) && u.active?) } | |
154 end | |
155 end | |
156 | |
157 def test_open_scope_on_issues_association | |
158 assert_kind_of Issue, Project.find(1).issues.open.first | |
159 end | |
160 | |
161 def test_archive | |
162 user = @ecookbook.members.first.user | |
163 @ecookbook.archive | |
164 @ecookbook.reload | |
165 | |
166 assert !@ecookbook.active? | |
167 assert @ecookbook.archived? | |
168 assert !user.projects.include?(@ecookbook) | |
169 # Subproject are also archived | |
170 assert !@ecookbook.children.empty? | |
171 assert @ecookbook.descendants.active.empty? | |
172 end | |
173 | |
174 def test_archive_should_fail_if_versions_are_used_by_non_descendant_projects | |
175 # Assign an issue of a project to a version of a child project | |
176 Issue.find(4).update_attribute :fixed_version_id, 4 | |
177 | |
178 assert_no_difference "Project.count(:all, :conditions => 'status = #{Project::STATUS_ARCHIVED}')" do | |
179 assert_equal false, @ecookbook.archive | |
180 end | |
181 @ecookbook.reload | |
182 assert @ecookbook.active? | |
183 end | |
184 | |
185 def test_unarchive | |
186 user = @ecookbook.members.first.user | |
187 @ecookbook.archive | |
188 # A subproject of an archived project can not be unarchived | |
189 assert !@ecookbook_sub1.unarchive | |
190 | |
191 # Unarchive project | |
192 assert @ecookbook.unarchive | |
193 @ecookbook.reload | |
194 assert @ecookbook.active? | |
195 assert !@ecookbook.archived? | |
196 assert user.projects.include?(@ecookbook) | |
197 # Subproject can now be unarchived | |
198 @ecookbook_sub1.reload | |
199 assert @ecookbook_sub1.unarchive | |
200 end | |
201 | |
202 def test_destroy | |
203 # 2 active members | |
204 assert_equal 2, @ecookbook.members.size | |
205 # and 1 is locked | |
206 assert_equal 3, Member.where('project_id = ?', @ecookbook.id).all.size | |
207 # some boards | |
208 assert @ecookbook.boards.any? | |
209 | |
210 @ecookbook.destroy | |
211 # make sure that the project non longer exists | |
212 assert_raise(ActiveRecord::RecordNotFound) { Project.find(@ecookbook.id) } | |
213 # make sure related data was removed | |
214 assert_nil Member.first(:conditions => {:project_id => @ecookbook.id}) | |
215 assert_nil Board.first(:conditions => {:project_id => @ecookbook.id}) | |
216 assert_nil Issue.first(:conditions => {:project_id => @ecookbook.id}) | |
217 end | |
218 | |
219 def test_destroy_should_destroy_subtasks | |
220 issues = (0..2).to_a.map {Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 1, :subject => 'test')} | |
221 issues[0].update_attribute :parent_issue_id, issues[1].id | |
222 issues[2].update_attribute :parent_issue_id, issues[1].id | |
223 assert_equal 2, issues[1].children.count | |
224 | |
225 assert_nothing_raised do | |
226 Project.find(1).destroy | |
227 end | |
228 assert Issue.find_all_by_id(issues.map(&:id)).empty? | |
229 end | |
230 | |
231 def test_destroying_root_projects_should_clear_data | |
232 Project.roots.each do |root| | |
233 root.destroy | |
234 end | |
235 | |
236 assert_equal 0, Project.count, "Projects were not deleted: #{Project.all.inspect}" | |
237 assert_equal 0, Member.count, "Members were not deleted: #{Member.all.inspect}" | |
238 assert_equal 0, MemberRole.count | |
239 assert_equal 0, Issue.count | |
240 assert_equal 0, Journal.count | |
241 assert_equal 0, JournalDetail.count | |
242 assert_equal 0, Attachment.count, "Attachments were not deleted: #{Attachment.all.inspect}" | |
243 assert_equal 0, EnabledModule.count | |
244 assert_equal 0, IssueCategory.count | |
245 assert_equal 0, IssueRelation.count | |
246 assert_equal 0, Board.count | |
247 assert_equal 0, Message.count | |
248 assert_equal 0, News.count | |
249 assert_equal 0, Query.count(:conditions => "project_id IS NOT NULL") | |
250 assert_equal 0, Repository.count | |
251 assert_equal 0, Changeset.count | |
252 assert_equal 0, Change.count | |
253 assert_equal 0, Comment.count | |
254 assert_equal 0, TimeEntry.count | |
255 assert_equal 0, Version.count | |
256 assert_equal 0, Watcher.count | |
257 assert_equal 0, Wiki.count | |
258 assert_equal 0, WikiPage.count | |
259 assert_equal 0, WikiContent.count | |
260 assert_equal 0, WikiContent::Version.count | |
261 assert_equal 0, Project.connection.select_all("SELECT * FROM projects_trackers").size | |
262 assert_equal 0, Project.connection.select_all("SELECT * FROM custom_fields_projects").size | |
263 assert_equal 0, CustomValue.count(:conditions => {:customized_type => ['Project', 'Issue', 'TimeEntry', 'Version']}) | |
264 end | |
265 | |
266 def test_move_an_orphan_project_to_a_root_project | |
267 sub = Project.find(2) | |
268 sub.set_parent! @ecookbook | |
269 assert_equal @ecookbook.id, sub.parent.id | |
270 @ecookbook.reload | |
271 assert_equal 4, @ecookbook.children.size | |
272 end | |
273 | |
274 def test_move_an_orphan_project_to_a_subproject | |
275 sub = Project.find(2) | |
276 assert sub.set_parent!(@ecookbook_sub1) | |
277 end | |
278 | |
279 def test_move_a_root_project_to_a_project | |
280 sub = @ecookbook | |
281 assert sub.set_parent!(Project.find(2)) | |
282 end | |
283 | |
284 def test_should_not_move_a_project_to_its_children | |
285 sub = @ecookbook | |
286 assert !(sub.set_parent!(Project.find(3))) | |
287 end | |
288 | |
289 def test_set_parent_should_add_roots_in_alphabetical_order | |
290 ProjectCustomField.delete_all | |
291 Project.delete_all | |
292 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(nil) | |
293 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(nil) | |
294 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(nil) | |
295 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(nil) | |
296 | |
297 assert_equal 4, Project.count | |
298 assert_equal Project.all.sort_by(&:name), Project.all.sort_by(&:lft) | |
299 end | |
300 | |
301 def test_set_parent_should_add_children_in_alphabetical_order | |
302 ProjectCustomField.delete_all | |
303 parent = Project.create!(:name => 'Parent', :identifier => 'parent') | |
304 Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(parent) | |
305 Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(parent) | |
306 Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(parent) | |
307 Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(parent) | |
308 | |
309 parent.reload | |
310 assert_equal 4, parent.children.size | |
311 assert_equal parent.children.all.sort_by(&:name), parent.children.all | |
312 end | |
313 | |
314 def test_set_parent_should_update_issue_fixed_version_associations_when_a_fixed_version_is_moved_out_of_the_hierarchy | |
315 # Parent issue with a hierarchy project's fixed version | |
316 parent_issue = Issue.find(1) | |
317 parent_issue.update_attribute(:fixed_version_id, 4) | |
318 parent_issue.reload | |
319 assert_equal 4, parent_issue.fixed_version_id | |
320 | |
321 # Should keep fixed versions for the issues | |
322 issue_with_local_fixed_version = Issue.find(5) | |
323 issue_with_local_fixed_version.update_attribute(:fixed_version_id, 4) | |
324 issue_with_local_fixed_version.reload | |
325 assert_equal 4, issue_with_local_fixed_version.fixed_version_id | |
326 | |
327 # Local issue with hierarchy fixed_version | |
328 issue_with_hierarchy_fixed_version = Issue.find(13) | |
329 issue_with_hierarchy_fixed_version.update_attribute(:fixed_version_id, 6) | |
330 issue_with_hierarchy_fixed_version.reload | |
331 assert_equal 6, issue_with_hierarchy_fixed_version.fixed_version_id | |
332 | |
333 # Move project out of the issue's hierarchy | |
334 moved_project = Project.find(3) | |
335 moved_project.set_parent!(Project.find(2)) | |
336 parent_issue.reload | |
337 issue_with_local_fixed_version.reload | |
338 issue_with_hierarchy_fixed_version.reload | |
339 | |
340 assert_equal 4, issue_with_local_fixed_version.fixed_version_id, "Fixed version was not keep on an issue local to the moved project" | |
341 assert_equal nil, issue_with_hierarchy_fixed_version.fixed_version_id, "Fixed version is still set after moving the Project out of the hierarchy where the version is defined in" | |
342 assert_equal nil, parent_issue.fixed_version_id, "Fixed version is still set after moving the Version out of the hierarchy for the issue." | |
343 end | |
344 | |
345 def test_parent | |
346 p = Project.find(6).parent | |
347 assert p.is_a?(Project) | |
348 assert_equal 5, p.id | |
349 end | |
350 | |
351 def test_ancestors | |
352 a = Project.find(6).ancestors | |
353 assert a.first.is_a?(Project) | |
354 assert_equal [1, 5], a.collect(&:id) | |
355 end | |
356 | |
357 def test_root | |
358 r = Project.find(6).root | |
359 assert r.is_a?(Project) | |
360 assert_equal 1, r.id | |
361 end | |
362 | |
363 def test_children | |
364 c = Project.find(1).children | |
365 assert c.first.is_a?(Project) | |
366 assert_equal [5, 3, 4], c.collect(&:id) | |
367 end | |
368 | |
369 def test_descendants | |
370 d = Project.find(1).descendants | |
371 assert d.first.is_a?(Project) | |
372 assert_equal [5, 6, 3, 4], d.collect(&:id) | |
373 end | |
374 | |
375 def test_allowed_parents_should_be_empty_for_non_member_user | |
376 Role.non_member.add_permission!(:add_project) | |
377 user = User.find(9) | |
378 assert user.memberships.empty? | |
379 User.current = user | |
380 assert Project.new.allowed_parents.compact.empty? | |
381 end | |
382 | |
383 def test_allowed_parents_with_add_subprojects_permission | |
384 Role.find(1).remove_permission!(:add_project) | |
385 Role.find(1).add_permission!(:add_subprojects) | |
386 User.current = User.find(2) | |
387 # new project | |
388 assert !Project.new.allowed_parents.include?(nil) | |
389 assert Project.new.allowed_parents.include?(Project.find(1)) | |
390 # existing root project | |
391 assert Project.find(1).allowed_parents.include?(nil) | |
392 # existing child | |
393 assert Project.find(3).allowed_parents.include?(Project.find(1)) | |
394 assert !Project.find(3).allowed_parents.include?(nil) | |
395 end | |
396 | |
397 def test_allowed_parents_with_add_project_permission | |
398 Role.find(1).add_permission!(:add_project) | |
399 Role.find(1).remove_permission!(:add_subprojects) | |
400 User.current = User.find(2) | |
401 # new project | |
402 assert Project.new.allowed_parents.include?(nil) | |
403 assert !Project.new.allowed_parents.include?(Project.find(1)) | |
404 # existing root project | |
405 assert Project.find(1).allowed_parents.include?(nil) | |
406 # existing child | |
407 assert Project.find(3).allowed_parents.include?(Project.find(1)) | |
408 assert Project.find(3).allowed_parents.include?(nil) | |
409 end | |
410 | |
411 def test_allowed_parents_with_add_project_and_subprojects_permission | |
412 Role.find(1).add_permission!(:add_project) | |
413 Role.find(1).add_permission!(:add_subprojects) | |
414 User.current = User.find(2) | |
415 # new project | |
416 assert Project.new.allowed_parents.include?(nil) | |
417 assert Project.new.allowed_parents.include?(Project.find(1)) | |
418 # existing root project | |
419 assert Project.find(1).allowed_parents.include?(nil) | |
420 # existing child | |
421 assert Project.find(3).allowed_parents.include?(Project.find(1)) | |
422 assert Project.find(3).allowed_parents.include?(nil) | |
423 end | |
424 | |
425 def test_users_by_role | |
426 users_by_role = Project.find(1).users_by_role | |
427 assert_kind_of Hash, users_by_role | |
428 role = Role.find(1) | |
429 assert_kind_of Array, users_by_role[role] | |
430 assert users_by_role[role].include?(User.find(2)) | |
431 end | |
432 | |
433 def test_rolled_up_trackers | |
434 parent = Project.find(1) | |
435 parent.trackers = Tracker.find([1,2]) | |
436 child = parent.children.find(3) | |
437 | |
438 assert_equal [1, 2], parent.tracker_ids | |
439 assert_equal [2, 3], child.trackers.collect(&:id) | |
440 | |
441 assert_kind_of Tracker, parent.rolled_up_trackers.first | |
442 assert_equal Tracker.find(1), parent.rolled_up_trackers.first | |
443 | |
444 assert_equal [1, 2, 3], parent.rolled_up_trackers.collect(&:id) | |
445 assert_equal [2, 3], child.rolled_up_trackers.collect(&:id) | |
446 end | |
447 | |
448 def test_rolled_up_trackers_should_ignore_archived_subprojects | |
449 parent = Project.find(1) | |
450 parent.trackers = Tracker.find([1,2]) | |
451 child = parent.children.find(3) | |
452 child.trackers = Tracker.find([1,3]) | |
453 parent.children.each(&:archive) | |
454 | |
455 assert_equal [1,2], parent.rolled_up_trackers.collect(&:id) | |
456 end | |
457 | |
458 test "#rolled_up_trackers should ignore projects with issue_tracking module disabled" do | |
459 parent = Project.generate! | |
460 parent.trackers = Tracker.find([1, 2]) | |
461 child = Project.generate_with_parent!(parent) | |
462 child.trackers = Tracker.find([2, 3]) | |
463 | |
464 assert_equal [1, 2, 3], parent.rolled_up_trackers.collect(&:id).sort | |
465 | |
466 assert child.disable_module!(:issue_tracking) | |
467 parent.reload | |
468 assert_equal [1, 2], parent.rolled_up_trackers.collect(&:id).sort | |
469 end | |
470 | |
471 test "#rolled_up_versions should include the versions for the current project" do | |
472 project = Project.generate! | |
473 parent_version_1 = Version.generate!(:project => project) | |
474 parent_version_2 = Version.generate!(:project => project) | |
475 assert_same_elements [parent_version_1, parent_version_2], project.rolled_up_versions | |
476 end | |
477 | |
478 test "#rolled_up_versions should include versions for a subproject" do | |
479 project = Project.generate! | |
480 parent_version_1 = Version.generate!(:project => project) | |
481 parent_version_2 = Version.generate!(:project => project) | |
482 subproject = Project.generate_with_parent!(project) | |
483 subproject_version = Version.generate!(:project => subproject) | |
484 | |
485 assert_same_elements [ | |
486 parent_version_1, | |
487 parent_version_2, | |
488 subproject_version | |
489 ], project.rolled_up_versions | |
490 end | |
491 | |
492 test "#rolled_up_versions should include versions for a sub-subproject" do | |
493 project = Project.generate! | |
494 parent_version_1 = Version.generate!(:project => project) | |
495 parent_version_2 = Version.generate!(:project => project) | |
496 subproject = Project.generate_with_parent!(project) | |
497 sub_subproject = Project.generate_with_parent!(subproject) | |
498 sub_subproject_version = Version.generate!(:project => sub_subproject) | |
499 project.reload | |
500 | |
501 assert_same_elements [ | |
502 parent_version_1, | |
503 parent_version_2, | |
504 sub_subproject_version | |
505 ], project.rolled_up_versions | |
506 end | |
507 | |
508 test "#rolled_up_versions should only check active projects" do | |
509 project = Project.generate! | |
510 parent_version_1 = Version.generate!(:project => project) | |
511 parent_version_2 = Version.generate!(:project => project) | |
512 subproject = Project.generate_with_parent!(project) | |
513 subproject_version = Version.generate!(:project => subproject) | |
514 assert subproject.archive | |
515 project.reload | |
516 | |
517 assert !subproject.active? | |
518 assert_same_elements [parent_version_1, parent_version_2], project.rolled_up_versions | |
519 end | |
520 | |
521 def test_shared_versions_none_sharing | |
522 p = Project.find(5) | |
523 v = Version.create!(:name => 'none_sharing', :project => p, :sharing => 'none') | |
524 assert p.shared_versions.include?(v) | |
525 assert !p.children.first.shared_versions.include?(v) | |
526 assert !p.root.shared_versions.include?(v) | |
527 assert !p.siblings.first.shared_versions.include?(v) | |
528 assert !p.root.siblings.first.shared_versions.include?(v) | |
529 end | |
530 | |
531 def test_shared_versions_descendants_sharing | |
532 p = Project.find(5) | |
533 v = Version.create!(:name => 'descendants_sharing', :project => p, :sharing => 'descendants') | |
534 assert p.shared_versions.include?(v) | |
535 assert p.children.first.shared_versions.include?(v) | |
536 assert !p.root.shared_versions.include?(v) | |
537 assert !p.siblings.first.shared_versions.include?(v) | |
538 assert !p.root.siblings.first.shared_versions.include?(v) | |
539 end | |
540 | |
541 def test_shared_versions_hierarchy_sharing | |
542 p = Project.find(5) | |
543 v = Version.create!(:name => 'hierarchy_sharing', :project => p, :sharing => 'hierarchy') | |
544 assert p.shared_versions.include?(v) | |
545 assert p.children.first.shared_versions.include?(v) | |
546 assert p.root.shared_versions.include?(v) | |
547 assert !p.siblings.first.shared_versions.include?(v) | |
548 assert !p.root.siblings.first.shared_versions.include?(v) | |
549 end | |
550 | |
551 def test_shared_versions_tree_sharing | |
552 p = Project.find(5) | |
553 v = Version.create!(:name => 'tree_sharing', :project => p, :sharing => 'tree') | |
554 assert p.shared_versions.include?(v) | |
555 assert p.children.first.shared_versions.include?(v) | |
556 assert p.root.shared_versions.include?(v) | |
557 assert p.siblings.first.shared_versions.include?(v) | |
558 assert !p.root.siblings.first.shared_versions.include?(v) | |
559 end | |
560 | |
561 def test_shared_versions_system_sharing | |
562 p = Project.find(5) | |
563 v = Version.create!(:name => 'system_sharing', :project => p, :sharing => 'system') | |
564 assert p.shared_versions.include?(v) | |
565 assert p.children.first.shared_versions.include?(v) | |
566 assert p.root.shared_versions.include?(v) | |
567 assert p.siblings.first.shared_versions.include?(v) | |
568 assert p.root.siblings.first.shared_versions.include?(v) | |
569 end | |
570 | |
571 def test_shared_versions | |
572 parent = Project.find(1) | |
573 child = parent.children.find(3) | |
574 private_child = parent.children.find(5) | |
575 | |
576 assert_equal [1,2,3], parent.version_ids.sort | |
577 assert_equal [4], child.version_ids | |
578 assert_equal [6], private_child.version_ids | |
579 assert_equal [7], Version.find_all_by_sharing('system').collect(&:id) | |
580 | |
581 assert_equal 6, parent.shared_versions.size | |
582 parent.shared_versions.each do |version| | |
583 assert_kind_of Version, version | |
584 end | |
585 | |
586 assert_equal [1,2,3,4,6,7], parent.shared_versions.collect(&:id).sort | |
587 end | |
588 | |
589 def test_shared_versions_should_ignore_archived_subprojects | |
590 parent = Project.find(1) | |
591 child = parent.children.find(3) | |
592 child.archive | |
593 parent.reload | |
594 | |
595 assert_equal [1,2,3], parent.version_ids.sort | |
596 assert_equal [4], child.version_ids | |
597 assert !parent.shared_versions.collect(&:id).include?(4) | |
598 end | |
599 | |
600 def test_shared_versions_visible_to_user | |
601 user = User.find(3) | |
602 parent = Project.find(1) | |
603 child = parent.children.find(5) | |
604 | |
605 assert_equal [1,2,3], parent.version_ids.sort | |
606 assert_equal [6], child.version_ids | |
607 | |
608 versions = parent.shared_versions.visible(user) | |
609 | |
610 assert_equal 4, versions.size | |
611 versions.each do |version| | |
612 assert_kind_of Version, version | |
613 end | |
614 | |
615 assert !versions.collect(&:id).include?(6) | |
616 end | |
617 | |
618 def test_shared_versions_for_new_project_should_include_system_shared_versions | |
619 p = Project.find(5) | |
620 v = Version.create!(:name => 'system_sharing', :project => p, :sharing => 'system') | |
621 | |
622 assert_include v, Project.new.shared_versions | |
623 end | |
624 | |
625 def test_next_identifier | |
626 ProjectCustomField.delete_all | |
627 Project.create!(:name => 'last', :identifier => 'p2008040') | |
628 assert_equal 'p2008041', Project.next_identifier | |
629 end | |
630 | |
631 def test_next_identifier_first_project | |
632 Project.delete_all | |
633 assert_nil Project.next_identifier | |
634 end | |
635 | |
636 def test_enabled_module_names | |
637 with_settings :default_projects_modules => ['issue_tracking', 'repository'] do | |
638 project = Project.new | |
639 | |
640 project.enabled_module_names = %w(issue_tracking news) | |
641 assert_equal %w(issue_tracking news), project.enabled_module_names.sort | |
642 end | |
643 end | |
644 | |
645 test "enabled_modules should define module by names and preserve ids" do | |
646 @project = Project.find(1) | |
647 # Remove one module | |
648 modules = @project.enabled_modules.slice(0..-2) | |
649 assert modules.any? | |
650 assert_difference 'EnabledModule.count', -1 do | |
651 @project.enabled_module_names = modules.collect(&:name) | |
652 end | |
653 @project.reload | |
654 # Ids should be preserved | |
655 assert_equal @project.enabled_module_ids.sort, modules.collect(&:id).sort | |
656 end | |
657 | |
658 test "enabled_modules should enable a module" do | |
659 @project = Project.find(1) | |
660 @project.enabled_module_names = [] | |
661 @project.reload | |
662 assert_equal [], @project.enabled_module_names | |
663 #with string | |
664 @project.enable_module!("issue_tracking") | |
665 assert_equal ["issue_tracking"], @project.enabled_module_names | |
666 #with symbol | |
667 @project.enable_module!(:gantt) | |
668 assert_equal ["issue_tracking", "gantt"], @project.enabled_module_names | |
669 #don't add a module twice | |
670 @project.enable_module!("issue_tracking") | |
671 assert_equal ["issue_tracking", "gantt"], @project.enabled_module_names | |
672 end | |
673 | |
674 test "enabled_modules should disable a module" do | |
675 @project = Project.find(1) | |
676 #with string | |
677 assert @project.enabled_module_names.include?("issue_tracking") | |
678 @project.disable_module!("issue_tracking") | |
679 assert ! @project.reload.enabled_module_names.include?("issue_tracking") | |
680 #with symbol | |
681 assert @project.enabled_module_names.include?("gantt") | |
682 @project.disable_module!(:gantt) | |
683 assert ! @project.reload.enabled_module_names.include?("gantt") | |
684 #with EnabledModule object | |
685 first_module = @project.enabled_modules.first | |
686 @project.disable_module!(first_module) | |
687 assert ! @project.reload.enabled_module_names.include?(first_module.name) | |
688 end | |
689 | |
690 def test_enabled_module_names_should_not_recreate_enabled_modules | |
691 project = Project.find(1) | |
692 # Remove one module | |
693 modules = project.enabled_modules.slice(0..-2) | |
694 assert modules.any? | |
695 assert_difference 'EnabledModule.count', -1 do | |
696 project.enabled_module_names = modules.collect(&:name) | |
697 end | |
698 project.reload | |
699 # Ids should be preserved | |
700 assert_equal project.enabled_module_ids.sort, modules.collect(&:id).sort | |
701 end | |
702 | |
703 def test_copy_from_existing_project | |
704 source_project = Project.find(1) | |
705 copied_project = Project.copy_from(1) | |
706 | |
707 assert copied_project | |
708 # Cleared attributes | |
709 assert copied_project.id.blank? | |
710 assert copied_project.name.blank? | |
711 assert copied_project.identifier.blank? | |
712 | |
713 # Duplicated attributes | |
714 assert_equal source_project.description, copied_project.description | |
715 assert_equal source_project.enabled_modules, copied_project.enabled_modules | |
716 assert_equal source_project.trackers, copied_project.trackers | |
717 | |
718 # Default attributes | |
719 assert_equal 1, copied_project.status | |
720 end | |
721 | |
722 def test_activities_should_use_the_system_activities | |
723 project = Project.find(1) | |
724 assert_equal project.activities, TimeEntryActivity.where(:active => true).all | |
725 end | |
726 | |
727 | |
728 def test_activities_should_use_the_project_specific_activities | |
729 project = Project.find(1) | |
730 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project}) | |
731 assert overridden_activity.save! | |
732 | |
733 assert project.activities.include?(overridden_activity), "Project specific Activity not found" | |
734 end | |
735 | |
736 def test_activities_should_not_include_the_inactive_project_specific_activities | |
737 project = Project.find(1) | |
738 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => TimeEntryActivity.first, :active => false}) | |
739 assert overridden_activity.save! | |
740 | |
741 assert !project.activities.include?(overridden_activity), "Inactive Project specific Activity found" | |
742 end | |
743 | |
744 def test_activities_should_not_include_project_specific_activities_from_other_projects | |
745 project = Project.find(1) | |
746 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => Project.find(2)}) | |
747 assert overridden_activity.save! | |
748 | |
749 assert !project.activities.include?(overridden_activity), "Project specific Activity found on a different project" | |
750 end | |
751 | |
752 def test_activities_should_handle_nils | |
753 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => Project.find(1), :parent => TimeEntryActivity.first}) | |
754 TimeEntryActivity.delete_all | |
755 | |
756 # No activities | |
757 project = Project.find(1) | |
758 assert project.activities.empty? | |
759 | |
760 # No system, one overridden | |
761 assert overridden_activity.save! | |
762 project.reload | |
763 assert_equal [overridden_activity], project.activities | |
764 end | |
765 | |
766 def test_activities_should_override_system_activities_with_project_activities | |
767 project = Project.find(1) | |
768 parent_activity = TimeEntryActivity.first | |
769 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => parent_activity}) | |
770 assert overridden_activity.save! | |
771 | |
772 assert project.activities.include?(overridden_activity), "Project specific Activity not found" | |
773 assert !project.activities.include?(parent_activity), "System Activity found when it should have been overridden" | |
774 end | |
775 | |
776 def test_activities_should_include_inactive_activities_if_specified | |
777 project = Project.find(1) | |
778 overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => TimeEntryActivity.first, :active => false}) | |
779 assert overridden_activity.save! | |
780 | |
781 assert project.activities(true).include?(overridden_activity), "Inactive Project specific Activity not found" | |
782 end | |
783 | |
784 test 'activities should not include active System activities if the project has an override that is inactive' do | |
785 project = Project.find(1) | |
786 system_activity = TimeEntryActivity.find_by_name('Design') | |
787 assert system_activity.active? | |
788 overridden_activity = TimeEntryActivity.create!(:name => "Project", :project => project, :parent => system_activity, :active => false) | |
789 assert overridden_activity.save! | |
790 | |
791 assert !project.activities.include?(overridden_activity), "Inactive Project specific Activity not found" | |
792 assert !project.activities.include?(system_activity), "System activity found when the project has an inactive override" | |
793 end | |
794 | |
795 def test_close_completed_versions | |
796 Version.update_all("status = 'open'") | |
797 project = Project.find(1) | |
798 assert_not_nil project.versions.detect {|v| v.completed? && v.status == 'open'} | |
799 assert_not_nil project.versions.detect {|v| !v.completed? && v.status == 'open'} | |
800 project.close_completed_versions | |
801 project.reload | |
802 assert_nil project.versions.detect {|v| v.completed? && v.status != 'closed'} | |
803 assert_not_nil project.versions.detect {|v| !v.completed? && v.status == 'open'} | |
804 end | |
805 | |
806 test "#start_date should be nil if there are no issues on the project" do | |
807 project = Project.generate! | |
808 assert_nil project.start_date | |
809 end | |
810 | |
811 test "#start_date should be nil when issues have no start date" do | |
812 project = Project.generate! | |
813 project.trackers << Tracker.generate! | |
814 early = 7.days.ago.to_date | |
815 Issue.generate!(:project => project, :start_date => nil) | |
816 | |
817 assert_nil project.start_date | |
818 end | |
819 | |
820 test "#start_date should be the earliest start date of it's issues" do | |
821 project = Project.generate! | |
822 project.trackers << Tracker.generate! | |
823 early = 7.days.ago.to_date | |
824 Issue.generate!(:project => project, :start_date => Date.today) | |
825 Issue.generate!(:project => project, :start_date => early) | |
826 | |
827 assert_equal early, project.start_date | |
828 end | |
829 | |
830 test "#due_date should be nil if there are no issues on the project" do | |
831 project = Project.generate! | |
832 assert_nil project.due_date | |
833 end | |
834 | |
835 test "#due_date should be nil if there are no issues with due dates" do | |
836 project = Project.generate! | |
837 project.trackers << Tracker.generate! | |
838 Issue.generate!(:project => project, :due_date => nil) | |
839 | |
840 assert_nil project.due_date | |
841 end | |
842 | |
843 test "#due_date should be the latest due date of it's issues" do | |
844 project = Project.generate! | |
845 project.trackers << Tracker.generate! | |
846 future = 7.days.from_now.to_date | |
847 Issue.generate!(:project => project, :due_date => future) | |
848 Issue.generate!(:project => project, :due_date => Date.today) | |
849 | |
850 assert_equal future, project.due_date | |
851 end | |
852 | |
853 test "#due_date should be the latest due date of it's versions" do | |
854 project = Project.generate! | |
855 future = 7.days.from_now.to_date | |
856 project.versions << Version.generate!(:effective_date => future) | |
857 project.versions << Version.generate!(:effective_date => Date.today) | |
858 | |
859 assert_equal future, project.due_date | |
860 end | |
861 | |
862 test "#due_date should pick the latest date from it's issues and versions" do | |
863 project = Project.generate! | |
864 project.trackers << Tracker.generate! | |
865 future = 7.days.from_now.to_date | |
866 far_future = 14.days.from_now.to_date | |
867 Issue.generate!(:project => project, :due_date => far_future) | |
868 project.versions << Version.generate!(:effective_date => future) | |
869 | |
870 assert_equal far_future, project.due_date | |
871 end | |
872 | |
873 test "#completed_percent with no versions should be 100" do | |
874 project = Project.generate! | |
875 assert_equal 100, project.completed_percent | |
876 end | |
877 | |
878 test "#completed_percent with versions should return 0 if the versions have no issues" do | |
879 project = Project.generate! | |
880 Version.generate!(:project => project) | |
881 Version.generate!(:project => project) | |
882 | |
883 assert_equal 0, project.completed_percent | |
884 end | |
885 | |
886 test "#completed_percent with versions should return 100 if the version has only closed issues" do | |
887 project = Project.generate! | |
888 project.trackers << Tracker.generate! | |
889 v1 = Version.generate!(:project => project) | |
890 Issue.generate!(:project => project, :status => IssueStatus.find_by_name('Closed'), :fixed_version => v1) | |
891 v2 = Version.generate!(:project => project) | |
892 Issue.generate!(:project => project, :status => IssueStatus.find_by_name('Closed'), :fixed_version => v2) | |
893 | |
894 assert_equal 100, project.completed_percent | |
895 end | |
896 | |
897 test "#completed_percent with versions should return the averaged completed percent of the versions (not weighted)" do | |
898 project = Project.generate! | |
899 project.trackers << Tracker.generate! | |
900 v1 = Version.generate!(:project => project) | |
901 Issue.generate!(:project => project, :status => IssueStatus.find_by_name('New'), :estimated_hours => 10, :done_ratio => 50, :fixed_version => v1) | |
902 v2 = Version.generate!(:project => project) | |
903 Issue.generate!(:project => project, :status => IssueStatus.find_by_name('New'), :estimated_hours => 10, :done_ratio => 50, :fixed_version => v2) | |
904 | |
905 assert_equal 50, project.completed_percent | |
906 end | |
907 | |
908 test "#notified_users" do | |
909 project = Project.generate! | |
910 role = Role.generate! | |
911 | |
912 user_with_membership_notification = User.generate!(:mail_notification => 'selected') | |
913 Member.create!(:project => project, :roles => [role], :principal => user_with_membership_notification, :mail_notification => true) | |
914 | |
915 all_events_user = User.generate!(:mail_notification => 'all') | |
916 Member.create!(:project => project, :roles => [role], :principal => all_events_user) | |
917 | |
918 no_events_user = User.generate!(:mail_notification => 'none') | |
919 Member.create!(:project => project, :roles => [role], :principal => no_events_user) | |
920 | |
921 only_my_events_user = User.generate!(:mail_notification => 'only_my_events') | |
922 Member.create!(:project => project, :roles => [role], :principal => only_my_events_user) | |
923 | |
924 only_assigned_user = User.generate!(:mail_notification => 'only_assigned') | |
925 Member.create!(:project => project, :roles => [role], :principal => only_assigned_user) | |
926 | |
927 only_owned_user = User.generate!(:mail_notification => 'only_owner') | |
928 Member.create!(:project => project, :roles => [role], :principal => only_owned_user) | |
929 | |
930 assert project.notified_users.include?(user_with_membership_notification), "should include members with a mail notification" | |
931 assert project.notified_users.include?(all_events_user), "should include users with the 'all' notification option" | |
932 assert !project.notified_users.include?(no_events_user), "should not include users with the 'none' notification option" | |
933 assert !project.notified_users.include?(only_my_events_user), "should not include users with the 'only_my_events' notification option" | |
934 assert !project.notified_users.include?(only_assigned_user), "should not include users with the 'only_assigned' notification option" | |
935 assert !project.notified_users.include?(only_owned_user), "should not include users with the 'only_owner' notification option" | |
936 end | |
937 end |