<commit_before># coding: utf-8
require 'spec_helper'

# Set ActionMailer stuff
ActionMailer::Base.prepend_view_path '.'
ActionMailer::Base.deliveries = []

# Test Mailer
class TestMailer < ActionMailer::Base
  def test_multipart(css_file = nil)
    setup_email(css_file)
    content_type 'multipart/alternative'
    part :content_type => 'text/html', :body => '<p class="text">Hello <a href="htt://example.com/">World</a></p>'
    part :content_type => 'text/plain', :body => 'Hello World'
  end

  def test_nested_multipart_mixed(css_file = nil)
    setup_email(css_file)
    content_type "multipart/mixed"
    part :content_type => "multipart/alternative", :content_disposition => "inline" do |p|
      p.part :content_type => 'text/html', :body => '<p class="text">Hello World</p>'
      p.part :content_type => 'text/plain', :body => 'Hello World'
    end
  end

  def test_inline_rules(rules)
    setup_email(nil)
    content_type 'multipart/alternative'
    part :content_type => 'text/html', :body => "#{rules}<p class=\"text\">Hello World</p>"
    part :content_type => 'text/plain', :body => 'Hello World'
  end

  def test_singlepart_html(css_file = nil)
    setup_email(css_file)
    content_type 'text/html'
    body '<p class="text">Hello World</p>'
  end

  def test_singlepart_plain(css_file = nil)
    setup_email(css_file)
    content_type 'text/plain'
    body '<p class="text">Hello World</p>'
  end

  def test_image_urls(css_file = nil)
    setup_email(css_file)
    content_type 'multipart/alternative'
    part :content_type => 'text/html', :body => '<p id="image">Hello World</p><p id="image2">Goodbye World</p><img src="/images/test.jpg" />'
    part :content_type => 'text/plain', :body => 'Hello World'
  end

  protected

  def setup_email(css_file = nil)
    css css_file unless css_file.nil?

    subject 'Test Multipart Email'
    recipients '<EMAIL>'
    from '<EMAIL>'
    sent_on Time.now
  end
end

TestMailer.configure do |config|
  config.default_url_options = {:host => "example.com"}
  config.perform_deliveries = true
  config.delivery_method = :test
end

shared_examples_for "inline styles" do
  before(:each) do
    css_rules <<-EOF
      body { background: #000 }
      .text { color: #0f0; font-size: 14px }
      p { color: #f00; line-height: 1.5 }
    EOF
  end

  it "should add the correct xml namespace" do
    should match(/<html xmlns="http:\/\/www\.w3\.org\/1999\/xhtml">/)
  end

  it "should write the xhtml 1.0 doctype" do
    should match(/<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD XHTML 1\.0 Transitional\/\/EN" "http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional\.dtd">/mi)
  end
<commit_msg>Use new ActionMailer API in specs<commit_after># coding: utf-8
require 'spec_helper'

# Set ActionMailer stuff
ActionMailer::Base.prepend_view_path '.'
ActionMailer::Base.deliveries = []

# Test Mailer
class TestMailer < ActionMailer::Base
  default :from => "<EMAIL>", :to => "<EMAIL>"

  def multipart(css_file = nil)
    mail(:subject => "Multipart email") do |format|
      format.html { render :text => '<p class="text">Hello <a href="htt://example.com/">World</a></p>' }
      format.text { render :text => 'Hello World' }
    end
  end

  # Not sure how to implement this one.
  # TODO: Either remove or implement
  def nested_multipart_mixed(css_file = nil)
    raise "Nested multipart mixed is not implemented"
    content_type "multipart/mixed"
    part :content_type => "multipart/alternative", :content_disposition => "inline" do |p|
      p.part :content_type => 'text/html', :body => '<p class="text">Hello World</p>'
      p.part :content_type => 'text/plain', :body => 'Hello World'
    end
  end

  def inline_rules(rules)
    mail(:subject => "Inline rules email") do |format|
      format.html { render :text => %(#{rules}<p class="text">Hello World</p>)}
      format.text { render :text => 'Hello World' }
    end
  end

  def singlepart_html(css_file = nil)
    mail(:subject => "HTML email") do |format|
      format.html { render :text => '<p class="text">Hello World</p>' }
    end
  end

  def singlepart_plain(css_file = nil)
    mail(:subject => "Text email") do |format|
      # NOTE: Rendering html in plain text
      format.text { render :text => '<p class="text">Hello World</p>' }
    end
  end

  def image_urls(css_file = nil)
    mail(:subject => "Image URLs email") do |format|
      format.html { render :text => '<p id="image">Hello World</p><p id="image2">Goodbye World</p><img src="/images/test.jpg" />' }
      format.text { render :text => 'Hello World' }
    end
  end
end

TestMailer.configure do |config|
  config.default_url_options = {:host => "example.com"}
  config.perform_deliveries = true
  config.delivery_method = :test
end

shared_examples_for "inline styles" do
  before(:each) do
    css_rules <<-EOF
      body { background: #000 }
      .text { color: #0f0; font-size: 14px }
      p { color: #f00; line-height: 1.5 }
    EOF
  end

  it "should add the correct xml namespace" do
    should match(/<html xmlns="http:\/\/www\.w3\.org\/1999\/xhtml">/)
  end

  it "should write the xhtml 1.0 doctype" do
    should match(/<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD XHTML 1\.0 Transitional\/\/EN" "http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional\.dtd">/mi)
  end

{'fraction_non_alphanumeric': 0.1552736224199962, 'fraction_numerical': 0.009846619958341223, 'mean_word_length': 3.4649196956889265, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 86, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4091098', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1787, 'n_words': 454}