Skip to content

Commit 812b929

Browse files
committed
Check for existing notifications using the manager
Signed-off-by: Matt Friedman <maf675@gmail.com>
1 parent a1cb5e8 commit 812b929

2 files changed

Lines changed: 37 additions & 31 deletions

File tree

factory/idea.php

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,15 @@ public function set_status($idea_id, $status)
7272
$this->update_idea_data($sql_ary, $idea_id, $this->table_ideas);
7373

7474
// Send a notification
75-
$method = $this->notification_exists($idea_id) ? 'update_notifications' : 'add_notifications';
76-
$this->notification_manager->$method(ext::NOTIFICATION_TYPE_STATUS, [
77-
'idea_id' => (int) $idea_id,
78-
'status' => (int) $status,
79-
'user_id' => (int) $this->user->data['user_id'],
80-
]);
75+
$notifications = $this->notification_manager->get_notified_users(ext::NOTIFICATION_TYPE_STATUS, ['item_id' => (int) $idea_id]);
76+
$this->notification_manager->{empty($notifications) ? 'add_notifications' : 'update_notifications'}(
77+
ext::NOTIFICATION_TYPE_STATUS,
78+
[
79+
'idea_id' => (int) $idea_id,
80+
'status' => (int) $status,
81+
'user_id' => (int) $this->user->data['user_id'],
82+
]
83+
);
8184
}
8285

8386
/**
@@ -454,24 +457,4 @@ protected function get_users_vote($idea_id, $user_id)
454457

455458
return $row;
456459
}
457-
458-
/**
459-
* Check if a notification already exists
460-
*
461-
* @param int $item_id The item identifier for the notification
462-
* @return bool
463-
*/
464-
protected function notification_exists($item_id)
465-
{
466-
$sql = 'SELECT notification_id
467-
FROM ' . NOTIFICATIONS_TABLE . '
468-
WHERE item_id = ' . (int) $item_id . '
469-
AND notification_type_id = ' . $this->notification_manager->get_notification_type_id(ext::NOTIFICATION_TYPE_STATUS);
470-
471-
$result = $this->db->sql_query_limit($sql, 1);
472-
$row = $this->db->sql_fetchrow($result);
473-
$this->db->sql_freeresult($result);
474-
475-
return $row !== false;
476-
}
477460
}

tests/ideas/idea_attributes_test.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ public function set_status_test_data()
6363
*/
6464
public function test_set_status($idea_id, $status)
6565
{
66-
$this->notification_manager->expects($this->once())
67-
->method('get_notification_type_id')
68-
->with(ext::NOTIFICATION_TYPE_STATUS)
69-
->willReturn(1);
70-
7166
$object = $this->get_idea_object();
7267

7368
$object->set_status($idea_id, $status);
@@ -77,6 +72,34 @@ public function test_set_status($idea_id, $status)
7772
self::assertEquals($status, $idea['idea_status']);
7873
}
7974

75+
public function test_set_status_notification_data()
76+
{
77+
return [
78+
[1, 1, [], 'add_notifications'],
79+
[1, 2, [2], 'update_notifications'],
80+
[2, 3, [], 'add_notifications'],
81+
[2, 4, [3], 'update_notifications'],
82+
];
83+
}
84+
85+
/**
86+
* @dataProvider test_set_status_notification_data
87+
*/
88+
public function test_set_status_notification($idea_id, $status, $notified_users, $expected)
89+
{
90+
$this->notification_manager->expects($this->once())
91+
->method('get_notified_users')
92+
->with(ext::NOTIFICATION_TYPE_STATUS, ['item_id' => $idea_id])
93+
->willReturn($notified_users);
94+
95+
$this->notification_manager->expects($this->once())
96+
->method($expected);
97+
98+
$object = $this->get_idea_object();
99+
100+
$object->set_status($idea_id, $status);
101+
}
102+
80103
/**
81104
* Data for idea attribute tests
82105
*

0 commit comments

Comments
 (0)