Revision 1297:0a574315af3e .svn/pristine

View differences:

.svn/pristine/00/009460d580c5e7ab7c5519f32165ee2250a363d7.svn-base
1
# Redmine - project management software
2
# Copyright (C) 2006-2012  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

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

  
20
class Redmine::UnifiedDiffTest < ActiveSupport::TestCase
21
  def test_subversion_diff
22
    diff = Redmine::UnifiedDiff.new(read_diff_fixture('subversion.diff'))
23
    # number of files
24
    assert_equal 4, diff.size
25
    assert diff.detect {|file| file.file_name =~ %r{^config/settings.yml}}
26
  end
27

  
28
  def test_truncate_diff
29
    diff = Redmine::UnifiedDiff.new(read_diff_fixture('subversion.diff'), :max_lines => 20)
30
    assert_equal 2, diff.size
31
  end
32

  
33
  def test_inline_partials
34
    diff = Redmine::UnifiedDiff.new(read_diff_fixture('partials.diff'))
35
    assert_equal 1, diff.size
36
    diff = diff.first
37
    assert_equal 43, diff.size
38

  
39
    assert_equal [51, -1], diff[0].offsets
40
    assert_equal [51, -1], diff[1].offsets
41
    assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>elit</span>', diff[0].html_line
42
    assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>xx</span>', diff[1].html_line
43

  
44
    assert_nil diff[2].offsets
45
    assert_equal 'Praesent et sagittis dui. Vivamus ac diam diam', diff[2].html_line
46

  
47
    assert_equal [0, -14], diff[3].offsets
48
    assert_equal [0, -14], diff[4].offsets
49
    assert_equal '<span>Ut sed</span> auctor justo', diff[3].html_line
50
    assert_equal '<span>xxx</span> auctor justo', diff[4].html_line
51

  
52
    assert_equal [13, -19], diff[6].offsets
53
    assert_equal [13, -19], diff[7].offsets
54

  
55
    assert_equal [24, -8], diff[9].offsets
56
    assert_equal [24, -8], diff[10].offsets
57

  
58
    assert_equal [37, -1], diff[12].offsets
59
    assert_equal [37, -1], diff[13].offsets
60

  
61
    assert_equal [0, -38], diff[15].offsets
62
    assert_equal [0, -38], diff[16].offsets
63
  end
64

  
65
  def test_side_by_side_partials
66
    diff = Redmine::UnifiedDiff.new(read_diff_fixture('partials.diff'), :type => 'sbs')
67
    assert_equal 1, diff.size
68
    diff = diff.first
69
    assert_equal 32, diff.size
70

  
71
    assert_equal [51, -1], diff[0].offsets
72
    assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>elit</span>', diff[0].html_line_left
73
    assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>xx</span>', diff[0].html_line_right
74

  
75
    assert_nil diff[1].offsets
76
    assert_equal 'Praesent et sagittis dui. Vivamus ac diam diam', diff[1].html_line_left
77
    assert_equal 'Praesent et sagittis dui. Vivamus ac diam diam', diff[1].html_line_right
78

  
79
    assert_equal [0, -14], diff[2].offsets
80
    assert_equal '<span>Ut sed</span> auctor justo', diff[2].html_line_left
81
    assert_equal '<span>xxx</span> auctor justo', diff[2].html_line_right
82

  
83
    assert_equal [13, -19], diff[4].offsets
84
    assert_equal [24, -8], diff[6].offsets
85
    assert_equal [37, -1], diff[8].offsets
86
    assert_equal [0, -38], diff[10].offsets
87

  
88
  end
89

  
90
  def test_partials_with_html_entities
91
    raw = <<-DIFF
92
--- test.orig.txt Wed Feb 15 16:10:39 2012
93
+++ test.new.txt  Wed Feb 15 16:11:25 2012
94
@@ -1,5 +1,5 @@
95
 Semicolons were mysteriously appearing in code diffs in the repository
96
 
97
-void DoSomething(std::auto_ptr<MyClass> myObj)
98
+void DoSomething(const MyClass& myObj)
99
 
100
DIFF
101

  
102
    diff = Redmine::UnifiedDiff.new(raw, :type => 'sbs')
103
    assert_equal 1, diff.size
104
    assert_equal 'void DoSomething(<span>std::auto_ptr&lt;MyClass&gt;</span> myObj)', diff.first[2].html_line_left
105
    assert_equal 'void DoSomething(<span>const MyClass&amp;</span> myObj)', diff.first[2].html_line_right
106

  
107
    diff = Redmine::UnifiedDiff.new(raw, :type => 'inline')
108
    assert_equal 1, diff.size
109
    assert_equal 'void DoSomething(<span>std::auto_ptr&lt;MyClass&gt;</span> myObj)', diff.first[2].html_line
110
    assert_equal 'void DoSomething(<span>const MyClass&amp;</span> myObj)', diff.first[3].html_line
111
  end
112

  
113
  def test_line_starting_with_dashes
114
    diff = Redmine::UnifiedDiff.new(<<-DIFF
115
--- old.txt Wed Nov 11 14:24:58 2009
116
+++ new.txt Wed Nov 11 14:25:02 2009
117
@@ -1,8 +1,4 @@
118
-Lines that starts with dashes:
119
-
120
-------------------------
121
--- file.c
122
-------------------------
123
+A line that starts with dashes:
124
 
125
 and removed.
126
 
127
@@ -23,4 +19,4 @@
128
 
129
 
130
 
131
-Another chunk of change
132
+Another chunk of changes
133

  
134
DIFF
135
    )
136
    assert_equal 1, diff.size
137
  end
138

  
139
  def test_one_line_new_files
140
    diff = Redmine::UnifiedDiff.new(<<-DIFF
141
diff -r 000000000000 -r ea98b14f75f0 README1
142
--- /dev/null
143
+++ b/README1
144
@@ -0,0 +1,1 @@
145
+test1
146
diff -r 000000000000 -r ea98b14f75f0 README2
147
--- /dev/null
148
+++ b/README2
149
@@ -0,0 +1,1 @@
150
+test2
151
diff -r 000000000000 -r ea98b14f75f0 README3
152
--- /dev/null
153
+++ b/README3
154
@@ -0,0 +1,3 @@
155
+test4
156
+test5
157
+test6
158
diff -r 000000000000 -r ea98b14f75f0 README4
159
--- /dev/null
160
+++ b/README4
161
@@ -0,0 +1,3 @@
162
+test4
163
+test5
164
+test6
165
DIFF
166
    )
