To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / test / unit / project_test.rb @ 441:cbce1fd3b1b7

History | View | Annotate | Download (39.9 KB)

1 441:cbce1fd3b1b7 Chris
# Redmine - project management software
2
# Copyright (C) 2006-2011  Jean-Philippe Lang
3 0:513646585e45 Chris
#
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 119:8661b858af72 Chris
require File.expand_path('../../test_helper', __FILE__)
19 0:513646585e45 Chris
20
class ProjectTest < ActiveSupport::TestCase
21
  fixtures :all
22
23
  def setup
24
    @ecookbook = Project.find(1)
25
    @ecookbook_sub1 = Project.find(3)
26
    User.current = nil
27
  end
28
29
  should_validate_presence_of :name
30
  should_validate_presence_of :identifier
31
32
  should_validate_uniqueness_of :identifier
33
34
  context "associations" do
35
    should_have_many :members
36
    should_have_many :users, :through => :members
37
    should_have_many :member_principals
38
    should_have_many :principals, :through => :member_principals
39
    should_have_many :enabled_modules
40
    should_have_many :issues
41
    should_have_many :issue_changes, :through => :issues
42
    should_have_many :versions
43
    should_have_many :time_entries
44
    should_have_many :queries
45
    should_have_many :documents
46
    should_have_many :news
47
    should_have_many :issue_categories
48
    should_have_many :boards
49
    should_have_many :changesets, :through => :repository
50
51
    should_have_one :repository
52
    should_have_one :wiki
53
54
    should_have_and_belong_to_many :trackers
55
    should_have_and_belong_to_many :issue_custom_fields
56
  end
57
58
  def test_truth
59
    assert_kind_of Project, @ecookbook
60
    assert_equal "eCookbook", @ecookbook.name
61
  end
62
63 119:8661b858af72 Chris
  def test_default_attributes
64
    with_settings :default_projects_public => '1' do
65
      assert_equal true, Project.new.is_public
66
      assert_equal false, Project.new(:is_public => false).is_public
67
    end
68
69
    with_settings :default_projects_public => '0' do
70
      assert_equal false, Project.new.is_public
71
      assert_equal true, Project.new(:is_public => true).is_public
72
    end
73
74
    with_settings :sequential_project_identifiers => '1' do
75
      assert !Project.new.identifier.blank?
76
      assert Project.new(:identifier => '').identifier.blank?
77
    end
78
79
    with_settings :sequential_project_identifiers => '0' do
80
      assert Project.new.identifier.blank?
81
      assert !Project.new(:identifier => 'test').blank?
82
    end
83
84
    with_settings :default_projects_modules => ['issue_tracking', 'repository'] do
85
      assert_equal ['issue_tracking', 'repository'], Project.new.enabled_module_names
86
    end
87
88
    assert_equal Tracker.all, Project.new.trackers
89
    assert_equal Tracker.find(1, 3), Project.new(:tracker_ids => [1, 3]).trackers
90
  end
91
92 0:513646585e45 Chris
  def test_update
93
    assert_equal "eCookbook", @ecookbook.name
94
    @ecookbook.name = "eCook"
95
    assert @ecookbook.save, @ecookbook.errors.full_messages.join("; ")
96
    @ecookbook.reload
97
    assert_equal "eCook", @ecookbook.name
98
  end
99
100
  def test_validate_identifier
101
    to_test = {"abc" => true,
102
               "ab12" => true,
103
               "ab-12" => true,
104
               "12" => false,
105
               "new" => false}
106
107
    to_test.each do |identifier, valid|
108
      p = Project.new
109
      p.identifier = identifier
110
      p.valid?
111
      assert_equal valid, p.errors.on('identifier').nil?
112
    end
113
  end
114
115
  def test_members_should_be_active_users
116
    Project.all.each do |project|
117
      assert_nil project.members.detect {|m| !(m.user.is_a?(User) && m.user.active?) }
118
    end
119
  end
120
121
  def test_users_should_be_active_users
122
    Project.all.each do |project|
123
      assert_nil project.users.detect {|u| !(u.is_a?(User) && u.active?) }
124
    end
125
  end
126
127
  def test_archive
128
    user = @ecookbook.members.first.user
129
    @ecookbook.archive
130
    @ecookbook.reload
131
132
    assert !@ecookbook.active?
133 37:94944d00e43c chris
    assert @ecookbook.archived?
134 0:513646585e45 Chris
    assert !user.projects.include?(@ecookbook)
135
    # Subproject are also archived
136
    assert !@ecookbook.children.empty?
137
    assert @ecookbook.descendants.active.empty?
138
  end
139
140
  def test_archive_should_fail_if_versions_are_used_by_non_descendant_projects
141
    # Assign an issue of a project to a version of a child project
142
    Issue.find(4).update_attribute :fixed_version_id, 4
143
144
    assert_no_difference "Project.count(:all, :conditions => 'status = #{Project::STATUS_ARCHIVED}')" do
145
      assert_equal false, @ecookbook.archive
146
    end
147
    @ecookbook.reload
148
    assert @ecookbook.active?
149
  end
150
151
  def test_unarchive
152
    user = @ecookbook.members.first.user
153
    @ecookbook.archive
154
    # A subproject of an archived project can not be unarchived
155
    assert !@ecookbook_sub1.unarchive
156
157
    # Unarchive project
158
    assert @ecookbook.unarchive
