@@ -1,223 +1,223 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2015 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.expand_path('../../test_helper', __FILE__) |
|
18 | require File.expand_path('../../test_helper', __FILE__) | |
19 |
|
19 | |||
20 | class JournalTest < ActiveSupport::TestCase |
|
20 | class JournalTest < ActiveSupport::TestCase | |
21 | fixtures :projects, :issues, :issue_statuses, :journals, :journal_details, |
|
21 | fixtures :projects, :issues, :issue_statuses, :journals, :journal_details, | |
22 | :issue_relations, :workflows, |
|
22 | :issue_relations, :workflows, | |
23 | :users, :members, :member_roles, :roles, :enabled_modules, |
|
23 | :users, :members, :member_roles, :roles, :enabled_modules, | |
24 | :groups_users, |
|
24 | :groups_users, :email_addresses, | |
25 | :enumerations, |
|
25 | :enumerations, | |
26 | :projects_trackers, :trackers, :custom_fields |
|
26 | :projects_trackers, :trackers, :custom_fields | |
27 |
|
27 | |||
28 | def setup |
|
28 | def setup | |
29 | @journal = Journal.find 1 |
|
29 | @journal = Journal.find 1 | |
30 | User.current = nil |
|
30 | User.current = nil | |
31 | end |
|
31 | end | |
32 |
|
32 | |||
33 | def test_journalized_is_an_issue |
|
33 | def test_journalized_is_an_issue | |
34 | issue = @journal.issue |
|
34 | issue = @journal.issue | |
35 | assert_kind_of Issue, issue |
|
35 | assert_kind_of Issue, issue | |
36 | assert_equal 1, issue.id |
|
36 | assert_equal 1, issue.id | |
37 | end |
|
37 | end | |
38 |
|
38 | |||
39 | def test_new_status |
|
39 | def test_new_status | |
40 | status = @journal.new_status |
|
40 | status = @journal.new_status | |
41 | assert_not_nil status |
|
41 | assert_not_nil status | |
42 | assert_kind_of IssueStatus, status |
|
42 | assert_kind_of IssueStatus, status | |
43 | assert_equal 2, status.id |
|
43 | assert_equal 2, status.id | |
44 | end |
|
44 | end | |
45 |
|
45 | |||
46 | def test_create_should_send_email_notification |
|
46 | def test_create_should_send_email_notification | |
47 | ActionMailer::Base.deliveries.clear |
|
47 | ActionMailer::Base.deliveries.clear | |
48 | issue = Issue.first |
|
48 | issue = Issue.first | |
49 | user = User.first |
|
49 | user = User.first | |
50 | journal = issue.init_journal(user, issue) |
|
50 | journal = issue.init_journal(user, issue) | |
51 |
|
51 | |||
52 | assert journal.save |
|
52 | assert journal.save | |
53 | assert_equal 1, ActionMailer::Base.deliveries.size |
|
53 | assert_equal 1, ActionMailer::Base.deliveries.size | |
54 | end |
|
54 | end | |
55 |
|
55 | |||
56 | def test_should_not_save_journal_with_blank_notes_and_no_details |
|
56 | def test_should_not_save_journal_with_blank_notes_and_no_details | |
57 | journal = Journal.new(:journalized => Issue.first, :user => User.first) |
|
57 | journal = Journal.new(:journalized => Issue.first, :user => User.first) | |
58 |
|
58 | |||
59 | assert_no_difference 'Journal.count' do |
|
59 | assert_no_difference 'Journal.count' do | |
60 | assert_equal false, journal.save |
|
60 | assert_equal false, journal.save | |
61 | end |
|
61 | end | |
62 | end |
|
62 | end | |
63 |
|
63 | |||
64 | def test_create_should_not_split_non_private_notes |
|
64 | def test_create_should_not_split_non_private_notes | |
65 | assert_difference 'Journal.count' do |
|
65 | assert_difference 'Journal.count' do | |
66 | assert_no_difference 'JournalDetail.count' do |
|
66 | assert_no_difference 'JournalDetail.count' do | |
67 | journal = Journal.generate!(:notes => 'Notes') |
|
67 | journal = Journal.generate!(:notes => 'Notes') | |
68 | end |
|
68 | end | |
69 | end |
|
69 | end | |
70 |
|
70 | |||
71 | assert_difference 'Journal.count' do |
|
71 | assert_difference 'Journal.count' do | |
72 | assert_difference 'JournalDetail.count' do |
|
72 | assert_difference 'JournalDetail.count' do | |
73 | journal = Journal.generate!(:notes => 'Notes', :details => [JournalDetail.new]) |
|
73 | journal = Journal.generate!(:notes => 'Notes', :details => [JournalDetail.new]) | |
74 | end |
|
74 | end | |
75 | end |
|
75 | end | |
76 |
|
76 | |||
77 | assert_difference 'Journal.count' do |
|
77 | assert_difference 'Journal.count' do | |
78 | assert_difference 'JournalDetail.count' do |
|
78 | assert_difference 'JournalDetail.count' do | |
79 | journal = Journal.generate!(:notes => '', :details => [JournalDetail.new]) |
|
79 | journal = Journal.generate!(:notes => '', :details => [JournalDetail.new]) | |
80 | end |
|
80 | end | |
81 | end |
|
81 | end | |
82 | end |
|
82 | end | |
83 |
|
83 | |||
84 | def test_create_should_split_private_notes |
|
84 | def test_create_should_split_private_notes | |
85 | assert_difference 'Journal.count' do |
|
85 | assert_difference 'Journal.count' do | |
86 | assert_no_difference 'JournalDetail.count' do |
|
86 | assert_no_difference 'JournalDetail.count' do | |
87 | journal = Journal.generate!(:notes => 'Notes', :private_notes => true) |
|
87 | journal = Journal.generate!(:notes => 'Notes', :private_notes => true) | |
88 | journal.reload |
|
88 | journal.reload | |
89 | assert_equal true, journal.private_notes |
|
89 | assert_equal true, journal.private_notes | |
90 | assert_equal 'Notes', journal.notes |
|
90 | assert_equal 'Notes', journal.notes | |
91 | end |
|
91 | end | |
92 | end |
|
92 | end | |
93 |
|
93 | |||
94 | assert_difference 'Journal.count', 2 do |
|
94 | assert_difference 'Journal.count', 2 do | |
95 | assert_difference 'JournalDetail.count' do |
|
95 | assert_difference 'JournalDetail.count' do | |
96 | journal = Journal.generate!(:notes => 'Notes', :private_notes => true, :details => [JournalDetail.new]) |
|
96 | journal = Journal.generate!(:notes => 'Notes', :private_notes => true, :details => [JournalDetail.new]) | |
97 | journal.reload |
|
97 | journal.reload | |
98 | assert_equal true, journal.private_notes |
|
98 | assert_equal true, journal.private_notes | |
99 | assert_equal 'Notes', journal.notes |
|
99 | assert_equal 'Notes', journal.notes | |
100 | assert_equal 0, journal.details.size |
|
100 | assert_equal 0, journal.details.size | |
101 |
|
101 | |||
102 | journal_with_changes = Journal.order('id DESC').offset(1).first |
|
102 | journal_with_changes = Journal.order('id DESC').offset(1).first | |
103 | assert_equal false, journal_with_changes.private_notes |
|
103 | assert_equal false, journal_with_changes.private_notes | |
104 | assert_nil journal_with_changes.notes |
|
104 | assert_nil journal_with_changes.notes | |
105 | assert_equal 1, journal_with_changes.details.size |
|
105 | assert_equal 1, journal_with_changes.details.size | |
106 | assert_equal journal.created_on, journal_with_changes.created_on |
|
106 | assert_equal journal.created_on, journal_with_changes.created_on | |
107 | end |
|
107 | end | |
108 | end |
|
108 | end | |
109 |
|
109 | |||
110 | assert_difference 'Journal.count' do |
|
110 | assert_difference 'Journal.count' do | |
111 | assert_difference 'JournalDetail.count' do |
|
111 | assert_difference 'JournalDetail.count' do | |
112 | journal = Journal.generate!(:notes => '', :private_notes => true, :details => [JournalDetail.new]) |
|
112 | journal = Journal.generate!(:notes => '', :private_notes => true, :details => [JournalDetail.new]) | |
113 | journal.reload |
|
113 | journal.reload | |
114 | assert_equal false, journal.private_notes |
|
114 | assert_equal false, journal.private_notes | |
115 | assert_equal '', journal.notes |
|
115 | assert_equal '', journal.notes | |
116 | assert_equal 1, journal.details.size |
|
116 | assert_equal 1, journal.details.size | |
117 | end |
|
117 | end | |
118 | end |
|
118 | end | |
119 | end |
|
119 | end | |
120 |
|
120 | |||
121 | def test_visible_scope_for_anonymous |
|
121 | def test_visible_scope_for_anonymous | |
122 | # Anonymous user should see issues of public projects only |
|
122 | # Anonymous user should see issues of public projects only | |
123 | journals = Journal.visible(User.anonymous).to_a |
|
123 | journals = Journal.visible(User.anonymous).to_a | |
124 | assert journals.any? |
|
124 | assert journals.any? | |
125 | assert_nil journals.detect {|journal| !journal.issue.project.is_public?} |
|
125 | assert_nil journals.detect {|journal| !journal.issue.project.is_public?} | |
126 | # Anonymous user should not see issues without permission |
|
126 | # Anonymous user should not see issues without permission | |
127 | Role.anonymous.remove_permission!(:view_issues) |
|
127 | Role.anonymous.remove_permission!(:view_issues) | |
128 | journals = Journal.visible(User.anonymous).to_a |
|
128 | journals = Journal.visible(User.anonymous).to_a | |
129 | assert journals.empty? |
|
129 | assert journals.empty? | |
130 | end |
|
130 | end | |
131 |
|
131 | |||
132 | def test_visible_scope_for_user |
|
132 | def test_visible_scope_for_user | |
133 | user = User.find(9) |
|
133 | user = User.find(9) | |
134 | assert user.projects.empty? |
|
134 | assert user.projects.empty? | |
135 | # Non member user should see issues of public projects only |
|
135 | # Non member user should see issues of public projects only | |
136 | journals = Journal.visible(user).to_a |
|
136 | journals = Journal.visible(user).to_a | |
137 | assert journals.any? |
|
137 | assert journals.any? | |
138 | assert_nil journals.detect {|journal| !journal.issue.project.is_public?} |
|
138 | assert_nil journals.detect {|journal| !journal.issue.project.is_public?} | |
139 | # Non member user should not see issues without permission |
|
139 | # Non member user should not see issues without permission | |
140 | Role.non_member.remove_permission!(:view_issues) |
|
140 | Role.non_member.remove_permission!(:view_issues) | |
141 | user.reload |
|
141 | user.reload | |
142 | journals = Journal.visible(user).to_a |
|
142 | journals = Journal.visible(user).to_a | |
143 | assert journals.empty? |
|
143 | assert journals.empty? | |
144 | # User should see issues of projects for which user has view_issues permissions only |
|
144 | # User should see issues of projects for which user has view_issues permissions only | |
145 | Member.create!(:principal => user, :project_id => 1, :role_ids => [1]) |
|
145 | Member.create!(:principal => user, :project_id => 1, :role_ids => [1]) | |
146 | user.reload |
|
146 | user.reload | |
147 | journals = Journal.visible(user).to_a |
|
147 | journals = Journal.visible(user).to_a | |
148 | assert journals.any? |
|
148 | assert journals.any? | |
149 | assert_nil journals.detect {|journal| journal.issue.project_id != 1} |
|
149 | assert_nil journals.detect {|journal| journal.issue.project_id != 1} | |
150 | end |
|
150 | end | |
151 |
|
151 | |||
152 | def test_visible_scope_for_admin |
|
152 | def test_visible_scope_for_admin | |
153 | user = User.find(1) |
|
153 | user = User.find(1) | |
154 | user.members.each(&:destroy) |
|
154 | user.members.each(&:destroy) | |
155 | assert user.projects.empty? |
|
155 | assert user.projects.empty? | |
156 | journals = Journal.visible(user).to_a |
|
156 | journals = Journal.visible(user).to_a | |
157 | assert journals.any? |
|
157 | assert journals.any? | |
158 | # Admin should see issues on private projects that admin does not belong to |
|
158 | # Admin should see issues on private projects that admin does not belong to | |
159 | assert journals.detect {|journal| !journal.issue.project.is_public?} |
|
159 | assert journals.detect {|journal| !journal.issue.project.is_public?} | |
160 | end |
|
160 | end | |
161 |
|
161 | |||
162 | def test_preload_journals_details_custom_fields_should_set_custom_field_instance_variable |
|
162 | def test_preload_journals_details_custom_fields_should_set_custom_field_instance_variable | |
163 | d = JournalDetail.new(:property => 'cf', :prop_key => '2') |
|
163 | d = JournalDetail.new(:property => 'cf', :prop_key => '2') | |
164 | journals = [Journal.new(:details => [d])] |
|
164 | journals = [Journal.new(:details => [d])] | |
165 |
|
165 | |||
166 | d.expects(:instance_variable_set).with("@custom_field", CustomField.find(2)).once |
|
166 | d.expects(:instance_variable_set).with("@custom_field", CustomField.find(2)).once | |
167 | Journal.preload_journals_details_custom_fields(journals) |
|
167 | Journal.preload_journals_details_custom_fields(journals) | |
168 | end |
|
168 | end | |
169 |
|
169 | |||
170 | def test_preload_journals_details_custom_fields_with_empty_set |
|
170 | def test_preload_journals_details_custom_fields_with_empty_set | |
171 | assert_nothing_raised do |
|
171 | assert_nothing_raised do | |
172 | Journal.preload_journals_details_custom_fields([]) |
|
172 | Journal.preload_journals_details_custom_fields([]) | |
173 | end |
|
173 | end | |
174 | end |
|
174 | end | |
175 |
|
175 | |||
176 | def test_details_should_normalize_dates |
|
176 | def test_details_should_normalize_dates | |
177 | j = JournalDetail.create!(:old_value => Date.parse('2012-11-03'), :value => Date.parse('2013-01-02')) |
|
177 | j = JournalDetail.create!(:old_value => Date.parse('2012-11-03'), :value => Date.parse('2013-01-02')) | |
178 | j.reload |
|
178 | j.reload | |
179 | assert_equal '2012-11-03', j.old_value |
|
179 | assert_equal '2012-11-03', j.old_value | |
180 | assert_equal '2013-01-02', j.value |
|
180 | assert_equal '2013-01-02', j.value | |
181 | end |
|
181 | end | |
182 |
|
182 | |||
183 | def test_details_should_normalize_true_values |
|
183 | def test_details_should_normalize_true_values | |
184 | j = JournalDetail.create!(:old_value => true, :value => true) |
|
184 | j = JournalDetail.create!(:old_value => true, :value => true) | |
185 | j.reload |
|
185 | j.reload | |
186 | assert_equal '1', j.old_value |
|
186 | assert_equal '1', j.old_value | |
187 | assert_equal '1', j.value |
|
187 | assert_equal '1', j.value | |
188 | end |
|
188 | end | |
189 |
|
189 | |||
190 | def test_details_should_normalize_false_values |
|
190 | def test_details_should_normalize_false_values | |
191 | j = JournalDetail.create!(:old_value => false, :value => false) |
|
191 | j = JournalDetail.create!(:old_value => false, :value => false) | |
192 | j.reload |
|
192 | j.reload | |
193 | assert_equal '0', j.old_value |
|
193 | assert_equal '0', j.old_value | |
194 | assert_equal '0', j.value |
|
194 | assert_equal '0', j.value | |
195 | end |
|
195 | end | |
196 |
|
196 | |||
197 | def test_custom_field_should_return_custom_field_for_cf_detail |
|
197 | def test_custom_field_should_return_custom_field_for_cf_detail | |
198 | d = JournalDetail.new(:property => 'cf', :prop_key => '2') |
|
198 | d = JournalDetail.new(:property => 'cf', :prop_key => '2') | |
199 | assert_equal CustomField.find(2), d.custom_field |
|
199 | assert_equal CustomField.find(2), d.custom_field | |
200 | end |
|
200 | end | |
201 |
|
201 | |||
202 | def test_custom_field_should_return_nil_for_non_cf_detail |
|
202 | def test_custom_field_should_return_nil_for_non_cf_detail | |
203 | d = JournalDetail.new(:property => 'subject') |
|
203 | d = JournalDetail.new(:property => 'subject') | |
204 | assert_equal nil, d.custom_field |
|
204 | assert_equal nil, d.custom_field | |
205 | end |
|
205 | end | |
206 |
|
206 | |||
207 | def test_visible_details_should_include_relations_to_visible_issues_only |
|
207 | def test_visible_details_should_include_relations_to_visible_issues_only | |
208 | issue = Issue.generate! |
|
208 | issue = Issue.generate! | |
209 | visible_issue = Issue.generate! |
|
209 | visible_issue = Issue.generate! | |
210 | hidden_issue = Issue.generate!(:is_private => true) |
|
210 | hidden_issue = Issue.generate!(:is_private => true) | |
211 |
|
211 | |||
212 | journal = Journal.new |
|
212 | journal = Journal.new | |
213 | journal.details << JournalDetail.new(:property => 'relation', :prop_key => 'relates', :value => visible_issue.id) |
|
213 | journal.details << JournalDetail.new(:property => 'relation', :prop_key => 'relates', :value => visible_issue.id) | |
214 | journal.details << JournalDetail.new(:property => 'relation', :prop_key => 'relates', :value => hidden_issue.id) |
|
214 | journal.details << JournalDetail.new(:property => 'relation', :prop_key => 'relates', :value => hidden_issue.id) | |
215 |
|
215 | |||
216 | visible_details = journal.visible_details(User.anonymous) |
|
216 | visible_details = journal.visible_details(User.anonymous) | |
217 | assert_equal 1, visible_details.size |
|
217 | assert_equal 1, visible_details.size | |
218 | assert_equal visible_issue.id.to_s, visible_details.first.value.to_s |
|
218 | assert_equal visible_issue.id.to_s, visible_details.first.value.to_s | |
219 |
|
219 | |||
220 | visible_details = journal.visible_details(User.find(2)) |
|
220 | visible_details = journal.visible_details(User.find(2)) | |
221 | assert_equal 2, visible_details.size |
|
221 | assert_equal 2, visible_details.size | |
222 | end |
|
222 | end | |
223 | end |
|
223 | end |
General Comments 0
You need to be logged in to leave comments.
Login now