167
    assert_equal 4, diff.size
168
    assert_equal "README1", diff[0].file_name
169
  end
170

  
171
  def test_both_git_diff
172
    diff = Redmine::UnifiedDiff.new(<<-DIFF
173
# HG changeset patch
174
# User test
175
# Date 1348014182 -32400
176
# Node ID d1c871b8ef113df7f1c56d41e6e3bfbaff976e1f
177
# Parent  180b6605936cdc7909c5f08b59746ec1a7c99b3e
178
modify test1.txt
179

  
180
diff -r 180b6605936c -r d1c871b8ef11 test1.txt
181
--- a/test1.txt
182
+++ b/test1.txt
183
@@ -1,1 +1,1 @@
184
-test1
185
+modify test1
186
DIFF
187
    )
188
    assert_equal 1, diff.size
189
    assert_equal "test1.txt", diff[0].file_name
190
  end
191

  
192
  def test_include_a_b_slash
193
    diff = Redmine::UnifiedDiff.new(<<-DIFF
194
--- test1.txt
195
+++ b/test02.txt
196
@@ -1 +0,0 @@
197
-modify test1
198
DIFF
199
    )
200
    assert_equal 1, diff.size
201
    assert_equal "b/test02.txt", diff[0].file_name
202

  
203
    diff = Redmine::UnifiedDiff.new(<<-DIFF
204
--- a/test1.txt
205
+++ a/test02.txt
206
@@ -1 +0,0 @@
207
-modify test1
208
DIFF
209
    )
210
    assert_equal 1, diff.size
211
    assert_equal "a/test02.txt", diff[0].file_name
212

  
213
    diff = Redmine::UnifiedDiff.new(<<-DIFF
214
--- a/test1.txt
215
+++ test02.txt
216
@@ -1 +0,0 @@
217
-modify test1
218
DIFF
219
    )
220
    assert_equal 1, diff.size
221
    assert_equal "test02.txt", diff[0].file_name
222
  end
223

  
224
  private
225

  
226
  def read_diff_fixture(filename)
227
    File.new(File.join(File.dirname(__FILE__), '/../../../fixtures/diffs', filename)).read
228
  end
229
end
.svn/pristine/00/0097cb632567412c9ae3b42993114578789fd825.svn-base
1
# Redmine - project management software
2
# Copyright (C) 2006-2012  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

  
18
class VersionCustomField < CustomField
19
  def type_name
20
    :label_version_plural
21
  end
22
end
.svn/pristine/00/00dbd874dfe947c78eb0cddc2e6a0cbc57c3f815.svn-base
1
require File.expand_path('../../../test_helper', __FILE__)
2

  
3
class ApiTest::DisabledRestApiTest < ActionController::IntegrationTest
4
  fixtures :projects, :trackers, :issue_statuses, :issues,
5
           :enumerations, :users, :issue_categories,
6
           :projects_trackers,
7
           :roles,
8
           :member_roles,
9
           :members,
10
           :enabled_modules,
11
           :workflows
12

  
13
  def setup
14
    Setting.rest_api_enabled = '0'
15
    Setting.login_required = '1'
16
  end
17

  
18
  def teardown
19
    Setting.rest_api_enabled = '1'
20
    Setting.login_required = '0'
21
  end
22

  
23
  def test_with_a_valid_api_token
24
    @user = User.generate!
25
    @token = Token.create!(:user => @user, :action => 'api')
26

  
27
    get "/news.xml?key=#{@token.value}"
28
    assert_response :unauthorized
29
    assert_equal User.anonymous, User.current
30

  
31
    get "/news.json?key=#{@token.value}"
32
    assert_response :unauthorized
33
    assert_equal User.anonymous, User.current
34
  end
35

  
36
  def test_with_valid_username_password_http_authentication
37
    @user = User.generate! do |user|
38
      user.password = 'my_password'
39
    end
40

  
41
    get "/news.xml", nil, credentials(@user.login, 'my_password')
42
    assert_response :unauthorized
43
    assert_equal User.anonymous, User.current
44

  
45
    get "/news.json", nil, credentials(@user.login, 'my_password')
46
    assert_response :unauthorized
47
    assert_equal User.anonymous, User.current
48
  end
49

  
50
  def test_with_valid_token_http_authentication
51
    @user = User.generate!
52
    @token = Token.create!(:user => @user, :action => 'api')
53

  
54
    get "/news.xml", nil, credentials(@token.value, 'X')
55
    assert_response :unauthorized
56
    assert_equal User.anonymous, User.current
57

  
58
    get "/news.json", nil, credentials(@token.value, 'X')
59
    assert_response :unauthorized
60
    assert_equal User.anonymous, User.current
61
  end
62
end
.svn/pristine/00/00e064a58814bd1506484c9e41b6bc1f8553a834.svn-base
1
# Redmine - project management software
2
# Copyright (C) 2006-2012  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

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

  
20
class ApiTest::IssuesTest < ActionController::IntegrationTest
21
  fixtures :projects,
22
    :users,
23
    :roles,
24
    :members,
25
    :member_roles,
26
    :issues,
27
    :issue_statuses,
28
    :issue_relations,
29
    :versions,
30
    :trackers,
31
    :projects_trackers,
32
    :issue_categories,
33
    :enabled_modules,
34
    :enumerations,
35
    :attachments,
36
    :workflows,
37
    :custom_fields,
38
    :custom_values,
39
    :custom_fields_projects,
40
    :custom_fields_trackers,
41
    :time_entries,
42
    :journals,
43
    :journal_details,
44
    :queries,
45
    :attachments
46

  
47
  def setup
48
    Setting.rest_api_enabled = '1'
49
  end
50

  
51
  context "/issues" do
52
    # Use a private project to make sure auth is really working and not just
53
    # only showing public issues.
54
    should_allow_api_authentication(:get, "/projects/private-child/issues.xml")
55

  
56
    should "contain metadata" do
57
      get '/issues.xml'
58

  
59
      assert_tag :tag => 'issues',
60
        :attributes => {
61
          :type => 'array',
62
          :total_count => assigns(:issue_count),
63
          :limit => 25,
64
          :offset => 0
65
        }
66
    end
