diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2010-01-28 19:46:17 +0000 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2010-01-28 19:46:17 +0000 |
commit | 285361d1589002fcdd1584c07e6eb295f13c9f37 (patch) | |
tree | 2d50a69b3b59b6fb3cb7577b990fe3b1aaf58f4f /actionmailer/test | |
parent | dfa19408651ecc82e2aeba95d93db871ba8a6e41 (diff) | |
parent | d58398c2b5e98aad18dc72790230f338c10d145c (diff) | |
download | rails-285361d1589002fcdd1584c07e6eb295f13c9f37.tar.gz rails-285361d1589002fcdd1584c07e6eb295f13c9f37.tar.bz2 rails-285361d1589002fcdd1584c07e6eb295f13c9f37.zip |
Merge remote branch 'mainstream/master'
Conflicts:
railties/lib/rails/railtie.rb
Diffstat (limited to 'actionmailer/test')
37 files changed, 976 insertions, 510 deletions
diff --git a/actionmailer/test/abstract_unit.rb b/actionmailer/test/abstract_unit.rb index 50b8a53006..ce09bb5d61 100644 --- a/actionmailer/test/abstract_unit.rb +++ b/actionmailer/test/abstract_unit.rb @@ -8,7 +8,6 @@ $:.unshift(lib) unless $:.include?('lib') || $:.include?(lib) require 'rubygems' require 'test/unit' - require 'action_mailer' # Show backtraces for deprecated behavior for quicker cleanup. @@ -18,14 +17,10 @@ ActiveSupport::Deprecation.debug = true ActionView::Template.register_template_handler :haml, lambda { |template| "Look its HAML!".inspect } ActionView::Template.register_template_handler :bak, lambda { |template| "Lame backup".inspect } -ActionView::Base::DEFAULT_CONFIG = { :assets_dir => '/nowhere' } - -$:.unshift "#{File.dirname(__FILE__)}/fixtures/helpers" +FIXTURE_LOAD_PATH = File.expand_path('fixtures', File.dirname(__FILE__)) +ActionMailer::Base.view_paths = FIXTURE_LOAD_PATH -FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures') -ActionMailer::Base.template_root = FIXTURE_LOAD_PATH - -class MockSMTP +class MockSMTP def self.deliveries @@deliveries end @@ -49,19 +44,11 @@ class Net::SMTP end end -def uses_gem(gem_name, test_name, version = '> 0') - gem gem_name.to_s, version - require gem_name.to_s - yield -rescue LoadError - $stderr.puts "Skipping #{test_name} tests. `gem install #{gem_name}` and try again." -end - -def set_delivery_method(delivery_method) +def set_delivery_method(method) @old_delivery_method = ActionMailer::Base.delivery_method - ActionMailer::Base.delivery_method = delivery_method + ActionMailer::Base.delivery_method = method end def restore_delivery_method ActionMailer::Base.delivery_method = @old_delivery_method -end +end
\ No newline at end of file diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb new file mode 100644 index 0000000000..03e3f81acd --- /dev/null +++ b/actionmailer/test/base_test.rb @@ -0,0 +1,488 @@ +# encoding: utf-8 +require 'abstract_unit' + +class BaseTest < ActiveSupport::TestCase + class BaseMailer < ActionMailer::Base + self.mailer_name = "base_mailer" + + default :to => 'system@test.lindsaar.net', + :from => 'jose@test.plataformatec.com', + :reply_to => 'mikel@test.lindsaar.net' + + def welcome(hash = {}) + headers['X-SPAM'] = "Not SPAM" + mail({:subject => "The first email on new API!"}.merge!(hash)) + end + + def simple(hash = {}) + mail(hash) + end + + def html_only(hash = {}) + mail(hash) + end + + def plain_text_only(hash = {}) + mail(hash) + end + + def simple_with_headers(hash = {}) + headers hash + mail + end + + def attachment_with_content(hash = {}) + attachments['invoice.pdf'] = 'This is test File content' + mail(hash) + end + + def attachment_with_hash + attachments['invoice.jpg'] = { :data => "you smiling", :mime_type => "image/x-jpg", + :transfer_encoding => "base64" } + mail + end + + def implicit_multipart(hash = {}) + attachments['invoice.pdf'] = 'This is test File content' if hash.delete(:attachments) + mail(hash) + end + + def implicit_with_locale(hash = {}) + mail(hash) + end + + def explicit_multipart(hash = {}) + attachments['invoice.pdf'] = 'This is test File content' if hash.delete(:attachments) + mail(hash) do |format| + format.text { render :text => "TEXT Explicit Multipart" } + format.html { render :text => "HTML Explicit Multipart" } + end + end + + def explicit_multipart_templates(hash = {}) + mail(hash) do |format| + format.html + format.text + end + end + + def explicit_multipart_with_any(hash = {}) + mail(hash) do |format| + format.any(:text, :html){ render :text => "Format with any!" } + end + end + + def custom_block(include_html=false) + mail do |format| + format.text(:content_transfer_encoding => "base64"){ render "welcome" } + format.html{ render "welcome" } if include_html + end + end + end + + test "method call to mail does not raise error" do + assert_nothing_raised { BaseMailer.welcome } + end + + # Basic mail usage without block + test "mail() should set the headers of the mail message" do + email = BaseMailer.welcome + assert_equal(['system@test.lindsaar.net'], email.to) + assert_equal(['jose@test.plataformatec.com'], email.from) + assert_equal('The first email on new API!', email.subject) + end + + test "mail() with from overwrites the class level default" do + email = BaseMailer.welcome(:from => 'someone@example.com', + :to => 'another@example.org') + assert_equal(['someone@example.com'], email.from) + assert_equal(['another@example.org'], email.to) + end + + test "mail() with bcc, cc, content_type, charset, mime_version, reply_to and date" do + @time = Time.now.beginning_of_day.to_datetime + email = BaseMailer.welcome(:bcc => 'bcc@test.lindsaar.net', + :cc => 'cc@test.lindsaar.net', + :content_type => 'multipart/mixed', + :charset => 'iso-8559-1', + :mime_version => '2.0', + :reply_to => 'reply-to@test.lindsaar.net', + :date => @time) + assert_equal(['bcc@test.lindsaar.net'], email.bcc) + assert_equal(['cc@test.lindsaar.net'], email.cc) + assert_equal('multipart/mixed', email.content_type) + assert_equal('iso-8559-1', email.charset) + assert_equal('2.0', email.mime_version) + assert_equal(['reply-to@test.lindsaar.net'], email.reply_to) + assert_equal(@time, email.date) + end + + test "mail() renders the template using the method being processed" do + email = BaseMailer.welcome + assert_equal("Welcome", email.body.encoded) + end + + test "can pass in :body to the mail method hash" do + email = BaseMailer.welcome(:body => "Hello there") + assert_equal("text/plain", email.mime_type) + assert_equal("Hello there", email.body.encoded) + end + + # Custom headers + test "custom headers" do + email = BaseMailer.welcome + assert_equal("Not SPAM", email['X-SPAM'].decoded) + end + + test "can pass random headers in as a hash to mail" do + hash = {'X-Special-Domain-Specific-Header' => "SecretValue", + 'In-Reply-To' => '1234@mikel.me.com' } + mail = BaseMailer.simple(hash) + assert_equal('SecretValue', mail['X-Special-Domain-Specific-Header'].decoded) + assert_equal('1234@mikel.me.com', mail['In-Reply-To'].decoded) + end + + test "can pass random headers in as a hash" do + hash = {'X-Special-Domain-Specific-Header' => "SecretValue", + 'In-Reply-To' => '1234@mikel.me.com' } + mail = BaseMailer.simple_with_headers(hash) + assert_equal('SecretValue', mail['X-Special-Domain-Specific-Header'].decoded) + assert_equal('1234@mikel.me.com', mail['In-Reply-To'].decoded) + end + + # Attachments + test "attachment with content" do + email = BaseMailer.attachment_with_content + assert_equal(1, email.attachments.length) + assert_equal('invoice.pdf', email.attachments[0].filename) + assert_equal('This is test File content', email.attachments['invoice.pdf'].decoded) + end + + test "attachment gets content type from filename" do + email = BaseMailer.attachment_with_content + assert_equal('invoice.pdf', email.attachments[0].filename) + end + + test "attachment with hash" do + email = BaseMailer.attachment_with_hash + assert_equal(1, email.attachments.length) + assert_equal('invoice.jpg', email.attachments[0].filename) + expected = "\312\213\254\232)b" + expected.force_encoding(Encoding::BINARY) if '1.9'.respond_to?(:force_encoding) + assert_equal expected, email.attachments['invoice.jpg'].decoded + end + + test "sets mime type to multipart/mixed when attachment is included" do + email = BaseMailer.attachment_with_content + assert_equal(1, email.attachments.length) + assert_equal("multipart/mixed", email.mime_type) + end + + test "adds the rendered template as part" do + email = BaseMailer.attachment_with_content + assert_equal(2, email.parts.length) + assert_equal("multipart/mixed", email.mime_type) + assert_equal("text/html", email.parts[0].mime_type) + assert_equal("Attachment with content", email.parts[0].body.encoded) + assert_equal("application/pdf", email.parts[1].mime_type) + assert_equal("VGhpcyBpcyB0ZXN0IEZpbGUgY29udGVudA==\r\n", email.parts[1].body.encoded) + end + + test "adds the given :body as part" do + email = BaseMailer.attachment_with_content(:body => "I'm the eggman") + assert_equal(2, email.parts.length) + assert_equal("multipart/mixed", email.mime_type) + assert_equal("text/plain", email.parts[0].mime_type) + assert_equal("I'm the eggman", email.parts[0].body.encoded) + assert_equal("application/pdf", email.parts[1].mime_type) + assert_equal("VGhpcyBpcyB0ZXN0IEZpbGUgY29udGVudA==\r\n", email.parts[1].body.encoded) + end + + # Defaults values + test "uses default charset from class" do + with_default BaseMailer, :charset => "US-ASCII" do + email = BaseMailer.welcome + assert_equal("US-ASCII", email.charset) + + email = BaseMailer.welcome(:charset => "iso-8559-1") + assert_equal("iso-8559-1", email.charset) + end + end + + test "uses default content type from class" do + with_default BaseMailer, :content_type => "text/html" do + email = BaseMailer.welcome + assert_equal("text/html", email.mime_type) + + email = BaseMailer.welcome(:content_type => "text/plain") + assert_equal("text/plain", email.mime_type) + end + end + + test "uses default mime version from class" do + with_default BaseMailer, :mime_version => "2.0" do + email = BaseMailer.welcome + assert_equal("2.0", email.mime_version) + + email = BaseMailer.welcome(:mime_version => "1.0") + assert_equal("1.0", email.mime_version) + end + end + + test "uses random default headers from class" do + with_default BaseMailer, "X-SPAM" => "Not spam" do + email = BaseMailer.simple + assert_equal("Not spam", email["X-SPAM"].decoded) + end + end + + test "subject gets default from I18n" do + BaseMailer.default[:subject] = nil + email = BaseMailer.welcome(:subject => nil) + assert_equal "Welcome", email.subject + + I18n.backend.store_translations('en', :actionmailer => {:base_mailer => {:welcome => {:subject => "New Subject!"}}}) + email = BaseMailer.welcome(:subject => nil) + assert_equal "New Subject!", email.subject + end + + # Implicit multipart + test "implicit multipart" do + email = BaseMailer.implicit_multipart + assert_equal(2, email.parts.size) + assert_equal("multipart/alternative", email.mime_type) + assert_equal("text/plain", email.parts[0].mime_type) + assert_equal("TEXT Implicit Multipart", email.parts[0].body.encoded) + assert_equal("text/html", email.parts[1].mime_type) + assert_equal("HTML Implicit Multipart", email.parts[1].body.encoded) + end + + test "implicit multipart with sort order" do + order = ["text/html", "text/plain"] + with_default BaseMailer, :parts_order => order do + email = BaseMailer.implicit_multipart + assert_equal("text/html", email.parts[0].mime_type) + assert_equal("text/plain", email.parts[1].mime_type) + + email = BaseMailer.implicit_multipart(:parts_order => order.reverse) + assert_equal("text/plain", email.parts[0].mime_type) + assert_equal("text/html", email.parts[1].mime_type) + end + end + + test "implicit multipart with attachments creates nested parts" do + email = BaseMailer.implicit_multipart(:attachments => true) + assert_equal("application/pdf", email.parts[0].mime_type) + assert_equal("multipart/alternative", email.parts[1].mime_type) + assert_equal("text/plain", email.parts[1].parts[0].mime_type) + assert_equal("TEXT Implicit Multipart", email.parts[1].parts[0].body.encoded) + assert_equal("text/html", email.parts[1].parts[1].mime_type) + assert_equal("HTML Implicit Multipart", email.parts[1].parts[1].body.encoded) + end + + test "implicit multipart with attachments and sort order" do + order = ["text/html", "text/plain"] + with_default BaseMailer, :parts_order => order do + email = BaseMailer.implicit_multipart(:attachments => true) + assert_equal("application/pdf", email.parts[0].mime_type) + assert_equal("multipart/alternative", email.parts[1].mime_type) + assert_equal("text/plain", email.parts[1].parts[1].mime_type) + assert_equal("text/html", email.parts[1].parts[0].mime_type) + end + end + + test "implicit multipart with default locale" do + email = BaseMailer.implicit_with_locale + assert_equal(2, email.parts.size) + assert_equal("multipart/alternative", email.mime_type) + assert_equal("text/plain", email.parts[0].mime_type) + assert_equal("Implicit with locale TEXT", email.parts[0].body.encoded) + assert_equal("text/html", email.parts[1].mime_type) + assert_equal("Implicit with locale EN HTML", email.parts[1].body.encoded) + end + + test "implicit multipart with other locale" do + swap I18n, :locale => :pl do + email = BaseMailer.implicit_with_locale + assert_equal(2, email.parts.size) + assert_equal("multipart/alternative", email.mime_type) + assert_equal("text/plain", email.parts[0].mime_type) + assert_equal("Implicit with locale PL TEXT", email.parts[0].body.encoded) + assert_equal("text/html", email.parts[1].mime_type) + assert_equal("Implicit with locale HTML", email.parts[1].body.encoded) + end + end + + test "implicit multipart with several view paths uses the first one with template" do + begin + BaseMailer.view_paths.unshift(File.join(FIXTURE_LOAD_PATH, "another.path")) + email = BaseMailer.welcome + assert_equal("Welcome from another path", email.body.encoded) + ensure + BaseMailer.view_paths.shift + end + end + + test "implicit multipart with inexistent templates uses the next view path" do + begin + BaseMailer.view_paths.unshift(File.join(FIXTURE_LOAD_PATH, "unknown")) + email = BaseMailer.welcome + assert_equal("Welcome", email.body.encoded) + ensure + BaseMailer.view_paths.shift + end + end + + # Explicit multipart + test "explicit multipart" do + email = BaseMailer.explicit_multipart + assert_equal(2, email.parts.size) + assert_equal("multipart/alternative", email.mime_type) + assert_equal("text/plain", email.parts[0].mime_type) + assert_equal("TEXT Explicit Multipart", email.parts[0].body.encoded) + assert_equal("text/html", email.parts[1].mime_type) + assert_equal("HTML Explicit Multipart", email.parts[1].body.encoded) + end + + test "explicit multipart does not sort order" do + order = ["text/html", "text/plain"] + with_default BaseMailer, :parts_order => order do + email = BaseMailer.explicit_multipart + assert_equal("text/plain", email.parts[0].mime_type) + assert_equal("text/html", email.parts[1].mime_type) + + email = BaseMailer.explicit_multipart(:parts_order => order.reverse) + assert_equal("text/plain", email.parts[0].mime_type) + assert_equal("text/html", email.parts[1].mime_type) + end + end + + test "explicit multipart with attachments creates nested parts" do + email = BaseMailer.explicit_multipart(:attachments => true) + assert_equal("application/pdf", email.parts[0].mime_type) + assert_equal("multipart/alternative", email.parts[1].mime_type) + assert_equal("text/plain", email.parts[1].parts[0].mime_type) + assert_equal("TEXT Explicit Multipart", email.parts[1].parts[0].body.encoded) + assert_equal("text/html", email.parts[1].parts[1].mime_type) + assert_equal("HTML Explicit Multipart", email.parts[1].parts[1].body.encoded) + end + + test "explicit multipart with templates" do + email = BaseMailer.explicit_multipart_templates + assert_equal(2, email.parts.size) + assert_equal("multipart/alternative", email.mime_type) + assert_equal("text/html", email.parts[0].mime_type) + assert_equal("HTML Explicit Multipart Templates", email.parts[0].body.encoded) + assert_equal("text/plain", email.parts[1].mime_type) + assert_equal("TEXT Explicit Multipart Templates", email.parts[1].body.encoded) + end + + test "explicit multipart with any" do + email = BaseMailer.explicit_multipart_with_any + assert_equal(2, email.parts.size) + assert_equal("multipart/alternative", email.mime_type) + assert_equal("text/plain", email.parts[0].mime_type) + assert_equal("Format with any!", email.parts[0].body.encoded) + assert_equal("text/html", email.parts[1].mime_type) + assert_equal("Format with any!", email.parts[1].body.encoded) + end + + test "explicit multipart with options" do + email = BaseMailer.custom_block(true) + email.ready_to_send! + assert_equal(2, email.parts.size) + assert_equal("multipart/alternative", email.mime_type) + assert_equal("text/plain", email.parts[0].mime_type) + assert_equal("base64", email.parts[0].content_transfer_encoding) + assert_equal("text/html", email.parts[1].mime_type) + assert_equal("7bit", email.parts[1].content_transfer_encoding) + end + + test "explicit multipart with one part is rendered as body" do + email = BaseMailer.custom_block + assert_equal(0, email.parts.size) + assert_equal("text/plain", email.mime_type) + assert_equal("base64", email.content_transfer_encoding) + end + + # Class level API with method missing + test "should respond to action methods" do + assert BaseMailer.respond_to?(:welcome) + assert BaseMailer.respond_to?(:implicit_multipart) + assert !BaseMailer.respond_to?(:mail) + assert !BaseMailer.respond_to?(:headers) + end + + test "calling just the action should return the generated mail object" do + BaseMailer.deliveries.clear + email = BaseMailer.welcome + assert_equal(0, BaseMailer.deliveries.length) + assert_equal('The first email on new API!', email.subject) + end + + test "calling deliver on the action should deliver the mail object" do + BaseMailer.deliveries.clear + BaseMailer.expects(:deliver_mail).once + mail = BaseMailer.welcome.deliver + assert_instance_of Mail::Message, mail + end + + test "calling deliver on the action should increment the deliveries collection" do + BaseMailer.deliveries.clear + BaseMailer.welcome.deliver + assert_equal(1, BaseMailer.deliveries.length) + end + + test "calling deliver, ActionMailer should yield back to mail to let it call :do_delivery on itself" do + mail = Mail::Message.new + mail.expects(:do_delivery).once + BaseMailer.expects(:welcome).returns(mail) + BaseMailer.welcome.deliver + end + + test "explicit multipart should be multipart" do + mail = BaseMailer.explicit_multipart + assert_not_nil(mail.content_type_parameters[:boundary]) + end + + test "should set a content type if only has an html part" do + mail = BaseMailer.html_only + assert_equal('text/html', mail.mime_type) + end + + test "should set a content type if only has an plain text part" do + mail = BaseMailer.plain_text_only + assert_equal('text/plain', mail.mime_type) + end + + protected + + # Execute the block setting the given values and restoring old values after + # the block is executed. + def swap(klass, new_values) + old_values = {} + new_values.each do |key, value| + old_values[key] = klass.send key + klass.send :"#{key}=", value + end + yield + ensure + old_values.each do |key, value| + klass.send :"#{key}=", value + end + end + + def with_default(klass, new_values) + hash = klass.default + old_values = {} + new_values.each do |key, value| + old_values[key] = hash[key] + hash[key] = value + end + yield + ensure + old_values.each do |key, value| + hash[key] = value + end + end +end diff --git a/actionmailer/test/delivery_method_test.rb b/actionmailer/test/delivery_method_test.rb deleted file mode 100644 index 8f8c6b0275..0000000000 --- a/actionmailer/test/delivery_method_test.rb +++ /dev/null @@ -1,101 +0,0 @@ -require 'abstract_unit' - -class DefaultDeliveryMethodMailer < ActionMailer::Base -end - -class NonDefaultDeliveryMethodMailer < ActionMailer::Base - self.delivery_method = :sendmail -end - -class FileDeliveryMethodMailer < ActionMailer::Base - self.delivery_method = :file -end - -class CustomDeliveryMethod - attr_accessor :custom_deliveries - def initialize() - @customer_deliveries = [] - end - - def self.perform_delivery(mail) - self.custom_deliveries << mail - end -end - -class CustomerDeliveryMailer < ActionMailer::Base - self.delivery_method = CustomDeliveryMethod.new -end - -class ActionMailerBase_delivery_method_Test < Test::Unit::TestCase - def setup - set_delivery_method :smtp - end - - def teardown - restore_delivery_method - end - - def test_should_be_the_default_smtp - assert_instance_of ActionMailer::DeliveryMethod::Smtp, ActionMailer::Base.delivery_method - end -end - -class DefaultDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase - def setup - set_delivery_method :smtp - end - - def teardown - restore_delivery_method - end - - def test_should_be_the_default_smtp - assert_instance_of ActionMailer::DeliveryMethod::Smtp, DefaultDeliveryMethodMailer.delivery_method - end -end - -class NonDefaultDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase - def setup - set_delivery_method :smtp - end - - def teardown - restore_delivery_method - end - - def test_should_be_the_set_delivery_method - assert_instance_of ActionMailer::DeliveryMethod::Sendmail, NonDefaultDeliveryMethodMailer.delivery_method - end -end - -class FileDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase - def setup - set_delivery_method :smtp - end - - def teardown - restore_delivery_method - end - - def test_should_be_the_set_delivery_method - assert_instance_of ActionMailer::DeliveryMethod::File, FileDeliveryMethodMailer.delivery_method - end - - def test_should_default_location_to_the_tmpdir - assert_equal "#{Dir.tmpdir}/mails", ActionMailer::Base.file_settings[:location] - end -end - -class CustomDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase - def setup - set_delivery_method :smtp - end - - def teardown - restore_delivery_method - end - - def test_should_be_the_set_delivery_method - assert_instance_of CustomDeliveryMethod, CustomerDeliveryMailer.delivery_method - end -end diff --git a/actionmailer/test/delivery_methods_test.rb b/actionmailer/test/delivery_methods_test.rb new file mode 100644 index 0000000000..4907ca0903 --- /dev/null +++ b/actionmailer/test/delivery_methods_test.rb @@ -0,0 +1,170 @@ +require 'abstract_unit' +require 'mail' + +class MyCustomDelivery +end + +class BogusDelivery + def initialize(*) + end + + def deliver!(mail) + raise "failed" + end +end + +class DefaultsDeliveryMethodsTest < ActiveSupport::TestCase + test "default smtp settings" do + settings = { :address => "localhost", + :port => 25, + :domain => 'localhost.localdomain', + :user_name => nil, + :password => nil, + :authentication => nil, + :enable_starttls_auto => true } + assert_equal settings, ActionMailer::Base.smtp_settings + end + + test "default file delivery settings" do + settings = {:location => "#{Dir.tmpdir}/mails"} + assert_equal settings, ActionMailer::Base.file_settings + end + + test "default sendmail settings" do + settings = {:location => '/usr/sbin/sendmail', + :arguments => '-i -t'} + assert_equal settings, ActionMailer::Base.sendmail_settings + end +end + +class CustomDeliveryMethodsTest < ActiveSupport::TestCase + def setup + @old_delivery_method = ActionMailer::Base.delivery_method + ActionMailer::Base.add_delivery_method :custom, MyCustomDelivery + end + + def teardown + ActionMailer::Base.delivery_method = @old_delivery_method + ActionMailer::Base.delivery_methods.delete(:custom) + end + + test "allow to add custom delivery method" do + ActionMailer::Base.delivery_method = :custom + assert_equal :custom, ActionMailer::Base.delivery_method + end + + test "allow to customize custom settings" do + ActionMailer::Base.custom_settings = { :foo => :bar } + assert_equal Hash[:foo => :bar], ActionMailer::Base.custom_settings + end + + test "respond to custom settings" do + assert_respond_to ActionMailer::Base, :custom_settings + assert_respond_to ActionMailer::Base, :custom_settings= + end + + test "does not respond to unknown settings" do + assert_raise NoMethodError do + ActionMailer::Base.another_settings + end + end +end + +class MailDeliveryTest < ActiveSupport::TestCase + class DeliveryMailer < ActionMailer::Base + DEFAULT_HEADERS = { + :to => 'mikel@test.lindsaar.net', + :from => 'jose@test.plataformatec.com' + } + + def welcome(hash={}) + mail(DEFAULT_HEADERS.merge(hash)) + end + end + + def setup + ActionMailer::Base.delivery_method = :smtp + end + + def teardown + DeliveryMailer.delivery_method = :smtp + DeliveryMailer.perform_deliveries = true + DeliveryMailer.raise_delivery_errors = true + end + + test "ActionMailer should be told when Mail gets delivered" do + DeliveryMailer.deliveries.clear + DeliveryMailer.expects(:deliver_mail).once + DeliveryMailer.welcome.deliver + end + + test "delivery method can be customized per instance" do + email = DeliveryMailer.welcome.deliver + assert_instance_of Mail::SMTP, email.delivery_method + email = DeliveryMailer.welcome(:delivery_method => :test).deliver + assert_instance_of Mail::TestMailer, email.delivery_method + end + + test "delivery method can be customized in subclasses not changing the parent" do + DeliveryMailer.delivery_method = :test + assert_equal :smtp, ActionMailer::Base.delivery_method + $BREAK = true + email = DeliveryMailer.welcome.deliver + assert_instance_of Mail::TestMailer, email.delivery_method + end + + test "non registered delivery methods raises errors" do + DeliveryMailer.delivery_method = :unknown + assert_raise RuntimeError do + DeliveryMailer.welcome.deliver + end + end + + test "does not perform deliveries if requested" do + DeliveryMailer.perform_deliveries = false + DeliveryMailer.deliveries.clear + Mail::Message.any_instance.expects(:deliver!).never + DeliveryMailer.welcome.deliver + end + + test "does not append the deliveries collection if told not to perform the delivery" do + DeliveryMailer.perform_deliveries = false + DeliveryMailer.deliveries.clear + DeliveryMailer.welcome.deliver + assert_equal(0, DeliveryMailer.deliveries.length) + end + + test "raise errors on bogus deliveries" do + DeliveryMailer.delivery_method = BogusDelivery + DeliveryMailer.deliveries.clear + assert_raise RuntimeError do + DeliveryMailer.welcome.deliver + end + end + + test "does not increment the deliveries collection on error" do + DeliveryMailer.delivery_method = BogusDelivery + DeliveryMailer.deliveries.clear + assert_raise RuntimeError do + DeliveryMailer.welcome.deliver + end + assert_equal(0, DeliveryMailer.deliveries.length) + end + + test "does not raise errors on bogus deliveries if set" do + DeliveryMailer.delivery_method = BogusDelivery + DeliveryMailer.raise_delivery_errors = false + assert_nothing_raised do + DeliveryMailer.welcome.deliver + end + end + + test "does not increment the deliveries collection on bogus deliveries" do + DeliveryMailer.delivery_method = BogusDelivery + DeliveryMailer.raise_delivery_errors = false + DeliveryMailer.deliveries.clear + DeliveryMailer.welcome.deliver + assert_equal(0, DeliveryMailer.deliveries.length) + end + +end
\ No newline at end of file diff --git a/actionmailer/test/fixtures/another.path/base_mailer/welcome.erb b/actionmailer/test/fixtures/another.path/base_mailer/welcome.erb new file mode 100644 index 0000000000..ef451298e2 --- /dev/null +++ b/actionmailer/test/fixtures/another.path/base_mailer/welcome.erb @@ -0,0 +1 @@ +Welcome from another path
\ No newline at end of file diff --git a/actionmailer/test/fixtures/base_mailer/attachment_with_content.erb b/actionmailer/test/fixtures/base_mailer/attachment_with_content.erb new file mode 100644 index 0000000000..deb9dbd03b --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/attachment_with_content.erb @@ -0,0 +1 @@ +Attachment with content
\ No newline at end of file diff --git a/actionmailer/test/fixtures/base_mailer/explicit_multipart_templates.html.erb b/actionmailer/test/fixtures/base_mailer/explicit_multipart_templates.html.erb new file mode 100644 index 0000000000..c0a61b6249 --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/explicit_multipart_templates.html.erb @@ -0,0 +1 @@ +HTML Explicit Multipart Templates
\ No newline at end of file diff --git a/actionmailer/test/fixtures/base_mailer/explicit_multipart_templates.text.erb b/actionmailer/test/fixtures/base_mailer/explicit_multipart_templates.text.erb new file mode 100644 index 0000000000..507230b1de --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/explicit_multipart_templates.text.erb @@ -0,0 +1 @@ +TEXT Explicit Multipart Templates
\ No newline at end of file diff --git a/actionmailer/test/fixtures/base_mailer/html_only.html.erb b/actionmailer/test/fixtures/base_mailer/html_only.html.erb new file mode 100644 index 0000000000..9c99a008e7 --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/html_only.html.erb @@ -0,0 +1 @@ +<h1>Testing</h1>
\ No newline at end of file diff --git a/actionmailer/test/fixtures/base_mailer/implicit_multipart.html.erb b/actionmailer/test/fixtures/base_mailer/implicit_multipart.html.erb new file mode 100644 index 0000000000..23745cd282 --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/implicit_multipart.html.erb @@ -0,0 +1 @@ +HTML Implicit Multipart
\ No newline at end of file diff --git a/actionmailer/test/fixtures/base_mailer/implicit_multipart.text.erb b/actionmailer/test/fixtures/base_mailer/implicit_multipart.text.erb new file mode 100644 index 0000000000..d51437fc72 --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/implicit_multipart.text.erb @@ -0,0 +1 @@ +TEXT Implicit Multipart
\ No newline at end of file diff --git a/actionmailer/test/fixtures/base_mailer/implicit_with_locale.en.html.erb b/actionmailer/test/fixtures/base_mailer/implicit_with_locale.en.html.erb new file mode 100644 index 0000000000..34e39c8fdb --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/implicit_with_locale.en.html.erb @@ -0,0 +1 @@ +Implicit with locale EN HTML
\ No newline at end of file diff --git a/actionmailer/test/fixtures/base_mailer/implicit_with_locale.html.erb b/actionmailer/test/fixtures/base_mailer/implicit_with_locale.html.erb new file mode 100644 index 0000000000..5ce283f221 --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/implicit_with_locale.html.erb @@ -0,0 +1 @@ +Implicit with locale HTML
\ No newline at end of file diff --git a/actionmailer/test/fixtures/base_mailer/implicit_with_locale.pl.text.erb b/actionmailer/test/fixtures/base_mailer/implicit_with_locale.pl.text.erb new file mode 100644 index 0000000000..c49cbae60a --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/implicit_with_locale.pl.text.erb @@ -0,0 +1 @@ +Implicit with locale PL TEXT
\ No newline at end of file diff --git a/actionmailer/test/fixtures/base_mailer/implicit_with_locale.text.erb b/actionmailer/test/fixtures/base_mailer/implicit_with_locale.text.erb new file mode 100644 index 0000000000..5a18ff62cd --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/implicit_with_locale.text.erb @@ -0,0 +1 @@ +Implicit with locale TEXT
\ No newline at end of file diff --git a/actionmailer/test/fixtures/base_mailer/plain_text_only.text.erb b/actionmailer/test/fixtures/base_mailer/plain_text_only.text.erb new file mode 100644 index 0000000000..0a90125685 --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/plain_text_only.text.erb @@ -0,0 +1 @@ +Testing
\ No newline at end of file diff --git a/actionmailer/test/fixtures/base_mailer/welcome.erb b/actionmailer/test/fixtures/base_mailer/welcome.erb new file mode 100644 index 0000000000..01f3f00c63 --- /dev/null +++ b/actionmailer/test/fixtures/base_mailer/welcome.erb @@ -0,0 +1 @@ +Welcome
\ No newline at end of file diff --git a/actionmailer/test/fixtures/helper_mailer/use_example_helper.erb b/actionmailer/test/fixtures/helper_mailer/use_example_helper.erb deleted file mode 100644 index fcff3bb1b4..0000000000 --- a/actionmailer/test/fixtures/helper_mailer/use_example_helper.erb +++ /dev/null @@ -1 +0,0 @@ -So, <%= example_format(@text) %> diff --git a/actionmailer/test/fixtures/helper_mailer/use_helper.erb b/actionmailer/test/fixtures/helper_mailer/use_helper.erb deleted file mode 100644 index 378777f8bb..0000000000 --- a/actionmailer/test/fixtures/helper_mailer/use_helper.erb +++ /dev/null @@ -1 +0,0 @@ -Hello, <%= person_name %>. Thanks for registering! diff --git a/actionmailer/test/fixtures/helper_mailer/use_helper_method.erb b/actionmailer/test/fixtures/helper_mailer/use_helper_method.erb deleted file mode 100644 index d5b8b285e7..0000000000 --- a/actionmailer/test/fixtures/helper_mailer/use_helper_method.erb +++ /dev/null @@ -1 +0,0 @@ -This message brought to you by <%= name_of_the_mailer_class %>. diff --git a/actionmailer/test/fixtures/helper_mailer/use_mail_helper.erb b/actionmailer/test/fixtures/helper_mailer/use_mail_helper.erb deleted file mode 100644 index 96ec49d18a..0000000000 --- a/actionmailer/test/fixtures/helper_mailer/use_mail_helper.erb +++ /dev/null @@ -1,5 +0,0 @@ -From "Romeo and Juliet": - -<%= block_format @text %> - -Good ol' Shakespeare. diff --git a/actionmailer/test/fixtures/helpers/example_helper.rb b/actionmailer/test/fixtures/helpers/example_helper.rb deleted file mode 100644 index f6a6a49ced..0000000000 --- a/actionmailer/test/fixtures/helpers/example_helper.rb +++ /dev/null @@ -1,5 +0,0 @@ -module ExampleHelper - def example_format(text) - "<em><strong><small>#{h(text)}</small></strong></em>".html_safe! - end -end diff --git a/actionmailer/test/fixtures/nested_layout_mailer/signup.html.erb b/actionmailer/test/fixtures/nested_layout_mailer/signup.html.erb new file mode 100644 index 0000000000..4789e888c6 --- /dev/null +++ b/actionmailer/test/fixtures/nested_layout_mailer/signup.html.erb @@ -0,0 +1 @@ +We do not spam
\ No newline at end of file diff --git a/actionmailer/test/fixtures/test_mailer/body_ivar.erb b/actionmailer/test/fixtures/test_mailer/body_ivar.erb deleted file mode 100644 index 1421e5c908..0000000000 --- a/actionmailer/test/fixtures/test_mailer/body_ivar.erb +++ /dev/null @@ -1,2 +0,0 @@ -body: <%= @body %> -bar: <%= @bar %>
\ No newline at end of file diff --git a/actionmailer/test/fixtures/test_mailer/multipart_alternative.html.erb b/actionmailer/test/fixtures/test_mailer/multipart_alternative.html.erb new file mode 100644 index 0000000000..73ea14f82f --- /dev/null +++ b/actionmailer/test/fixtures/test_mailer/multipart_alternative.html.erb @@ -0,0 +1 @@ +<strong>foo</strong> <%= @foo %>
\ No newline at end of file diff --git a/actionmailer/test/fixtures/test_mailer/multipart_alternative.plain.erb b/actionmailer/test/fixtures/test_mailer/multipart_alternative.plain.erb new file mode 100644 index 0000000000..779fe4c1ea --- /dev/null +++ b/actionmailer/test/fixtures/test_mailer/multipart_alternative.plain.erb @@ -0,0 +1 @@ +foo: <%= @foo %>
\ No newline at end of file diff --git a/actionmailer/test/mail_helper_test.rb b/actionmailer/test/mail_helper_test.rb index 2d3565d159..7cc0804323 100644 --- a/actionmailer/test/mail_helper_test.rb +++ b/actionmailer/test/mail_helper_test.rb @@ -1,96 +1,54 @@ require 'abstract_unit' -module MailerHelper - def person_name - "Mr. Joe Person" - end -end - class HelperMailer < ActionMailer::Base - helper MailerHelper - helper :example - - def use_helper(recipient) - recipients recipient - subject "using helpers" - from "tester@example.com" - end - - def use_example_helper(recipient) - recipients recipient - subject "using helpers" - from "tester@example.com" - - @text = "emphasize me!" - end - - def use_mail_helper(recipient) - recipients recipient - subject "using mailing helpers" - from "tester@example.com" - + def use_mail_helper @text = "But soft! What light through yonder window breaks? It is the east, " + "and Juliet is the sun. Arise, fair sun, and kill the envious moon, " + "which is sick and pale with grief that thou, her maid, art far more " + "fair than she. Be not her maid, for she is envious! Her vestal " + "livery is but sick and green, and none but fools do wear it. Cast " + "it off!" - end - def use_helper_method(recipient) - recipients recipient - subject "using helpers" - from "tester@example.com" - - @text = "emphasize me!" + mail_with_defaults do |format| + format.html { render(:inline => "<%= block_format @text %>") } + end end - private - - def name_of_the_mailer_class - self.class.name + def use_mailer + mail_with_defaults do |format| + format.html { render(:inline => "<%= mailer.message.subject %>") } end - helper_method :name_of_the_mailer_class -end + end -class MailerHelperTest < Test::Unit::TestCase - def new_mail( charset="utf-8" ) - mail = Mail.new - mail.set_content_type "text", "plain", { "charset" => charset } if charset - mail + def use_message + mail_with_defaults do |format| + format.html { render(:inline => "<%= message.subject %>") } + end end - def setup - set_delivery_method :test - ActionMailer::Base.perform_deliveries = true - ActionMailer::Base.deliveries = [] + protected - @recipient = 'test@localhost' - end - - def teardown - restore_delivery_method - end - - def test_use_helper - mail = HelperMailer.create_use_helper(@recipient) - assert_match %r{Mr. Joe Person}, mail.encoded + def mail_with_defaults(&block) + mail(:to => "test@localhost", :from => "tester@example.com", + :subject => "using helpers", &block) end +end - def test_use_example_helper - mail = HelperMailer.create_use_example_helper(@recipient) - assert_match %r{<em><strong><small>emphasize me!}, mail.encoded +class MailerHelperTest < ActionMailer::TestCase + def test_use_mail_helper + mail = HelperMailer.use_mail_helper + assert_match %r{ But soft!}, mail.body.encoded + assert_match %r{east, and\r\n Juliet}, mail.body.encoded end - def test_use_helper_method - mail = HelperMailer.create_use_helper_method(@recipient) - assert_match %r{HelperMailer}, mail.encoded + def test_use_mailer + mail = HelperMailer.use_mailer + assert_match "using helpers", mail.body.encoded end - def test_use_mail_helper - mail = HelperMailer.create_use_mail_helper(@recipient) - assert_match %r{ But soft!}, mail.encoded - assert_match %r{east, and\r\n Juliet}, mail.encoded + def test_use_message + mail = HelperMailer.use_message + assert_match "using helpers", mail.body.encoded end end diff --git a/actionmailer/test/mail_test.rb b/actionmailer/test/mail_test.rb deleted file mode 100644 index ea6f25d157..0000000000 --- a/actionmailer/test/mail_test.rb +++ /dev/null @@ -1,24 +0,0 @@ -require 'abstract_unit' - -class MailTest < Test::Unit::TestCase - def test_body - m = Mail.new - expected = 'something_with_underscores' - m.content_transfer_encoding = 'quoted-printable' - quoted_body = [expected].pack('*M') - m.body = quoted_body - assert_equal "something_with_underscores=\r\n", m.body.encoded - # CHANGED: body returns object, not string, Changed m.body to m.body.to_s - assert_equal expected, m.body.to_s - end - - def test_nested_attachments_are_recognized_correctly - fixture = File.read("#{File.dirname(__FILE__)}/fixtures/raw_email_with_nested_attachment") - mail = Mail.new(fixture) - assert_equal 2, mail.attachments.length - assert_equal "image/png", mail.attachments.first.mime_type - assert_equal 1902, mail.attachments.first.decoded.length - assert_equal "application/pkcs7-signature", mail.attachments.last.mime_type - end - -end diff --git a/actionmailer/test/adv_attr_test.rb b/actionmailer/test/old_base/adv_attr_test.rb index f22d733bc5..f22d733bc5 100644 --- a/actionmailer/test/adv_attr_test.rb +++ b/actionmailer/test/old_base/adv_attr_test.rb diff --git a/actionmailer/test/asset_host_test.rb b/actionmailer/test/old_base/asset_host_test.rb index f3383e5608..124032f1d9 100644 --- a/actionmailer/test/asset_host_test.rb +++ b/actionmailer/test/old_base/asset_host_test.rb @@ -1,8 +1,8 @@ require 'abstract_unit' class AssetHostMailer < ActionMailer::Base - def email_with_asset(recipient) - recipients recipient + def email_with_asset + recipients 'test@localhost' subject "testing email containing asset path while asset_host is set" from "tester@example.com" end @@ -12,9 +12,7 @@ class AssetHostTest < Test::Unit::TestCase def setup set_delivery_method :test ActionMailer::Base.perform_deliveries = true - ActionMailer::Base.deliveries = [] - - @recipient = 'test@localhost' + ActionMailer::Base.deliveries.clear end def teardown @@ -23,7 +21,7 @@ class AssetHostTest < Test::Unit::TestCase def test_asset_host_as_string ActionController::Base.asset_host = "http://www.example.com" - mail = AssetHostMailer.deliver_email_with_asset(@recipient) + mail = AssetHostMailer.email_with_asset assert_equal "<img alt=\"Somelogo\" src=\"http://www.example.com/images/somelogo.png\" />", mail.body.to_s.strip end @@ -35,7 +33,7 @@ class AssetHostTest < Test::Unit::TestCase "http://assets.example.com" end } - mail = AssetHostMailer.deliver_email_with_asset(@recipient) + mail = AssetHostMailer.email_with_asset assert_equal "<img alt=\"Somelogo\" src=\"http://images.example.com/images/somelogo.png\" />", mail.body.to_s.strip end @@ -48,7 +46,7 @@ class AssetHostTest < Test::Unit::TestCase end } mail = nil - assert_nothing_raised { mail = AssetHostMailer.deliver_email_with_asset(@recipient) } + assert_nothing_raised { mail = AssetHostMailer.email_with_asset } assert_equal "<img alt=\"Somelogo\" src=\"http://www.example.com/images/somelogo.png\" />", mail.body.to_s.strip end end
\ No newline at end of file diff --git a/actionmailer/test/mail_layout_test.rb b/actionmailer/test/old_base/mail_layout_test.rb index 0877e7b2cb..5679aa5a64 100644 --- a/actionmailer/test/mail_layout_test.rb +++ b/actionmailer/test/old_base/mail_layout_test.rb @@ -1,32 +1,33 @@ require 'abstract_unit' class AutoLayoutMailer < ActionMailer::Base - def hello(recipient) - recipients recipient + + def hello + recipients 'test@localhost' subject "You have a mail" from "tester@example.com" end - def spam(recipient) - recipients recipient + def spam + recipients 'test@localhost' subject "You have a mail" from "tester@example.com" @world = "Earth" - render(:inline => "Hello, <%= @world %>", :layout => 'spam') + body render(:inline => "Hello, <%= @world %>", :layout => 'spam') end - def nolayout(recipient) - recipients recipient + def nolayout + recipients 'test@localhost' subject "You have a mail" from "tester@example.com" @world = "Earth" - render(:inline => "Hello, <%= @world %>", :layout => false) + body render(:inline => "Hello, <%= @world %>", :layout => false) end - def multipart(recipient, type = nil) - recipients recipient + def multipart(type = nil) + recipients 'test@localhost' subject "You have a mail" from "tester@example.com" @@ -37,14 +38,14 @@ end class ExplicitLayoutMailer < ActionMailer::Base layout 'spam', :except => [:logout] - def signup(recipient) - recipients recipient + def signup + recipients 'test@localhost' subject "You have a mail" from "tester@example.com" end - def logout(recipient) - recipients recipient + def logout + recipients 'test@localhost' subject "You have a mail" from "tester@example.com" end @@ -54,9 +55,7 @@ class LayoutMailerTest < Test::Unit::TestCase def setup set_delivery_method :test ActionMailer::Base.perform_deliveries = true - ActionMailer::Base.deliveries = [] - - @recipient = 'test@localhost' + ActionMailer::Base.deliveries.clear end def teardown @@ -64,12 +63,12 @@ class LayoutMailerTest < Test::Unit::TestCase end def test_should_pickup_default_layout - mail = AutoLayoutMailer.create_hello(@recipient) + mail = AutoLayoutMailer.hello assert_equal "Hello from layout Inside", mail.body.to_s.strip end def test_should_pickup_multipart_layout - mail = AutoLayoutMailer.create_multipart(@recipient) + mail = AutoLayoutMailer.multipart # CHANGED: content_type returns an object # assert_equal "multipart/alternative", mail.content_type assert_equal "multipart/alternative", mail.mime_type @@ -78,7 +77,7 @@ class LayoutMailerTest < Test::Unit::TestCase # CHANGED: content_type returns an object # assert_equal 'text/plain', mail.parts.first.content_type assert_equal 'text/plain', mail.parts.first.mime_type - + # CHANGED: body returns an object # assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body assert_equal "text/plain layout - text/plain multipart", mail.parts.first.body.to_s @@ -93,7 +92,7 @@ class LayoutMailerTest < Test::Unit::TestCase end def test_should_pickup_multipartmixed_layout - mail = AutoLayoutMailer.create_multipart(@recipient, "multipart/mixed") + mail = AutoLayoutMailer.multipart("multipart/mixed") # CHANGED: content_type returns an object # assert_equal "multipart/mixed", mail.content_type assert_equal "multipart/mixed", mail.mime_type @@ -115,7 +114,7 @@ class LayoutMailerTest < Test::Unit::TestCase end def test_should_fix_multipart_layout - mail = AutoLayoutMailer.create_multipart(@recipient, "text/plain") + mail = AutoLayoutMailer.multipart("text/plain") assert_equal "multipart/alternative", mail.mime_type assert_equal 2, mail.parts.size @@ -128,22 +127,22 @@ class LayoutMailerTest < Test::Unit::TestCase def test_should_pickup_layout_given_to_render - mail = AutoLayoutMailer.create_spam(@recipient) + mail = AutoLayoutMailer.spam assert_equal "Spammer layout Hello, Earth", mail.body.to_s.strip end def test_should_respect_layout_false - mail = AutoLayoutMailer.create_nolayout(@recipient) + mail = AutoLayoutMailer.nolayout assert_equal "Hello, Earth", mail.body.to_s.strip end def test_explicit_class_layout - mail = ExplicitLayoutMailer.create_signup(@recipient) + mail = ExplicitLayoutMailer.signup assert_equal "Spammer layout We do not spam", mail.body.to_s.strip end def test_explicit_layout_exceptions - mail = ExplicitLayoutMailer.create_logout(@recipient) + mail = ExplicitLayoutMailer.logout assert_equal "You logged out", mail.body.to_s.strip end end diff --git a/actionmailer/test/mail_render_test.rb b/actionmailer/test/old_base/mail_render_test.rb index 09ce5e4854..405d43d7d3 100644 --- a/actionmailer/test/mail_render_test.rb +++ b/actionmailer/test/old_base/mail_render_test.rb @@ -1,60 +1,43 @@ require 'abstract_unit' class RenderMailer < ActionMailer::Base - def inline_template(recipient) - recipients recipient + def inline_template + recipients 'test@localhost' subject "using helpers" from "tester@example.com" @world = "Earth" - render :inline => "Hello, <%= @world %>" + body render(:inline => "Hello, <%= @world %>") end - def file_template(recipient) - recipients recipient + def file_template + recipients 'test@localhost' subject "using helpers" from "tester@example.com" - @recipient = recipient - render :file => "templates/signed_up" - end - - def implicit_body(recipient) - recipients recipient - subject "using helpers" - from "tester@example.com" - - @recipient = recipient - render :template => "templates/signed_up" + @recipient = 'test@localhost' + body render(:file => "templates/signed_up") end - def rxml_template(recipient) - recipients recipient + def rxml_template + recipients 'test@localhost' subject "rendering rxml template" from "tester@example.com" end - def included_subtemplate(recipient) - recipients recipient + def included_subtemplate + recipients 'test@localhost' subject "Including another template in the one being rendered" from "tester@example.com" end - def mailer_accessor(recipient) - recipients recipient - subject "Mailer Accessor" - from "tester@example.com" - - render :inline => "Look, <%= mailer.subject %>!" - end - - def no_instance_variable(recipient) - recipients recipient + def no_instance_variable + recipients 'test@localhost' subject "No Instance Variable" from "tester@example.com" silence_warnings do - render :inline => "Look, subject.nil? is <%= @subject.nil? %>!" + body render(:inline => "Look, subject.nil? is <%= @subject.nil? %>!") end end @@ -62,19 +45,46 @@ class RenderMailer < ActionMailer::Base super mailer_name "test_mailer" end + + def multipart_alternative + recipients 'test@localhost' + subject 'multipart/alternative' + from 'tester@example.com' + + build_multipart_message(:foo => "bar") + end + + private + def build_multipart_message(assigns = {}) + content_type "multipart/alternative" + + part "text/plain" do |p| + p.body = build_body_part('plain', assigns, :layout => false) + end + + part "text/html" do |p| + p.body = build_body_part('html', assigns) + end + end + + def build_body_part(content_type, assigns, options = {}) + ActiveSupport::Deprecation.silence do + render "#{template}.#{content_type}", :body => assigns + end + end end class FirstMailer < ActionMailer::Base - def share(recipient) - recipients recipient + def share + recipients 'test@localhost' subject "using helpers" from "tester@example.com" end end class SecondMailer < ActionMailer::Base - def share(recipient) - recipients recipient + def share + recipients 'test@localhost' subject "using helpers" from "tester@example.com" end @@ -86,7 +96,7 @@ class RenderHelperTest < Test::Unit::TestCase def setup set_delivery_method :test ActionMailer::Base.perform_deliveries = true - ActionMailer::Base.deliveries = [] + ActionMailer::Base.deliveries.clear @recipient = 'test@localhost' end @@ -95,47 +105,47 @@ class RenderHelperTest < Test::Unit::TestCase restore_delivery_method end - def test_implicit_body - mail = RenderMailer.create_implicit_body(@recipient) - assert_equal "Hello there, \n\nMr. test@localhost", mail.body.to_s.strip - end - def test_inline_template - mail = RenderMailer.create_inline_template(@recipient) + mail = RenderMailer.inline_template assert_equal "Hello, Earth", mail.body.to_s.strip end def test_file_template - mail = RenderMailer.create_file_template(@recipient) + mail = RenderMailer.file_template assert_equal "Hello there, \n\nMr. test@localhost", mail.body.to_s.strip end def test_rxml_template - mail = RenderMailer.deliver_rxml_template(@recipient) + mail = RenderMailer.rxml_template.deliver assert_equal "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<test/>", mail.body.to_s.strip end def test_included_subtemplate - mail = RenderMailer.deliver_included_subtemplate(@recipient) + mail = RenderMailer.included_subtemplate.deliver assert_equal "Hey Ho, let's go!", mail.body.to_s.strip end - def test_mailer_accessor - mail = RenderMailer.deliver_mailer_accessor(@recipient) - assert_equal "Look, Mailer Accessor!", mail.body.to_s.strip - end - def test_no_instance_variable - mail = RenderMailer.deliver_no_instance_variable(@recipient) + mail = RenderMailer.no_instance_variable.deliver assert_equal "Look, subject.nil? is true!", mail.body.to_s.strip end + + def test_legacy_multipart_alternative + mail = RenderMailer.multipart_alternative.deliver + assert_equal(2, mail.parts.size) + assert_equal("multipart/alternative", mail.mime_type) + assert_equal("text/plain", mail.parts[0].mime_type) + assert_equal("foo: bar", mail.parts[0].body.encoded) + assert_equal("text/html", mail.parts[1].mime_type) + assert_equal("<strong>foo</strong> bar", mail.parts[1].body.encoded) + end end class FirstSecondHelperTest < Test::Unit::TestCase def setup set_delivery_method :test ActionMailer::Base.perform_deliveries = true - ActionMailer::Base.deliveries = [] + ActionMailer::Base.deliveries.clear @recipient = 'test@localhost' end @@ -145,13 +155,13 @@ class FirstSecondHelperTest < Test::Unit::TestCase end def test_ordering - mail = FirstMailer.create_share(@recipient) + mail = FirstMailer.share assert_equal "first mail", mail.body.to_s.strip - mail = SecondMailer.create_share(@recipient) + mail = SecondMailer.share assert_equal "second mail", mail.body.to_s.strip - mail = FirstMailer.create_share(@recipient) + mail = FirstMailer.share assert_equal "first mail", mail.body.to_s.strip - mail = SecondMailer.create_share(@recipient) + mail = SecondMailer.share assert_equal "second mail", mail.body.to_s.strip end end diff --git a/actionmailer/test/mail_service_test.rb b/actionmailer/test/old_base/mail_service_test.rb index f87d9b2e5b..70dafaf33c 100644 --- a/actionmailer/test/mail_service_test.rb +++ b/actionmailer/test/old_base/mail_service_test.rb @@ -2,7 +2,7 @@ require 'abstract_unit' class FunkyPathMailer < ActionMailer::Base - self.template_root = "#{File.dirname(__FILE__)}/fixtures/path.with.dots" + self.view_paths = "#{File.dirname(__FILE__)}/../fixtures/path.with.dots" def multipart_with_template_path_with_dots(recipient) recipients recipient @@ -27,20 +27,19 @@ class TestMailer < ActionMailer::Base subject "[Cancelled] Goodbye #{recipient}" from "system@loudthinking.com" sent_on Time.local(2004, 12, 12) - - render :text => "Goodbye, Mr. #{recipient}" + body "Goodbye, Mr. #{recipient}" end def from_with_name from "System <system@loudthinking.com>" recipients "root@loudthinking.com" - render :text => "Nothing to see here." + body "Nothing to see here." end def from_without_name from "system@loudthinking.com" recipients "root@loudthinking.com" - render :text => "Nothing to see here." + body "Nothing to see here." end def cc_bcc(recipient) @@ -51,7 +50,7 @@ class TestMailer < ActionMailer::Base cc "nobody@loudthinking.com" bcc "root@loudthinking.com" - render :text => "Nothing to see here." + body "Nothing to see here." end def different_reply_to(recipient) @@ -61,7 +60,7 @@ class TestMailer < ActionMailer::Base sent_on Time.local(2008, 5, 23) reply_to "atraver@gmail.com" - render :text => "Nothing to see here." + body "Nothing to see here." end def iso_charset(recipient) @@ -73,7 +72,7 @@ class TestMailer < ActionMailer::Base bcc "root@loudthinking.com" charset "iso-8859-1" - render :text => "Nothing to see here." + body "Nothing to see here." end def unencoded_subject(recipient) @@ -84,7 +83,7 @@ class TestMailer < ActionMailer::Base cc "nobody@loudthinking.com" bcc "root@loudthinking.com" - render :text => "Nothing to see here." + body "Nothing to see here." end def extended_headers(recipient) @@ -96,7 +95,7 @@ class TestMailer < ActionMailer::Base bcc "Grytøyr <stian3@example.net>" charset "iso-8859-1" - render :text => "Nothing to see here." + body "Nothing to see here." end def utf8_body(recipient) @@ -108,7 +107,7 @@ class TestMailer < ActionMailer::Base bcc "Foo áëô îü <extended@example.net>" charset "utf-8" - render :text => "åœö blah" + body "åœö blah" end def multipart_with_mime_version(recipient) @@ -120,11 +119,11 @@ class TestMailer < ActionMailer::Base content_type "multipart/alternative" part "text/plain" do |p| - p.body = render_message(:text => "blah") + p.body = render(:text => "blah") end part "text/html" do |p| - p.body = render_message(:inline => "<%= content_tag(:b, 'blah') %>") + p.body = render(:inline => "<%= content_tag(:b, 'blah') %>") end end @@ -158,7 +157,7 @@ class TestMailer < ActionMailer::Base attachment :content_type => "image/jpeg", :filename => File.join(File.dirname(__FILE__), "fixtures", "attachments", "foo.jpg"), :data => "123456789" - render :text => "plain text default" + body "plain text default" end def implicitly_multipart_example(recipient, cs = nil, order = nil) @@ -187,12 +186,12 @@ class TestMailer < ActionMailer::Base from "test@example.com" content_type "text/html" - render :text => "<em>Emphasize</em> <strong>this</strong>" + body "<em>Emphasize</em> <strong>this</strong>" end def html_mail_with_underscores(recipient) subject "html mail with underscores" - render :text => %{<a href="http://google.com" target="_blank">_Google</a>} + body %{<a href="http://google.com" target="_blank">_Google</a>} end def custom_template(recipient) @@ -219,7 +218,7 @@ class TestMailer < ActionMailer::Base subject "various newlines" from "test@example.com" - render :text => "line #1\nline #2\rline #3\r\nline #4\r\r" + + body "line #1\nline #2\rline #3\r\nline #4\r\r" + "line #5\n\nline#6\r\n\r\nline #7" end @@ -282,7 +281,7 @@ class TestMailer < ActionMailer::Base from "One: Two <test@example.com>" cc "Three: Four <test@example.com>" bcc "Five: Six <test@example.com>" - render :text => "testing" + body "testing" end def custom_content_type_attributes @@ -290,28 +289,21 @@ class TestMailer < ActionMailer::Base subject "custom content types" from "some.one@somewhere.test" content_type "text/plain; format=flowed" - render :text => "testing" + body "testing" end def return_path recipients "no.one@nowhere.test" subject "return path test" from "some.one@somewhere.test" - headers "return-path" => "another@somewhere.test" - render :text => "testing" - end - - def body_ivar(recipient) - recipients recipient - subject "Body as a local variable" - from "test@example.com" - body :body => "foo", :bar => "baz" + headers["return-path"] = "another@somewhere.test" + body "testing" end def subject_with_i18n(recipient) recipients recipient from "system@loudthinking.com" - render :text => "testing" + body "testing" end class << self @@ -344,7 +336,7 @@ class ActionMailerTest < Test::Unit::TestCase set_delivery_method :test ActionMailer::Base.perform_deliveries = true ActionMailer::Base.raise_delivery_errors = true - ActionMailer::Base.deliveries = [] + ActionMailer::Base.deliveries.clear @original_logger = TestMailer.logger @recipient = 'test@localhost' @@ -357,7 +349,7 @@ class ActionMailerTest < Test::Unit::TestCase def test_nested_parts created = nil - assert_nothing_raised { created = TestMailer.create_nested_multipart(@recipient)} + assert_nothing_raised { created = TestMailer.nested_multipart(@recipient)} assert_equal 2, created.parts.size assert_equal 2, created.parts.first.parts.size @@ -373,8 +365,8 @@ class ActionMailerTest < Test::Unit::TestCase def test_nested_parts_with_body created = nil - TestMailer.create_nested_multipart_with_body(@recipient) - assert_nothing_raised { created = TestMailer.create_nested_multipart_with_body(@recipient)} + TestMailer.nested_multipart_with_body(@recipient) + assert_nothing_raised { created = TestMailer.nested_multipart_with_body(@recipient)} assert_equal 1,created.parts.size assert_equal 2,created.parts.first.parts.size @@ -389,11 +381,13 @@ class ActionMailerTest < Test::Unit::TestCase def test_attachment_with_custom_header created = nil - assert_nothing_raised { created = TestMailer.create_attachment_with_custom_header(@recipient) } + assert_nothing_raised { created = TestMailer.attachment_with_custom_header(@recipient) } assert created.parts.any? { |p| p.header['content-id'].to_s == "<test@test.com>" } end def test_signed_up + TestMailer.delivery_method = :test + Time.stubs(:now => Time.now) expected = new_mail @@ -404,7 +398,7 @@ class ActionMailerTest < Test::Unit::TestCase expected.date = Time.now created = nil - assert_nothing_raised { created = TestMailer.create_signed_up(@recipient) } + assert_nothing_raised { created = TestMailer.signed_up(@recipient) } assert_not_nil created expected.message_id = '<123@456>' @@ -412,7 +406,7 @@ class ActionMailerTest < Test::Unit::TestCase assert_equal expected.encoded, created.encoded - assert_nothing_raised { TestMailer.deliver_signed_up(@recipient) } + assert_nothing_raised { TestMailer.signed_up(@recipient).deliver } delivered = ActionMailer::Base.deliveries.first assert_not_nil delivered @@ -423,15 +417,6 @@ class ActionMailerTest < Test::Unit::TestCase assert_equal expected.encoded, delivered.encoded end - def test_subject_with_i18n - assert_nothing_raised { TestMailer.deliver_subject_with_i18n(@recipient) } - assert_equal "Subject with i18n", ActionMailer::Base.deliveries.first.subject.to_s - - I18n.backend.store_translations('en', :actionmailer => {:test_mailer => {:subject_with_i18n => {:subject => "New Subject!"}}}) - assert_nothing_raised { TestMailer.deliver_subject_with_i18n(@recipient) } - assert_equal "New Subject!", ActionMailer::Base.deliveries.last.subject.to_s - end - def test_custom_template expected = new_mail expected.to = @recipient @@ -441,7 +426,7 @@ class ActionMailerTest < Test::Unit::TestCase expected.date = Time.local(2004, 12, 12) created = nil - assert_nothing_raised { created = TestMailer.create_custom_template(@recipient) } + assert_nothing_raised { created = TestMailer.custom_template(@recipient) } assert_not_nil created expected.message_id = '<123@456>' created.message_id = '<123@456>' @@ -464,7 +449,7 @@ class ActionMailerTest < Test::Unit::TestCase # Now that the template is registered, there should be one part. The text/plain part. created = nil - assert_nothing_raised { created = TestMailer.create_custom_templating_extension(@recipient) } + assert_nothing_raised { created = TestMailer.custom_templating_extension(@recipient) } assert_not_nil created assert_equal 2, created.parts.length assert_equal 'text/plain', created.parts[0].mime_type @@ -480,13 +465,13 @@ class ActionMailerTest < Test::Unit::TestCase expected.date = Time.local(2004, 12, 12) created = nil - assert_nothing_raised { created = TestMailer.create_cancelled_account(@recipient) } + assert_nothing_raised { created = TestMailer.cancelled_account(@recipient) } assert_not_nil created expected.message_id = '<123@456>' created.message_id = '<123@456>' assert_equal expected.encoded, created.encoded - assert_nothing_raised { TestMailer.deliver_cancelled_account(@recipient) } + assert_nothing_raised { TestMailer.cancelled_account(@recipient).deliver } assert_not_nil ActionMailer::Base.deliveries.first delivered = ActionMailer::Base.deliveries.first expected.message_id = '<123@456>' @@ -507,7 +492,7 @@ class ActionMailerTest < Test::Unit::TestCase created = nil assert_nothing_raised do - created = TestMailer.create_cc_bcc @recipient + created = TestMailer.cc_bcc @recipient end assert_not_nil created expected.message_id = '<123@456>' @@ -515,7 +500,7 @@ class ActionMailerTest < Test::Unit::TestCase assert_equal expected.encoded, created.encoded assert_nothing_raised do - TestMailer.deliver_cc_bcc @recipient + TestMailer.cc_bcc(@recipient).deliver end assert_not_nil ActionMailer::Base.deliveries.first @@ -527,8 +512,8 @@ class ActionMailerTest < Test::Unit::TestCase end def test_from_without_name_for_smtp - ActionMailer::Base.delivery_method = :smtp - TestMailer.deliver_from_without_name + TestMailer.delivery_method = :smtp + TestMailer.from_without_name.deliver mail = MockSMTP.deliveries.first assert_not_nil mail @@ -538,17 +523,19 @@ class ActionMailerTest < Test::Unit::TestCase end def test_from_with_name_for_smtp - ActionMailer::Base.delivery_method = :smtp - TestMailer.deliver_from_with_name + TestMailer.delivery_method = :smtp + TestMailer.from_with_name.deliver mail = MockSMTP.deliveries.first assert_not_nil mail mail, from, to = mail - assert_equal 'system@loudthinking.com', from.addresses.first + assert_equal 'system@loudthinking.com', from end def test_reply_to + TestMailer.delivery_method = :test + expected = new_mail expected.to = @recipient @@ -560,7 +547,7 @@ class ActionMailerTest < Test::Unit::TestCase created = nil assert_nothing_raised do - created = TestMailer.create_different_reply_to @recipient + created = TestMailer.different_reply_to @recipient end assert_not_nil created @@ -570,7 +557,7 @@ class ActionMailerTest < Test::Unit::TestCase assert_equal expected.encoded, created.encoded assert_nothing_raised do - TestMailer.deliver_different_reply_to @recipient + TestMailer.different_reply_to(@recipient).deliver end delivered = ActionMailer::Base.deliveries.first @@ -583,6 +570,8 @@ class ActionMailerTest < Test::Unit::TestCase end def test_iso_charset + TestMailer.delivery_method = :test + expected = new_mail( "iso-8859-1" ) expected.to = @recipient expected.subject = encode "testing isø charsets", "iso-8859-1" @@ -594,7 +583,7 @@ class ActionMailerTest < Test::Unit::TestCase created = nil assert_nothing_raised do - created = TestMailer.create_iso_charset @recipient + created = TestMailer.iso_charset @recipient end assert_not_nil created @@ -604,7 +593,7 @@ class ActionMailerTest < Test::Unit::TestCase assert_equal expected.encoded, created.encoded assert_nothing_raised do - TestMailer.deliver_iso_charset @recipient + TestMailer.iso_charset(@recipient).deliver end delivered = ActionMailer::Base.deliveries.first @@ -617,6 +606,7 @@ class ActionMailerTest < Test::Unit::TestCase end def test_unencoded_subject + TestMailer.delivery_method = :test expected = new_mail expected.to = @recipient expected.subject = "testing unencoded subject" @@ -628,7 +618,7 @@ class ActionMailerTest < Test::Unit::TestCase created = nil assert_nothing_raised do - created = TestMailer.create_unencoded_subject @recipient + created = TestMailer.unencoded_subject @recipient end assert_not_nil created @@ -638,7 +628,7 @@ class ActionMailerTest < Test::Unit::TestCase assert_equal expected.encoded, created.encoded assert_nothing_raised do - TestMailer.deliver_unencoded_subject @recipient + TestMailer.unencoded_subject(@recipient).deliver end delivered = ActionMailer::Base.deliveries.first @@ -650,41 +640,33 @@ class ActionMailerTest < Test::Unit::TestCase assert_equal expected.encoded, delivered.encoded end - def test_instances_are_nil - assert_nil ActionMailer::Base.new - assert_nil TestMailer.new - end - def test_deliveries_array assert_not_nil ActionMailer::Base.deliveries assert_equal 0, ActionMailer::Base.deliveries.size - TestMailer.deliver_signed_up(@recipient) + TestMailer.signed_up(@recipient).deliver assert_equal 1, ActionMailer::Base.deliveries.size assert_not_nil ActionMailer::Base.deliveries.first end def test_perform_deliveries_flag ActionMailer::Base.perform_deliveries = false - TestMailer.deliver_signed_up(@recipient) + TestMailer.signed_up(@recipient).deliver assert_equal 0, ActionMailer::Base.deliveries.size ActionMailer::Base.perform_deliveries = true - TestMailer.deliver_signed_up(@recipient) + TestMailer.signed_up(@recipient).deliver assert_equal 1, ActionMailer::Base.deliveries.size end def test_doesnt_raise_errors_when_raise_delivery_errors_is_false ActionMailer::Base.raise_delivery_errors = false - TestMailer.delivery_method.expects(:perform_delivery).raises(Exception) - assert_nothing_raised { TestMailer.deliver_signed_up(@recipient) } + Mail::TestMailer.any_instance.expects(:deliver!).raises(Exception) + assert_nothing_raised { TestMailer.signed_up(@recipient).deliver } end def test_performs_delivery_via_sendmail - sm = mock() - sm.expects(:print).with(anything) - sm.expects(:flush) - IO.expects(:popen).once.with('/usr/sbin/sendmail -i -t', 'w+').yields(sm) - ActionMailer::Base.delivery_method = :sendmail - TestMailer.deliver_signed_up(@recipient) + IO.expects(:popen).once.with('/usr/sbin/sendmail -i -t -f "system@loudthinking.com" test@localhost', 'w+') + TestMailer.delivery_method = :sendmail + TestMailer.signed_up(@recipient).deliver end def test_unquote_quoted_printable_subject @@ -769,7 +751,7 @@ EOF created = nil assert_nothing_raised do - created = TestMailer.create_extended_headers @recipient + created = TestMailer.extended_headers @recipient end assert_not_nil created @@ -779,7 +761,7 @@ EOF assert_equal expected.encoded, created.encoded assert_nothing_raised do - TestMailer.deliver_extended_headers @recipient + TestMailer.extended_headers(@recipient).deliver end delivered = ActionMailer::Base.deliveries.first @@ -802,7 +784,7 @@ EOF expected.bcc = quote_address_if_necessary @recipient, "utf-8" expected.date = Time.local 2004, 12, 12 - created = TestMailer.create_utf8_body @recipient + created = TestMailer.utf8_body @recipient assert_match(/åœö blah/, created.encoded) end @@ -817,82 +799,82 @@ EOF expected.bcc = quote_address_if_necessary @recipient, "utf-8" expected.date = Time.local 2004, 12, 12 - created = TestMailer.create_utf8_body @recipient + created = TestMailer.utf8_body @recipient assert_match(/\nFrom: =\?utf-8\?Q\?Foo_.*?\?= <extended@example.net>\r/, created.encoded) assert_match(/\nTo: =\?utf-8\?Q\?Foo_.*?\?= <extended@example.net>, \r\n\tExample Recipient <me/, created.encoded) end def test_receive_decodes_base64_encoded_mail - fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email") + fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email") TestMailer.receive(fixture) assert_match(/Jamis/, TestMailer.received_body.to_s) end def test_receive_attachments - fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email2") + fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email2") mail = Mail.new(fixture) attachment = mail.attachments.last - assert_equal "smime.p7s", attachment.original_filename + assert_equal "smime.p7s", attachment.filename assert_equal "application/pkcs7-signature", mail.parts.last.mime_type end def test_decode_attachment_without_charset - fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email3") + fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email3") mail = Mail.new(fixture) attachment = mail.attachments.last assert_equal 1026, attachment.read.length end def test_attachment_using_content_location - fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email12") + fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email12") mail = Mail.new(fixture) assert_equal 1, mail.attachments.length - assert_equal "Photo25.jpg", mail.attachments.first.original_filename + assert_equal "Photo25.jpg", mail.attachments.first.filename end def test_attachment_with_text_type - fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email13") + fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email13") mail = Mail.new(fixture) assert mail.has_attachments? assert_equal 1, mail.attachments.length - assert_equal "hello.rb", mail.attachments.first.original_filename + assert_equal "hello.rb", mail.attachments.first.filename end def test_decode_part_without_content_type - fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email4") + fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email4") mail = Mail.new(fixture) assert_nothing_raised { mail.body } end def test_decode_message_without_content_type - fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email5") + fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email5") mail = Mail.new(fixture) assert_nothing_raised { mail.body } end def test_decode_message_with_incorrect_charset - fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email6") + fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email6") mail = Mail.new(fixture) assert_nothing_raised { mail.body } end def test_multipart_with_mime_version - mail = TestMailer.create_multipart_with_mime_version(@recipient) + mail = TestMailer.multipart_with_mime_version(@recipient) assert_equal "1.1", mail.mime_version end def test_multipart_with_utf8_subject - mail = TestMailer.create_multipart_with_utf8_subject(@recipient) + mail = TestMailer.multipart_with_utf8_subject(@recipient) assert_match(/\nSubject: =\?utf-8\?Q\?Foo_.*?\?=/, mail.encoded) end def test_implicitly_multipart_with_utf8 - mail = TestMailer.create_implicitly_multipart_with_utf8 + mail = TestMailer.implicitly_multipart_with_utf8 assert_match(/\nSubject: =\?utf-8\?Q\?Foo_.*?\?=/, mail.encoded) end def test_explicitly_multipart_messages - mail = TestMailer.create_explicitly_multipart_example(@recipient) + mail = TestMailer.explicitly_multipart_example(@recipient) assert_equal 3, mail.parts.length assert_equal 'multipart/mixed', mail.mime_type assert_equal "text/plain", mail.parts[0].mime_type @@ -901,6 +883,7 @@ EOF assert_equal "iso-8859-1", mail.parts[1].charset assert_equal "image/jpeg", mail.parts[2].mime_type + assert_equal "attachment", mail.parts[2][:content_disposition].disposition_type assert_equal "foo.jpg", mail.parts[2][:content_disposition].filename assert_equal "foo.jpg", mail.parts[2][:content_type].filename @@ -908,13 +891,13 @@ EOF end def test_explicitly_multipart_with_content_type - mail = TestMailer.create_explicitly_multipart_example(@recipient, "multipart/alternative") + mail = TestMailer.explicitly_multipart_example(@recipient, "multipart/alternative") assert_equal 3, mail.parts.length assert_equal "multipart/alternative", mail.mime_type end def test_explicitly_multipart_with_invalid_content_type - mail = TestMailer.create_explicitly_multipart_example(@recipient, "text/xml") + mail = TestMailer.explicitly_multipart_example(@recipient, "text/xml") assert_equal 3, mail.parts.length assert_equal 'multipart/mixed', mail.mime_type end @@ -922,7 +905,7 @@ EOF def test_implicitly_multipart_messages assert ActionView::Template.template_handler_extensions.include?("bak"), "bak extension was not registered" - mail = TestMailer.create_implicitly_multipart_example(@recipient) + mail = TestMailer.implicitly_multipart_example(@recipient) assert_equal 3, mail.parts.length assert_equal "1.0", mail.mime_version.to_s assert_equal "multipart/alternative", mail.mime_type @@ -937,7 +920,7 @@ EOF def test_implicitly_multipart_messages_with_custom_order assert ActionView::Template.template_handler_extensions.include?("bak"), "bak extension was not registered" - mail = TestMailer.create_implicitly_multipart_example(@recipient, nil, ["application/x-yaml", "text/plain"]) + mail = TestMailer.implicitly_multipart_example(@recipient, nil, ["application/x-yaml", "text/plain"]) assert_equal 3, mail.parts.length assert_equal "application/x-yaml", mail.parts[0].mime_type assert_equal "text/plain", mail.parts[1].mime_type @@ -945,7 +928,7 @@ EOF end def test_implicitly_multipart_messages_with_charset - mail = TestMailer.create_implicitly_multipart_example(@recipient, 'iso-8859-1') + mail = TestMailer.implicitly_multipart_example(@recipient, 'iso-8859-1') assert_equal "multipart/alternative", mail.header['content-type'].content_type @@ -955,23 +938,23 @@ EOF end def test_html_mail - mail = TestMailer.create_html_mail(@recipient) + mail = TestMailer.html_mail(@recipient) assert_equal "text/html", mail.mime_type end def test_html_mail_with_underscores - mail = TestMailer.create_html_mail_with_underscores(@recipient) + mail = TestMailer.html_mail_with_underscores(@recipient) assert_equal %{<a href="http://google.com" target="_blank">_Google</a>}, mail.body.to_s end def test_various_newlines - mail = TestMailer.create_various_newlines(@recipient) + mail = TestMailer.various_newlines(@recipient) assert_equal("line #1\nline #2\nline #3\nline #4\n\n" + "line #5\n\nline#6\n\nline #7", mail.body.to_s) end def test_various_newlines_multipart - mail = TestMailer.create_various_newlines_multipart(@recipient) + mail = TestMailer.various_newlines_multipart(@recipient) assert_equal "line #1\nline #2\nline #3\nline #4\n\n", mail.parts[0].body.to_s assert_equal "<p>line #1</p>\n<p>line #2</p>\n<p>line #3</p>\n<p>line #4</p>\n\n", mail.parts[1].body.to_s assert_equal "line #1\r\nline #2\r\nline #3\r\nline #4\r\n\r\n", mail.parts[0].body.encoded @@ -979,8 +962,8 @@ EOF end def test_headers_removed_on_smtp_delivery - ActionMailer::Base.delivery_method = :smtp - TestMailer.deliver_cc_bcc(@recipient) + TestMailer.delivery_method = :smtp + TestMailer.cc_bcc(@recipient).deliver assert MockSMTP.deliveries[0][2].include?("root@loudthinking.com") assert MockSMTP.deliveries[0][2].include?("nobody@loudthinking.com") assert MockSMTP.deliveries[0][2].include?(@recipient) @@ -990,10 +973,10 @@ EOF end def test_file_delivery_should_create_a_file - ActionMailer::Base.delivery_method = :file - tmp_location = ActionMailer::Base.file_settings[:location] + TestMailer.delivery_method = :file + tmp_location = TestMailer.file_settings[:location] - TestMailer.deliver_cc_bcc(@recipient) + result = TestMailer.cc_bcc(@recipient).deliver assert File.exists?(tmp_location) assert File.directory?(tmp_location) assert File.exists?(File.join(tmp_location, @recipient)) @@ -1002,7 +985,7 @@ EOF end def test_recursive_multipart_processing - fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email7") + fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email7") mail = Mail.new(fixture) assert_equal(2, mail.parts.length) assert_equal(4, mail.parts.first.parts.length) @@ -1013,36 +996,36 @@ EOF end def test_decode_encoded_attachment_filename - fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email8") + fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email8") mail = Mail.new(fixture) attachment = mail.attachments.last expected = "01 Quien Te Dij\212at. Pitbull.mp3" if expected.respond_to?(:force_encoding) - result = attachment.original_filename.dup + result = attachment.filename.dup expected.force_encoding(Encoding::ASCII_8BIT) result.force_encoding(Encoding::ASCII_8BIT) assert_equal expected, result else - assert_equal expected, attachment.original_filename + assert_equal expected, attachment.filename end end def test_decode_message_with_unknown_charset - fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email10") + fixture = File.read(File.dirname(__FILE__) + "/../fixtures/raw_email10") mail = Mail.new(fixture) assert_nothing_raised { mail.body } end def test_empty_header_values_omitted - result = TestMailer.create_unnamed_attachment(@recipient).encoded + result = TestMailer.unnamed_attachment(@recipient).encoded assert_match %r{Content-Type: application/octet-stream;}, result - assert_match %r{Content-Disposition: attachment[^;]}, result + assert_match %r{Content-Disposition: attachment;}, result end def test_headers_with_nonalpha_chars - mail = TestMailer.create_headers_with_nonalpha_chars(@recipient) + mail = TestMailer.headers_with_nonalpha_chars(@recipient) assert !mail.from_addrs.empty? assert !mail.cc_addrs.empty? assert !mail.bcc_addrs.empty? @@ -1051,98 +1034,90 @@ EOF assert_match(/:/, mail[:bcc].decoded) end - def test_deliver_with_mail_object - mail = TestMailer.create_headers_with_nonalpha_chars(@recipient) - assert_nothing_raised { TestMailer.deliver(mail) } + def test_with_mail_object_deliver + TestMailer.delivery_method = :test + mail = TestMailer.headers_with_nonalpha_chars(@recipient) + assert_nothing_raised { mail.deliver } assert_equal 1, TestMailer.deliveries.length end def test_multipart_with_template_path_with_dots - mail = FunkyPathMailer.create_multipart_with_template_path_with_dots(@recipient) + mail = FunkyPathMailer.multipart_with_template_path_with_dots(@recipient) assert_equal 2, mail.parts.length assert "text/plain", mail.parts[1].mime_type assert "utf-8", mail.parts[1].charset end def test_custom_content_type_attributes - mail = TestMailer.create_custom_content_type_attributes + mail = TestMailer.custom_content_type_attributes assert_match %r{format=flowed}, mail.content_type assert_match %r{charset=utf-8}, mail.content_type end def test_return_path_with_create - mail = TestMailer.create_return_path - assert_equal "another@somewhere.test", mail['return-path'].to_s - end - - def test_return_path_with_create - mail = TestMailer.create_return_path - assert_equal ["another@somewhere.test"], mail.return_path + mail = TestMailer.return_path + assert_equal "another@somewhere.test", mail.return_path end def test_return_path_with_deliver - ActionMailer::Base.delivery_method = :smtp - TestMailer.deliver_return_path + TestMailer.delivery_method = :smtp + TestMailer.return_path.deliver assert_match %r{^Return-Path: <another@somewhere.test>}, MockSMTP.deliveries[0][0] assert_equal "another@somewhere.test", MockSMTP.deliveries[0][1].to_s end - def test_body_is_stored_as_an_ivar - mail = TestMailer.create_body_ivar(@recipient) - assert_equal "body: foo\nbar: baz", mail.body.to_s - end - def test_starttls_is_enabled_if_supported - ActionMailer::Base.smtp_settings[:enable_starttls_auto] = true + TestMailer.smtp_settings.merge!(:enable_starttls_auto => true) MockSMTP.any_instance.expects(:respond_to?).with(:enable_starttls_auto).returns(true) MockSMTP.any_instance.expects(:enable_starttls_auto) - ActionMailer::Base.delivery_method = :smtp - TestMailer.deliver_signed_up(@recipient) + TestMailer.delivery_method = :smtp + TestMailer.signed_up(@recipient).deliver end def test_starttls_is_disabled_if_not_supported - ActionMailer::Base.smtp_settings[:enable_starttls_auto] = true + TestMailer.smtp_settings.merge!(:enable_starttls_auto => true) MockSMTP.any_instance.expects(:respond_to?).with(:enable_starttls_auto).returns(false) MockSMTP.any_instance.expects(:enable_starttls_auto).never - ActionMailer::Base.delivery_method = :smtp - TestMailer.deliver_signed_up(@recipient) + TestMailer.delivery_method = :smtp + TestMailer.signed_up(@recipient).deliver end def test_starttls_is_not_enabled - ActionMailer::Base.smtp_settings[:enable_starttls_auto] = false + TestMailer.smtp_settings.merge!(:enable_starttls_auto => false) MockSMTP.any_instance.expects(:respond_to?).never - MockSMTP.any_instance.expects(:enable_starttls_auto).never - ActionMailer::Base.delivery_method = :smtp - TestMailer.deliver_signed_up(@recipient) + TestMailer.delivery_method = :smtp + TestMailer.signed_up(@recipient).deliver ensure - ActionMailer::Base.smtp_settings[:enable_starttls_auto] = true + TestMailer.smtp_settings.merge!(:enable_starttls_auto => true) end end -class InheritableTemplateRootTest < Test::Unit::TestCase +class InheritableTemplateRootTest < ActiveSupport::TestCase def test_attr - expected = File.expand_path("#{File.dirname(__FILE__)}/fixtures/path.with.dots") + expected = File.expand_path("#{File.dirname(__FILE__)}/../fixtures/path.with.dots") assert_equal expected, FunkyPathMailer.template_root.to_s sub = Class.new(FunkyPathMailer) - sub.template_root = 'test/path' + assert_deprecated do + sub.template_root = 'test/path' + end assert_equal File.expand_path('test/path'), sub.template_root.to_s assert_equal expected, FunkyPathMailer.template_root.to_s end end -class MethodNamingTest < Test::Unit::TestCase +class MethodNamingTest < ActiveSupport::TestCase class TestMailer < ActionMailer::Base def send - render :text => 'foo' + body 'foo' end end def setup set_delivery_method :test ActionMailer::Base.perform_deliveries = true - ActionMailer::Base.deliveries = [] + ActionMailer::Base.deliveries.clear end def teardown @@ -1152,12 +1127,13 @@ class MethodNamingTest < Test::Unit::TestCase def test_send_method assert_nothing_raised do assert_emails 1 do - TestMailer.deliver_send + assert_deprecated do + TestMailer.deliver_send + end end end end end - class RespondToTest < Test::Unit::TestCase class RespondToMailer < ActionMailer::Base; end @@ -1220,4 +1196,4 @@ class RespondToTest < Test::Unit::TestCase assert_match(/undefined method.*not_a_method/, error.message) end -end +end
\ No newline at end of file diff --git a/actionmailer/test/tmail_compat_test.rb b/actionmailer/test/old_base/tmail_compat_test.rb index a1ca6a7243..7c1d9a07c1 100644 --- a/actionmailer/test/tmail_compat_test.rb +++ b/actionmailer/test/old_base/tmail_compat_test.rb @@ -1,21 +1,23 @@ require 'abstract_unit' -class TmailCompatTest < Test::Unit::TestCase +class TmailCompatTest < ActiveSupport::TestCase def test_set_content_type_raises_deprecation_warning mail = Mail.new - STDERR.expects(:puts) # Deprecation warning - assert_nothing_raised do - mail.set_content_type "text/plain" + assert_deprecated do + assert_nothing_raised do + mail.set_content_type "text/plain" + end end assert_equal mail.mime_type, "text/plain" end def test_transfer_encoding_raises_deprecation_warning mail = Mail.new - STDERR.expects(:puts) # Deprecation warning - assert_nothing_raised do - mail.transfer_encoding "base64" + assert_deprecated do + assert_nothing_raised do + mail.transfer_encoding "base64" + end end assert_equal mail.content_transfer_encoding, "base64" end diff --git a/actionmailer/test/url_test.rb b/actionmailer/test/old_base/url_test.rb index 12bf609dce..5affb47997 100644 --- a/actionmailer/test/url_test.rb +++ b/actionmailer/test/old_base/url_test.rb @@ -44,7 +44,7 @@ class ActionMailerUrlTest < Test::Unit::TestCase def setup set_delivery_method :test ActionMailer::Base.perform_deliveries = true - ActionMailer::Base.deliveries = [] + ActionMailer::Base.deliveries.clear @recipient = 'test@localhost' end @@ -54,6 +54,8 @@ class ActionMailerUrlTest < Test::Unit::TestCase end def test_signed_up_with_url + TestMailer.delivery_method = :test + ActionController::Routing::Routes.draw do |map| map.connect ':controller/:action/:id' map.welcome 'welcome', :controller=>"foo", :action=>"bar" @@ -67,14 +69,14 @@ class ActionMailerUrlTest < Test::Unit::TestCase expected.date = Time.local(2004, 12, 12) created = nil - assert_nothing_raised { created = TestMailer.create_signed_up_with_url(@recipient) } + assert_nothing_raised { created = TestMailer.signed_up_with_url(@recipient) } assert_not_nil created expected.message_id = '<123@456>' created.message_id = '<123@456>' assert_equal expected.encoded, created.encoded - assert_nothing_raised { TestMailer.deliver_signed_up_with_url(@recipient) } + assert_nothing_raised { TestMailer.signed_up_with_url(@recipient).deliver } assert_not_nil ActionMailer::Base.deliveries.first delivered = ActionMailer::Base.deliveries.first diff --git a/actionmailer/test/subscriber_test.rb b/actionmailer/test/subscriber_test.rb index aed5d2ca7e..3d1736d64f 100644 --- a/actionmailer/test/subscriber_test.rb +++ b/actionmailer/test/subscriber_test.rb @@ -11,7 +11,7 @@ class AMSubscriberTest < ActionMailer::TestCase recipients "somewhere@example.com" subject "basic" from "basic@example.com" - render :text => "Hello world" + body "Hello world" end def receive(mail) @@ -24,21 +24,21 @@ class AMSubscriberTest < ActionMailer::TestCase end def test_deliver_is_notified - TestMailer.deliver_basic + TestMailer.basic.deliver wait - assert_equal 1, @logger.logged(:info).size - assert_match /Sent mail to somewhere@example.com/, @logger.logged(:info).first - assert_equal 1, @logger.logged(:debug).size - assert_match /Hello world/, @logger.logged(:debug).first + assert_equal(1, @logger.logged(:info).size) + assert_match(/Sent mail to somewhere@example.com/, @logger.logged(:info).first) + assert_equal(1, @logger.logged(:debug).size) + assert_match(/Hello world/, @logger.logged(:debug).first) end def test_receive_is_notified fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email") TestMailer.receive(fixture) wait - assert_equal 1, @logger.logged(:info).size - assert_match /Received mail/, @logger.logged(:info).first - assert_equal 1, @logger.logged(:debug).size - assert_match /Jamis/, @logger.logged(:debug).first + assert_equal(1, @logger.logged(:info).size) + assert_match(/Received mail/, @logger.logged(:info).first) + assert_equal(1, @logger.logged(:debug).size) + assert_match(/Jamis/, @logger.logged(:debug).first) end end
\ No newline at end of file diff --git a/actionmailer/test/test_helper_test.rb b/actionmailer/test/test_helper_test.rb index 1fed26f78f..3a38a91c28 100644 --- a/actionmailer/test/test_helper_test.rb +++ b/actionmailer/test/test_helper_test.rb @@ -12,7 +12,7 @@ end class TestHelperMailerTest < ActionMailer::TestCase def test_setup_sets_right_action_mailer_options - assert_instance_of ActionMailer::DeliveryMethod::Test, ActionMailer::Base.delivery_method + assert_equal :test, ActionMailer::Base.delivery_method assert ActionMailer::Base.perform_deliveries assert_equal [], ActionMailer::Base.deliveries end @@ -44,7 +44,7 @@ class TestHelperMailerTest < ActionMailer::TestCase def test_assert_emails assert_nothing_raised do assert_emails 1 do - TestHelperMailer.deliver_test + TestHelperMailer.test.deliver end end end @@ -52,27 +52,27 @@ class TestHelperMailerTest < ActionMailer::TestCase def test_repeated_assert_emails_calls assert_nothing_raised do assert_emails 1 do - TestHelperMailer.deliver_test + TestHelperMailer.test.deliver end end assert_nothing_raised do assert_emails 2 do - TestHelperMailer.deliver_test - TestHelperMailer.deliver_test + TestHelperMailer.test.deliver + TestHelperMailer.test.deliver end end end def test_assert_emails_with_no_block assert_nothing_raised do - TestHelperMailer.deliver_test + TestHelperMailer.test.deliver assert_emails 1 end assert_nothing_raised do - TestHelperMailer.deliver_test - TestHelperMailer.deliver_test + TestHelperMailer.test.deliver + TestHelperMailer.test.deliver assert_emails 3 end end @@ -80,7 +80,7 @@ class TestHelperMailerTest < ActionMailer::TestCase def test_assert_no_emails assert_nothing_raised do assert_no_emails do - TestHelperMailer.create_test + TestHelperMailer.test end end end @@ -88,7 +88,7 @@ class TestHelperMailerTest < ActionMailer::TestCase def test_assert_emails_too_few_sent error = assert_raise ActiveSupport::TestCase::Assertion do assert_emails 2 do - TestHelperMailer.deliver_test + TestHelperMailer.test.deliver end end @@ -98,8 +98,8 @@ class TestHelperMailerTest < ActionMailer::TestCase def test_assert_emails_too_many_sent error = assert_raise ActiveSupport::TestCase::Assertion do assert_emails 1 do - TestHelperMailer.deliver_test - TestHelperMailer.deliver_test + TestHelperMailer.test.deliver + TestHelperMailer.test.deliver end end @@ -109,7 +109,7 @@ class TestHelperMailerTest < ActionMailer::TestCase def test_assert_no_emails_failure error = assert_raise ActiveSupport::TestCase::Assertion do assert_no_emails do - TestHelperMailer.deliver_test + TestHelperMailer.test.deliver end end |