159
    @ecookbook.reload
160
    assert @ecookbook.active?
161 37:94944d00e43c chris
    assert !@ecookbook.archived?
162 0:513646585e45 Chris
    assert user.projects.include?(@ecookbook)
163
    # Subproject can now be unarchived
164
    @ecookbook_sub1.reload
165
    assert @ecookbook_sub1.unarchive
166
  end
167
168
  def test_destroy
169
    # 2 active members
170
    assert_equal 2, @ecookbook.members.size
171
    # and 1 is locked
172
    assert_equal 3, Member.find(:all, :conditions => ['project_id = ?', @ecookbook.id]).size
173
    # some boards
174
    assert @ecookbook.boards.any?
175
176
    @ecookbook.destroy
177
    # make sure that the project non longer exists
178
    assert_raise(ActiveRecord::RecordNotFound) { Project.find(@ecookbook.id) }
179
    # make sure related data was removed
180
    assert_nil Member.first(:conditions => {:project_id => @ecookbook.id})
181
    assert_nil Board.first(:conditions => {:project_id => @ecookbook.id})
182
    assert_nil Issue.first(:conditions => {:project_id => @ecookbook.id})
183
  end
184
185 441:cbce1fd3b1b7 Chris
  def test_destroying_root_projects_should_clear_data
186
    Project.roots.each do |root|
187
      root.destroy
188
    end
189
190
    assert_equal 0, Project.count, "Projects were not deleted: #{Project.all.inspect}"
191
    assert_equal 0, Member.count, "Members were not deleted: #{Member.all.inspect}"
192
    assert_equal 0, MemberRole.count
193
    assert_equal 0, Issue.count
194
    assert_equal 0, Journal.count
195
    assert_equal 0, JournalDetail.count
196
    assert_equal 0, Attachment.count
197
    assert_equal 0, EnabledModule.count
198
    assert_equal 0, IssueCategory.count
199
    assert_equal 0, IssueRelation.count
200
    assert_equal 0, Board.count
201
    assert_equal 0, Message.count
202
    assert_equal 0, News.count
203
    assert_equal 0, Query.count(:conditions => "project_id IS NOT NULL")
204
    assert_equal 0, Repository.count
205
    assert_equal 0, Changeset.count
206
    assert_equal 0, Change.count
207
    assert_equal 0, Comment.count
208
    assert_equal 0, TimeEntry.count
209
    assert_equal 0, Version.count
210
    assert_equal 0, Watcher.count
211
    assert_equal 0, Wiki.count
212
    assert_equal 0, WikiPage.count
213
    assert_equal 0, WikiContent.count
214
    assert_equal 0, WikiContent::Version.count
215
    assert_equal 0, Project.connection.select_all("SELECT * FROM projects_trackers").size
216
    assert_equal 0, Project.connection.select_all("SELECT * FROM custom_fields_projects").size
217
    assert_equal 0, CustomValue.count(:conditions => {:customized_type => ['Project', 'Issue', 'TimeEntry', 'Version']})
218
  end
219
220 0:513646585e45 Chris
  def test_move_an_orphan_project_to_a_root_project
221
    sub = Project.find(2)
222
    sub.set_parent! @ecookbook
223
    assert_equal @ecookbook.id, sub.parent.id
224
    @ecookbook.reload
225
    assert_equal 4, @ecookbook.children.size
226
  end
227
228
  def test_move_an_orphan_project_to_a_subproject
229
    sub = Project.find(2)
230
    assert sub.set_parent!(@ecookbook_sub1)
231
  end
232
233
  def test_move_a_root_project_to_a_project
234
    sub = @ecookbook
235
    assert sub.set_parent!(Project.find(2))
236
  end
237
238
  def test_should_not_move_a_project_to_its_children
239
    sub = @ecookbook
240
    assert !(sub.set_parent!(Project.find(3)))
241
  end
242
243
  def test_set_parent_should_add_roots_in_alphabetical_order
244
    ProjectCustomField.delete_all
245
    Project.delete_all
246
    Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(nil)
247
    Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(nil)
248
    Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(nil)
249
    Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(nil)
250
251
    assert_equal 4, Project.count
252
    assert_equal Project.all.sort_by(&:name), Project.all.sort_by(&:lft)
253
  end
254
255
  def test_set_parent_should_add_children_in_alphabetical_order
256
    ProjectCustomField.delete_all
257
    parent = Project.create!(:name => 'Parent', :identifier => 'parent')
258
    Project.create!(:name => 'Project C', :identifier => 'project-c').set_parent!(parent)
259
    Project.create!(:name => 'Project B', :identifier => 'project-b').set_parent!(parent)
260
    Project.create!(:name => 'Project D', :identifier => 'project-d').set_parent!(parent)
261
    Project.create!(:name => 'Project A', :identifier => 'project-a').set_parent!(parent)
262
263
    parent.reload
264
    assert_equal 4, parent.children.size
265
    assert_equal parent.children.sort_by(&:name), parent.children
266
  end
267
268
  def test_rebuild_should_sort_children_alphabetically
269
    ProjectCustomField.delete_all
270
    parent = Project.create!(:name => 'Parent', :identifier => 'parent')
271
    Project.create!(:name => 'Project C', :identifier => 'project-c').move_to_child_of(parent)
272
    Project.create!(:name => 'Project B', :identifier => 'project-b').move_to_child_of(parent)