67

  
68
    context "with offset and limit" do
69
      should "use the params" do
70
        get '/issues.xml?offset=2&limit=3'
71

  
72
        assert_equal 3, assigns(:limit)
73
        assert_equal 2, assigns(:offset)
74
        assert_tag :tag => 'issues', :children => {:count => 3, :only => {:tag => 'issue'}}
75
      end
76
    end
77

  
78
    context "with nometa param" do
79
      should "not contain metadata" do
80
        get '/issues.xml?nometa=1'
81

  
82
        assert_tag :tag => 'issues',
83
          :attributes => {
84
            :type => 'array',
85
            :total_count => nil,
86
            :limit => nil,
87
            :offset => nil
88
          }
89
      end
90
    end
91

  
92
    context "with nometa header" do
93
      should "not contain metadata" do
94
        get '/issues.xml', {}, {'X-Redmine-Nometa' => '1'}
95

  
96
        assert_tag :tag => 'issues',
97
          :attributes => {
98
            :type => 'array',
99
            :total_count => nil,
100
            :limit => nil,
101
            :offset => nil
102
          }
103
      end
104
    end
105

  
106
    context "with relations" do
107
      should "display relations" do
108
        get '/issues.xml?include=relations'
109

  
110
        assert_response :success
111
        assert_equal 'application/xml', @response.content_type
112
        assert_tag 'relations',
113
          :parent => {:tag => 'issue', :child => {:tag => 'id', :content => '3'}},
114
          :children => {:count => 1},
115
          :child => {
116
            :tag => 'relation',
117
            :attributes => {:id => '2', :issue_id => '2', :issue_to_id => '3',
118
                            :relation_type => 'relates'}
119
          }
120
        assert_tag 'relations',
121
          :parent => {:tag => 'issue', :child => {:tag => 'id', :content => '1'}},
122
          :children => {:count => 0}
123
      end
124
    end
125

  
126
    context "with invalid query params" do
127
      should "return errors" do
128
        get '/issues.xml', {:f => ['start_date'], :op => {:start_date => '='}}
129

  
130
        assert_response :unprocessable_entity
131
        assert_equal 'application/xml', @response.content_type
132
        assert_tag 'errors', :child => {:tag => 'error', :content => "Start date can't be blank"}
133
      end
134
    end
135

  
136
    context "with custom field filter" do
137
      should "show only issues with the custom field value" do
138
        get '/issues.xml',
139
            {:set_filter => 1, :f => ['cf_1'], :op => {:cf_1 => '='},
140
             :v => {:cf_1 => ['MySQL']}}
141
        expected_ids = Issue.visible.all(
142
            :include => :custom_values,
143
            :conditions => {:custom_values => {:custom_field_id => 1, :value => 'MySQL'}}).map(&:id)
144
        assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
145
           ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
146
        end
147
      end
148
    end
149

  
150
    context "with custom field filter (shorthand method)" do
151
      should "show only issues with the custom field value" do
152
        get '/issues.xml', { :cf_1 => 'MySQL' }
153

  
154
        expected_ids = Issue.visible.all(
155
            :include => :custom_values,
156
            :conditions => {:custom_values => {:custom_field_id => 1, :value => 'MySQL'}}).map(&:id)
157

  
158
        assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
159
          ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
160
        end
161
      end
162
    end
163
  end
164

  
165
  context "/index.json" do
166
    should_allow_api_authentication(:get, "/projects/private-child/issues.json")
167
  end
168

  
169
  context "/index.xml with filter" do
170
    should "show only issues with the status_id" do
171
      get '/issues.xml?status_id=5'
172

  
173
      expected_ids = Issue.visible.all(:conditions => {:status_id => 5}).map(&:id)
174

  
175
      assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
176
         ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
177
      end
178
    end
179
  end
180

  
181
  context "/index.json with filter" do
182
    should "show only issues with the status_id" do
183
      get '/issues.json?status_id=5'
184

  
185
      json = ActiveSupport::JSON.decode(response.body)
186
      status_ids_used = json['issues'].collect {|j| j['status']['id'] }
187
      assert_equal 3, status_ids_used.length
188
      assert status_ids_used.all? {|id| id == 5 }
189
    end
190

  
191
  end
192

  
193
  # Issue 6 is on a private project
194
  context "/issues/6.xml" do
195
    should_allow_api_authentication(:get, "/issues/6.xml")
196
  end
197

  
198
  context "/issues/6.json" do
199
    should_allow_api_authentication(:get, "/issues/6.json")
200
  end
201

  
202
  context "GET /issues/:id" do
203
    context "with journals" do
204
      context ".xml" do
205
        should "display journals" do
206
          get '/issues/1.xml?include=journals'
207

  
208
          assert_tag :tag => 'issue',
209
            :child => {
210
              :tag => 'journals',
211
              :attributes => { :type => 'array' },
212
              :child => {
213
                :tag => 'journal',
214
                :attributes => { :id => '1'},
215
                :child => {
216
                  :tag => 'details',
217
                  :attributes => { :type => 'array' },
218
                  :child => {
219
                    :tag => 'detail',
220
                    :attributes => { :name => 'status_id' },
221
                    :child => {
222
                      :tag => 'old_value',
223
                      :content => '1',
224
                      :sibling => {
225
                        :tag => 'new_value',
226
                        :content => '2'
227
                      }
228
                    }
229
                  }
230
                }
231
              }
232
            }
233
        end
234
      end
235
    end
236

  
237
    context "with custom fields" do
238
      context ".xml" do
239
        should "display custom fields" do
240
          get '/issues/3.xml'
241

  
242
          assert_tag :tag => 'issue',
243
            :child => {
244
              :tag => 'custom_fields',
245
              :attributes => { :type => 'array' },
246
              :child => {
247
                :tag => 'custom_field',
248
                :attributes => { :id => '1'},
249
                :child => {
250
                  :tag => 'value',
251
                  :content => 'MySQL'
252
                }
253
              }
254
            }
255

  
256
          assert_nothing_raised do
257
            Hash.from_xml(response.body).to_xml
258
          end
259
        end
260
      end
261
    end
262

  
263
    context "with multi custom fields" do
264
      setup do
265
        field = CustomField.find(1)
266
        field.update_attribute :multiple, true
267
        issue = Issue.find(3)
268
        issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
269
        issue.save!
270
      end
271

  
272
      context ".xml" do
