require File.dirname(__FILE__) + '/../test_helper'

#--######################################################################
# Copyright (c) 2006 Logica
# 
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
# 
# Onno van der Straaten:: initial implementation
#++######################################################################
# {Copyright (c) 2006 Logica}[link:files/COPYRIGHT.html]

class UpdateTest < ActiveSupport::TestCase

  fixtures :users 

  def setup
    logger.debug "Test Case: #{name}"  
    @andy, @george, @cash, @tony = users(:andy), users(:george), users(:cash), users(:tony)
    @emails = ActionMailer::Base::deliveries
    @emails.clear
  end
  
  # Shows:
  # 1. No notify_immediate users, no email
  # 2. Notify_immediate users are receiving email
  def test_notification
    # 1. Note: create_templates uses update.do_update which sends emails
    @emails.clear
    wiki = create_templates
    wiki.reload
    bp = wiki.baseline_process
    assert_equal 2, Site.count # 1 wiki, 1 baseline
    assert_equal 3, @emails.size # STARTED and FINISHED
    assert_not_nil wiki
    assert_not_nil bp
    @emails.clear
    update = Update.create(:wiki_id => wiki.id, :baseline_process_id => bp.id, :user_id => @andy.id)
    @andy.notify_immediate = 1
    @george.notify_immediate = 1
    @cash.notify_immediate = 1
    @andy.save!
    @george.save!
    @cash.save!
    update.do_update
    assert_equal 4, @emails.size
    email = @emails[3]
    assert_equal ["[EPF Wiki - Test Enviroment] SCHEDULED update of Wiki Templates with Baseline Process templates_20080828",
 "[EPF Wiki - Test Enviroment] STARTED update of Wiki Templates with Baseline Process templates_20080828",
 "[EPF Wiki - Test Enviroment] FINISHED update of Wiki Templates with Baseline Process templates_20080828",
 "[EPF Wiki - Test Enviroment] Wiki Templates Updated with Baseline Process templates_20080828"],
 [@emails[0].subject, @emails[1].subject, @emails[2].subject, @emails[3].subject]
    assert_equal ["[EPF Wiki - Test Enviroment] Wiki Templates Updated with Baseline Process templates_20080828",["andy.kaufman@epf.eclipse.org",
 "cash.oshman@epf.eclipse.org",
 "george.shapiro@epf.eclipse.org"], true], [email.subject, email.bcc, email.body.include?('http://localhost:3000/users/account')]
  end

  # Shows
  # 1. That #job_daily can be used to notify users on the status of contributions
  # 2. Users are auto subscribed to receive alerts on the page
  # 3. Tony receives notification on update of the harvested comment and version
  # 4. Andy receives notification of having been processed
  def test_reviewed_notification_email
    @emails.clear
    create_templates
    wiki = Wiki.find_by_folder('templates')
    assert_not_nil wiki
    assert_equal 'Ready', wiki.status
    p1, p2 = wiki.pages[23], wiki.pages[6]
    # create some versions
    co1 = Checkout.new(:note => 'Checkout to test notification email', :page => p1, :site => wiki, :source_version => p1.current_version, :user => @tony)
    co2 = Checkout.new(:note => 'Checkout to test notification email', :page => p2, :site => wiki, :source_version => p2.current_version, :user => @cash)
    # 2
    assert !Notification.find_all_users(p1, Page.name).include?(@tony) 
    assert !Notification.find_all_users(p2, Page.name).include?(@cash)    
    assert co1.save 
    assert co2.save
    v1 = co1.version
    v2 = co2.version
    assert_equal 2, Checkout.count
    co1.checkin(@tony)
    co2.checkin(@cash)
    assert Notification.find_all_users(p1, Page.name).include?(@tony) 
    assert Notification.find_all_users(p2, Page.name).include?(@cash)    
    assert_equal 0, Checkout.count
    v1.review_note = "Review note 1"
    v2.review_note = "Review note 2"
    v1.done = 'Y'
    v2.done = 'Y'
    v1.reviewer = @andy
    v2.reviewer = @george
    assert v1.save
    assert v2.save
    # create some comments
    cmt1 = Comment.create(:text => 'Comment 1', :user => @tony, :version => v1, :page => v1.page, :site => v1.wiki)
    cmt2 = Comment.create(:text => 'Comment 2', :user => @cash, :version => v1, :page => v1.page, :site => v1.wiki)
    cmt3 = Comment.create(:text => 'Comment 3', :user => @george, :version => v2, :page => v2.page, :site => v2.wiki)
    assert cmt1.save
    assert cmt2.save
    assert cmt3.save
    assert Notification.find_all_users(p2, Page.name).include?(@cash)    
    assert Notification.find_all_users(p1, Page.name).include?(@tony) 
    assert Notification.find_all_users(p2, Page.name).include?(@cash)    
    assert Notification.find_all_users(p2, Page.name).include?(@george)    
    [cmt1, cmt2, cmt3].each do |cmt|
      assert cmt.save
      cmt.review_note = cmt.text
      cmt.done = 'Y'
      cmt.reviewer = @andy
      assert cmt.save
    end
    for i in 1..3
      Upload.create(:filename => 'index.html', :upload_type => 'Image', :description => 'Description of upload ' + i.to_s, :user => @cash, :reviewer => @andy, :review_note => 'Review note of upload ' + i.to_s, :done => 'Y')
    end
    @emails.clear 
    update = Update.new(:wiki => Wiki.find_by_folder('templates'), :baseline_process => BaselineProcess.find(:first), :user => @andy)
    assert update.save
    job_daily
    assert_equal ['[EPF Wiki - Test Enviroment] SCHEDULED update of Wiki Templates with Baseline Process templates_20080828',
'[EPF Wiki - Test Enviroment] STARTED update of Wiki Templates with Baseline Process templates_20080828',
'[EPF Wiki - Test Enviroment] FINISHED update of Wiki Templates with Baseline Process templates_20080828',
'[EPF Wiki - Test Enviroment] Your contribution has been processed',
'[EPF Wiki - Test Enviroment] Your contribution has been processed',
'[EPF Wiki - Test Enviroment] Your contribution has been processed',
'[EPF Wiki - Test Enviroment] Daily Summary'],
@emails.collect {|e|e.subject}
    assert_equal [["cash.oshman@epf.eclipse.org"],
 ["tony.clifton@epf.eclipse.org"], ['george.shapiro@epf.eclipse.org'],
 "[EPF Wiki - Test Enviroment] Your contribution has been processed",
 "[EPF Wiki - Test Enviroment] Your contribution has been processed",
 "[EPF Wiki - Test Enviroment] Your contribution has been processed"], [@emails[3].to, @emails[4].to, @emails[5].to, @emails[3].subject, @emails[4].subject, @emails[5].subject]
    assert IO.readlines(File.expand_path('test/unit/notifier_test/contributions_processed.html', RAILS_ROOT)).join.index('Hi Cash Oshman')
    # 3
    #v1, cmt1
    email = @emails[4]
    assert_equal ["tony.clifton@epf.eclipse.org"], email.to
    assert email.body.include?(v1.page.url(true))
    assert email.body.include?(v1.page.presentation_name)
    assert email.body.include?(v1.reviewer.name)    
    assert email.body.include?(v1.review_note)    
    assert email.body.include?(cmt1.page.url(true))
    assert email.body.include?(cmt1.text)
    assert email.body.include?(cmt1.reviewer.name)    
    assert email.body.include?(cmt1.review_note)    
    # 4
    u = Upload.find(:first)
    email = @emails[3]
    assert_equal ["cash.oshman@epf.eclipse.org"], email.to
    assert email.body.include?(u.url(true))
    assert email.body.include?(u.filename)
    assert email.body.include?(u.reviewer.name)    
    assert email.body.include?(u.review_note)    
  end
  
end