273
    Project.create!(:name => 'Project D', :identifier => 'project-d').move_to_child_of(parent)
274
    Project.create!(:name => 'Project A', :identifier => 'project-a').move_to_child_of(parent)
275
276
    Project.update_all("lft = NULL, rgt = NULL")
277
    Project.rebuild!
278
279
    parent.reload
280
    assert_equal 4, parent.children.size
281
    assert_equal parent.children.sort_by(&:name), parent.children
282
  end
283
284
285
  def test_set_parent_should_update_issue_fixed_version_associations_when_a_fixed_version_is_moved_out_of_the_hierarchy
286
    # Parent issue with a hierarchy project's fixed version
287
    parent_issue = Issue.find(1)
288
    parent_issue.update_attribute(:fixed_version_id, 4)
289
    parent_issue.reload
290
    assert_equal 4, parent_issue.fixed_version_id
291
292
    # Should keep fixed versions for the issues
293
    issue_with_local_fixed_version = Issue.find(5)
294
    issue_with_local_fixed_version.update_attribute(:fixed_version_id, 4)
295
    issue_with_local_fixed_version.reload
296
    assert_equal 4, issue_with_local_fixed_version.fixed_version_id
297
298
    # Local issue with hierarchy fixed_version
299
    issue_with_hierarchy_fixed_version = Issue.find(13)
300
    issue_with_hierarchy_fixed_version.update_attribute(:fixed_version_id, 6)
301
    issue_with_hierarchy_fixed_version.reload
302
    assert_equal 6, issue_with_hierarchy_fixed_version.fixed_version_id
303
304
    # Move project out of the issue's hierarchy
305
    moved_project = Project.find(3)
306
    moved_project.set_parent!(Project.find(2))
307
    parent_issue.reload
308
    issue_with_local_fixed_version.reload
309
    issue_with_hierarchy_fixed_version.reload
310
311
    assert_equal 4, issue_with_local_fixed_version.fixed_version_id, "Fixed version was not keep on an issue local to the moved project"
312
    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"
313
    assert_equal nil, parent_issue.fixed_version_id, "Fixed version is still set after moving the Version out of the hierarchy for the issue."
314
  end
315
316
  def test_parent
317
    p = Project.find(6).parent
318
    assert p.is_a?(Project)
319
    assert_equal 5, p.id
320
  end
321
322
  def test_ancestors
323
    a = Project.find(6).ancestors
324
    assert a.first.is_a?(Project)
325
    assert_equal [1, 5], a.collect(&:id)
326
  end
327
328
  def test_root
329
    r = Project.find(6).root
330
    assert r.is_a?(Project)
331
    assert_equal 1, r.id
332
  end
333
334
  def test_children
335
    c = Project.find(1).children
336
    assert c.first.is_a?(Project)
337
    assert_equal [5, 3, 4], c.collect(&:id)
338
  end
339
340
  def test_descendants
341
    d = Project.find(1).descendants
342
    assert d.first.is_a?(Project)
343
    assert_equal [5, 6, 3, 4], d.collect(&:id)
344
  end
345
346
  def test_allowed_parents_should_be_empty_for_non_member_user
347
    Role.non_member.add_permission!(:add_project)
348
    user = User.find(9)
349
    assert user.memberships.empty?
350
    User.current = user
351
    assert Project.new.allowed_parents.compact.empty?
352
  end
353
354
  def test_allowed_parents_with_add_subprojects_permission
355
    Role.find(1).remove_permission!(:add_project)
356
    Role.find(1).add_permission!(:add_subprojects)
357
    User.current = User.find(2)
358
    # new project
359
    assert !Project.new.allowed_parents.include?(nil)
360
    assert Project.new.allowed_parents.include?(Project.find(1))
361
    # existing root project
362
    assert Project.find(1).allowed_parents.include?(nil)
363
    # existing child
364
    assert Project.find(3).allowed_parents.include?(Project.find(1))
365
    assert !Project.find(3).allowed_parents.include?(nil)
366
  end
367
368
  def test_allowed_parents_with_add_project_permission
369
    Role.find(1).add_permission!(:add_project)
370
    Role.find(1).remove_permission!(:add_subprojects)
371
    User.current = User.find(2)
372
    # new project
373
    assert Project.new.allowed_parents.include?(nil)
374
    assert !Project.new.allowed_parents.include?(Project.find(1))
375
    # existing root project
376
    assert Project.find(1).allowed_parents.include?(nil)
377
    # existing child
378
    assert Project.find(3).allowed_parents.include?(Project.find(1))
379
    assert Project.find(3).allowed_parents.include?(nil)
380
  end
381
382
  def test_allowed_parents_with_add_project_and_subprojects_permission
383
    Role.find(1).add_permission!(:add_project)
384
    Role.find(1).add_permission!(:add_subprojects)
385
    User.current = User.find(2)
386
    # new project
387
    assert Project.new.allowed_parents.include?(nil)
388
    assert Project.new.allowed_parents.include?(Project.find(1))
389
    # existing root project
390
    assert Project.find(1).allowed_parents.include?(nil)
391
    # existing child
392
    assert Project.find(3).allowed_parents.include?(Project.find(1))
393
    assert Project.find(3).allowed_parents.include?(nil)
394
  end
395
396
  def test_users_by_role
397
    users_by_role = Project.find(1).users_by_role
398
    assert_kind_of Hash, users_by_role
399
    role = Role.find(1)