273
        should "display custom fields" do
274
          get '/issues/3.xml'
275
          assert_response :success
276
          assert_tag :tag => 'issue',
277
            :child => {
278
              :tag => 'custom_fields',
279
              :attributes => { :type => 'array' },
280
              :child => {
281
                :tag => 'custom_field',
282
                :attributes => { :id => '1'},
283
                :child => {
284
                  :tag => 'value',
285
                  :attributes => { :type => 'array' },
286
                  :children => { :count => 2 }
287
                }
288
              }
289
            }
290

  
291
          xml = Hash.from_xml(response.body)
292
          custom_fields = xml['issue']['custom_fields']
293
          assert_kind_of Array, custom_fields
294
          field = custom_fields.detect {|f| f['id'] == '1'}
295
          assert_kind_of Hash, field
296
          assert_equal ['MySQL', 'Oracle'], field['value'].sort
297
        end
298
      end
299

  
300
      context ".json" do
301
        should "display custom fields" do
302
          get '/issues/3.json'
303
          assert_response :success
304
          json = ActiveSupport::JSON.decode(response.body)
305
          custom_fields = json['issue']['custom_fields']
306
          assert_kind_of Array, custom_fields
307
          field = custom_fields.detect {|f| f['id'] == 1}
308
          assert_kind_of Hash, field
309
          assert_equal ['MySQL', 'Oracle'], field['value'].sort
310
        end
311
      end
312
    end
313

  
314
    context "with empty value for multi custom field" do
315
      setup do
316
        field = CustomField.find(1)
317
        field.update_attribute :multiple, true
318
        issue = Issue.find(3)
319
        issue.custom_field_values = {1 => ['']}
320
        issue.save!
321
      end
322

  
323
      context ".xml" do
324
        should "display custom fields" do
325
          get '/issues/3.xml'
326
          assert_response :success
327
          assert_tag :tag => 'issue',
328
            :child => {
329
              :tag => 'custom_fields',
330
              :attributes => { :type => 'array' },
331
              :child => {
332
                :tag => 'custom_field',
333
                :attributes => { :id => '1'},
334
                :child => {
335
                  :tag => 'value',
336
                  :attributes => { :type => 'array' },
337
                  :children => { :count => 0 }
338
                }
339
              }
340
            }
341

  
342
          xml = Hash.from_xml(response.body)
343
          custom_fields = xml['issue']['custom_fields']
344
          assert_kind_of Array, custom_fields
345
          field = custom_fields.detect {|f| f['id'] == '1'}
346
          assert_kind_of Hash, field
347
          assert_equal [], field['value']
348
        end
349
      end
350

  
351
      context ".json" do
352
        should "display custom fields" do
353
          get '/issues/3.json'
354
          assert_response :success
355
          json = ActiveSupport::JSON.decode(response.body)
356
          custom_fields = json['issue']['custom_fields']
357
          assert_kind_of Array, custom_fields
358
          field = custom_fields.detect {|f| f['id'] == 1}
359
          assert_kind_of Hash, field
360
          assert_equal [], field['value'].sort
361
        end
362
      end
363
    end
364

  
365
    context "with attachments" do
366
      context ".xml" do
367
        should "display attachments" do
368
          get '/issues/3.xml?include=attachments'
369

  
370
          assert_tag :tag => 'issue',
371
            :child => {
372
              :tag => 'attachments',
373
              :children => {:count => 5},
374
              :child => {
375
                :tag => 'attachment',
376
                :child => {
377
                  :tag => 'filename',
378
                  :content => 'source.rb',
379
                  :sibling => {
380
                    :tag => 'content_url',
381
                    :content => 'http://www.example.com/attachments/download/4/source.rb'
382
                  }
383
                }
384
              }
385
            }
386
        end
387
      end
388
    end
389

  
390
    context "with subtasks" do
391
      setup do
392
        @c1 = Issue.create!(
393
                :status_id => 1, :subject => "child c1",
394
                :tracker_id => 1, :project_id => 1, :author_id => 1,
395
                :parent_issue_id => 1
396
              )
397
        @c2 = Issue.create!(
398
                :status_id => 1, :subject => "child c2",
399
                :tracker_id => 1, :project_id => 1, :author_id => 1,
400
                :parent_issue_id => 1
401
              )
402
        @c3 = Issue.create!(
403
                :status_id => 1, :subject => "child c3",
404
                :tracker_id => 1, :project_id => 1, :author_id => 1,
405
                :parent_issue_id => @c1.id
406
              )
407
      end
408

  
409
      context ".xml" do
410
        should "display children" do
411
          get '/issues/1.xml?include=children'
412

  
413
          assert_tag :tag => 'issue',
414
            :child => {
415
              :tag => 'children',
416
              :children => {:count => 2},
417
              :child => {
418
                :tag => 'issue',
419
                :attributes => {:id => @c1.id.to_s},
420
                :child => {
421
                  :tag => 'subject',
422
                  :content => 'child c1',
423
                  :sibling => {
424
                    :tag => 'children',
425
                    :children => {:count => 1},
426
                    :child => {
427
                      :tag => 'issue',
428
                      :attributes => {:id => @c3.id.to_s}
429
                    }
430
                  }
431
                }
432
              }
433
            }
434
        end
435

  
436
        context ".json" do
437
          should "display children" do
438
            get '/issues/1.json?include=children'
439

  
440
            json = ActiveSupport::JSON.decode(response.body)
441
            assert_equal([
442
              {
443
                'id' => @c1.id, 'subject' => 'child c1', 'tracker' => {'id' => 1, 'name' => 'Bug'},
444
                'children' => [{'id' => @c3.id, 'subject' => 'child c3',
445
                                'tracker' => {'id' => 1, 'name' => 'Bug'} }]
446
              },
447
              { 'id' => @c2.id, 'subject' => 'child c2', 'tracker' => {'id' => 1, 'name' => 'Bug'} }
448
              ],
449
              json['issue']['children'])
450
          end
451
        end
452
      end
453
    end
454
  end
455

  
456
  context "POST /issues.xml" do
457
    should_allow_api_authentication(
458
      :post,
459
      '/issues.xml',
460
      {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}},
461
      {:success_code => :created}
462
    )
463
    should "create an issue with the attributes" do
464
      assert_difference('Issue.count') do
465
        post '/issues.xml',
