Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent duplicate entries from being recorded into the 'wp_bpges_queued_items' DB table #164

Merged
merged 4 commits into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,10 @@ function bpges_install_queued_items_table() {
type varchar(75) NOT NULL,
date_recorded datetime NOT NULL default '0000-00-00 00:00:00',
KEY user_id (user_id),
KEY group_id (user_id),
KEY group_id (group_id),
KEY activity_id (activity_id),
KEY user_group_type_date (user_id,type,date_recorded)
KEY user_group_type_date (user_id,type,date_recorded),
UNIQUE KEY user_group_activity_type (user_id,group_id,activity_id,type)
) {$charset_collate};";

dbDelta( $sql );
Expand Down
2 changes: 1 addition & 1 deletion classes/class-bpges-queued-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static function bulk_insert( $data ) {

$table_name = self::get_table_name();
$values_sql = implode( ', ', $values );
$sql = "INSERT INTO {$table_name} (user_id, group_id, activity_id, type, date_recorded) VALUES {$values_sql}";
$sql = "INSERT IGNORE INTO {$table_name} (user_id, group_id, activity_id, type, date_recorded) VALUES {$values_sql}";
return $wpdb->query( $sql );
}

Expand Down
30 changes: 30 additions & 0 deletions tests/testcases/subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,34 @@ public function test_digest_record_activity() {

$this->assertSame( $expected, $queue );
}

public function test_record_duplicate_activity_should_not_be_allowed() {
$activity_id = 123;

$activity = array(
'user_id' => self::$user_ids[0],
'group_id' => self::$group_ids[0],
'activity_id' => $activity_id,
'type' => 'immediate',
'date_recorded' => date( 'Y-m-d H:i:s', time() - 3600 ),
);

// Add the item to the queue.
$add = array( $activity );
BPGES_Queued_Item::bulk_insert( $add );

// Add the same item to the queue with a slightly different date.
$activity['date_recorded'] = date( 'Y-m-d H:i:s' );
$add = array( $activity );
BPGES_Queued_Item::bulk_insert( $add );

// Fetch queued items for our group.
$sub = new BPGES_Queued_Item_Query( array(
'group_id' => self::$group_ids[0],
) );
$sub = $sub->get_results();

// Assert that only 1 item was added to the queued items DB table.
$this->assertSame( 1, count( $sub ) );
}
}