2010-01-26 07:57:11 -05:00
|
|
|
require "isolation/abstract_unit"
|
2012-03-10 16:13:12 -05:00
|
|
|
require 'set'
|
2010-01-26 07:57:11 -05:00
|
|
|
|
|
|
|
module ApplicationTests
|
2012-01-05 20:30:17 -05:00
|
|
|
class FrameworksTest < ActiveSupport::TestCase
|
2010-01-26 07:57:11 -05:00
|
|
|
include ActiveSupport::Testing::Isolation
|
|
|
|
|
|
|
|
def setup
|
|
|
|
build_app
|
|
|
|
boot_rails
|
|
|
|
FileUtils.rm_rf "#{app_path}/config/environments"
|
|
|
|
end
|
|
|
|
|
2011-06-05 14:10:30 -04:00
|
|
|
def teardown
|
2011-06-06 08:54:05 -04:00
|
|
|
teardown_app
|
2011-06-05 14:10:30 -04:00
|
|
|
end
|
|
|
|
|
2010-01-26 07:57:11 -05:00
|
|
|
# AC & AM
|
|
|
|
test "set load paths set only if action controller or action mailer are in use" do
|
|
|
|
assert_nothing_raised NameError do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.root = "#{app_path}"
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
use_frameworks []
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "sets action_controller and action_mailer load paths" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.root = "#{app_path}"
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
2010-06-24 14:25:36 -04:00
|
|
|
|
|
|
|
expanded_path = File.expand_path("app/views", app_path)
|
|
|
|
assert_equal ActionController::Base.view_paths[0].to_s, expanded_path
|
|
|
|
assert_equal ActionMailer::Base.view_paths[0].to_s, expanded_path
|
2010-01-26 07:57:11 -05:00
|
|
|
end
|
|
|
|
|
2010-02-21 07:19:21 -05:00
|
|
|
test "allows me to configure default url options for ActionMailer" do
|
|
|
|
app_file "config/environments/development.rb", <<-RUBY
|
2010-07-01 11:07:48 -04:00
|
|
|
AppTemplate::Application.configure do
|
2010-02-21 07:19:21 -05:00
|
|
|
config.action_mailer.default_url_options = { :host => "test.rails" }
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
2010-06-24 14:27:11 -04:00
|
|
|
assert_equal "test.rails", ActionMailer::Base.default_url_options[:host]
|
2010-02-21 07:19:21 -05:00
|
|
|
end
|
|
|
|
|
2010-07-07 06:50:38 -04:00
|
|
|
test "does not include url helpers as action methods" do
|
|
|
|
app_file "config/routes.rb", <<-RUBY
|
|
|
|
AppTemplate::Application.routes.draw do
|
|
|
|
get "/foo", :to => lambda { |env| [200, {}, []] }, :as => :foo
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file "app/mailers/foo.rb", <<-RUBY
|
|
|
|
class Foo < ActionMailer::Base
|
|
|
|
def notify
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert Foo.method_defined?(:foo_path)
|
2010-09-07 08:53:37 -04:00
|
|
|
assert Foo.method_defined?(:main_app)
|
2012-03-10 16:13:12 -05:00
|
|
|
assert_equal Set.new(["notify"]), Foo.action_methods
|
2010-07-07 06:50:38 -04:00
|
|
|
end
|
|
|
|
|
2010-11-17 18:06:03 -05:00
|
|
|
test "allows to not load all helpers for controllers" do
|
|
|
|
add_to_config "config.action_controller.include_all_helpers = false"
|
|
|
|
|
|
|
|
app_file "app/controllers/application_controller.rb", <<-RUBY
|
|
|
|
class ApplicationController < ActionController::Base
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file "app/controllers/foo_controller.rb", <<-RUBY
|
|
|
|
class FooController < ApplicationController
|
|
|
|
def included_helpers
|
|
|
|
render :inline => "<%= from_app_helper -%> <%= from_foo_helper %>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def not_included_helper
|
|
|
|
render :inline => "<%= respond_to?(:from_bar_helper) -%>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file "app/helpers/application_helper.rb", <<-RUBY
|
|
|
|
module ApplicationHelper
|
|
|
|
def from_app_helper
|
|
|
|
"from_app_helper"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file "app/helpers/foo_helper.rb", <<-RUBY
|
|
|
|
module FooHelper
|
|
|
|
def from_foo_helper
|
|
|
|
"from_foo_helper"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file "app/helpers/bar_helper.rb", <<-RUBY
|
|
|
|
module BarHelper
|
|
|
|
def from_bar_helper
|
|
|
|
"from_bar_helper"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file "config/routes.rb", <<-RUBY
|
|
|
|
AppTemplate::Application.routes.draw do
|
|
|
|
match "/:controller(/:action)"
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require 'rack/test'
|
|
|
|
extend Rack::Test::Methods
|
|
|
|
|
|
|
|
get "/foo/included_helpers"
|
|
|
|
assert_equal "from_app_helper from_foo_helper", last_response.body
|
|
|
|
|
|
|
|
get "/foo/not_included_helper"
|
|
|
|
assert_equal "false", last_response.body
|
|
|
|
end
|
|
|
|
|
2010-07-27 10:39:28 -04:00
|
|
|
# AD
|
|
|
|
test "action_dispatch extensions are applied to ActionDispatch" do
|
|
|
|
add_to_config "config.action_dispatch.tld_length = 2"
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert_equal 2, ActionDispatch::Http::URL.tld_length
|
|
|
|
end
|
|
|
|
|
2011-12-07 07:11:08 -05:00
|
|
|
test "assignment config.encoding to default_charset" do
|
2011-12-25 06:34:58 -05:00
|
|
|
charset = 'Shift_JIS'
|
2011-12-08 11:27:58 -05:00
|
|
|
add_to_config "config.encoding = '#{charset}'"
|
2011-12-07 07:11:08 -05:00
|
|
|
require "#{app_path}/config/environment"
|
2011-12-08 11:27:58 -05:00
|
|
|
assert_equal charset, ActionDispatch::Response.default_charset
|
2011-12-07 07:11:08 -05:00
|
|
|
end
|
|
|
|
|
2010-01-26 07:57:11 -05:00
|
|
|
# AS
|
|
|
|
test "if there's no config.active_support.bare, all of ActiveSupport is required" do
|
|
|
|
use_frameworks []
|
|
|
|
require "#{app_path}/config/environment"
|
2010-06-04 19:13:37 -04:00
|
|
|
assert_nothing_raised { [1,2,3].sample }
|
2010-01-26 07:57:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "config.active_support.bare does not require all of ActiveSupport" do
|
|
|
|
add_to_config "config.active_support.bare = true"
|
|
|
|
|
|
|
|
use_frameworks []
|
|
|
|
|
|
|
|
Dir.chdir("#{app_path}/app") do
|
|
|
|
require "#{app_path}/config/environment"
|
2010-06-24 04:55:58 -04:00
|
|
|
assert_raises(NoMethodError) { [1,2,3].forty_two }
|
2010-01-26 07:57:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# AR
|
|
|
|
test "database middleware doesn't initialize when session store is not active_record" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.root = "#{app_path}"
|
2010-03-04 18:06:25 -05:00
|
|
|
config.session_store :cookie_store, { :key => "blahblahblah" }
|
2010-01-26 07:57:11 -05:00
|
|
|
RUBY
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert !Rails.application.config.middleware.include?(ActiveRecord::SessionStore)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "database middleware initializes when session store is active record" do
|
2010-03-04 18:06:25 -05:00
|
|
|
add_to_config "config.session_store :active_record_store"
|
2010-01-26 07:57:11 -05:00
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
2011-05-10 22:00:16 -04:00
|
|
|
expects = [ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActiveRecord::SessionStore]
|
2010-01-26 07:57:11 -05:00
|
|
|
middleware = Rails.application.config.middleware.map { |m| m.klass }
|
|
|
|
assert_equal expects, middleware & expects
|
|
|
|
end
|
|
|
|
|
2010-01-27 15:20:32 -05:00
|
|
|
test "active_record extensions are applied to ActiveRecord" do
|
|
|
|
add_to_config "config.active_record.table_name_prefix = 'tbl_'"
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert_equal 'tbl_', ActiveRecord::Base.table_name_prefix
|
|
|
|
end
|
|
|
|
|
2010-01-26 07:57:11 -05:00
|
|
|
test "database middleware doesn't initialize when activerecord is not in frameworks" do
|
|
|
|
use_frameworks []
|
|
|
|
require "#{app_path}/config/environment"
|
2010-03-02 15:31:29 -05:00
|
|
|
assert_nil defined?(ActiveRecord::Base)
|
2010-01-26 07:57:11 -05:00
|
|
|
end
|
2012-02-29 11:13:14 -05:00
|
|
|
|
|
|
|
test "use schema cache dump" do
|
|
|
|
Dir.chdir(app_path) do
|
2012-03-08 17:32:41 -05:00
|
|
|
`rails generate model post title:string;
|
|
|
|
bundle exec rake db:migrate db:schema:cache:dump`
|
2012-02-29 11:13:14 -05:00
|
|
|
end
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
ActiveRecord::Base.connection.drop_table("posts") # force drop posts table for test.
|
|
|
|
assert ActiveRecord::Base.connection.schema_cache.tables["posts"]
|
|
|
|
end
|
|
|
|
|
2012-02-29 11:19:27 -05:00
|
|
|
test "expire schema cache dump" do
|
|
|
|
Dir.chdir(app_path) do
|
2012-03-08 17:32:41 -05:00
|
|
|
`rails generate model post title:string;
|
|
|
|
bundle exec rake db:migrate db:schema:cache:dump db:rollback`
|
2012-02-29 11:19:27 -05:00
|
|
|
end
|
|
|
|
silence_warnings {
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert !ActiveRecord::Base.connection.schema_cache.tables["posts"]
|
|
|
|
}
|
|
|
|
end
|
2010-01-26 07:57:11 -05:00
|
|
|
end
|
|
|
|
end
|