400
    assert_kind_of Array, users_by_role[role]
401
    assert users_by_role[role].include?(User.find(2))
402
  end
403
404
  def test_rolled_up_trackers
405
    parent = Project.find(1)
406
    parent.trackers = Tracker.find([1,2])
407
    child = parent.children.find(3)
408
409
    assert_equal [1, 2], parent.tracker_ids
410
    assert_equal [2, 3], child.trackers.collect(&:id)
411
412
    assert_kind_of Tracker, parent.rolled_up_trackers.first
413
    assert_equal Tracker.find(1), parent.rolled_up_trackers.first
414
415
    assert_equal [1, 2, 3], parent.rolled_up_trackers.collect(&:id)
416
    assert_equal [2, 3], child.rolled_up_trackers.collect(&:id)
417
  end
418
419
  def test_rolled_up_trackers_should_ignore_archived_subprojects
420
    parent = Project.find(1)
421
    parent.trackers = Tracker.find([1,2])
422
    child = parent.children.find(3)
423
    child.trackers = Tracker.find([1,3])
424
    parent.children.each(&:archive)
425
426
    assert_equal [1,2], parent.rolled_up_trackers.collect(&:id)
427
  end
428
429
  context "#rolled_up_versions" do
430
    setup do
431
      @project = Project.generate!
432
      @parent_version_1 = Version.generate!(:project => @project)
433
      @parent_version_2 = Version.generate!(:project => @project)
434
    end
435
436
    should "include the versions for the current project" do
437
      assert_same_elements [@parent_version_1, @parent_version_2], @project.rolled_up_versions
438
    end
439
440
    should "include versions for a subproject" do
441
      @subproject = Project.generate!
442
      @subproject.set_parent!(@project)
443
      @subproject_version = Version.generate!(:project => @subproject)
444
445
      assert_same_elements [
446
                            @parent_version_1,
447
                            @parent_version_2,
448
                            @subproject_version
449
                           ], @project.rolled_up_versions
450
    end
451
452
    should "include versions for a sub-subproject" do
453
      @subproject = Project.generate!
454
      @subproject.set_parent!(@project)
455
      @sub_subproject = Project.generate!
456
      @sub_subproject.set_parent!(@subproject)
457
      @sub_subproject_version = Version.generate!(:project => @sub_subproject)
458
459
      @project.reload
460
461
      assert_same_elements [
462
                            @parent_version_1,
463
                            @parent_version_2,
464
                            @sub_subproject_version
465
                           ], @project.rolled_up_versions
466
    end
467
468
469
    should "only check active projects" do
470
      @subproject = Project.generate!
471
      @subproject.set_parent!(@project)
472
      @subproject_version = Version.generate!(:project => @subproject)
473
      assert @subproject.archive
474
475
      @project.reload
476
477
      assert !@subproject.active?
478
      assert_same_elements [@parent_version_1, @parent_version_2], @project.rolled_up_versions
479
    end
480
  end
481
482
  def test_shared_versions_none_sharing
483
    p = Project.find(5)
484
    v = Version.create!(:name => 'none_sharing', :project => p, :sharing => 'none')
485
    assert p.shared_versions.include?(v)
486
    assert !p.children.first.shared_versions.include?(v)
487
    assert !p.root.shared_versions.include?(v)
488
    assert !p.siblings.first.shared_versions.include?(v)
489
    assert !p.root.siblings.first.shared_versions.include?(v)
490
  end
491
492
  def test_shared_versions_descendants_sharing
493
    p = Project.find(5)
494
    v = Version.create!(:name => 'descendants_sharing', :project => p, :sharing => 'descendants')
495
    assert p.shared_versions.include?(v)
496
    assert p.children.first.shared_versions.include?(v)
497
    assert !p.root.shared_versions.include?(v)
498
    assert !p.siblings.first.shared_versions.include?(v)
499
    assert !p.root.siblings.first.shared_versions.include?(v)
500
  end
501
502
  def test_shared_versions_hierarchy_sharing
503
    p = Project.find(5)
504
    v = Version.create!(:name => 'hierarchy_sharing', :project => p, :sharing => 'hierarchy')
505
    assert p.shared_versions.include?(v)
506
    assert p.children.first.shared_versions.include?(v)
507
    assert p.root.shared_versions.include?(v)
508
    assert !p.siblings.first.shared_versions.include?(v)
509
    assert !p.root.siblings.first.shared_versions.include?(v)
510
  end
511
512
  def test_shared_versions_tree_sharing
513
    p = Project.find(5)
514
    v = Version.create!(:name => 'tree_sharing', :project => p, :sharing => 'tree')
515
    assert p.shared_versions.include?(v)
516
    assert p.children.first.shared_versions.include?(v)
517
    assert p.root.shared_versions.include?(v)
518
    assert p.siblings.first.shared_versions.include?(v)
519
    assert !p.root.siblings.first.shared_versions.include?(v)
520
  end
521
522
  def test_shared_versions_system_sharing
523
    p = Project.find(5)
524
    v = Version.create!(:name => 'system_sharing', :project => p, :sharing => 'system')
525
    assert p.shared_versions.include?(v)
526
    assert p.children.first.shared_versions.include?(v)
527
    assert p.root.shared_versions.include?(v)
528
    assert p.siblings.first.shared_versions.include?(v)
529
    assert p.root.siblings.first.shared_versions.include?(v)
530
  end
