diff --git a/phpBB/includes/functions_posting.php b/phpBB/includes/functions_posting.php
index 1faa58d1db..f6428b974f 100644
--- a/phpBB/includes/functions_posting.php
+++ b/phpBB/includes/functions_posting.php
@@ -1353,7 +1353,7 @@ function delete_post($forum_id, $topic_id, $post_id, &$data, $is_soft = false, $
{
$post_mode = 'delete_first_post';
}
- else if ($data['topic_last_post_id'] == $post_id)
+ else if ($data['topic_last_post_id'] <= $post_id)
{
$post_mode = 'delete_last_post';
}
@@ -2879,7 +2879,14 @@ function phpbb_handle_post_delete($forum_id, $topic_id, $post_id, &$post_data, $
$delete_reason
));
- $meta_info = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "p=$next_post_id") . "#p$next_post_id";
+ if ($next_post_id > 0)
+ {
+ $meta_info = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "p=$next_post_id") . "#p$next_post_id";
+ }
+ else
+ {
+ $meta_info = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "t=$topic_id");
+ }
$message = $user->lang['POST_DELETED'];
if (!$request->is_ajax())
diff --git a/phpBB/phpbb/content_visibility.php b/phpBB/phpbb/content_visibility.php
index 99f89580d7..091a5c20ea 100644
--- a/phpBB/phpbb/content_visibility.php
+++ b/phpBB/phpbb/content_visibility.php
@@ -521,11 +521,15 @@ class content_visibility
$postcounts[$num_posts][] = $poster_id;
}
+ $postcount_change = 0;
+
// Update users postcounts
foreach ($postcounts as $num_posts => $poster_ids)
{
if (in_array($visibility, array(ITEM_REAPPROVE, ITEM_DELETED)))
{
+ $postcount_change -= $num_posts;
+
$sql = 'UPDATE ' . $this->users_table . '
SET user_posts = 0
WHERE ' . $this->db->sql_in_set('user_id', $poster_ids) . '
@@ -540,6 +544,8 @@ class content_visibility
}
else
{
+ $postcount_change += $num_posts;
+
$sql = 'UPDATE ' . $this->users_table . '
SET user_posts = user_posts + ' . $num_posts . '
WHERE ' . $this->db->sql_in_set('user_id', $poster_ids);
@@ -547,6 +553,11 @@ class content_visibility
}
}
+ if ($postcount_change != 0)
+ {
+ $this->config->increment('num_posts', $postcount_change, false);
+ }
+
$update_topic_postcount = true;
// Sync the first/last topic information if needed
diff --git a/tests/content_visibility/delete_post_test.php b/tests/content_visibility/delete_post_test.php
index 8f3c6295e6..b880c8f08f 100644
--- a/tests/content_visibility/delete_post_test.php
+++ b/tests/content_visibility/delete_post_test.php
@@ -44,6 +44,7 @@ class phpbb_content_visibility_delete_post_test extends phpbb_database_test_case
'post_time' => 2,
)),
false, 'harddelete',
+ 3, // expected next post id
array(
array('post_id' => 1, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
//array('post_id' => 2, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
@@ -73,6 +74,7 @@ class phpbb_content_visibility_delete_post_test extends phpbb_database_test_case
'post_time' => 1,
)),
false, 'harddelete',
+ 2, // expected next post id
array(
//array('post_id' => 1, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
array('post_id' => 2, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
@@ -102,6 +104,7 @@ class phpbb_content_visibility_delete_post_test extends phpbb_database_test_case
'post_time' => 3,
)),
false, 'harddelete',
+ 2, // expected next post id
array(
array('post_id' => 1, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
array('post_id' => 2, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
@@ -131,6 +134,7 @@ class phpbb_content_visibility_delete_post_test extends phpbb_database_test_case
'post_time' => 2,
)),
true, 'soft delete',
+ 3, // expected next post id
array(
array('post_id' => 1, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
array('post_id' => 2, 'post_visibility' => ITEM_DELETED, 'post_delete_reason' => 'soft delete'),
@@ -160,6 +164,7 @@ class phpbb_content_visibility_delete_post_test extends phpbb_database_test_case
'post_time' => 1,
)),
true, 'soft delete',
+ 2, // expected next post id
array(
array('post_id' => 1, 'post_visibility' => ITEM_DELETED, 'post_delete_reason' => 'soft delete'),
array('post_id' => 2, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
@@ -189,6 +194,7 @@ class phpbb_content_visibility_delete_post_test extends phpbb_database_test_case
'post_time' => 3,
)),
true, 'soft delete',
+ 3, // expected next post id
array(
array('post_id' => 1, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
array('post_id' => 2, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
@@ -229,6 +235,7 @@ class phpbb_content_visibility_delete_post_test extends phpbb_database_test_case
'post_reported' => false,
),
false, 'harddelete',
+ false, // expected next post id
array(
),
array(
@@ -257,6 +264,7 @@ class phpbb_content_visibility_delete_post_test extends phpbb_database_test_case
'post_reported' => false,
),
true, 'soft delete',
+ false, // expected next post id
array(
array('post_id' => 4, 'post_visibility' => ITEM_DELETED, 'post_delete_reason' => ''),
),
@@ -278,13 +286,93 @@ class phpbb_content_visibility_delete_post_test extends phpbb_database_test_case
array('user_posts' => 3),
),
),
+ // Delete actual last post that is unapproved
+ array(
+ 3, 3, 6,
+ array(
+ 'topic_first_post_id' => 5,
+ 'topic_last_post_id' => 5,
+ 'topic_posts_approved' => 1,
+ 'topic_posts_unapproved' => 1,
+ 'topic_posts_softdeleted' => 0,
+ 'topic_visibility' => ITEM_APPROVED,
+ 'post_time' => 4,
+ 'post_visibility' => ITEM_UNAPPROVED,
+ 'post_postcount' => true,
+ 'poster_id' => 1,
+ 'post_reported' => false,
+ ),
+ false, 'harddelete',
+ 5, // expected next post id
+ array(
+ array('post_id' => 5, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
+ // array('post_id' => 6, 'post_visibility' => ITEM_UNAPPROVED, 'post_delete_reason' => ''),
+ ),
+ array(
+ array(
+ 'topic_visibility' => ITEM_APPROVED,
+ 'topic_first_post_id' => 5,
+ 'topic_last_post_id' => 5,
+ 'topic_posts_approved' => 1,
+ 'topic_posts_unapproved' => 0,
+ 'topic_posts_softdeleted' => 0,
+ 'topic_delete_reason' => '',
+ ),
+ ),
+ array(
+ array('forum_posts_approved' => 1, 'forum_posts_unapproved' => 0, 'forum_posts_softdeleted' => 0, 'forum_topics_approved' => 1, 'forum_topics_unapproved' => 0, 'forum_topics_softdeleted' => 0, 'forum_last_post_id' => 5),
+ ),
+ array(
+ array('user_posts' => 4),
+ ),
+ ),
+ // Hard delete last approved post
+ array(
+ 3, 3, 5,
+ array(
+ 'topic_first_post_id' => 5,
+ 'topic_last_post_id' => 5,
+ 'topic_posts_approved' => 1,
+ 'topic_posts_unapproved' => 1,
+ 'topic_posts_softdeleted' => 0,
+ 'topic_visibility' => ITEM_APPROVED,
+ 'post_time' => 4,
+ 'post_visibility' => ITEM_APPROVED,
+ 'post_postcount' => true,
+ 'poster_id' => 1,
+ 'post_reported' => false,
+ ),
+ false, 'harddelete',
+ 6, // expected next post id
+ array(
+ //array('post_id' => 5, 'post_visibility' => ITEM_APPROVED, 'post_delete_reason' => ''),
+ array('post_id' => 6, 'post_visibility' => ITEM_UNAPPROVED, 'post_delete_reason' => ''),
+ ),
+ array(
+ array(
+ 'topic_visibility' => ITEM_APPROVED,
+ 'topic_first_post_id' => 6,
+ 'topic_last_post_id' => 5, // can't be updated with no valid data
+ 'topic_posts_approved' => 0,
+ 'topic_posts_unapproved' => 1,
+ 'topic_posts_softdeleted' => 0,
+ 'topic_delete_reason' => '',
+ ),
+ ),
+ array(
+ array('forum_posts_approved' => 0, 'forum_posts_unapproved' => 1, 'forum_posts_softdeleted' => 0, 'forum_topics_approved' => 1, 'forum_topics_unapproved' => 0, 'forum_topics_softdeleted' => 0, 'forum_last_post_id' => 5),
+ ),
+ array(
+ array('user_posts' => 3),
+ ),
+ ),
);
}
/**
* @dataProvider delete_post_data
*/
- public function test_delete_post($forum_id, $topic_id, $post_id, $data, $is_soft, $reason, $expected_posts, $expected_topic, $expected_forum, $expected_user)
+ public function test_delete_post($forum_id, $topic_id, $post_id, $data, $is_soft, $reason, $expected_next_post_id, $expected_posts, $expected_topic, $expected_forum, $expected_user)
{
global $auth, $cache, $config, $db, $user, $phpbb_container, $phpbb_dispatcher, $phpbb_root_path, $phpEx;
@@ -327,8 +415,7 @@ class phpbb_content_visibility_delete_post_test extends phpbb_database_test_case
$search_backend_factory->method('get_active')->willReturn($search_backend);
$phpbb_container->set('search.backend_factory', $search_backend_factory);
- delete_post($forum_id, $topic_id, $post_id, $data, $is_soft, $reason);
-
+ $this->assertSame($expected_next_post_id, delete_post($forum_id, $topic_id, $post_id, $data, $is_soft, $reason));
$result = $db->sql_query('SELECT post_id, post_visibility, post_delete_reason
FROM phpbb_posts
WHERE topic_id = ' . $topic_id . '
diff --git a/tests/content_visibility/fixtures/delete_post.xml b/tests/content_visibility/fixtures/delete_post.xml
index c29ad23019..5d4e703fec 100644
--- a/tests/content_visibility/fixtures/delete_post.xml
+++ b/tests/content_visibility/fixtures/delete_post.xml
@@ -39,6 +39,19 @@