PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/railties/test/railties/engine_test.rb

https://github.com/ghar/rails
Ruby | 669 lines | 584 code | 84 blank | 1 comment | 5 complexity | cbbbd4c30fa4a93981efc4f679529ec5 MD5 | raw file
  1. require "isolation/abstract_unit"
  2. require "railties/shared_tests"
  3. require "stringio"
  4. require "rack/test"
  5. module RailtiesTest
  6. class EngineTest < Test::Unit::TestCase
  7. include ActiveSupport::Testing::Isolation
  8. include SharedTests
  9. include Rack::Test::Methods
  10. def setup
  11. build_app
  12. @plugin = engine "bukkits" do |plugin|
  13. plugin.write "lib/bukkits.rb", <<-RUBY
  14. module Bukkits
  15. class Engine < ::Rails::Engine
  16. railtie_name "bukkits"
  17. end
  18. end
  19. RUBY
  20. plugin.write "lib/another.rb", "class Another; end"
  21. end
  22. end
  23. def teardown
  24. teardown_app
  25. end
  26. test "Rails::Engine itself does not respond to config" do
  27. boot_rails
  28. assert !Rails::Engine.respond_to?(:config)
  29. end
  30. test "initializers are executed after application configuration initializers" do
  31. @plugin.write "lib/bukkits.rb", <<-RUBY
  32. module Bukkits
  33. class Engine < ::Rails::Engine
  34. initializer "dummy_initializer" do
  35. end
  36. end
  37. end
  38. RUBY
  39. boot_rails
  40. initializers = Rails.application.initializers.tsort
  41. index = initializers.index { |i| i.name == "dummy_initializer" }
  42. selection = initializers[(index-3)..(index)].map(&:name).map(&:to_s)
  43. assert_equal %w(
  44. load_config_initializers
  45. load_config_initializers
  46. engines_blank_point
  47. dummy_initializer
  48. ), selection
  49. assert index < initializers.index { |i| i.name == :build_middleware_stack }
  50. end
  51. class Upcaser
  52. def initialize(app)
  53. @app = app
  54. end
  55. def call(env)
  56. response = @app.call(env)
  57. response[2].each { |b| b.upcase! }
  58. response
  59. end
  60. end
  61. test "engine is a rack app and can have his own middleware stack" do
  62. add_to_config("config.action_dispatch.show_exceptions = false")
  63. @plugin.write "lib/bukkits.rb", <<-RUBY
  64. module Bukkits
  65. class Engine < ::Rails::Engine
  66. endpoint lambda { |env| [200, {'Content-Type' => 'text/html'}, ['Hello World']] }
  67. config.middleware.use ::RailtiesTest::EngineTest::Upcaser
  68. end
  69. end
  70. RUBY
  71. app_file "config/routes.rb", <<-RUBY
  72. AppTemplate::Application.routes.draw do
  73. mount(Bukkits::Engine => "/bukkits")
  74. end
  75. RUBY
  76. boot_rails
  77. get("/bukkits")
  78. assert_equal "HELLO WORLD", last_response.body
  79. end
  80. test "pass the value of the segment" do
  81. controller "foo", <<-RUBY
  82. class FooController < ActionController::Base
  83. def index
  84. render :text => params[:username]
  85. end
  86. end
  87. RUBY
  88. @plugin.write "config/routes.rb", <<-RUBY
  89. Bukkits::Engine.routes.draw do
  90. root :to => "foo#index"
  91. end
  92. RUBY
  93. app_file "config/routes.rb", <<-RUBY
  94. Rails.application.routes.draw do
  95. mount(Bukkits::Engine => "/:username")
  96. end
  97. RUBY
  98. boot_rails
  99. get("/arunagw")
  100. assert_equal "arunagw", last_response.body
  101. end
  102. test "it provides routes as default endpoint" do
  103. @plugin.write "lib/bukkits.rb", <<-RUBY
  104. module Bukkits
  105. class Engine < ::Rails::Engine
  106. end
  107. end
  108. RUBY
  109. @plugin.write "config/routes.rb", <<-RUBY
  110. Bukkits::Engine.routes.draw do
  111. match "/foo" => lambda { |env| [200, {'Content-Type' => 'text/html'}, ['foo']] }
  112. end
  113. RUBY
  114. app_file "config/routes.rb", <<-RUBY
  115. Rails.application.routes.draw do
  116. mount(Bukkits::Engine => "/bukkits")
  117. end
  118. RUBY
  119. boot_rails
  120. get("/bukkits/foo")
  121. assert_equal "foo", last_response.body
  122. end
  123. test "engine can load its own plugins" do
  124. @plugin.write "lib/bukkits.rb", <<-RUBY
  125. module Bukkits
  126. class Engine < ::Rails::Engine
  127. end
  128. end
  129. RUBY
  130. @plugin.write "vendor/plugins/yaffle/init.rb", <<-RUBY
  131. config.yaffle_loaded = true
  132. RUBY
  133. boot_rails
  134. assert Bukkits::Engine.config.yaffle_loaded
  135. end
  136. test "engine does not load plugins that already exists in application" do
  137. @plugin.write "lib/bukkits.rb", <<-RUBY
  138. module Bukkits
  139. class Engine < ::Rails::Engine
  140. end
  141. end
  142. RUBY
  143. @plugin.write "vendor/plugins/yaffle/init.rb", <<-RUBY
  144. config.engine_yaffle_loaded = true
  145. RUBY
  146. app_file "vendor/plugins/yaffle/init.rb", <<-RUBY
  147. config.app_yaffle_loaded = true
  148. RUBY
  149. warnings = capture(:stderr) { boot_rails }
  150. assert !warnings.empty?
  151. assert !Bukkits::Engine.config.respond_to?(:engine_yaffle_loaded)
  152. assert Rails.application.config.app_yaffle_loaded
  153. end
  154. test "it loads its environment file" do
  155. @plugin.write "lib/bukkits.rb", <<-RUBY
  156. module Bukkits
  157. class Engine < ::Rails::Engine
  158. end
  159. end
  160. RUBY
  161. @plugin.write "config/environments/development.rb", <<-RUBY
  162. Bukkits::Engine.configure do
  163. config.environment_loaded = true
  164. end
  165. RUBY
  166. boot_rails
  167. assert Bukkits::Engine.config.environment_loaded
  168. end
  169. test "it passes router in env" do
  170. @plugin.write "lib/bukkits.rb", <<-RUBY
  171. module Bukkits
  172. class Engine < ::Rails::Engine
  173. endpoint lambda { |env| [200, {'Content-Type' => 'text/html'}, 'hello'] }
  174. end
  175. end
  176. RUBY
  177. require "rack/file"
  178. boot_rails
  179. env = Rack::MockRequest.env_for("/")
  180. Bukkits::Engine.call(env)
  181. assert_equal Bukkits::Engine.routes, env['action_dispatch.routes']
  182. env = Rack::MockRequest.env_for("/")
  183. Rails.application.call(env)
  184. assert_equal Rails.application.routes, env['action_dispatch.routes']
  185. end
  186. test "isolated engine should include only its own routes and helpers" do
  187. @plugin.write "lib/bukkits.rb", <<-RUBY
  188. module Bukkits
  189. class Engine < ::Rails::Engine
  190. isolate_namespace Bukkits
  191. end
  192. end
  193. RUBY
  194. @plugin.write "app/models/bukkits/post.rb", <<-RUBY
  195. module Bukkits
  196. class Post
  197. extend ActiveModel::Naming
  198. def to_param
  199. "1"
  200. end
  201. end
  202. end
  203. RUBY
  204. app_file "config/routes.rb", <<-RUBY
  205. AppTemplate::Application.routes.draw do
  206. match "/bar" => "bar#index", :as => "bar"
  207. mount Bukkits::Engine => "/bukkits", :as => "bukkits"
  208. end
  209. RUBY
  210. @plugin.write "config/routes.rb", <<-RUBY
  211. Bukkits::Engine.routes.draw do
  212. match "/foo" => "foo#index", :as => "foo"
  213. match "/foo/show" => "foo#show"
  214. match "/from_app" => "foo#from_app"
  215. match "/routes_helpers_in_view" => "foo#routes_helpers_in_view"
  216. match "/polymorphic_path_without_namespace" => "foo#polymorphic_path_without_namespace"
  217. resources :posts
  218. end
  219. RUBY
  220. app_file "app/helpers/some_helper.rb", <<-RUBY
  221. module SomeHelper
  222. def something
  223. "Something... Something... Something..."
  224. end
  225. end
  226. RUBY
  227. @plugin.write "app/helpers/engine_helper.rb", <<-RUBY
  228. module EngineHelper
  229. def help_the_engine
  230. "Helped."
  231. end
  232. end
  233. RUBY
  234. @plugin.write "app/controllers/bukkits/foo_controller.rb", <<-RUBY
  235. class Bukkits::FooController < ActionController::Base
  236. def index
  237. render :inline => "<%= help_the_engine %>"
  238. end
  239. def show
  240. render :text => foo_path
  241. end
  242. def from_app
  243. render :inline => "<%= (self.respond_to?(:bar_path) || self.respond_to?(:something)) %>"
  244. end
  245. def routes_helpers_in_view
  246. render :inline => "<%= foo_path %>, <%= main_app.bar_path %>"
  247. end
  248. def polymorphic_path_without_namespace
  249. render :text => polymorphic_path(Post.new)
  250. end
  251. end
  252. RUBY
  253. @plugin.write "app/mailers/bukkits/my_mailer.rb", <<-RUBY
  254. module Bukkits
  255. class MyMailer < ActionMailer::Base
  256. end
  257. end
  258. RUBY
  259. add_to_config("config.action_dispatch.show_exceptions = false")
  260. boot_rails
  261. assert_equal "bukkits_", Bukkits.table_name_prefix
  262. assert_equal "bukkits", Bukkits::Engine.engine_name
  263. assert_equal Bukkits._railtie, Bukkits::Engine
  264. assert ::Bukkits::MyMailer.method_defined?(:foo_path)
  265. assert !::Bukkits::MyMailer.method_defined?(:bar_path)
  266. get("/bukkits/from_app")
  267. assert_equal "false", last_response.body
  268. get("/bukkits/foo/show")
  269. assert_equal "/bukkits/foo", last_response.body
  270. get("/bukkits/foo")
  271. assert_equal "Helped.", last_response.body
  272. get("/bukkits/routes_helpers_in_view")
  273. assert_equal "/bukkits/foo, /bar", last_response.body
  274. get("/bukkits/polymorphic_path_without_namespace")
  275. assert_equal "/bukkits/posts/1", last_response.body
  276. end
  277. test "isolated engine should avoid namespace in names if that's possible" do
  278. @plugin.write "lib/bukkits.rb", <<-RUBY
  279. module Bukkits
  280. class Engine < ::Rails::Engine
  281. isolate_namespace Bukkits
  282. end
  283. end
  284. RUBY
  285. @plugin.write "app/models/bukkits/post.rb", <<-RUBY
  286. module Bukkits
  287. class Post
  288. extend ActiveModel::Naming
  289. include ActiveModel::Conversion
  290. attr_accessor :title
  291. def to_param
  292. "1"
  293. end
  294. def persisted?
  295. false
  296. end
  297. end
  298. end
  299. RUBY
  300. app_file "config/routes.rb", <<-RUBY
  301. AppTemplate::Application.routes.draw do
  302. mount Bukkits::Engine => "/bukkits", :as => "bukkits"
  303. end
  304. RUBY
  305. @plugin.write "config/routes.rb", <<-RUBY
  306. Bukkits::Engine.routes.draw do
  307. resources :posts
  308. end
  309. RUBY
  310. @plugin.write "app/controllers/bukkits/posts_controller.rb", <<-RUBY
  311. class Bukkits::PostsController < ActionController::Base
  312. def new
  313. end
  314. end
  315. RUBY
  316. @plugin.write "app/views/bukkits/posts/new.html.erb", <<-ERB
  317. <%= form_for(Bukkits::Post.new) do |f| %>
  318. <%= f.text_field :title %>
  319. <% end %>
  320. ERB
  321. add_to_config("config.action_dispatch.show_exceptions = false")
  322. boot_rails
  323. get("/bukkits/posts/new")
  324. assert_match(/name="post\[title\]"/, last_response.body)
  325. end
  326. test "isolated engine should set correct route module prefix for nested namespace" do
  327. @plugin.write "lib/bukkits.rb", <<-RUBY
  328. module Bukkits
  329. module Awesome
  330. class Engine < ::Rails::Engine
  331. isolate_namespace Bukkits::Awesome
  332. end
  333. end
  334. end
  335. RUBY
  336. app_file "config/routes.rb", <<-RUBY
  337. AppTemplate::Application.routes.draw do
  338. mount Bukkits::Awesome::Engine => "/bukkits", :as => "bukkits"
  339. end
  340. RUBY
  341. @plugin.write "config/routes.rb", <<-RUBY
  342. Bukkits::Awesome::Engine.routes.draw do
  343. match "/foo" => "foo#index"
  344. end
  345. RUBY
  346. @plugin.write "app/controllers/bukkits/awesome/foo_controller.rb", <<-RUBY
  347. class Bukkits::Awesome::FooController < ActionController::Base
  348. def index
  349. render :text => "ok"
  350. end
  351. end
  352. RUBY
  353. add_to_config("config.action_dispatch.show_exceptions = false")
  354. boot_rails
  355. get("/bukkits/foo")
  356. assert_equal "ok", last_response.body
  357. end
  358. test "loading seed data" do
  359. @plugin.write "db/seeds.rb", <<-RUBY
  360. Bukkits::Engine.config.bukkits_seeds_loaded = true
  361. RUBY
  362. app_file "db/seeds.rb", <<-RUBY
  363. Rails.application.config.app_seeds_loaded = true
  364. RUBY
  365. boot_rails
  366. Rails.application.load_seed
  367. assert Rails.application.config.app_seeds_loaded
  368. assert_raise(NoMethodError) do Bukkits::Engine.config.bukkits_seeds_loaded end
  369. Bukkits::Engine.load_seed
  370. assert Bukkits::Engine.config.bukkits_seeds_loaded
  371. end
  372. test "using namespace more than once on one module should not overwrite _railtie method" do
  373. @plugin.write "lib/bukkits.rb", <<-RUBY
  374. module AppTemplate
  375. class Engine < ::Rails::Engine
  376. isolate_namespace(AppTemplate)
  377. end
  378. end
  379. RUBY
  380. add_to_config "isolate_namespace AppTemplate"
  381. app_file "config/routes.rb", <<-RUBY
  382. AppTemplate::Application.routes.draw do end
  383. RUBY
  384. boot_rails
  385. assert_equal AppTemplate._railtie, AppTemplate::Engine
  386. end
  387. test "properly reload routes" do
  388. # when routes are inside application class definition
  389. # they should not be reloaded when engine's routes
  390. # file has changed
  391. add_to_config <<-RUBY
  392. routes do
  393. mount lambda{|env| [200, {}, ["foo"]]} => "/foo"
  394. mount Bukkits::Engine => "/bukkits"
  395. end
  396. RUBY
  397. FileUtils.rm(File.join(app_path, "config/routes.rb"))
  398. @plugin.write "config/routes.rb", <<-RUBY
  399. Bukkits::Engine.routes.draw do
  400. mount lambda{|env| [200, {}, ["bar"]]} => "/bar"
  401. end
  402. RUBY
  403. @plugin.write "lib/bukkits.rb", <<-RUBY
  404. module Bukkits
  405. class Engine < ::Rails::Engine
  406. isolate_namespace(Bukkits)
  407. end
  408. end
  409. RUBY
  410. boot_rails
  411. require "#{rails_root}/config/environment"
  412. get("/foo")
  413. assert_equal "foo", last_response.body
  414. get("/bukkits/bar")
  415. assert_equal "bar", last_response.body
  416. end
  417. test "setting generators for engine and overriding app generator's" do
  418. @plugin.write "lib/bukkits.rb", <<-RUBY
  419. module Bukkits
  420. class Engine < ::Rails::Engine
  421. config.generators do |g|
  422. g.orm :datamapper
  423. g.template_engine :haml
  424. g.test_framework :rspec
  425. end
  426. config.app_generators do |g|
  427. g.orm :mongoid
  428. g.template_engine :liquid
  429. g.test_framework :shoulda
  430. end
  431. end
  432. end
  433. RUBY
  434. add_to_config <<-RUBY
  435. config.generators do |g|
  436. g.test_framework :test_unit
  437. end
  438. RUBY
  439. boot_rails
  440. require "#{rails_root}/config/environment"
  441. app_generators = Rails.application.config.generators.options[:rails]
  442. assert_equal :mongoid , app_generators[:orm]
  443. assert_equal :liquid , app_generators[:template_engine]
  444. assert_equal :test_unit, app_generators[:test_framework]
  445. generators = Bukkits::Engine.config.generators.options[:rails]
  446. assert_equal :datamapper, generators[:orm]
  447. assert_equal :haml , generators[:template_engine]
  448. assert_equal :rspec , generators[:test_framework]
  449. end
  450. test "engine should get default generators with ability to overwrite them" do
  451. @plugin.write "lib/bukkits.rb", <<-RUBY
  452. module Bukkits
  453. class Engine < ::Rails::Engine
  454. config.generators.test_framework :rspec
  455. end
  456. end
  457. RUBY
  458. boot_rails
  459. require "#{rails_root}/config/environment"
  460. generators = Bukkits::Engine.config.generators.options[:rails]
  461. assert_equal :active_record, generators[:orm]
  462. assert_equal :rspec , generators[:test_framework]
  463. app_generators = Rails.application.config.generators.options[:rails]
  464. assert_equal :test_unit , app_generators[:test_framework]
  465. end
  466. test "do not create table_name_prefix method if it already exists" do
  467. @plugin.write "lib/bukkits.rb", <<-RUBY
  468. module Bukkits
  469. def self.table_name_prefix
  470. "foo"
  471. end
  472. class Engine < ::Rails::Engine
  473. isolate_namespace(Bukkits)
  474. end
  475. end
  476. RUBY
  477. boot_rails
  478. require "#{rails_root}/config/environment"
  479. assert_equal "foo", Bukkits.table_name_prefix
  480. end
  481. test "fetching engine by path" do
  482. @plugin.write "lib/bukkits.rb", <<-RUBY
  483. module Bukkits
  484. class Engine < ::Rails::Engine
  485. end
  486. end
  487. RUBY
  488. boot_rails
  489. require "#{rails_root}/config/environment"
  490. assert_equal Bukkits::Engine.instance, Rails::Engine.find(@plugin.path)
  491. # check expanding paths
  492. engine_dir = @plugin.path.chomp("/").split("/").last
  493. engine_path = File.join(@plugin.path, '..', engine_dir)
  494. assert_equal Bukkits::Engine.instance, Rails::Engine.find(engine_path)
  495. end
  496. test "gather isolated engine's helpers in Engine#helpers" do
  497. @plugin.write "lib/bukkits.rb", <<-RUBY
  498. module Bukkits
  499. class Engine < ::Rails::Engine
  500. isolate_namespace Bukkits
  501. end
  502. end
  503. RUBY
  504. app_file "app/helpers/some_helper.rb", <<-RUBY
  505. module SomeHelper
  506. def foo
  507. 'foo'
  508. end
  509. end
  510. RUBY
  511. @plugin.write "app/helpers/bukkits/engine_helper.rb", <<-RUBY
  512. module Bukkits
  513. module EngineHelper
  514. def bar
  515. 'bar'
  516. end
  517. end
  518. end
  519. RUBY
  520. @plugin.write "app/helpers/engine_helper.rb", <<-RUBY
  521. module EngineHelper
  522. def baz
  523. 'baz'
  524. end
  525. end
  526. RUBY
  527. add_to_config("config.action_dispatch.show_exceptions = false")
  528. boot_rails
  529. require "#{rails_root}/config/environment"
  530. methods = Bukkits::Engine.helpers.public_instance_methods.collect(&:to_s).sort
  531. expected = ["bar", "baz"]
  532. assert_equal expected, methods
  533. end
  534. private
  535. def app
  536. Rails.application
  537. end
  538. end
  539. end