531
532
  def test_shared_versions
533
    parent = Project.find(1)
534
    child = parent.children.find(3)
535
    private_child = parent.children.find(5)
536
537
    assert_equal [1,2,3], parent.version_ids.sort
538
    assert_equal [4], child.version_ids
539
    assert_equal [6], private_child.version_ids
540
    assert_equal [7], Version.find_all_by_sharing('system').collect(&:id)
541
542
    assert_equal 6, parent.shared_versions.size
543
    parent.shared_versions.each do |version|
544
      assert_kind_of Version, version
545
    end
546
547
    assert_equal [1,2,3,4,6,7], parent.shared_versions.collect(&:id).sort
548
  end
549
550
  def test_shared_versions_should_ignore_archived_subprojects
551
    parent = Project.find(1)
552
    child = parent.children.find(3)
553
    child.archive
554
    parent.reload
555
556
    assert_equal [1,2,3], parent.version_ids.sort
557
    assert_equal [4], child.version_ids
558
    assert !parent.shared_versions.collect(&:id).include?(4)
559
  end
560
561
  def test_shared_versions_visible_to_user
562
    user = User.find(3)
563
    parent = Project.find(1)
564
    child = parent.children.find(5)
565
566
    assert_equal [1,2,3], parent.version_ids.sort
567
    assert_equal [6], child.version_ids
568
569
    versions = parent.shared_versions.visible(user)
570
571
    assert_equal 4, versions.size
572
    versions.each do |version|
573
      assert_kind_of Version, version
574
    end
575
576
    assert !versions.collect(&:id).include?(6)
577
  end
578
579
580
  def test_next_identifier
581
    ProjectCustomField.delete_all
582
    Project.create!(:name => 'last', :identifier => 'p2008040')
583
    assert_equal 'p2008041', Project.next_identifier
584
  end
585
586
  def test_next_identifier_first_project
587
    Project.delete_all
588
    assert_nil Project.next_identifier
589
  end
590
591 441:cbce1fd3b1b7 Chris
  def test_enabled_module_names
592
    with_settings :default_projects_modules => ['issue_tracking', 'repository'] do
593
      project = Project.new
594
595
      project.enabled_module_names = %w(issue_tracking news)
596
      assert_equal %w(issue_tracking news), project.enabled_module_names.sort
597
    end
598
  end
599 0:513646585e45 Chris
600
  def test_enabled_module_names_should_not_recreate_enabled_modules
601
    project = Project.find(1)
602
    # Remove one module
603
    modules = project.enabled_modules.slice(0..-2)
604
    assert modules.any?
605
    assert_difference 'EnabledModule.count', -1 do
606
      project.enabled_module_names = modules.collect(&:name)
607
    end
608
    project.reload
609
    # Ids should be preserved
610
    assert_equal project.enabled_module_ids.sort, modules.collect(&:id).sort
611
  end
612
613
  def test_copy_from_existing_project
614
    source_project = Project.find(1)
615
    copied_project = Project.copy_from(1)
616
617
    assert copied_project
618
    # Cleared attributes
619
    assert copied_project.id.blank?
620
    assert copied_project.name.blank?
621
    assert copied_project.identifier.blank?
622
623
    # Duplicated attributes
624
    assert_equal source_project.description, copied_project.description
625
    assert_equal source_project.enabled_modules, copied_project.enabled_modules
626
    assert_equal source_project.trackers, copied_project.trackers
627
628
    # Default attributes
629
    assert_equal 1, copied_project.status
630
  end
631
632
  def test_activities_should_use_the_system_activities
633
    project = Project.find(1)
634
    assert_equal project.activities, TimeEntryActivity.find(:all, :conditions => {:active => true} )
635
  end
636
637
638
  def test_activities_should_use_the_project_specific_activities
639
    project = Project.find(1)
640
    overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project})
641
    assert overridden_activity.save!
642
643
    assert project.activities.include?(overridden_activity), "Project specific Activity not found"
644
  end
645
646
  def test_activities_should_not_include_the_inactive_project_specific_activities
647
    project = Project.find(1)
648
    overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => TimeEntryActivity.find(:first), :active => false})
649
    assert overridden_activity.save!
650
651
    assert !project.activities.include?(overridden_activity), "Inactive Project specific Activity found"
652
  end
653
654
  def test_activities_should_not_include_project_specific_activities_from_other_projects
655
    project = Project.find(1)
656
    overridden_activity = TimeEntryActivity.new({:name => "Project", :project => Project.find(2)})
657
    assert overridden_activity.save!
658
659
    assert !project.activities.include?(overridden_activity), "Project specific Activity found on a different project"
660
  end
661
662
  def test_activities_should_handle_nils
663
    overridden_activity = TimeEntryActivity.new({:name => "Project", :project => Project.find(1), :parent => TimeEntryActivity.find(:first)})
664
    TimeEntryActivity.delete_all
665
666
    # No activities
667
    project = Project.find(1)
668
    assert project.activities.empty?
669
670
    # No system, one overridden
671
    assert overridden_activity.save!
672
    project.reload
673
    assert_equal [overridden_activity], project.activities
674
  end
675
676
  def test_activities_should_override_system_activities_with_project_activities
677
    project = Project.find(1)
678
    parent_activity = TimeEntryActivity.find(:first)
679
    overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => parent_activity})
680
    assert overridden_activity.save!
681
682
    assert project.activities.include?(overridden_activity), "Project specific Activity not found"
