##// END OF EJS Templates
Added unit tests for IssuesHelper#show_detail...
Eric Davis -
r3438:1a73f8fa0f46
parent child
Show More
@@ -0,0 +1,159
1 # Redmine - project management software
2 # Copyright (C) 2006-2010 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.dirname(__FILE__) + '/../../test_helper'
19
20 class IssuesHelperTest < HelperTestCase
21 include ApplicationHelper
22 include IssuesHelper
23
24 include ActionController::Assertions::SelectorAssertions
25 fixtures :all
26
27 # Used by assert_select
28 def html_document
29 HTML::Document.new(@response.body)
30 end
31
32 def setup
33 super
34 set_language_if_valid('en')
35 User.current = nil
36 @response = ActionController::TestResponse.new
37 end
38
39 def controller
40 @controller ||= IssuesController.new
41 end
42
43 def request
44 @request ||= ActionController::TestRequest.new
45 end
46
47 context "IssuesHelper#show_detail" do
48 context "with no_html" do
49 should 'show a changing attribute' do
50 @detail = JournalDetail.generate!(:property => 'attr', :old_value => '40', :value => '100', :prop_key => 'done_ratio')
51 assert_equal "% Done changed from 40 to 100", show_detail(@detail, true)
52 end
53
54 should 'show a new attribute' do
55 @detail = JournalDetail.generate!(:property => 'attr', :old_value => nil, :value => '100', :prop_key => 'done_ratio')
56 assert_equal "% Done set to 100", show_detail(@detail, true)
57 end
58
59 should 'show a deleted attribute' do
60 @detail = JournalDetail.generate!(:property => 'attr', :old_value => '50', :value => nil, :prop_key => 'done_ratio')
61 assert_equal "% Done deleted (50)", show_detail(@detail, true)
62 end
63 end
64
65 context "with html" do
66 should 'show a changing attribute with HTML highlights' do
67 @detail = JournalDetail.generate!(:property => 'attr', :old_value => '40', :value => '100', :prop_key => 'done_ratio')
68 @response.body = show_detail(@detail, false)
69
70 assert_select 'strong', :text => '% Done'
71 assert_select 'i', :text => '40'
72 assert_select 'i', :text => '100'
73 end
74
75 should 'show a new attribute with HTML highlights' do
76 @detail = JournalDetail.generate!(:property => 'attr', :old_value => nil, :value => '100', :prop_key => 'done_ratio')
77 @response.body = show_detail(@detail, false)
78
79 assert_select 'strong', :text => '% Done'
80 assert_select 'i', :text => '100'
81 end
82
83 should 'show a deleted attribute with HTML highlights' do
84 @detail = JournalDetail.generate!(:property => 'attr', :old_value => '50', :value => nil, :prop_key => 'done_ratio')
85 @response.body = show_detail(@detail, false)
86
87 assert_select 'strong', :text => '% Done'
88 assert_select 'strike' do
89 assert_select 'i', :text => '50'
90 end
91 end
92 end
93
94 context "with a start_date attribute" do
95 should "format the current date" do
96 @detail = JournalDetail.generate!(:property => 'attr', :old_value => '2010-01-01', :value => '2010-01-31', :prop_key => 'start_date')
97 assert_match "01/31/2010", show_detail(@detail, true)
98 end
99
100 should "format the old date" do
101 @detail = JournalDetail.generate!(:property => 'attr', :old_value => '2010-01-01', :value => '2010-01-31', :prop_key => 'start_date')
102 assert_match "01/01/2010", show_detail(@detail, true)
103 end
104 end
105
106 context "with a due_date attribute" do
107 should "format the current date" do
108 @detail = JournalDetail.generate!(:property => 'attr', :old_value => '2010-01-01', :value => '2010-01-31', :prop_key => 'due_date')
109 assert_match "01/31/2010", show_detail(@detail, true)
110 end
111
112 should "format the old date" do
113 @detail = JournalDetail.generate!(:property => 'attr', :old_value => '2010-01-01', :value => '2010-01-31', :prop_key => 'due_date')
114 assert_match "01/01/2010", show_detail(@detail, true)
115 end
116 end
117
118 context "with a project attribute" do
119 should_show_the_old_and_new_values_for('project_id', Project)
120 end
121
122 context "with a issue status attribute" do
123 should_show_the_old_and_new_values_for('status_id', IssueStatus)
124 end
125
126 context "with a tracker attribute" do
127 should_show_the_old_and_new_values_for('tracker_id', Tracker)
128 end
129
130 context "with a assigned to attribute" do
131 should_show_the_old_and_new_values_for('assigned_to_id', User)
132 end
133
134 context "with a priority attribute" do
135 should_show_the_old_and_new_values_for('priority_id', IssuePriority) do
136 @old_value = IssuePriority.generate!(:type => 'IssuePriority')
137 @new_value = IssuePriority.generate!(:type => 'IssuePriority')
138 end
139 end
140
141 context "with a category attribute" do
142 should_show_the_old_and_new_values_for('category_id', IssueCategory)
143 end
144
145 context "with a fixed version attribute" do
146 should_show_the_old_and_new_values_for('fixed_version_id', Version)
147 end
148
149 context "with a estimated hours attribute" do
150 should "format the time into two decimal places"
151 should "format the old time into two decimal places"
152 end
153
154 should "test custom fields"
155 should "test attachments"
156
157 end
158
159 end
@@ -1,114 +1,145
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
2 # Copyright (C) 2006 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 ENV["RAILS_ENV"] ||= "test"
18 ENV["RAILS_ENV"] ||= "test"
19 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
19 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
20 require 'test_help'
20 require 'test_help'
21 require File.expand_path(File.dirname(__FILE__) + '/helper_testcase')
21 require File.expand_path(File.dirname(__FILE__) + '/helper_testcase')
22 require File.join(RAILS_ROOT,'test', 'mocks', 'open_id_authentication_mock.rb')
22 require File.join(RAILS_ROOT,'test', 'mocks', 'open_id_authentication_mock.rb')
23
23
24 require File.expand_path(File.dirname(__FILE__) + '/object_daddy_helpers')
24 require File.expand_path(File.dirname(__FILE__) + '/object_daddy_helpers')
25 include ObjectDaddyHelpers
25 include ObjectDaddyHelpers
26
26
27 class ActiveSupport::TestCase
27 class ActiveSupport::TestCase
28 # Transactional fixtures accelerate your tests by wrapping each test method
28 # Transactional fixtures accelerate your tests by wrapping each test method
29 # in a transaction that's rolled back on completion. This ensures that the
29 # in a transaction that's rolled back on completion. This ensures that the
30 # test database remains unchanged so your fixtures don't have to be reloaded
30 # test database remains unchanged so your fixtures don't have to be reloaded
31 # between every test method. Fewer database queries means faster tests.
31 # between every test method. Fewer database queries means faster tests.
32 #
32 #
33 # Read Mike Clark's excellent walkthrough at
33 # Read Mike Clark's excellent walkthrough at
34 # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
34 # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
35 #
35 #
36 # Every Active Record database supports transactions except MyISAM tables
36 # Every Active Record database supports transactions except MyISAM tables
37 # in MySQL. Turn off transactional fixtures in this case; however, if you
37 # in MySQL. Turn off transactional fixtures in this case; however, if you
38 # don't care one way or the other, switching from MyISAM to InnoDB tables
38 # don't care one way or the other, switching from MyISAM to InnoDB tables
39 # is recommended.
39 # is recommended.
40 self.use_transactional_fixtures = true
40 self.use_transactional_fixtures = true
41
41
42 # Instantiated fixtures are slow, but give you @david where otherwise you
42 # Instantiated fixtures are slow, but give you @david where otherwise you
43 # would need people(:david). If you don't want to migrate your existing
43 # would need people(:david). If you don't want to migrate your existing
44 # test cases which use the @david style and don't mind the speed hit (each
44 # test cases which use the @david style and don't mind the speed hit (each
45 # instantiated fixtures translates to a database query per test method),
45 # instantiated fixtures translates to a database query per test method),
46 # then set this back to true.
46 # then set this back to true.
47 self.use_instantiated_fixtures = false
47 self.use_instantiated_fixtures = false
48
48
49 # Add more helper methods to be used by all tests here...
49 # Add more helper methods to be used by all tests here...
50
50
51 def log_user(login, password)
51 def log_user(login, password)
52 User.anonymous
52 User.anonymous
53 get "/login"
53 get "/login"
54 assert_equal nil, session[:user_id]
54 assert_equal nil, session[:user_id]
55 assert_response :success
55 assert_response :success
56 assert_template "account/login"
56 assert_template "account/login"
57 post "/login", :username => login, :password => password
57 post "/login", :username => login, :password => password
58 assert_equal login, User.find(session[:user_id]).login
58 assert_equal login, User.find(session[:user_id]).login
59 end
59 end
60
60
61 def uploaded_test_file(name, mime)
61 def uploaded_test_file(name, mime)
62 ActionController::TestUploadedFile.new(ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime)
62 ActionController::TestUploadedFile.new(ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime)
63 end
63 end
64
64
65 # Use a temporary directory for attachment related tests
65 # Use a temporary directory for attachment related tests
66 def set_tmp_attachments_directory
66 def set_tmp_attachments_directory
67 Dir.mkdir "#{RAILS_ROOT}/tmp/test" unless File.directory?("#{RAILS_ROOT}/tmp/test")
67 Dir.mkdir "#{RAILS_ROOT}/tmp/test" unless File.directory?("#{RAILS_ROOT}/tmp/test")
68 Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments")
68 Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments")
69 Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments"
69 Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments"
70 end
70 end
71
71
72 def with_settings(options, &block)
72 def with_settings(options, &block)
73 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h}
73 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h}
74 options.each {|k, v| Setting[k] = v}
74 options.each {|k, v| Setting[k] = v}
75 yield
75 yield
76 saved_settings.each {|k, v| Setting[k] = v}
76 saved_settings.each {|k, v| Setting[k] = v}
77 end
77 end
78
78
79 def self.ldap_configured?
79 def self.ldap_configured?
80 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
80 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
81 return @test_ldap.bind
81 return @test_ldap.bind
82 rescue Exception => e
82 rescue Exception => e
83 # LDAP is not listening
83 # LDAP is not listening
84 return nil
84 return nil
85 end
85 end
86
86
87 # Shoulda macros
87 # Shoulda macros
88 def self.should_render_404
88 def self.should_render_404
89 should_respond_with :not_found
89 should_respond_with :not_found
90 should_render_template 'common/404'
90 should_render_template 'common/404'
91 end
91 end
92
92
93 def self.should_have_before_filter(expected_method, options = {})
93 def self.should_have_before_filter(expected_method, options = {})
94 should_have_filter('before', expected_method, options)
94 should_have_filter('before', expected_method, options)
95 end
95 end
96
96
97 def self.should_have_after_filter(expected_method, options = {})
97 def self.should_have_after_filter(expected_method, options = {})
98 should_have_filter('after', expected_method, options)
98 should_have_filter('after', expected_method, options)
99 end
99 end
100
100
101 def self.should_have_filter(filter_type, expected_method, options)
101 def self.should_have_filter(filter_type, expected_method, options)
102 description = "have #{filter_type}_filter :#{expected_method}"
102 description = "have #{filter_type}_filter :#{expected_method}"
103 description << " with #{options.inspect}" unless options.empty?
103 description << " with #{options.inspect}" unless options.empty?
104
104
105 should description do
105 should description do
106 klass = "action_controller/filters/#{filter_type}_filter".classify.constantize
106 klass = "action_controller/filters/#{filter_type}_filter".classify.constantize
107 expected = klass.new(:filter, expected_method.to_sym, options)
107 expected = klass.new(:filter, expected_method.to_sym, options)
108 assert_equal 1, @controller.class.filter_chain.select { |filter|
108 assert_equal 1, @controller.class.filter_chain.select { |filter|
109 filter.method == expected.method && filter.kind == expected.kind &&
109 filter.method == expected.method && filter.kind == expected.kind &&
110 filter.options == expected.options && filter.class == expected.class
110 filter.options == expected.options && filter.class == expected.class
111 }.size
111 }.size
112 end
112 end
113 end
113 end
114
115 def self.should_show_the_old_and_new_values_for(prop_key, model, &block)
116 context "" do
117 setup do
118 if block_given?
119 instance_eval &block
120 else
121 @old_value = model.generate!
122 @new_value = model.generate!
123 end
124 end
125
126 should "use the new value's name" do
127 @detail = JournalDetail.generate!(:property => 'attr',
128 :old_value => @old_value.id,
129 :value => @new_value.id,
130 :prop_key => prop_key)
131
132 assert_match @new_value.name, show_detail(@detail, true)
133 end
134
135 should "use the old value's name" do
136 @detail = JournalDetail.generate!(:property => 'attr',
137 :old_value => @old_value.id,
138 :value => @new_value.id,
139 :prop_key => prop_key)
140
141 assert_match @old_value.name, show_detail(@detail, true)
142 end
143 end
144 end
114 end
145 end
General Comments 0
You need to be logged in to leave comments. Login now