466
             {:issue => {:project_id => 1, :subject => 'API test',
467
              :tracker_id => 2, :status_id => 3}}, credentials('jsmith')
468
      end
469
      issue = Issue.first(:order => 'id DESC')
470
      assert_equal 1, issue.project_id
471
      assert_equal 2, issue.tracker_id
472
      assert_equal 3, issue.status_id
473
      assert_equal 'API test', issue.subject
474

  
475
      assert_response :created
476
      assert_equal 'application/xml', @response.content_type
477
      assert_tag 'issue', :child => {:tag => 'id', :content => issue.id.to_s}
478
    end
479
  end
480

  
481
  context "POST /issues.xml with failure" do
482
    should "have an errors tag" do
483
      assert_no_difference('Issue.count') do
484
        post '/issues.xml', {:issue => {:project_id => 1}}, credentials('jsmith')
485
      end
486

  
487
      assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
488
    end
489
  end
490

  
491
  context "POST /issues.json" do
492
    should_allow_api_authentication(:post,
493
                                    '/issues.json',
494
                                    {:issue => {:project_id => 1, :subject => 'API test',
495
                                     :tracker_id => 2, :status_id => 3}},
496
                                    {:success_code => :created})
497

  
498
    should "create an issue with the attributes" do
499
      assert_difference('Issue.count') do
500
        post '/issues.json',
501
             {:issue => {:project_id => 1, :subject => 'API test',
502
                         :tracker_id => 2, :status_id => 3}},
503
             credentials('jsmith')
504
      end
505

  
506
      issue = Issue.first(:order => 'id DESC')
507
      assert_equal 1, issue.project_id
508
      assert_equal 2, issue.tracker_id
509
      assert_equal 3, issue.status_id
510
      assert_equal 'API test', issue.subject
511
    end
512

  
513
  end
514

  
515
  context "POST /issues.json with failure" do
516
    should "have an errors element" do
517
      assert_no_difference('Issue.count') do
518
        post '/issues.json', {:issue => {:project_id => 1}}, credentials('jsmith')
519
      end
520

  
521
      json = ActiveSupport::JSON.decode(response.body)
522
      assert json['errors'].include?("Subject can't be blank")
523
    end
524
  end
525

  
526
  # Issue 6 is on a private project
527
  context "PUT /issues/6.xml" do
528
    setup do
529
      @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}}
530
    end
531

  
532
    should_allow_api_authentication(:put,
533
                                    '/issues/6.xml',
534
                                    {:issue => {:subject => 'API update', :notes => 'A new note'}},
535
                                    {:success_code => :ok})
536

  
537
    should "not create a new issue" do
538
      assert_no_difference('Issue.count') do
539
        put '/issues/6.xml', @parameters, credentials('jsmith')
540
      end
541
    end
542

  
543
    should "create a new journal" do
544
      assert_difference('Journal.count') do
545
        put '/issues/6.xml', @parameters, credentials('jsmith')
546
      end
547
    end
548

  
549
    should "add the note to the journal" do
550
      put '/issues/6.xml', @parameters, credentials('jsmith')
551

  
552
      journal = Journal.last
553
      assert_equal "A new note", journal.notes
554
    end
555

  
556
    should "update the issue" do
557
      put '/issues/6.xml', @parameters, credentials('jsmith')
558

  
559
      issue = Issue.find(6)
560
      assert_equal "API update", issue.subject
561
    end
562

  
563
  end
564

  
565
  context "PUT /issues/3.xml with custom fields" do
566
    setup do
567
      @parameters = {
568
        :issue => {:custom_fields => [{'id' => '1', 'value' => 'PostgreSQL' },
569
        {'id' => '2', 'value' => '150'}]}
570
      }
571
    end
572

  
573
    should "update custom fields" do
574
      assert_no_difference('Issue.count') do
575
        put '/issues/3.xml', @parameters, credentials('jsmith')
576
      end
577

  
578
      issue = Issue.find(3)
579
      assert_equal '150', issue.custom_value_for(2).value
580
      assert_equal 'PostgreSQL', issue.custom_value_for(1).value
581
    end
582
  end
583

  
584
  context "PUT /issues/3.xml with multi custom fields" do
585
    setup do
586
      field = CustomField.find(1)
587
      field.update_attribute :multiple, true
588
      @parameters = {
589
        :issue => {:custom_fields => [{'id' => '1', 'value' => ['MySQL', 'PostgreSQL'] },
590
        {'id' => '2', 'value' => '150'}]}
591
      }
592
    end
593

  
594
    should "update custom fields" do
595
      assert_no_difference('Issue.count') do
596
        put '/issues/3.xml', @parameters, credentials('jsmith')
597
      end
598

  
599
      issue = Issue.find(3)
600
      assert_equal '150', issue.custom_value_for(2).value
601
      assert_equal ['MySQL', 'PostgreSQL'], issue.custom_field_value(1).sort
602
    end
603
  end
604

  
605
  context "PUT /issues/3.xml with project change" do
606
    setup do
607
      @parameters = {:issue => {:project_id => 2, :subject => 'Project changed'}}
608
    end
609

  
610
    should "update project" do
611
      assert_no_difference('Issue.count') do
612
        put '/issues/3.xml', @parameters, credentials('jsmith')
613
      end
614

  
615
      issue = Issue.find(3)
616
      assert_equal 2, issue.project_id
617
      assert_equal 'Project changed', issue.subject
618
    end
619
  end
620

  
621
  context "PUT /issues/6.xml with failed update" do
622
    setup do
623
      @parameters = {:issue => {:subject => ''}}
624
    end
625

  
626
    should "not create a new issue" do
627
      assert_no_difference('Issue.count') do
628
        put '/issues/6.xml', @parameters, credentials('jsmith')
629
      end
630
    end
631

  
632
    should "not create a new journal" do
633
      assert_no_difference('Journal.count') do
634
        put '/issues/6.xml', @parameters, credentials('jsmith')
635
      end
636
    end
637

  
638
    should "have an errors tag" do
639
      put '/issues/6.xml', @parameters, credentials('jsmith')
640

  
641
      assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
642
    end
643
  end
644

  
645
  context "PUT /issues/6.json" do
646
    setup do
647
      @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}}
648
    end
649

  
650
    should_allow_api_authentication(:put,
651
                                    '/issues/6.json',
652
                                    {:issue => {:subject => 'API update', :notes => 'A new note'}},
653
                                    {:success_code => :ok})