683
    assert !project.activities.include?(parent_activity), "System Activity found when it should have been overridden"
684
  end
685
686
  def test_activities_should_include_inactive_activities_if_specified
687
    project = Project.find(1)
688
    overridden_activity = TimeEntryActivity.new({:name => "Project", :project => project, :parent => TimeEntryActivity.find(:first), :active => false})
689
    assert overridden_activity.save!
690
691
    assert project.activities(true).include?(overridden_activity), "Inactive Project specific Activity not found"
692
  end
693
694
  test 'activities should not include active System activities if the project has an override that is inactive' do
695
    project = Project.find(1)
696
    system_activity = TimeEntryActivity.find_by_name('Design')
697
    assert system_activity.active?
698
    overridden_activity = TimeEntryActivity.generate!(:project => project, :parent => system_activity, :active => false)
699
    assert overridden_activity.save!
700
701
    assert !project.activities.include?(overridden_activity), "Inactive Project specific Activity not found"
702
    assert !project.activities.include?(system_activity), "System activity found when the project has an inactive override"
703
  end
704
705
  def test_close_completed_versions
706
    Version.update_all("status = 'open'")
707
    project = Project.find(1)
708
    assert_not_nil project.versions.detect {|v| v.completed? && v.status == 'open'}
709
    assert_not_nil project.versions.detect {|v| !v.completed? && v.status == 'open'}
710
    project.close_completed_versions
711
    project.reload
712
    assert_nil project.versions.detect {|v| v.completed? && v.status != 'closed'}
713
    assert_not_nil project.versions.detect {|v| !v.completed? && v.status == 'open'}
714
  end
715
716
  context "Project#copy" do
717
    setup do
718
      ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
719
      Project.destroy_all :identifier => "copy-test"
720
      @source_project = Project.find(2)
721
      @project = Project.new(:name => 'Copy Test', :identifier => 'copy-test')
722
      @project.trackers = @source_project.trackers
723
      @project.enabled_module_names = @source_project.enabled_modules.collect(&:name)
724
    end
725
726
    should "copy issues" do
727
      @source_project.issues << Issue.generate!(:status => IssueStatus.find_by_name('Closed'),
728
                                                :subject => "copy issue status",
729
                                                :tracker_id => 1,
730
                                                :assigned_to_id => 2,
731
                                                :project_id => @source_project.id)
732
      assert @project.valid?
733
      assert @project.issues.empty?
734
      assert @project.copy(@source_project)
735
736
      assert_equal @source_project.issues.size, @project.issues.size
737
      @project.issues.each do |issue|
738
        assert issue.valid?
739
        assert ! issue.assigned_to.blank?
740
        assert_equal @project, issue.project
741
      end
742
743
      copied_issue = @project.issues.first(:conditions => {:subject => "copy issue status"})
744
      assert copied_issue
745
      assert copied_issue.status
746
      assert_equal "Closed", copied_issue.status.name
747
    end
748
749
    should "change the new issues to use the copied version" do
750
      User.current = User.find(1)
751
      assigned_version = Version.generate!(:name => "Assigned Issues", :status => 'open')
752
      @source_project.versions << assigned_version
753
      assert_equal 3, @source_project.versions.size
754
      Issue.generate_for_project!(@source_project,
755
                                  :fixed_version_id => assigned_version.id,
756
                                  :subject => "change the new issues to use the copied version",
757
                                  :tracker_id => 1,
758
                                  :project_id => @source_project.id)
759
760
      assert @project.copy(@source_project)
761
      @project.reload
762
      copied_issue = @project.issues.first(:conditions => {:subject => "change the new issues to use the copied version"})
763
764
      assert copied_issue
765
      assert copied_issue.fixed_version
766
      assert_equal "Assigned Issues", copied_issue.fixed_version.name # Same name
767
      assert_not_equal assigned_version.id, copied_issue.fixed_version.id # Different record
768
    end
769
770
    should "copy issue relations" do
771
      Setting.cross_project_issue_relations = '1'
772
773
      second_issue = Issue.generate!(:status_id => 5,
774
                                     :subject => "copy issue relation",
775
                                     :tracker_id => 1,
776
                                     :assigned_to_id => 2,
777
                                     :project_id => @source_project.id)
778
      source_relation = IssueRelation.generate!(:issue_from => Issue.find(4),
779
                                                :issue_to => second_issue,
780
                                                :relation_type => "relates")
781
      source_relation_cross_project = IssueRelation.generate!(:issue_from => Issue.find(1),
782
                                                              :issue_to => second_issue,
783
                                                              :relation_type => "duplicates")
784
785
      assert @project.copy(@source_project)
786
      assert_equal @source_project.issues.count, @project.issues.count
787
      copied_issue = @project.issues.find_by_subject("Issue on project 2") # Was #4
788
      copied_second_issue = @project.issues.find_by_subject("copy issue relation")
789
790
      # First issue with a relation on project
791
      assert_equal 1, copied_issue.relations.size, "Relation not copied"
792
      copied_relation = copied_issue.relations.first
793
      assert_equal "relates", copied_relation.relation_type
794
      assert_equal copied_second_issue.id, copied_relation.issue_to_id
795
      assert_not_equal source_relation.id, copied_relation.id
796
797
      # Second issue with a cross project relation
798
      assert_equal 2, copied_second_issue.relations.size, "Relation not copied"
