journal_test.rb 4.28 KB
Newer Older
1
#-- encoding: UTF-8
2 3
#-- copyright
# ChiliProject is a project management system.
4
#
Holger Just's avatar
Holger Just committed
5
# Copyright (C) 2010-2013 the ChiliProject Team
6
#
7 8 9 10
# This program 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 2
# of the License, or (at your option) any later version.
11
#
12 13
# See doc/COPYRIGHT.rdoc for more details.
#++
14
require File.expand_path('../../test_helper', __FILE__)
15

16
class JournalTest < ActiveSupport::TestCase
17
  fixtures :issues, :issue_statuses, :journals, :enumerations
18 19

  def setup
20
    @journal = IssueJournal.find(1)
21 22 23
  end

  def test_journalized_is_an_issue
24
    issue = @journal.journalized
25 26 27 28 29 30 31 32
    assert_kind_of Issue, issue
    assert_equal 1, issue.id
  end

  def test_new_status
    status = @journal.new_status
    assert_not_nil status
    assert_kind_of IssueStatus, status
33
    assert_equal 2, status.id
34
  end
35

36 37 38
  def test_create_should_send_email_notification
    ActionMailer::Base.deliveries.clear
    issue = Issue.find(:first)
Tim Felgentreff's avatar
Tim Felgentreff committed
39
    if issue.journals.empty?
40
      issue.init_journal(User.current, "This journal represents the creationa of journal version 1")
Tim Felgentreff's avatar
Tim Felgentreff committed
41 42
      issue.save
    end
43 44
    user = User.find(:first)

45
    assert_equal 0, ActionMailer::Base.deliveries.size
46
    issue.reload
47
    issue.update_attribute(:subject, "New subject to trigger automatic journal entry")
48 49
    assert_equal 1, ActionMailer::Base.deliveries.size
  end
50

51 52 53 54
  def test_create_should_not_send_email_notification_if_told_not_to
    ActionMailer::Base.deliveries.clear
    issue = Issue.find(:first)
    user = User.find(:first)
55
    journal = issue.init_journal(user, "A note")
56 57
    JournalObserver.instance.send_notification = false

58 59 60
    assert_difference("Journal.count") do
      assert issue.save
    end
61 62
    assert_equal 0, ActionMailer::Base.deliveries.size
  end
63 64 65 66 67 68 69 70 71 72 73 74 75 76

  test "creating the initial journal should track the changes from creation" do
    @project = Project.generate!
    issue = Issue.new do |i|
      i.project = @project
      i.subject = "Test initial journal"
      i.tracker = @project.trackers.first
      i.author = User.generate!
      i.description = "Some content"
    end

    assert_difference("Journal.count") do
      assert issue.save
    end
Holger Just's avatar
Holger Just committed
77

78 79 80 81 82
    journal = issue.reload.journals.first
    assert_equal ["","Test initial journal"], journal.changes["subject"]
    assert_equal [0, @project.id], journal.changes["project_id"]
    assert_equal [nil, "Some content"], journal.changes["description"]
  end
Holger Just's avatar
Holger Just committed
83

84 85 86 87 88
  test "creating a journal should update the updated_on value of the parent record (touch)" do
    @user = User.generate!
    @project = Project.generate!
    @issue = Issue.generate_for_project!(@project).reload
    start = @issue.updated_on
89
    sleep(1) # TODO: massive hack to make sure the timestamps are different. switch to timecop later
90 91 92 93 94 95 96 97

    assert_difference("Journal.count") do
      @issue.init_journal(@user, "A note")
      @issue.save
    end

    assert_not_equal start, @issue.reload.updated_on
  end
98 99 100 101 102 103 104

  test "accessing #journaled on a Journal should not error (parent class)" do
    journal = Journal.new
    assert_nothing_raised do
      assert_equal nil, journal.journaled
    end
  end
105 106 107 108 109 110 111 112 113 114 115 116 117

  test "setting journal fields through the journaled object for creation" do
    @issue = Issue.generate_for_project!(Project.generate!)

    @issue.journal_user = @issue.author
    @issue.journal_notes = 'Test setting fields on Journal from Issue'
    assert_difference('Journal.count') do
      assert @issue.save
    end

    assert_equal "Test setting fields on Journal from Issue", @issue.last_journal.notes
    assert_equal @issue.author, @issue.last_journal.user
  end
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

  test "subclasses of journaled models should have journal of parent type" do
    Ticket = Class.new(Issue)

    project = Project.generate!
    ticket = Ticket.new do |t|
      t.project = project
      t.subject = "Test initial journal"
      t.tracker = project.trackers.first
      t.author = User.generate!
      t.description = "Some content"
    end

    begin
      oldstdout = $stdout
      $stdout = StringIO.new
      ticket.save!
      assert $stdout.string.empty?, "No errors should be logged to stdout."
    ensure
      $stdout = oldstdout
    end

    journal = ticket.journals.first
    assert_equal IssueJournal, journal.class
  end
143
end