654

  
655
    should "update the issue" do
656
      assert_no_difference('Issue.count') do
657
        assert_difference('Journal.count') do
658
          put '/issues/6.json', @parameters, credentials('jsmith')
659

  
660
          assert_response :ok
661
          assert_equal '', response.body
662
        end
663
      end
664

  
665
      issue = Issue.find(6)
666
      assert_equal "API update", issue.subject
667
      journal = Journal.last
668
      assert_equal "A new note", journal.notes
669
    end
670
  end
671

  
672
  context "PUT /issues/6.json with failed update" do
673
    should "return errors" do
674
      assert_no_difference('Issue.count') do
675
        assert_no_difference('Journal.count') do
676
          put '/issues/6.json', {:issue => {:subject => ''}}, credentials('jsmith')
677

  
678
          assert_response :unprocessable_entity
679
        end
680
      end
681

  
682
      json = ActiveSupport::JSON.decode(response.body)
683
      assert json['errors'].include?("Subject can't be blank")
684
    end
685
  end
686

  
687
  context "DELETE /issues/1.xml" do
688
    should_allow_api_authentication(:delete,
689
                                    '/issues/6.xml',
690
                                    {},
691
                                    {:success_code => :ok})
692

  
693
    should "delete the issue" do
694
      assert_difference('Issue.count', -1) do
695
        delete '/issues/6.xml', {}, credentials('jsmith')
696

  
697
        assert_response :ok
698
        assert_equal '', response.body
699
      end
700

  
701
      assert_nil Issue.find_by_id(6)
702
    end
703
  end
704

  
705
  context "DELETE /issues/1.json" do
706
    should_allow_api_authentication(:delete,
707
                                    '/issues/6.json',
708
                                    {},
709
                                    {:success_code => :ok})
710

  
711
    should "delete the issue" do
712
      assert_difference('Issue.count', -1) do
713
        delete '/issues/6.json', {}, credentials('jsmith')
714

  
715
        assert_response :ok
716
        assert_equal '', response.body
717
      end
718

  
719
      assert_nil Issue.find_by_id(6)
720
    end
721
  end
722

  
723
  def test_create_issue_with_uploaded_file
724
    set_tmp_attachments_directory
725
    # upload the file
726
    assert_difference 'Attachment.count' do
727
      post '/uploads.xml', 'test_create_with_upload',