799
      copied_relation = copied_second_issue.relations.select {|r| r.relation_type == 'duplicates'}.first
800
      assert_equal "duplicates", copied_relation.relation_type
801
      assert_equal 1, copied_relation.issue_from_id, "Cross project relation not kept"
802
      assert_not_equal source_relation_cross_project.id, copied_relation.id
803
    end
804
805
    should "copy memberships" do
806
      assert @project.valid?
807
      assert @project.members.empty?
808
      assert @project.copy(@source_project)
809
810
      assert_equal @source_project.memberships.size, @project.memberships.size
811
      @project.memberships.each do |membership|
812
        assert membership
813
        assert_equal @project, membership.project
814
      end
815
    end
816 119:8661b858af72 Chris
817
    should "copy memberships with groups and additional roles" do
818
      group = Group.create!(:lastname => "Copy group")
819
      user = User.find(7)
820
      group.users << user
821
      # group role
822
      Member.create!(:project_id => @source_project.id, :principal => group, :role_ids => [2])
823
      member = Member.find_by_user_id_and_project_id(user.id, @source_project.id)
824
      # additional role
825
      member.role_ids = [1]
826
827
      assert @project.copy(@source_project)
828
      member = Member.find_by_user_id_and_project_id(user.id, @project.id)
829
      assert_not_nil member
830
      assert_equal [1, 2], member.role_ids.sort
831
    end
832 0:513646585e45 Chris
833
    should "copy project specific queries" do
834
      assert @project.valid?
835
      assert @project.queries.empty?
836
      assert @project.copy(@source_project)
837
838
      assert_equal @source_project.queries.size, @project.queries.size
839
      @project.queries.each do |query|
840
        assert query
841
        assert_equal @project, query.project
842
      end
843
    end
844
845
    should "copy versions" do
846
      @source_project.versions << Version.generate!
847
      @source_project.versions << Version.generate!
848
849
      assert @project.versions.empty?
850
      assert @project.copy(@source_project)
851
852
      assert_equal @source_project.versions.size, @project.versions.size
853
      @project.versions.each do |version|
854
        assert version
855
        assert_equal @project, version.project
856
      end
857
    end
858
859
    should "copy wiki" do
860
      assert_difference 'Wiki.count' do
861
        assert @project.copy(@source_project)
862
      end
863
864
      assert @project.wiki
865
      assert_not_equal @source_project.wiki, @project.wiki
866
      assert_equal "Start page", @project.wiki.start_page
867
    end
868
869
    should "copy wiki pages and content with hierarchy" do
870
      assert_difference 'WikiPage.count', @source_project.wiki.pages.size do
871
        assert @project.copy(@source_project)
872
      end
873
874
      assert @project.wiki
875
      assert_equal @source_project.wiki.pages.size, @project.wiki.pages.size
876
877
      @project.wiki.pages.each do |wiki_page|
878
        assert wiki_page.content
879
        assert !@source_project.wiki.pages.include?(wiki_page)
880
      end
881
882
      parent = @project.wiki.find_page('Parent_page')
883
      child1 = @project.wiki.find_page('Child_page_1')
884
      child2 = @project.wiki.find_page('Child_page_2')
885
      assert_equal parent, child1.parent
886
      assert_equal parent, child2.parent
887
    end
888
889
    should "copy issue categories" do
890
      assert @project.copy(@source_project)
891
892
      assert_equal 2, @project.issue_categories.size
893
      @project.issue_categories.each do |issue_category|
894
        assert !@source_project.issue_categories.include?(issue_category)
895
      end
896
    end
897
898
    should "copy boards" do
899
      assert @project.copy(@source_project)
900
901
      assert_equal 1, @project.boards.size
902
      @project.boards.each do |board|
903
        assert !@source_project.boards.include?(board)
904
      end
905
    end
906
907
    should "change the new issues to use the copied issue categories" do
908
      issue = Issue.find(4)
909
      issue.update_attribute(:category_id, 3)
910
911
      assert @project.copy(@source_project)
912
913
      @project.issues.each do |issue|
914
        assert issue.category
915
        assert_equal "Stock management", issue.category.name # Same name
916
        assert_not_equal IssueCategory.find(3), issue.category # Different record
917
      end
918
    end
919
920
    should "limit copy with :only option" do
921
      assert @project.members.empty?
922
      assert @project.issue_categories.empty?
923
      assert @source_project.issues.any?
924
925
      assert @project.copy(@source_project, :only => ['members', 'issue_categories'])
926
927
      assert @project.members.any?
928
      assert @project.issue_categories.any?
929
      assert @project.issues.empty?
930
    end
931
932
  end
933
934 22:40f7cfd4df19 chris
  context "#start_date" do
935
    setup do
936
      ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
937
      @project = Project.generate!(:identifier => 'test0')
938
      @project.trackers << Tracker.generate!
939
    end
940
941
    should "be nil if there are no issues on the project" do
942
      assert_nil @project.start_date
943
    end
944 37:94944d00e43c chris
945
    should "be tested when issues have no start date"
946 22:40f7cfd4df19 chris
947
    should "be the earliest start date of it's issues" do
948
      early = 7.days.ago.to_date
949
      Issue.generate_for_project!(@project, :start_date => Date.today)
950
      Issue.generate_for_project!(@project, :start_date => early)
951
952
      assert_equal early, @project.start_date
953
    end
954
955
  end
