2017-06-22 22:59:18 -04:00
|
|
|
# frozen_string_literal: true
|
2016-08-06 13:01:31 -04:00
|
|
|
require "active_support/test_case"
|
|
|
|
require "rails-dom-testing"
|
2010-11-19 12:29:33 -05:00
|
|
|
|
2007-10-25 22:21:21 -04:00
|
|
|
module ActionMailer
|
|
|
|
class NonInferrableMailerError < ::StandardError
|
|
|
|
def initialize(name)
|
2017-01-12 03:39:16 -05:00
|
|
|
super "Unable to determine the mailer to test from #{name}. " \
|
|
|
|
"You'll need to specify it using tests YourMailer in your " \
|
2007-10-25 22:21:21 -04:00
|
|
|
"test case definition"
|
|
|
|
end
|
|
|
|
end
|
2008-01-05 08:34:15 -05:00
|
|
|
|
2007-10-25 22:21:21 -04:00
|
|
|
class TestCase < ActiveSupport::TestCase
|
2016-02-16 05:57:06 -05:00
|
|
|
module ClearTestDeliveries
|
2016-02-08 05:47:25 -05:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2016-04-22 08:51:39 -04:00
|
|
|
setup :clear_test_deliveries
|
2016-04-15 13:27:09 -04:00
|
|
|
teardown :clear_test_deliveries
|
2016-02-08 05:47:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2016-04-22 08:51:39 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def clear_test_deliveries
|
|
|
|
if ActionMailer::Base.delivery_method == :test
|
|
|
|
ActionMailer::Base.deliveries.clear
|
|
|
|
end
|
2016-02-08 05:47:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-12 09:18:42 -04:00
|
|
|
module Behavior
|
|
|
|
extend ActiveSupport::Concern
|
2007-10-25 22:21:21 -04:00
|
|
|
|
2012-09-25 18:04:09 -04:00
|
|
|
include ActiveSupport::Testing::ConstantLookup
|
2010-06-12 09:18:42 -04:00
|
|
|
include TestHelper
|
2014-10-07 19:01:40 -04:00
|
|
|
include Rails::Dom::Testing::Assertions::SelectorAssertions
|
|
|
|
include Rails::Dom::Testing::Assertions::DomAssertions
|
2008-01-05 08:34:15 -05:00
|
|
|
|
2011-11-30 12:06:07 -05:00
|
|
|
included do
|
|
|
|
class_attribute :_mailer_class
|
|
|
|
setup :initialize_test_deliveries
|
|
|
|
setup :set_expected_mail
|
2014-06-06 12:11:24 -04:00
|
|
|
teardown :restore_test_deliveries
|
2016-08-25 03:22:48 -04:00
|
|
|
ActiveSupport.run_load_hooks(:action_mailer_test_case, self)
|
2011-11-30 12:06:07 -05:00
|
|
|
end
|
|
|
|
|
2010-06-12 09:18:42 -04:00
|
|
|
module ClassMethods
|
|
|
|
def tests(mailer)
|
2011-10-03 03:04:22 -04:00
|
|
|
case mailer
|
|
|
|
when String, Symbol
|
|
|
|
self._mailer_class = mailer.to_s.camelize.constantize
|
|
|
|
when Module
|
|
|
|
self._mailer_class = mailer
|
|
|
|
else
|
|
|
|
raise NonInferrableMailerError.new(mailer)
|
|
|
|
end
|
2010-06-12 09:18:42 -04:00
|
|
|
end
|
2007-10-25 22:21:21 -04:00
|
|
|
|
2010-06-12 09:18:42 -04:00
|
|
|
def mailer_class
|
2017-01-05 03:20:57 -05:00
|
|
|
if mailer = _mailer_class
|
2010-06-12 09:18:42 -04:00
|
|
|
mailer
|
|
|
|
else
|
|
|
|
tests determine_default_mailer(name)
|
|
|
|
end
|
2007-10-25 22:21:21 -04:00
|
|
|
end
|
|
|
|
|
2010-06-12 09:18:42 -04:00
|
|
|
def determine_default_mailer(name)
|
2012-09-24 16:46:58 -04:00
|
|
|
mailer = determine_constant_from_test_name(name) do |constant|
|
|
|
|
Class === constant && constant < ActionMailer::Base
|
|
|
|
end
|
|
|
|
raise NonInferrableMailerError.new(name) if mailer.nil?
|
|
|
|
mailer
|
2010-06-12 09:18:42 -04:00
|
|
|
end
|
2007-10-25 22:21:21 -04:00
|
|
|
end
|
|
|
|
|
2016-12-22 04:57:02 -05:00
|
|
|
private
|
2007-10-25 22:21:21 -04:00
|
|
|
|
2016-12-22 04:57:02 -05:00
|
|
|
def initialize_test_deliveries
|
2014-05-28 06:55:01 -04:00
|
|
|
set_delivery_method :test
|
2014-06-06 12:11:24 -04:00
|
|
|
@old_perform_deliveries = ActionMailer::Base.perform_deliveries
|
2010-06-12 09:18:42 -04:00
|
|
|
ActionMailer::Base.perform_deliveries = true
|
2016-04-22 08:51:39 -04:00
|
|
|
ActionMailer::Base.deliveries.clear
|
2014-06-06 12:11:24 -04:00
|
|
|
end
|
|
|
|
|
2016-12-22 04:57:02 -05:00
|
|
|
def restore_test_deliveries
|
2014-05-28 06:55:01 -04:00
|
|
|
restore_delivery_method
|
2014-06-06 12:11:24 -04:00
|
|
|
ActionMailer::Base.perform_deliveries = @old_perform_deliveries
|
2010-06-12 09:18:42 -04:00
|
|
|
end
|
|
|
|
|
2016-12-22 04:57:02 -05:00
|
|
|
def set_delivery_method(method)
|
2014-08-19 22:26:45 -04:00
|
|
|
@old_delivery_method = ActionMailer::Base.delivery_method
|
|
|
|
ActionMailer::Base.delivery_method = method
|
|
|
|
end
|
|
|
|
|
2016-12-22 04:57:02 -05:00
|
|
|
def restore_delivery_method
|
2016-04-22 08:51:39 -04:00
|
|
|
ActionMailer::Base.deliveries.clear
|
2014-08-19 22:26:45 -04:00
|
|
|
ActionMailer::Base.delivery_method = @old_delivery_method
|
|
|
|
end
|
|
|
|
|
2016-12-22 04:57:02 -05:00
|
|
|
def set_expected_mail
|
2010-06-12 09:18:42 -04:00
|
|
|
@expected = Mail.new
|
|
|
|
@expected.content_type ["text", "plain", { "charset" => charset }]
|
2016-08-06 13:01:31 -04:00
|
|
|
@expected.mime_version = "1.0"
|
2010-06-12 09:18:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def charset
|
|
|
|
"UTF-8"
|
|
|
|
end
|
|
|
|
|
|
|
|
def encode(subject)
|
|
|
|
Mail::Encodings.q_value_encode(subject, charset)
|
|
|
|
end
|
2007-10-25 22:21:21 -04:00
|
|
|
|
2010-06-12 09:18:42 -04:00
|
|
|
def read_fixture(action)
|
2016-08-06 13:01:31 -04:00
|
|
|
IO.readlines(File.join(Rails.root, "test", "fixtures", self.class.mailer_class.name.underscore, action))
|
2010-06-12 09:18:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
include Behavior
|
2007-10-25 22:21:21 -04:00
|
|
|
end
|
|
|
|
end
|