728
           {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
729
      assert_response :created
730
    end
731
    xml = Hash.from_xml(response.body)
732
    token = xml['upload']['token']
733
    attachment = Attachment.first(:order => 'id DESC')
734

  
735
    # create the issue with the upload's token
736
    assert_difference 'Issue.count' do
737
      post '/issues.xml',
738
           {:issue => {:project_id => 1, :subject => 'Uploaded file',
739
                       :uploads => [{:token => token, :filename => 'test.txt',
740
                                     :content_type => 'text/plain'}]}},
741
           credentials('jsmith')
742
      assert_response :created
743
    end
744
    issue = Issue.first(:order => 'id DESC')
745
    assert_equal 1, issue.attachments.count
746
    assert_equal attachment, issue.attachments.first
747

  
748
    attachment.reload
749
    assert_equal 'test.txt', attachment.filename
750
    assert_equal 'text/plain', attachment.content_type
751
    assert_equal 'test_create_with_upload'.size, attachment.filesize
752
    assert_equal 2, attachment.author_id
753

  
754
    # get the issue with its attachments
755
    get "/issues/#{issue.id}.xml", :include => 'attachments'
756
    assert_response :success
757
    xml = Hash.from_xml(response.body)
758
    attachments = xml['issue']['attachments']
759
    assert_kind_of Array, attachments
760
    assert_equal 1, attachments.size
761
    url = attachments.first['content_url']
762
    assert_not_nil url
763

  
764
    # download the attachment
765
    get url
766
    assert_response :success
767
  end
768

  
769
  def test_update_issue_with_uploaded_file
770
    set_tmp_attachments_directory
771
    # upload the file
772
    assert_difference 'Attachment.count' do
773
      post '/uploads.xml', 'test_upload_with_upload',
774
           {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
775
      assert_response :created
776
    end
777
    xml = Hash.from_xml(response.body)
778
    token = xml['upload']['token']
779
    attachment = Attachment.first(:order => 'id DESC')
780

  
781
    # update the issue with the upload's token
782
    assert_difference 'Journal.count' do
783
      put '/issues/1.xml',
784
          {:issue => {:notes => 'Attachment added',
785
                      :uploads => [{:token => token, :filename => 'test.txt',
786
                                    :content_type => 'text/plain'}]}},
787
          credentials('jsmith')
788
      assert_response :ok
789
      assert_equal '', @response.body
790
    end
791

  
792
    issue = Issue.find(1)
793
    assert_include attachment, issue.attachments
794
  end
795
end
.svn/pristine/01/012acdb93702d840e809c52a4f350ace647a5f34.svn-base
1
# encoding: utf-8
2
#
3
# Redmine - project management software
4
# Copyright (C) 2006-2012  Jean-Philippe Lang
5
#
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU General Public License
8
# as published by the Free Software Foundation; either version 2
9
# of the License, or (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19

  
20
require 'forwardable'
21
require 'cgi'
22

  
23
module ApplicationHelper
24
  include Redmine::WikiFormatting::Macros::Definitions
25
  include Redmine::I18n
26
  include GravatarHelper::PublicMethods
27

  
28
  extend Forwardable
29
  def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter
30

  
31
  # Return true if user is authorized for controller/action, otherwise false
32
  def authorize_for(controller, action)
33
    User.current.allowed_to?({:controller => controller, :action => action}, @project)
34
  end
35

  
36
  # Display a link if user is authorized
37
  #
38
  # @param [String] name Anchor text (passed to link_to)
39
  # @param [Hash] options Hash params. This will checked by authorize_for to see if the user is authorized
40
  # @param [optional, Hash] html_options Options passed to link_to
41
  # @param [optional, Hash] parameters_for_method_reference Extra parameters for link_to
42
  def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
43
    link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
44
  end
45

  
46
  # Displays a link to user's account page if active
47
  def link_to_user(user, options={})
48
    if user.is_a?(User)
49
      name = h(user.name(options[:format]))
50
      if user.active? || (User.current.admin? && user.logged?)
51
        link_to name, user_path(user), :class => user.css_classes
52
      else
53
        name
54
      end
55
    else
56
      h(user.to_s)
57
    end
58
  end
59

  
60
  # Displays a link to +issue+ with its subject.
61
  # Examples:
62
  #
63
  #   link_to_issue(issue)                        # => Defect #6: This is the subject
64
  #   link_to_issue(issue, :truncate => 6)        # => Defect #6: This i...
65
  #   link_to_issue(issue, :subject => false)     # => Defect #6
66
  #   link_to_issue(issue, :project => true)      # => Foo - Defect #6
67
  #   link_to_issue(issue, :subject => false, :tracker => false)     # => #6
68
  #
69
  def link_to_issue(issue, options={})
70
    title = nil
71
    subject = nil
72
    text = options[:tracker] == false ? "##{issue.id}" : "#{issue.tracker} ##{issue.id}"
73
    if options[:subject] == false
74
      title = truncate(issue.subject, :length => 60)
75
    else
76
      subject = issue.subject
77
      if options[:truncate]
78
        subject = truncate(subject, :length => options[:truncate])
79
      end
80
    end
81
    s = link_to text, issue_path(issue), :class => issue.css_classes, :title => title
82
    s << h(": #{subject}") if subject
83
    s = h("#{issue.project} - ") + s if options[:project]
84
    s
85
  end
86

  
87
  # Generates a link to an attachment.
88
  # Options:
89
  # * :text - Link text (default to attachment filename)
90
  # * :download - Force download (default: false)
91
  def link_to_attachment(attachment, options={})
92
    text = options.delete(:text) || attachment.filename
93
    action = options.delete(:download) ? 'download' : 'show'
94
    opt_only_path = {}
95
    opt_only_path[:only_path] = (options[:only_path] == false ? false : true)
96
    options.delete(:only_path)
97
    link_to(h(text),
98
           {:controller => 'attachments', :action => action,
99
            :id => attachment, :filename => attachment.filename}.merge(opt_only_path),
100
           options)
101
  end
102

  
103
  # Generates a link to a SCM revision
104
  # Options:
105
  # * :text - Link text (default to the formatted revision)
106
  def link_to_revision(revision, repository, options={})
107
    if repository.is_a?(Project)
108
      repository = repository.repository
109
    end
110
    text = options.delete(:text) || format_revision(revision)
111
    rev = revision.respond_to?(:identifier) ? revision.identifier : revision
112
    link_to(
113
        h(text),
114
        {:controller => 'repositories', :action => 'revision', :id => repository.project, :repository_id => repository.identifier_param, :rev => rev},
115
        :title => l(:label_revision_id, format_revision(revision))
116
      )
117
  end
118

  
119
  # Generates a link to a message
120
  def link_to_message(message, options={}, html_options = nil)
121
    link_to(
122
      h(truncate(message.subject, :length => 60)),
123
      { :controller => 'messages', :action => 'show',
124
        :board_id => message.board_id,
125
        :id => (message.parent_id || message.id),
126
        :r => (message.parent_id && message.id),
127
        :anchor => (message.parent_id ? "message-#{message.id}" : nil)
128
      }.merge(options),
129
      html_options
130
    )
131
  end
132

  
133
  # Generates a link to a project if active
134
  # Examples:
135
  #
136
  #   link_to_project(project)                          # => link to the specified project overview
137
  #   link_to_project(project, :action=>'settings')     # => link to project settings
138
  #   link_to_project(project, {:only_path => false}, :class => "project") # => 3rd arg adds html options
139
  #   link_to_project(project, {}, :class => "project") # => html options with default url (project overview)
140
  #
141
  def link_to_project(project, options={}, html_options = nil)
142
    if project.archived?
143
      h(project)
144
    else
145
      url = {:controller => 'projects', :action => 'show', :id => project}.merge(options)
146
      link_to(h(project), url, html_options)
147
    end
148
  end
149

  
150
  def wiki_page_path(page, options={})
151
    url_for({:controller => 'wiki', :action => 'show', :project_id => page.project, :id => page.title}.merge(options))
152
  end
153

  
154
  def thumbnail_tag(attachment)
155
    link_to image_tag(url_for(:controller => 'attachments', :action => 'thumbnail', :id => attachment)),
156
      {:controller => 'attachments', :action => 'show', :id => attachment, :filename => attachment.filename},
157
      :title => attachment.filename
158
  end
159

  
160
  def toggle_link(name, id, options={})
161
    onclick = "$('##{id}').toggle(); "
162
    onclick << (options[:focus] ? "$('##{options[:focus]}').focus(); " : "this.blur(); ")
163
    onclick << "return false;"
164
    link_to(name, "#", :onclick => onclick)
165
  end
166

  
167
  def image_to_function(name, function, html_options = {})
168
    html_options.symbolize_keys!
169
    tag(:input, html_options.merge({
170
        :type => "image", :src => image_path(name),
171
        :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
172
        }))
173
  end
174

  
175
  def format_activity_title(text)
176
    h(truncate_single_line(text, :length => 100))
177
  end
178

  
179
  def format_activity_day(date)
180
    date == User.current.today ? l(:label_today).titleize : format_date(date)
181
  end
182

  
183
  def format_activity_description(text)
184
    h(truncate(text.to_s, :length => 120).gsub(%r{[\r\n]*<(pre|code)>.*$}m, '...')
185
       ).gsub(/[\r\n]+/, "<br />").html_safe
186
  end
187

  
188
  def format_version_name(version)
189
    if version.project == @project
190
    	h(version)
191
    else
192
      h("#{version.project} - #{version}")
193
    end
194
  end
195

  
196
  def due_date_distance_in_words(date)
197
    if date
198
      l((date < Date.today ? :label_roadmap_overdue : :label_roadmap_due_in), distance_of_date_in_words(Date.today, date))
199
    end
200
  end
201

  
202
  # Renders a tree of projects as a nested set of unordered lists
203
  # The given collection may be a subset of the whole project tree
204
  # (eg. some intermediate nodes are private and can not be seen)
205
  def render_project_nested_lists(projects)
206
    s = ''
207
    if projects.any?
208
      ancestors = []
209
      original_project = @project
210
      projects.sort_by(&:lft).each do |project|
211
        # set the project environment to please macros.
212
        @project = project
213
        if (ancestors.empty? || project.is_descendant_of?(ancestors.last))
214
          s << "<ul class='projects #{ ancestors.empty? ? 'root' : nil}'>\n"
215
        else
216
          ancestors.pop
217
          s << "</li>"
218
          while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
219
            ancestors.pop
220
            s << "</ul></li>\n"
221
          end
222
        end
223
        classes = (ancestors.empty? ? 'root' : 'child')
224
        s << "<li class='#{classes}'><div class='#{classes}'>"
225
        s << h(block_given? ? yield(project) : project.name)
226
        s << "</div>\n"
227
        ancestors << project
228
      end
229
      s << ("</li></ul>\n" * ancestors.size)
230
      @project = original_project
231
    end
232
    s.html_safe
233
  end
234

  
235
  def render_page_hierarchy(pages, node=nil, options={})
236
    content = ''
237
    if pages[node]
238
      content << "<ul class=\"pages-hierarchy\">\n"
239
      pages[node].each do |page|
240
        content << "<li>"
241
        content << link_to(h(page.pretty_title), {:controller => 'wiki', :action => 'show', :project_id => page.project, :id => page.title, :version => nil},
242
                           :title => (options[:timestamp] && page.updated_on ? l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)) : nil))
