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 / .svn / pristine / e1 / e15233fcde4e3b590807c2f76281829a10e3c2ae.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (5.14 KB)

1 1296:038ba2d95de8 Chris
# 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 CustomFieldsControllerTest < ActionController::TestCase
21
  fixtures :custom_fields, :custom_values, :trackers, :users
22
23
  def setup
24
    @request.session[:user_id] = 1
25
  end
26
27
  def test_index
28
    get :index
29
    assert_response :success
30
    assert_template 'index'
31
  end
32
33
  def test_new
34
    custom_field_classes.each do |klass|
35
      get :new, :type => klass.name
36
      assert_response :success
37
      assert_template 'new'
38
      assert_kind_of klass, assigns(:custom_field)
39
      assert_select 'form#custom_field_form' do
40
        assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]'
41
        assert_select 'input[type=hidden][name=type][value=?]', klass.name
42
      end
43
    end
44
  end
45
46
  def test_new_issue_custom_field
47
    get :new, :type => 'IssueCustomField'
48
    assert_response :success
49
    assert_template 'new'
50
    assert_select 'form#custom_field_form' do
51
      assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' do
52
        assert_select 'option[value=user]', :text => 'User'
53
        assert_select 'option[value=version]', :text => 'Version'
54
      end
55
      assert_select 'input[type=hidden][name=type][value=IssueCustomField]'
56
    end
57
  end
58
59
  def test_new_js
60
    get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'list'}, :format => 'js'
61
    assert_response :success
62
    assert_template 'new'
63
    assert_equal 'text/javascript', response.content_type
64
65
    field = assigns(:custom_field)
66
    assert_equal 'list', field.field_format
67
  end
68
69
  def test_new_with_invalid_custom_field_class_should_render_404
70
    get :new, :type => 'UnknownCustomField'
71
    assert_response 404
72
  end
73
74
  def test_create_list_custom_field
75
    assert_difference 'CustomField.count' do
76
      post :create, :type => "IssueCustomField",
77
                 :custom_field => {:name => "test_post_new_list",
78
                                   :default_value => "",
79
                                   :min_length => "0",
80
                                   :searchable => "0",
81
                                   :regexp => "",
82
                                   :is_for_all => "1",
83
                                   :possible_values => "0.1\n0.2\n",
84
                                   :max_length => "0",
85
                                   :is_filter => "0",
86
                                   :is_required =>"0",
87
                                   :field_format => "list",
88
                                   :tracker_ids => ["1", ""]}
89
    end
90
    assert_redirected_to '/custom_fields?tab=IssueCustomField'
91
    field = IssueCustomField.find_by_name('test_post_new_list')
92
    assert_not_nil field
93
    assert_equal ["0.1", "0.2"], field.possible_values
94
    assert_equal 1, field.trackers.size
95
  end
96
97
  def test_create_with_failure
98
    assert_no_difference 'CustomField.count' do
99
      post :create, :type => "IssueCustomField", :custom_field => {:name => ''}
100
    end
101
    assert_response :success
102
    assert_template 'new'
103
  end
104
105
  def test_edit
106
    get :edit, :id => 1
107
    assert_response :success
108
    assert_template 'edit'
109
    assert_tag 'input', :attributes => {:name => 'custom_field[name]', :value => 'Database'}
110
  end
111
112
  def test_edit_invalid_custom_field_should_render_404
113
    get :edit, :id => 99
114
    assert_response 404
115
  end
116
117
  def test_update
118
    put :update, :id => 1, :custom_field => {:name => 'New name'}
119
    assert_redirected_to '/custom_fields?tab=IssueCustomField'
120
121
    field = CustomField.find(1)
122
    assert_equal 'New name', field.name
123
  end
124
125
  def test_update_with_failure
126
    put :update, :id => 1, :custom_field => {:name => ''}
127
    assert_response :success
128
    assert_template 'edit'
129
  end
130
131
  def test_destroy
132
    custom_values_count = CustomValue.count(:conditions => {:custom_field_id => 1})
133
    assert custom_values_count > 0
134
135
    assert_difference 'CustomField.count', -1 do
136
      assert_difference 'CustomValue.count', - custom_values_count do
137
        delete :destroy, :id => 1
138
      end
139
    end
140
141
    assert_redirected_to '/custom_fields?tab=IssueCustomField'
142
    assert_nil CustomField.find_by_id(1)
143
    assert_nil CustomValue.find_by_custom_field_id(1)
144
  end
145
146
  def custom_field_classes
147
    files = Dir.glob(File.join(Rails.root, 'app/models/*_custom_field.rb')).map {|f| File.basename(f).sub(/\.rb$/, '') }
148
    classes = files.map(&:classify).map(&:constantize)
149
    assert classes.size > 0
150
    classes
151
  end
152
end