ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

ActiveSupport::Dependencies.autoload_paths.unshift Rails.root.to_s + '/test/mocks/test'

class ActiveSupport::TestCase
  def self.main_scenario
    fixtures :parties, :users, :groups, :groups_users, :projects, :periods, :tasks,
    :task_files, :works, :estimates, :work_lock_subscriptions, :work_locks, :absences
  end
  
  # Transactional fixtures accelerate your tests by wrapping each test method
  # in a transaction that's rolled back on completion.  This ensures that the
  # test database remains unchanged so your fixtures don't have to be reloaded
  # between every test method.  Fewer database queries means faster tests.
  #
  # Read Mike Clark's excellent walkthrough at
  #   http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
  #
  # Every Active Record database supports transactions except MyISAM tables
  # in MySQL.  Turn off transactional fixtures in this case; however, if you
  # don't care one way or the other, switching from MyISAM to InnoDB tables
  # is recommended.
  self.use_transactional_fixtures = true
  
  # Instantiated fixtures are slow, but give you @david where otherwise you
  # would need people(:david).  If you don't want to migrate your existing
  # test cases which use the @david style and don't mind the speed hit (each
  # instantiated fixtures translates to a database query per test method),
  # then set this back to true.
  self.use_instantiated_fixtures  = false
  
  # Add more helper methods to be used by all tests here...
  
  def add_stored_detour
    @request.session[:detours] = [{:controller => 'bogus', :action => :location}]
  end
  
  def assert_sequences
    Project.find(:all).each do |b|
      b.unplanned_tasks.each_with_index do |t, i|
        assert_equal i+1, t.position
      end
    end
    
    Party.find(:all).each do |p|
      p.periods.each_with_index do |period, i|
        assert_equal i+1, period.position
      end
    end
    
    Period.find(:all).each do |p|
      p.open_tasks.each_with_index do |t, i|
        assert_equal i+1, t.position, "Open tasks for period #{p.id} are not in sequence:\n#{p.open_tasks.map{|t|[t.id, t.position]}.inspect}\nTask #{t.id} position does not match."
      end
    end
  end
  
  # TODO (uwe): This method should be removed
  # It is here only because ClassTableInheritanceInRails broke reading fixtures by name
  def users(login)
    User.find_by_login(login.to_s)
  end  
  
  def set_logged_in( user )
    @request.session[:user_id] = user.id
  end
  
  def set_logged_out
    @request.session[:user_id] = nil
  end
  
  def assert_logged_in( user )
    assert_equal user.id, @request.session[:user_id]
    assert_equal user, Thread.current[:user]
  end
  
  def assert_not_logged_in
    assert_nil @request.session[:user_id]
    assert_nil assigns(:current_user)
  end
  
  def assert_redirected_to_login
    assert_equal @controller.url_for(:action => "login"), response.redirect_url
  end
  
  def post_signup( user_params )
    post :signup, "user" => user_params
  end
  
  def assert_password_validation_fails
    user = assigns(:user)
    assert_equal 1, user.errors.size
    assert_not_nil user.errors['password']
    assert_response :success
    assert_equal 0, ActionMailer::Base.deliveries.size
  end
  
  def assert_contains( target, container )
    assert !container.nil?, %Q( Failed to find "#{target}" in nil String )
    assert container.include?(target)
  end
  
end

module Mail
  class TestMailer
    @@inject_one_error = false
    cattr_accessor :inject_one_error

    def deliver_with_exception!(mail)
      if inject_one_error
        Mail::TestMailer::inject_one_error = false
        raise "Failed to send email" if raise_delivery_errors
      end
      deliver_without_exception!(mail)
    end
    alias_method_chain :deliver!, :exception
  end
end