# encoding: utf-8
#
# This file is a part of Redmine CRM (redmine_contacts) plugin,
# customer relationship management plugin for Redmine
#
# Copyright (C) 2010-2018 RedmineUP
# http://www.redmineup.com/
#
# redmine_contacts is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# redmine_contacts is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with redmine_contacts.  If not, see <http://www.gnu.org/licenses/>.

require File.expand_path('../../test_helper', __FILE__)

class MailerPatchTest < ActiveSupport::TestCase
  fixtures :projects,
           :users,
           :roles,
           :members,
           :member_roles,
           :issues,
           :issue_statuses,
           :versions,
           :trackers,
           :projects_trackers,
           :issue_categories,
           :enabled_modules,
           :enumerations,
           :attachments,
           :workflows,
           :custom_fields,
           :custom_values,
           :custom_fields_projects,
           :custom_fields_trackers,
           :time_entries,
           :journals,
           :journal_details,
           :queries

  fixtures :email_addresses if ActiveRecord::VERSION::MAJOR >= 4

  RedmineContacts::TestCase.create_fixtures(Redmine::Plugin.find(:redmine_contacts).directory + '/test/fixtures/', [:contacts,
                                                                                                                    :contacts_projects,
                                                                                                                    :contacts_issues,
                                                                                                                    :deal_processes,
                                                                                                                    :deals,
                                                                                                                    :notes,
                                                                                                                    :tags,
                                                                                                                    :taggings,
                                                                                                                    :queries])

  FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures/contacts_mailer'

  def setup
    RedmineContacts::TestCase.prepare
    Setting.host_name = 'mydomain.foo'
    Setting.protocol = 'http'
    Setting.plain_text_mail = '0'
    ActionMailer::Base.deliveries.clear
    Setting.notified_events = Redmine::Notifiable.all.collect(&:name)
  end

  def test_crm_note_add
    note = Note.find(1)
    assert Mailer.crm_note_add(note).deliver
    assert_match 'Note 1', last_email.text_part.to_s
  end

  def test_crm_note_add_to_company
    note = Note.find(4)
    assert Mailer.crm_note_add(note).deliver
    assert_match 'Note 4', last_email.text_part.to_s
  end

  def test_crm_contact_add
    contact = Contact.find(1)
    assert Mailer.crm_contact_add(contact).deliver
    assert_match 'Contact #1: Ivan Ivanov', last_email.text_part.to_s
  end
  def test_crm_note_add_to_deal
    note = Note.find(5)
    assert Mailer.crm_note_add(note).deliver
    assert_match 'Note 5', last_email.text_part.to_s
  end

  def test_crm_deal_add
    deal = Deal.find(1)
    assert Mailer.crm_deal_add(deal).deliver
    assert_match 'Deal #1', last_email.text_part.to_s
  end

  def test_crm_deal_updated
    deal_process = DealProcess.last
    deal_process.author = User.find(2)
    deal_process.save
    assert Mailer.crm_deal_updated(deal_process).deliver
    assert_match 'John Smith', last_email.text_part.to_s
  end

  private

  def last_email
    mail = ActionMailer::Base.deliveries.last
    assert_not_nil mail
    mail
  end

  def text_part
    last_email.parts.detect { |part| part.content_type.include?('text/plain') }
  end

  def html_part
    last_email.parts.detect { |part| part.content_type.include?('text/html') }
  end
end
