Merge pull request #2426 from Nicofuma/ticket/12174

[ticket/12174] Update topic_attachment flag when a post is soft-deleted

* Nicofuma/ticket/12174:
  [ticket/12174] Add tests
  [ticket/12174] Coding style
  [ticket/12174] Remove $update_topic_attachments_flag
  [ticket/12174] Don't update the flag for a post without attachment
  [ticket/12174] Update the conditions
  [ticket/12174] Remove inline assignment
  [ticket/12174] Update sql query
  [ticket/12174] Revert the changes on $topic_update_array
  [ticket/12174] Corrections
  [ticket/12174] Update topic_attachment flag when a post is soft-deleted
This commit is contained in:
Joas Schilling 2014-05-28 21:45:39 +02:00
commit e2308df14d
4 changed files with 172 additions and 11 deletions

View file

@ -387,6 +387,7 @@ class content_visibility
$update_topic_postcount = false; $update_topic_postcount = false;
} }
$topic_update_array = array();
// Update the topic's reply count and the forum's post count // Update the topic's reply count and the forum's post count
if ($update_topic_postcount) if ($update_topic_postcount)
{ {
@ -424,20 +425,14 @@ class content_visibility
if (sizeof($sql_ary)) if (sizeof($sql_ary))
{ {
$topic_sql = $forum_sql = array(); $forum_sql = array();
foreach ($sql_ary as $field => $value_change) foreach ($sql_ary as $field => $value_change)
{ {
$topic_sql[] = 'topic_' . $field . ' = topic_' . $field . $value_change; $topic_update_array[] = 'topic_' . $field . ' = topic_' . $field . $value_change;
$forum_sql[] = 'forum_' . $field . ' = forum_' . $field . $value_change; $forum_sql[] = 'forum_' . $field . ' = forum_' . $field . $value_change;
} }
// Update the number for replies and posts
$sql = 'UPDATE ' . $this->topics_table . '
SET ' . implode(', ', $topic_sql) . '
WHERE topic_id = ' . (int) $topic_id;
$this->db->sql_query($sql);
$sql = 'UPDATE ' . $this->forums_table . ' $sql = 'UPDATE ' . $this->forums_table . '
SET ' . implode(', ', $forum_sql) . ' SET ' . implode(', ', $forum_sql) . '
WHERE forum_id = ' . (int) $forum_id; WHERE forum_id = ' . (int) $forum_id;
@ -445,6 +440,38 @@ class content_visibility
} }
} }
if ($post_id)
{
$sql = 'SELECT 1 AS has_attachments
FROM ' . POSTS_TABLE . '
WHERE topic_id = ' . (int) $topic_id . '
AND post_attachment = 1
AND post_visibility = ' . ITEM_APPROVED . '
AND ' . $this->db->sql_in_set('post_id', $post_id, true);
$result = $this->db->sql_query_limit($sql, 1);
$has_attachment = (bool) $this->db->sql_fetchfield('has_attachments');
$this->db->sql_freeresult($result);
if ($has_attachment && $visibility == ITEM_APPROVED)
{
$topic_update_array[] = 'topic_attachment = 1';
}
else if (!$has_attachment && $visibility != ITEM_APPROVED)
{
$topic_update_array[] = 'topic_attachment = 0';
}
}
if (!empty($topic_update_array))
{
// Update the number for replies and posts, and update the attachments flag
$sql = 'UPDATE ' . $this->topics_table . '
SET ' . implode(', ', $topic_update_array) . '
WHERE topic_id = ' . (int) $topic_id;
$this->db->sql_query($sql);
}
return $data; return $data;
} }

View file

