require File.dirname(__FILE__) + '/../test_helper' class FileStream < StringIO attr_accessor :original_filename, :content_type end class TicketChangeTest < Test::Unit::TestCase fixtures :ticket_changes, :tickets, :statuses, :projects, :users, :priorities, :milestones, :groups, :groups_users, :groups_projects def setup RetroCM.reload Project.current = projects(:retro) end def teardown Project.current = nil end def test_crud tk = Project.current.tickets.first tk.monitor_attribute_changes! o1 = tk.ticket_changes.build(:author => 'Some Name', :email=> 'some@email.com', :content => 'Some comment ...') assert(tk.save) o2 = TicketChange.find(o1.id) assert_equal(o1.email, o2.email) o2.email = 'test@test.com' assert(o2.save) assert_not_equal(o1.email, o2.email) o1.reload assert_equal(o1.email, o2.email) assert(o1.destroy) end def test_presence_validations tk = Project.current.tickets.first tk.monitor_attribute_changes! o1 = tk.ticket_changes.build assert(!tk.save) assert_not_nil(o1.errors[:base]) assert_not_nil(o1.errors[:author]) o1 = build_change_without_changes assert(!o1.save) assert_not_nil(o1.errors[:base]) o1.content = 'Comment is a change' assert(o1.save) o1 = build_change_without_changes assert(!o1.save) assert_not_nil(o1.errors[:base]) tk = o1.ticket.dup tk.status_id = 3 assert(tk.save) end def test_accessors tk = Project.current.tickets.first o1 = tk.ticket_changes.new( :changes => {:status => {:old => 'X', :new=>'Y'}}, :approved => true, :spam => true, :user => users(:admin) ) assert_equal(tk, o1.ticket) assert_equal(false, o1.approved) assert_equal(false, o1.spam) assert_nil(o1.user) assert_equal({}, o1.changes) end def test_user_assignment_with_attributes o1 = build_change_without_changes(:email => 'test@mail.com', :content => 'Comment') u = users(:admin) o1.user = u assert(o1.save) assert_equal(u.name, o1.author) assert_equal(u.email, o1.email) end def test_attachments o1 = build_change_without_changes(:email => 'test@mail.com', :content => 'Comment') str = ('x' * 2048) stream = FileStream.new(str) stream.original_filename = 'file.diff' stream.content_type = 'text/x-diff' o1.attachment = Attachment.new(stream, 1) assert(!o1.save) assert_not_nil(o1.errors[:attachment]) o1.attachment = Attachment.new(stream) assert(o1.save) assert(o1.destroy) o2 = build_change_without_changes(:email => 'test@mail.com') o2.attachment = Attachment.new(stream, 1) assert(!o2.save) assert_not_nil(o2.errors[:content]) end def test_class_methods assert_equal('content', TicketChange.content_type) end private def build_change_without_changes(attributes = {}) options = {:author => 'Someone'} options.merge!(attributes) ticket = Project.current.tickets.first ticket.monitor_attribute_changes! ticket.ticket_changes.build(options) end end