243
        content << "\n" + render_page_hierarchy(pages, page.id, options) if pages[page.id]
244
        content << "</li>\n"
245
      end
246
      content << "</ul>\n"
247
    end
248
    content.html_safe
249
  end
250

  
251
  # Renders flash messages
252
  def render_flash_messages
253
    s = ''
254
    flash.each do |k,v|
255
      s << content_tag('div', v.html_safe, :class => "flash #{k}", :id => "flash_#{k}")
256
    end
257
    s.html_safe
258
  end
259

  
260
  # Renders tabs and their content
261
  def render_tabs(tabs)
262
    if tabs.any?
263
      render :partial => 'common/tabs', :locals => {:tabs => tabs}
264
    else
265
      content_tag 'p', l(:label_no_data), :class => "nodata"
266
    end
267
  end
268

  
269
  # Renders the project quick-jump box
270
  def render_project_jump_box
271
    return unless User.current.logged?
272
    projects = User.current.memberships.collect(&:project).compact.select(&:active?).uniq
273
    if projects.any?
274
      options =
275
        ("<option value=''>#{ l(:label_jump_to_a_project) }</option>" +
276
         '<option value="" disabled="disabled">---</option>').html_safe
277

  
278
      options << project_tree_options_for_select(projects, :selected => @project) do |p|
279
        { :value => project_path(:id => p, :jump => current_menu_item) }
280
      end
281

  
282
      select_tag('project_quick_jump_box', options, :onchange => 'if (this.value != \'\') { window.location = this.value; }')
283
    end
284
  end
285

  
286
  def project_tree_options_for_select(projects, options = {})
287
    s = ''
288
    project_tree(projects) do |project, level|
289
      name_prefix = (level > 0 ? '&nbsp;' * 2 * level + '&#187; ' : '').html_safe
290
      tag_options = {:value => project.id}
291
      if project == options[:selected] || (options[:selected].respond_to?(:include?) && options[:selected].include?(project))
292
        tag_options[:selected] = 'selected'
293
      else
294
        tag_options[:selected] = nil
295
      end
296
      tag_options.merge!(yield(project)) if block_given?
297
      s << content_tag('option', name_prefix + h(project), tag_options)
298
    end
299
    s.html_safe
300
  end
301

  
302
  # Yields the given block for each project with its level in the tree
303
  #
304
  # Wrapper for Project#project_tree
305
  def project_tree(projects, &block)
306
    Project.project_tree(projects, &block)
307
  end
308

  
309
  def principals_check_box_tags(name, principals)
310
    s = ''
311
    principals.sort.each do |principal|
312
      s << "<label>#{ check_box_tag name, principal.id, false } #{h principal}</label>\n"
313
    end
314
    s.html_safe
315
  end
316

  
317
  # Returns a string for users/groups option tags
318
  def principals_options_for_select(collection, selected=nil)
319
    s = ''
320
    if collection.include?(User.current)
321
      s << content_tag('option', "<< #{l(:label_me)} >>", :value => User.current.id)
322
    end
323
    groups = ''
324
    collection.sort.each do |element|
325
      selected_attribute = ' selected="selected"' if option_value_selected?(element, selected)
326
      (element.is_a?(Group) ? groups : s) << %(<option value="#{element.id}"#{selected_attribute}>#{h element.name}</option>)
327
    end
328
    unless groups.empty?
329
      s << %(<optgroup label="#{h(l(:label_group_plural))}">#{groups}</optgroup>)
330
    end
331
    s.html_safe
332
  end
333

  
334
  # Options for the new membership projects combo-box
335
  def options_for_membership_project_select(principal, projects)
336
    options = content_tag('option', "--- #{l(:actionview_instancetag_blank_option)} ---")
337
    options << project_tree_options_for_select(projects) do |p|
338
      {:disabled => principal.projects.include?(p)}
339
    end
340
    options
341
  end
342

  
343
  # Truncates and returns the string as a single line
344
  def truncate_single_line(string, *args)
345
    truncate(string.to_s, *args).gsub(%r{[\r\n]+}m, ' ')
346
  end
347

  
348
  # Truncates at line break after 250 characters or options[:length]
349
  def truncate_lines(string, options={})
350
    length = options[:length] || 250
351
    if string.to_s =~ /\A(.{#{length}}.*?)$/m
352
      "#{$1}..."
353
    else
354
      string
355
    end
356
  end
357

  
358
  def anchor(text)
359
    text.to_s.gsub(' ', '_')
360
  end
361

  
362
  def html_hours(text)
363
    text.gsub(%r{(\d+)\.(\d+)}, '<span class="hours hours-int">\1</span><span class="hours hours-dec">.\2</span>').html_safe
364
  end
365

  
366
  def authoring(created, author, options={})
367
    l(options[:label] || :label_added_time_by, :author => link_to_user(author), :age => time_tag(created)).html_safe
368
  end
369

  
370
  def time_tag(time)
371
    text = distance_of_time_in_words(Time.now, time)
372
    if @project
373
      link_to(text, {:controller => 'activities', :action => 'index', :id => @project, :from => User.current.time_to_date(time)}, :title => format_time(time))
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff