test_helper.rb
7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'rails/test_help'
require 'mocha'
require 'noosfero/test'
require 'authenticated_test_helper'
require File.dirname(__FILE__) + '/factories'
require File.dirname(__FILE__) + '/noosfero_doc_test'
require File.dirname(__FILE__) + '/action_tracker_test_helper'
FileUtils.rm_rf(Rails.root.join('index', 'test'))
Image.attachment_options[:path_prefix] = 'test/tmp/public/images'
Thumbnail.attachment_options[:path_prefix] = 'test/tmp/public/thumbnails'
FastGettext.add_text_domain 'noosferotest', :type => :chain, :chain => []
FastGettext.default_text_domain = 'noosferotest'
class ActiveSupport::TestCase
# Transactional fixtures accelerate your tests by wrapping each test method
# in a transaction that's rolled back on completion. This ensures that the
# test database remains unchanged so your fixtures don't have to be reloaded
# between every test method. Fewer database queries means faster tests.
#
# Read Mike Clark's excellent walkthrough at
# http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
#
# Every Active Record database supports transactions except MyISAM tables
# in MySQL. Turn off transactional fixtures in this case; however, if you
# don't care one way or the other, switching from MyISAM to InnoDB tables
# is recommended.
self.use_transactional_fixtures = true
# Instantiated fixtures are slow, but give you @david where otherwise you
# would need people(:david). If you don't want to migrate your existing
# test cases which use the @david style and don't mind the speed hit (each
# instantiated fixtures translates to a database query per test method),
# then set this back to true.
self.use_instantiated_fixtures = false
# Add more helper methods to be used by all tests here...
# for fixture_file_upload
include ActionDispatch::TestProcess
include Noosfero::Factory
include AuthenticatedTestHelper
fixtures :environments, :roles
def self.all_fixtures
Dir.glob(Rails.root.join('test', 'fixtures', '*.yml')).each do |item|
fixtures File.basename(item).sub(/\.yml$/, '').to_s
end
end
def self.should(name, &block)
@shoulds ||= []
destname = 'test_should_' + name.gsub(/[^a-zA-z0-9]+/, '_')
if @shoulds.include?(destname)
raise "there is already a test named \"#{destname}\""
end
@shoulds << destname
if block_given?
self.send(:define_method, destname, &block)
else
self.send(:define_method, destname) do
flunk 'pending: should ' + name
end
end
end
setup :global_setup
def global_setup
User.current = nil
end
alias :ok :assert_block
def assert_equivalent(enum1, enum2)
assert( ((enum1 - enum2) == []) && ((enum2 - enum1) == []), "<#{enum1.inspect}> expected to be equivalent to <#{enum2.inspect}>")
end
def assert_mandatory(object, attribute, test_value = 'some random string')
object.send("#{attribute}=", nil)
object.valid?
assert object.errors[attribute.to_s].present?, "Attribute \"#{attribute.to_s}\" expected to be mandatory."
object.send("#{attribute}=", test_value)
object.valid?
assert !object.errors[attribute.to_s].present?, "Attribute \"#{attribute.to_s}\" expected to accept value #{test_value.inspect}"
end
def assert_optional(object, attribute)
object.send("#{attribute}=", nil)
object.valid?
assert !object.errors[attribute.to_s].present?
end
# this check only if text has html tag
def assert_sanitized(text)
assert !text.index('<'), "Text '#{text}' expected to be sanitized"
end
def assert_tag_in_string(text, options)
doc = HTML::Document.new(text, false, false)
tag = doc.find(options)
assert tag, "expected tag #{options.inspect}, but not found in #{text.inspect}"
end
def assert_no_tag_in_string(text, options)
doc = HTML::Document.new(text, false, false)
tag = doc.find(options)
assert !tag, "expected no tag #{options.inspect}, but tag found in #{text.inspect}"
end
def assert_order(reference, original)
original.each do |value|
if reference.include?(value)
if reference.first == value
reference.shift
else
assert false, "'#{value.inspect}' was found before it should be on: #{original.inspect}"
end
end
end
assert reference.blank?, "The following elements are not in the collection: #{reference.inspect}"
end
# For models that render views (blocks, articles, ...)
def self.action_view
@action_view ||= begin
view_paths = ActionController::Base.view_paths
action_view = ActionView::Base.new view_paths, {}
# for using Noosfero helpers inside render calls
action_view.extend ApplicationHelper
action_view
end
end
def render *args
self.class.action_view.render(*args)
end
def url_for args = {}
args
end
# url_for inside views (partials)
# from http://stackoverflow.com/a/13704257/670229
ActionView::TestCase::TestController.instance_eval do
helper Noosfero::Application.routes.url_helpers
end
ActionView::TestCase::TestController.class_eval do
def _routes
Noosfero::Application.routes
end
end
private
def uses_host(name)
#@request.instance_variable_set('@host', name)
@request.host = name
end
def process_delayed_job_queue
silence_stream(STDOUT) do
Delayed::Worker.new.work_off
end
end
def uses_postgresql(schema_name = 'test_schema')
adapter = ActiveRecord::Base.connection.class
adapter.any_instance.stubs(:adapter_name).returns('PostgreSQL')
adapter.any_instance.stubs(:schema_search_path).returns(schema_name)
Noosfero::MultiTenancy.stubs(:on?).returns(true)
end
def uses_sqlite
adapter = ActiveRecord::Base.connection.class
adapter.any_instance.stubs(:adapter_name).returns('SQLite')
Noosfero::MultiTenancy.stubs(:on?).returns(false)
end
def unsafe(string)
ret = ActiveSupport::SafeBuffer.new(string)
ret.instance_eval { @html_safe = false }
ret
end
end
module NoosferoTestHelper
def link_to(content, url, options = {})
"<a href='#{url.inspect}'>#{content}</a>"
end
def content_tag(tag, content, options = {})
tag_attr = options.blank? ? '' : ' ' + options.collect{ |o| "#{o[0]}=\"#{o[1]}\"" }.join(' ')
"<#{tag}#{tag_attr}>#{content}</#{tag}>"
end
def submit_tag(content, options = {})
content
end
def remote_function(options = {})
''
end
def tag(tag, args = {})
attrs = args.map{|k,v| "#{k}='#{v}'"}.join(' ')
"<#{tag} #{attrs} />"
end
def options_from_collection_for_select(collection, value_method, content_method)
"<option value='fake value'>fake content</option>"
end
def select_tag(id, collection, options = {})
"<select id='#{id}'>fake content</select>"
end
def options_for_select(collection, selected = nil)
collection.map{|item| "<option value='#{item[1]}'>#{item[0]}</option>"}.join("\n")
end
def params
{}
end
def ui_icon(icon)
icon
end
def will_paginate(arg1, arg2)
end
def javascript_tag(any)
''
end
def javascript_include_tag(any)
''
end
def check_box_tag(name, value = 1, checked = false, options = {})
name
end
def stylesheet_link_tag(arg)
arg
end
def strip_tags(html)
html.gsub(/<[^>]+>/, '')
end
def icon_for_article(article)
''
end
end
class ActionController::IntegrationTest
def assert_can_login
assert_tag :tag => 'a', :attributes => { :id => 'link_login' }
end
def assert_can_signup
assert_tag :tag => 'a', :attributes => { :href => '/account/signup'}
end
def login(username, password)
ActionController::Integration::Session.any_instance.stubs(:https?).returns(true)
post '/account/login', :user => { :login => username, :password => password }
assert_response :redirect
follow_redirect!
assert_not_equal '/account/login', path
end
end
Profile