@ -935,7 +935,7 @@ else
// Container for user details, only process once // Container for user details, only process once
$post_list = $user_cache = $id_cache = $attachments = $attach_list = $rowset = $update_count = $post_edit_list = $post_delete_list = array(); $post_list = $user_cache = $id_cache = $attachments = $attach_list = $rowset = $update_count = $post_edit_list = $post_delete_list = array();
$has_attachments = $display_notice = false; $has_unapproved_attachments = $has_approved_attachments = $display_notice = false;
$bbcode_bitfield = ''; $bbcode_bitfield = '';
$i = $i_total = 0; $i = $i_total = 0;
@ -1046,7 +1046,11 @@ while ($row = $db->sql_fetchrow($result))
if ($row['post_visibility'] == ITEM_UNAPPROVED || $row['post_visibility'] == ITEM_REAPPROVE) if ($row['post_visibility'] == ITEM_UNAPPROVED || $row['post_visibility'] == ITEM_REAPPROVE)
{ {
$has_attachments = true; $has_unapproved_attachments = true;
}
else if ($row['post_visibility'] == ITEM_APPROVED)
{
$has_approved_attachments = true;
} }
} }
@ -1350,7 +1354,7 @@ if (sizeof($attach_list))
$db->sql_query($sql); $db->sql_query($sql);
} }
} }
else if ($has_attachments && !$topic_data['topic_attachment']) else if ($has_approved_attachments && !$topic_data['topic_attachment'])
{ {
// Topic has approved attachments but its flag is wrong // Topic has approved attachments but its flag is wrong
$sql = 'UPDATE ' . TOPICS_TABLE . " $sql = 'UPDATE ' . TOPICS_TABLE . "
@ -1360,6 +1364,11 @@ if (sizeof($attach_list))
$topic_data['topic_attachment'] = 1; $topic_data['topic_attachment'] = 1;
} }
else if ($has_unapproved_attachments && !$topic_data['topic_attachment'])
{
// Topic has only unapproved attachments but we have the right to see and download them
$topic_data['topic_attachment'] = 1;
}
} }
else else
{ {

View file

@ -10,6 +10,7 @@
<column>topic_posts_approved</column> <column>topic_posts_approved</column>
<column>topic_posts_softdeleted</column> <column>topic_posts_softdeleted</column>
<column>topic_posts_unapproved</column> <column>topic_posts_unapproved</column>
<column>topic_attachment</column>
<row> <row>
<value>1</value> <value>1</value>
<value>1</value> <value>1</value>
@ -20,6 +21,7 @@
<value>1</value> <value>1</value>
<value>1</value> <value>1</value>
<value>1</value> <value>1</value>
<value>0</value>
</row> </row>
<row> <row>
@ -32,6 +34,7 @@
<value>1</value> <value>1</value>
<value>1</value> <value>1</value>
<value>1</value> <value>1</value>
<value>0</value>
</row> </row>
<row> <row>
@ -44,6 +47,33 @@
<value>1</value> <value>1</value>
<value>0</value> <value>0</value>
<value>0</value> <value>0</value>
<value>0</value>
</row>
<row>
<value>10</value>
<value>10</value>
<value>1</value>
<value>Only 3 posts (2 with attachments)</value>
<value>10</value>
<value>12</value>
<value>3</value>
<value>0</value>
<value>0</value>
<value>1</value>
</row>
<row>
<value>11</value>
<value>10</value>
<value>1</value>
<value>Only 2 posts (1 with attachments)</value>
<value>13</value>
<value>14</value>
<value>3</value>
<value>0</value>
<value>0</value>
<value>1</value>
</row> </row>
</table> </table>
<table name="phpbb_posts"> <table name="phpbb_posts">
@ -53,6 +83,7 @@
<column>forum_id</column> <column>forum_id</column>
<column>post_visibility</column> <column>post_visibility</column>
<column>post_text</column> <column>post_text</column>
<column>post_attachment</column>
<row> <row>
<value>1</value> <value>1</value>
<value>1</value> <value>1</value>
@ -60,6 +91,7 @@
<value>1</value> <value>1</value>
<value>0</value> <value>0</value>
<value>Unapproved</value> <value>Unapproved</value>
<value>0</value>
</row> </row>
<row> <row>
<value>2</value> <value>2</value>
@ -68,6 +100,7 @@
<value>1</value> <value>1</value>
<value>1</value> <value>1</value>
<value>Approved</value> <value>Approved</value>
<value>0</value>
</row> </row>
<row> <row>
<value>3</value> <value>3</value>
@ -76,6 +109,7 @@
<value>1</value> <value>1</value>
<value>2</value> <value>2</value>
<value>Softdeleted</value> <value>Softdeleted</value>
<value>0</value>
</row> </row>
<row> <row>
@ -85,6 +119,7 @@
<value>1</value> <value>1</value>
<value>0</value> <value>0</value>
<value>Unapproved</value> <value>Unapproved</value>
<value>0</value>
</row> </row>
<row> <row>
<value>5</value> <value>5</value>
@ -93,6 +128,7 @@
<value>1</value> <value>1</value>
<value>1</value> <value>1</value>
<value>Approved</value> <value>Approved</value>
<value>0</value>
</row> </row>
<row> <row>
<value>6</value> <value>6</value>
@ -101,6 +137,7 @@
<value>1</value> <value>1</value>
<value>1</value> <value>1</value>
<value>Approved 2</value> <value>Approved 2</value>
<value>0</value>
</row> </row>
<row> <row>
<value>7</value> <value>7</value>
@ -109,6 +146,7 @@
<value>1</value> <value>1</value>
<value>2</value> <value>2</value>
<value>Softdeleted</value> <value>Softdeleted</value>
<value>0</value>
</row> </row>
<row> <row>
<value>8</value> <value>8</value>
@ -117,6 +155,52 @@
<value>1</value> <value>1</value>
<value>1</value> <value>1</value>
<value>Approved</value> <value>Approved</value>
<value>0</value>
</row>
<row>
<value>10</value>
<value>1</value>
<value>10</value>
<value>10</value>
<value>1</value>
<value>Softdeleted</value>
<value>1</value>
</row>
<row>
<value>11</value>
<value>1</value>
<value>10</value>
<value>10</value>
<value>1</value>
<value>Softdeleted</value>
<value>1</value>
</row>
<row>
<value>12</value>
<value>1</value>
<value>10</value>
<value>10</value>
<value>1</value>
<value>Approved</value>
<value>0</value>
</row>
<row>
<value>13</value>
<value>1</value>
<value>11</value>
<value>10</value>
<value>1</value>
<value>Approved</value>
<value>1</value>
</row>
<row>
<value>14</value>
<value>1</value>
<value>11</value>
<value>10</value>
<value>1</value>
<value>Approved</value>
<value>0</value>
</row> </row>
</table> </table>
<table name="phpbb_users"> <table name="phpbb_users">

View file

@ -144,4 +144,45 @@ class phpbb_content_visibility_set_post_visibility_test extends phpbb_database_t
$this->assertEquals($expected_topic, $db->sql_fetchrowset($result)); $this->assertEquals($expected_topic, $db->sql_fetchrowset($result));
$db->sql_freeresult($result); $db->sql_freeresult($result);
} }
public function set_post_soft_deleted_data()
{
return array(
array(
10, 10, 10,
1, time(), 'soft-deleted',
true, false,
array(array('topic_attachment' => 1)),
),
array(
13, 11, 10,
1, time(), 'soft-deleted',
true, false,
array(array('topic_attachment' => 0)),
),
);
}
/**
* @dataProvider set_post_soft_deleted_data
*/
public function test_set_post_soft_deleted($post_id, $topic_id, $forum_id, $user_id, $time, $reason, $is_starter, $is_latest, $expected)
{
global $cache, $db, $auth, $phpbb_root_path, $phpEx;
$cache = new phpbb_mock_cache;
$db = $this->new_dbal();
$auth = $this->getMock('\phpbb\auth\auth');
$user = $this->getMock('\phpbb\user');
$content_visibility = new \phpbb\content_visibility($auth, $db, $user, $phpbb_root_path, $phpEx, FORUMS_TABLE, POSTS_TABLE, TOPICS_TABLE, USERS_TABLE);
$content_visibility->set_post_visibility(ITEM_DELETED, $post_id, $topic_id, $forum_id, $user_id, $time, $reason, $is_starter, $is_latest);
$result = $db->sql_query('SELECT topic_attachment
FROM phpbb_topics
WHERE topic_id = ' . $topic_id);
$this->assertEquals($expected, $db->sql_fetchrowset($result));
$db->sql_freeresult($result);
}
} }