956
957
  context "#due_date" do
958
    setup do
959
      ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
960
      @project = Project.generate!(:identifier => 'test0')
961
      @project.trackers << Tracker.generate!
962
    end
963
964
    should "be nil if there are no issues on the project" do
965
      assert_nil @project.due_date
966
    end
967 37:94944d00e43c chris
968
    should "be tested when issues have no due date"
969 22:40f7cfd4df19 chris
970
    should "be the latest due date of it's issues" do
971
      future = 7.days.from_now.to_date
972
      Issue.generate_for_project!(@project, :due_date => future)
973
      Issue.generate_for_project!(@project, :due_date => Date.today)
974
975
      assert_equal future, @project.due_date
976
    end
977
978
    should "be the latest due date of it's versions" do
979
      future = 7.days.from_now.to_date
980
      @project.versions << Version.generate!(:effective_date => future)
981
      @project.versions << Version.generate!(:effective_date => Date.today)
982
983
984
      assert_equal future, @project.due_date
985
986
    end
987
988
    should "pick the latest date from it's issues and versions" do
989
      future = 7.days.from_now.to_date
990
      far_future = 14.days.from_now.to_date
991
      Issue.generate_for_project!(@project, :due_date => far_future)
992
      @project.versions << Version.generate!(:effective_date => future)
993
994
      assert_equal far_future, @project.due_date
995
    end
996
997
  end
998
999
  context "Project#completed_percent" do
1000
    setup do
1001
      ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
1002
      @project = Project.generate!(:identifier => 'test0')
1003
      @project.trackers << Tracker.generate!
1004
    end
1005
1006
    context "no versions" do
1007
      should "be 100" do
1008
        assert_equal 100, @project.completed_percent
1009
      end
1010
    end
1011
1012
    context "with versions" do
1013
      should "return 0 if the versions have no issues" do
1014
        Version.generate!(:project => @project)
1015
        Version.generate!(:project => @project)
1016
1017
        assert_equal 0, @project.completed_percent
1018
      end
1019
1020
      should "return 100 if the version has only closed issues" do
1021
        v1 = Version.generate!(:project => @project)
1022
        Issue.generate_for_project!(@project, :status => IssueStatus.find_by_name('Closed'), :fixed_version => v1)
1023
        v2 = Version.generate!(:project => @project)
1024
        Issue.generate_for_project!(@project, :status => IssueStatus.find_by_name('Closed'), :fixed_version => v2)
1025
1026
        assert_equal 100, @project.completed_percent
1027
      end
1028
1029
      should "return the averaged completed percent of the versions (not weighted)" do
1030
        v1 = Version.generate!(:project => @project)
1031
        Issue.generate_for_project!(@project, :status => IssueStatus.find_by_name('New'), :estimated_hours => 10, :done_ratio => 50, :fixed_version => v1)
1032
        v2 = Version.generate!(:project => @project)
1033
        Issue.generate_for_project!(@project, :status => IssueStatus.find_by_name('New'), :estimated_hours => 10, :done_ratio => 50, :fixed_version => v2)
1034
1035
        assert_equal 50, @project.completed_percent
1036
      end
1037
1038
    end
1039
  end
1040 37:94944d00e43c chris
1041
  context "#notified_users" do
1042
    setup do
1043
      @project = Project.generate!
1044
      @role = Role.generate!
1045
1046
      @user_with_membership_notification = User.generate!(:mail_notification => 'selected')
1047
      Member.generate!(:project => @project, :roles => [@role], :principal => @user_with_membership_notification, :mail_notification => true)
1048
1049
      @all_events_user = User.generate!(:mail_notification => 'all')
1050
      Member.generate!(:project => @project, :roles => [@role], :principal => @all_events_user)
1051
1052
      @no_events_user = User.generate!(:mail_notification => 'none')
1053
      Member.generate!(:project => @project, :roles => [@role], :principal => @no_events_user)
1054
1055
      @only_my_events_user = User.generate!(:mail_notification => 'only_my_events')
1056
      Member.generate!(:project => @project, :roles => [@role], :principal => @only_my_events_user)
1057
1058
      @only_assigned_user = User.generate!(:mail_notification => 'only_assigned')
1059
      Member.generate!(:project => @project, :roles => [@role], :principal => @only_assigned_user)
1060
1061
      @only_owned_user = User.generate!(:mail_notification => 'only_owner')
1062
      Member.generate!(:project => @project, :roles => [@role], :principal => @only_owned_user)
1063
    end
1064
1065
    should "include members with a mail notification" do
1066
      assert @project.notified_users.include?(@user_with_membership_notification)
1067
    end
1068
1069
    should "include users with the 'all' notification option" do
1070
      assert @project.notified_users.include?(@all_events_user)
1071
    end
1072
1073
    should "not include users with the 'none' notification option" do
1074
      assert !@project.notified_users.include?(@no_events_user)
1075
    end
1076
1077
    should "not include users with the 'only_my_events' notification option" do
1078
      assert !@project.notified_users.include?(@only_my_events_user)
1079
    end
1080
1081
    should "not include users with the 'only_assigned' notification option" do
1082
      assert !@project.notified_users.include?(@only_assigned_user)
1083
    end
1084
1085
    should "not include users with the 'only_owner' notification option" do
1086
      assert !@project.notified_users.include?(@only_owned_user)
1087
    end
1088
  end
1089
1090 0:513646585e45 Chris
end