From 67c77ae88d900d4c286dd5e49134c75d822eca16 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 27 Jun 2014 00:42:27 +0200 Subject: [PATCH] Fixed activity log bug, added unit tests. --- app/models/activity_stream.rb | 4 +- test/unit/activity_stream_test.rb | 79 +++++++++++++++++++++---------- 2 files changed, 56 insertions(+), 27 deletions(-) diff --git a/app/models/activity_stream.rb b/app/models/activity_stream.rb index 9bc64eeb2..088c50750 100644 --- a/app/models/activity_stream.rb +++ b/app/models/activity_stream.rb @@ -42,14 +42,14 @@ add a new activity entry for an object role_id = role.id end - # check if entry is needed + # check newest entry - is needed result = ActivityStream.where( :o_id => data[:o_id], # :activity_stream_type_id => type.id, :role_id => role_id, :activity_stream_object_id => object.id, :created_by_id => data[:created_by_id] - ).order('created_at DESC, id ASC').last + ).order('created_at DESC, id DESC').first # resturn if old entry is really fresh return result if result && result.created_at.to_i >= ( data[:created_at].to_i - 12 ) diff --git a/test/unit/activity_stream_test.rb b/test/unit/activity_stream_test.rb index e221a085e..ee3a6b843 100644 --- a/test/unit/activity_stream_test.rb +++ b/test/unit/activity_stream_test.rb @@ -51,17 +51,28 @@ class ActivityStreamTest < ActiveSupport::TestCase :priority_id => Ticket::Priority.lookup( :name => '1 low' ).id, }, }, + :update2 => { + :ticket => { + :title => 'Unit Test 2 (äöüß) - update!', + :priority_id => Ticket::Priority.lookup( :name => '2 normal' ).id, + }, + }, :check => [ - { + { :result => true, :object => 'Ticket', - :type => 'created', + :type => 'updated', }, { :result => true, :object => 'Ticket::Article', :type => 'created', }, + { + :result => true, + :object => 'Ticket', + :type => 'created', + }, { :result => false, :object => 'User', @@ -80,8 +91,9 @@ class ActivityStreamTest < ActiveSupport::TestCase ticket = Ticket.create( test[:create][:ticket] ) test[:check][0][:o_id] = ticket.id - test[:check][0][:created_at] = ticket.created_at - test[:check][0][:created_by_id] = current_user.id + test[:check][2][:o_id] = ticket.id + test[:check][2][:created_at] = ticket.created_at + test[:check][2][:created_by_id] = current_user.id sleep 2 test[:create][:article][:ticket_id] = ticket.id @@ -98,14 +110,26 @@ class ActivityStreamTest < ActiveSupport::TestCase ticket.update_attributes( test[:update][:ticket] ) # check updated user - test[:check][2][:o_id] = current_user.id - test[:check][2][:created_at] = ticket.created_at - test[:check][2][:created_by_id] = current_user.id + test[:check][3][:o_id] = current_user.id + test[:check][3][:created_at] = ticket.created_at + test[:check][3][:created_by_id] = current_user.id + end + if test[:update2][:ticket] + ticket = Ticket.find( ticket.id ) + ticket.update_attributes( test[:update2][:ticket] ) end if test[:update][:article] article.update_attributes( test[:update][:article] ) end + sleep 15 + if test[:update][:ticket] + ticket.update_attributes( test[:update][:ticket] ) + end + if test[:update2][:ticket] + ticket.update_attributes( test[:update2][:ticket] ) + end + # remember ticket tickets.push ticket @@ -148,12 +172,12 @@ class ActivityStreamTest < ActiveSupport::TestCase { :result => true, :object => 'Organization', - :type => 'created', + :type => 'updated', }, { :result => true, :object => 'Organization', - :type => 'updated', + :type => 'created', }, ] }, @@ -252,7 +276,7 @@ class ActivityStreamTest < ActiveSupport::TestCase users.push user # check activity_stream - activity_stream_check( admin_user.activity_stream(2), test[:check] ) + activity_stream_check( admin_user.activity_stream(3), test[:check] ) } # delete tickets @@ -293,12 +317,12 @@ class ActivityStreamTest < ActiveSupport::TestCase { :result => true, :object => 'User', - :type => 'created', + :type => 'updated', }, { :result => true, :object => 'User', - :type => 'updated', + :type => 'created', }, ] }, @@ -344,25 +368,30 @@ class ActivityStreamTest < ActiveSupport::TestCase end def activity_stream_check( activity_stream_list, checks ) + #activity_stream_list = activity_stream_list.reverse #puts 'AS ' + activity_stream_list.inspect + check_count = 0 checks.each { |check_item| + check_count += 1 + #puts '+++++++++++' #puts check_item.inspect - match = false + check_list = 0 activity_stream_list.each { |item| - next if match - #puts '--------' - #puts item.inspect - next if item['object'] != check_item[:object] - next if item['type'] != check_item[:type] - next if item['o_id'] != check_item[:o_id] - match = true + check_list += 1 + next if check_list != check_count +# next if match + #puts '--------' + #puts item.inspect + #puts check_item.inspect + if check_item[:result] + assert_equal( check_item[:object], item['object'] ) + assert_equal( check_item[:type], item['type'] ) + assert_equal( check_item[:o_id], item['o_id'] ) + else + assert_not_equal( check_item[:object], item['object'] ) + end } - if check_item[:result] - assert( match, "activity stream check not matched! #{ check_item.inspect } not in #{ activity_stream_list.inspect }") - else - assert( !match, "activity stream check matched but should not! #{ check_item.inspect } not in #{ activity_stream_list.inspect }") - end } end