Commit 4e17520d8373f43c6cd4ff14f0320ffe1970aba3
1 parent
76c22144
Exists in
master
and in
29 other branches
Fix friend removal bug
(ActionItem3215) Signed-off-by: Arthur Del Esposte <arthurmde@gmail.com> Signed-off-by: Daniel Bucher <daniel.bucher88@gmail.com>
Showing
5 changed files
with
59 additions
and
3 deletions
Show diff stats
app/controllers/my_profile/friends_controller.rb
... | ... | @@ -11,7 +11,7 @@ class FriendsController < MyProfileController |
11 | 11 | def remove |
12 | 12 | @friend = profile.friends.find(params[:id]) |
13 | 13 | if request.post? && params[:confirmation] |
14 | - profile.remove_friend(@friend) | |
14 | + Friendship.remove_friendship(profile, @friend) | |
15 | 15 | redirect_to :action => 'index' |
16 | 16 | end |
17 | 17 | end | ... | ... |
app/models/friendship.rb
... | ... | @@ -15,4 +15,9 @@ class Friendship < ActiveRecord::Base |
15 | 15 | Friendship.update_cache_counter(:friends_count, friendship.person, -1) |
16 | 16 | Friendship.update_cache_counter(:friends_count, friendship.friend, -1) |
17 | 17 | end |
18 | + | |
19 | + def self.remove_friendship(person1, person2) | |
20 | + person1.remove_friend(person2) | |
21 | + person2.remove_friend(person1) | |
22 | + end | |
18 | 23 | end | ... | ... |
test/functional/friends_controller_test.rb
... | ... | @@ -36,12 +36,12 @@ class FriendsControllerTest < ActionController::TestCase |
36 | 36 | |
37 | 37 | should 'actually remove friend' do |
38 | 38 | profile.add_friend(friend) |
39 | + friend.add_friend(profile) | |
39 | 40 | |
40 | - assert_difference 'Friendship.count', -1 do | |
41 | + assert_difference 'Friendship.count', -2 do | |
41 | 42 | post :remove, :id => friend.id, :confirmation => '1' |
42 | 43 | assert_redirected_to :action => 'index' |
43 | 44 | end |
44 | - assert_equal friend, Profile.find(friend.id) | |
45 | 45 | end |
46 | 46 | |
47 | 47 | should 'display find people button' do | ... | ... |
... | ... | @@ -0,0 +1,37 @@ |
1 | +require "#{File.dirname(__FILE__)}/../test_helper" | |
2 | + | |
3 | +class ManageFriendshipsTest < ActionController::IntegrationTest | |
4 | + | |
5 | + def setup | |
6 | + FriendsController.any_instance.stubs(:get_layout).returns('application') | |
7 | + ProfileController.any_instance.stubs(:get_layout).returns('application') | |
8 | + | |
9 | + Friendship.delete_all | |
10 | + Person.delete_all | |
11 | + @person = create_user("albert", :password => 'test', | |
12 | + :password_confirmation => 'test').person | |
13 | + @person.user.activate | |
14 | + | |
15 | + @friend = fast_create(Person, :identifier => "isaac") | |
16 | + | |
17 | + login(@person.identifier, 'test') | |
18 | + end | |
19 | + | |
20 | + should 'remove friendships' do | |
21 | + @person.add_friend(@friend) | |
22 | + @friend.add_friend(@person) | |
23 | + | |
24 | + get "/myprofile/#{@person.identifier}/friends/remove/#{@friend.id}" | |
25 | + assert_response :success | |
26 | + | |
27 | + post "/myprofile/#{@person.identifier}/friends/remove/#{@friend.id}", | |
28 | + :confirmation => '1' | |
29 | + assert_response :redirect | |
30 | + | |
31 | + follow_redirect! | |
32 | + | |
33 | + assert assigns(:friends).empty? | |
34 | + assert !@person.is_a_friend?(@friend) | |
35 | + assert !@friend.is_a_friend?(@person) | |
36 | + end | |
37 | +end | ... | ... |
test/unit/friendship_test.rb
... | ... | @@ -58,4 +58,18 @@ class FriendshipTest < ActiveSupport::TestCase |
58 | 58 | assert_equal ['a'], ta.get_friend_name |
59 | 59 | end |
60 | 60 | |
61 | + should 'remove friendships when a friend removal occurs' do | |
62 | + p1 = create_user('testuser1').person | |
63 | + p2 = create_user('testuser2').person | |
64 | + p1.add_friend(p2, 'friends') | |
65 | + p2.add_friend(p1, 'friends') | |
66 | + | |
67 | + assert_difference 'Friendship.count', -2 do | |
68 | + Friendship.remove_friendship(p1, p2) | |
69 | + end | |
70 | + | |
71 | + assert_not_includes p1.friends(true), p2 | |
72 | + assert_not_includes p2.friends(true), p1 | |
73 | + end | |
74 | + | |
61 | 75 | end | ... | ... |