From 67393751356e9f02f7c1ab0a2f7a3a508403edc3 Mon Sep 17 00:00:00 2001 From: Josh Woody Date: Tue, 29 Jun 2010 21:47:18 -0500 Subject: [PATCH] [feature/soft-delete] Add unit tests for the phpbb_visibility class Add unit tests for the phpbb_visibility class. Adjust the phpbb_visibility class to pass those unit tests. The changes are pretty small, actually. PHPBB3-9657 --- phpBB/includes/class_visibility.php | 17 +- .../prosilver/template/viewtopic_body.html | 1 - tests/all_tests.php | 58 ++++ tests/class_visibility/all_tests.php | 40 +++ tests/class_visibility/auth_mock.php | 290 ++++++++++++++++++ .../class_visibility_test.php | 194 ++++++++++++ tests/class_visibility/user_mock.php | 17 + 7 files changed, 609 insertions(+), 8 deletions(-) create mode 100644 tests/all_tests.php create mode 100644 tests/class_visibility/all_tests.php create mode 100644 tests/class_visibility/auth_mock.php create mode 100644 tests/class_visibility/class_visibility_test.php create mode 100644 tests/class_visibility/user_mock.php diff --git a/phpBB/includes/class_visibility.php b/phpBB/includes/class_visibility.php index 9798e938b1..869f1078c1 100644 --- a/phpBB/includes/class_visibility.php +++ b/phpBB/includes/class_visibility.php @@ -48,11 +48,12 @@ class phpbb_visibility $clause = $db->sql_in_set($table_alias . $mode . '_visibility', $status_ary); // only allow the user to view deleted posts he himself made - if ($auth->acl_get('f_restore', $forum_id)) + if ($auth->acl_get('f_restore', $forum_id) && !$auth->acl_get('m_restore', $forum_id)) { - $clause = 'AND (' . $clause . " + $poster_column = ($mode == 'topic') ? 'topic_poster' : 'poster_id'; + $clause = '(' . $clause . " OR ($table_alias{$mode}_visibility = " . ITEM_DELETED . " - AND {$table_alias}poster_id = " . $user->data['user_id'] . '))'; + AND $table_alias$poster_column = " . $user->data['user_id'] . '))'; } @@ -92,10 +93,12 @@ class phpbb_visibility // we also allow the user to view deleted posts he himself made $user_restore_forums = array_diff(array_keys($auth->acl_getf('f_restore', true)), $exclude_forum_ids); - if (sizeof($user_restore_forums)) + if (sizeof($user_restore_forums) && !sizeof($restore_forums)) { + $poster_column = ($mode == 'topic') ? 'topic_poster' : 'poster_id'; + // specify the poster ID, the visibility type, and the forums we're interested in - $where_sql .= " OR ($table_alias{$mode}poster_id = " . $user->data['user_id'] . " + $where_sql .= " OR ($table_alias$poster_column = " . $user->data['user_id'] . " AND $table_alias{$mode}_visibility = " . ITEM_DELETED . " AND " . $db->sql_in_set($table_alias . 'forum_id', $user_restore_forums) . ')'; } @@ -187,7 +190,7 @@ class phpbb_visibility { return true; } - else if ($auth->acl_get('f_softdelete', $forum_id) && $poster_id == $user->data['poster_id'] && !$post_locked) + else if ($auth->acl_get('f_softdelete', $forum_id) && $poster_id == $user->data['user_id'] && !$post_locked) { return true; } @@ -245,7 +248,7 @@ class phpbb_visibility // If this is an edited topic or the first post the topic gets completely disapproved later on... $sql_data[FORUMS_TABLE] = 'forum_topics = forum_topics - 1'; - $sql_data[FORUMS_TABLE] = 'forum_posts = forum_posts - ' . ($topic_row['topic_replies'] + 1); + $sql_data[FORUMS_TABLE] .= ', forum_posts = forum_posts - ' . ($topic_row['topic_replies'] + 1); set_config_count('num_topics', -1, true); set_config_count('num_posts', ($topic_row['topic_replies'] + 1) * (-1), true); diff --git a/phpBB/styles/prosilver/template/viewtopic_body.html b/phpBB/styles/prosilver/template/viewtopic_body.html index 0c4a7df7f4..60fb080da1 100644 --- a/phpBB/styles/prosilver/template/viewtopic_body.html +++ b/phpBB/styles/prosilver/template/viewtopic_body.html @@ -138,7 +138,6 @@

class="first"> {postrow.POST_SUBJECT}

{postrow.MINI_POST_IMG}{postrow.MINI_POST_IMG}{L_POST_BY_AUTHOR} {postrow.POST_AUTHOR_FULL} » {postrow.POST_DATE}

-<<<<<<< HEAD

diff --git a/tests/all_tests.php b/tests/all_tests.php new file mode 100644 index 0000000000..896085f581 --- /dev/null +++ b/tests/all_tests.php @@ -0,0 +1,58 @@ +addTest(phpbb_utf_all_tests::suite()); + $suite->addTest(phpbb_request_all_tests::suite()); + $suite->addTest(phpbb_security_all_tests::suite()); + $suite->addTest(phpbb_template_all_tests::suite()); + $suite->addTest(phpbb_text_processing_all_tests::suite()); + $suite->addTest(phpbb_dbal_all_tests::suite()); + $suite->addTest(phpbb_visibility_all_tests::suite()); + + return $suite; + } +} + +if (PHPUnit_MAIN_METHOD == 'phpbb_all_tests::main') +{ + phpbb_all_tests::main(); +} + diff --git a/tests/class_visibility/all_tests.php b/tests/class_visibility/all_tests.php new file mode 100644 index 0000000000..1e5f5b8fd9 --- /dev/null +++ b/tests/class_visibility/all_tests.php @@ -0,0 +1,40 @@ +addTestSuite('phpbb_class_visibility_test'); + + return $suite; + } +} + +if (PHPUnit_MAIN_METHOD == 'phpbb_visibility_all_tests::main') +{ + phpbb_visibility_all_tests::main(); +} diff --git a/tests/class_visibility/auth_mock.php b/tests/class_visibility/auth_mock.php new file mode 100644 index 0000000000..d829da4541 --- /dev/null +++ b/tests/class_visibility/auth_mock.php @@ -0,0 +1,290 @@ +assertEquals('topic_visibility = 1', $sql1); + + $sql2 = phpbb_visibility::get_visibility_sql('post', 1, ''); + $this->assertEquals('post_visibility = 1', $sql2); + + $GLOBALS['auth'] = new phpbb_acl_mock_founder; + + $sql3 = phpbb_visibility::get_visibility_sql('topic', 1, ''); + $this->assertEquals('topic_visibility IN (1, 0, 2)', $sql3); + + $sql4 = phpbb_visibility::get_visibility_sql('post', 1, ''); + $this->assertEquals('post_visibility IN (1, 0, 2)', $sql4); + + $GLOBALS['auth'] = new phpbb_acl_mock_user; + $GLOBALS['user'] = new phpbb_user_mock; + $GLOBALS['user']->data['user_id'] = 2; + + $sql1 = phpbb_visibility::get_visibility_sql('topic', 1, ''); + $this->assertEquals('(topic_visibility = 1 + OR (topic_visibility = 2 + AND topic_poster = 2))', $sql1); + + $sql2 = phpbb_visibility::get_visibility_sql('post', 1, ''); + $this->assertEquals('(post_visibility = 1 + OR (post_visibility = 2 + AND poster_id = 2))', $sql2); + } + + public function test_get_visibility_sql_global() + { + $GLOBALS['auth'] = new phpbb_acl_mock_none; + + $sql1 = phpbb_visibility::get_visibility_sql_global('topic', array(), ''); + $this->assertEquals('(topic_visibility = 1)', $sql1); + + $sql2 = phpbb_visibility::get_visibility_sql_global('post', array(), ''); + $this->assertEquals('(post_visibility = 1)', $sql2); + + $sql3 = phpbb_visibility::get_visibility_sql_global('post', range(2, 15), ''); + $this->assertEquals('(post_visibility = 1)', $sql3); + + $GLOBALS['auth'] = new phpbb_acl_mock_founder; + + $sql1 = phpbb_visibility::get_visibility_sql_global('topic', array(), ''); + $this->assertEquals('(topic_visibility = 1 OR (topic_visibility = 0 + AND forum_id IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)) OR (topic_visibility = 2 + AND forum_id IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)))', $sql1); + + $sql2 = phpbb_visibility::get_visibility_sql_global('post', array(), ''); + $this->assertEquals('(post_visibility = 1 OR (post_visibility = 0 + AND forum_id IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)) OR (post_visibility = 2 + AND forum_id IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)))', $sql2); + + $sql3 = phpbb_visibility::get_visibility_sql_global('post', range(2, 14), ''); + $this->assertEquals('(post_visibility = 1 OR (post_visibility = 0 + AND forum_id = 1) OR (post_visibility = 2 + AND forum_id = 1))', $sql3); + + $GLOBALS['auth'] = new phpbb_acl_mock_user; + $GLOBALS['user'] = new phpbb_user_mock; + $GLOBALS['user']->data['user_id'] = 2; + + $sql1 = phpbb_visibility::get_visibility_sql_global('topic', array(), ''); + $this->assertEquals('(topic_visibility = 1 OR (topic_poster = 2 + AND topic_visibility = 2 + AND forum_id IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)))', $sql1); + + $sql2 = phpbb_visibility::get_visibility_sql_global('post', array(), ''); + $this->assertEquals('(post_visibility = 1 OR (poster_id = 2 + AND post_visibility = 2 + AND forum_id IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)))', $sql2); + + $sql3 = phpbb_visibility::get_visibility_sql_global('post', range(2, 14), ''); + $this->assertEquals('(post_visibility = 1 OR (poster_id = 2 + AND post_visibility = 2 + AND forum_id = 1))', $sql3); + } + + public function test_can_soft_delete() + { + $GLOBALS['user'] = new phpbb_user_mock; + $GLOBALS['user']->data['user_id'] = 2; + + $GLOBALS['auth'] = new phpbb_acl_mock_founder; + $result = phpbb_visibility::can_soft_delete(1, 4, true); + $this->assertEquals(true, $result); + + $result = phpbb_visibility::can_soft_delete(1, 2, false); + $this->assertEquals(true, $result); + + $GLOBALS['auth'] = new phpbb_acl_mock_none; + $result = phpbb_visibility::can_soft_delete(1, 4, true); + $this->assertEquals(false, $result); + + $result = phpbb_visibility::can_soft_delete(1, 2, false); + $this->assertEquals(false, $result); + + $GLOBALS['auth'] = new phpbb_acl_mock_user; + $result = phpbb_visibility::can_soft_delete(1, 4, true); + $this->assertEquals(false, $result); + + $result = phpbb_visibility::can_soft_delete(1, 2, false); + $this->assertEquals(true, $result); + + $result = phpbb_visibility::can_soft_delete(1, 2, true); + $this->assertEquals(false, $result); + } + + public function test_can_restore() + { + $GLOBALS['user'] = new phpbb_user_mock; + $GLOBALS['user']->data['user_id'] = 2; + + $GLOBALS['auth'] = new phpbb_acl_mock_founder; + $result = phpbb_visibility::can_restore(1, 4, true); + $this->assertEquals(true, $result); + + $result = phpbb_visibility::can_restore(1, 2, false); + $this->assertEquals(true, $result); + + $GLOBALS['auth'] = new phpbb_acl_mock_none; + $result = phpbb_visibility::can_restore(1, 4, true); + $this->assertEquals(false, $result); + + $result = phpbb_visibility::can_restore(1, 2, false); + $this->assertEquals(false, $result); + + $GLOBALS['auth'] = new phpbb_acl_mock_user; + $result = phpbb_visibility::can_restore(1, 4, true); + $this->assertEquals(false, $result); + + $result = phpbb_visibility::can_restore(1, 2, false); + $this->assertEquals(true, $result); + + $result = phpbb_visibility::can_restore(1, 2, true); + $this->assertEquals(false, $result); + } + + public function test_hide_topic() + { + $GLOBALS['auth'] = new phpbb_acl_mock_founder; + + $topic_row = array('topic_replies' => 3); + $sql_data = array(); + phpbb_visibility::hide_topic(4, 2, $topic_row, $sql_data); + $this->assertEquals( + array(FORUMS_TABLE => 'forum_topics = forum_topics - 1, forum_posts = forum_posts - 4', USERS_TABLE => 'user_posts = user_posts - 1'), + $sql_data); + } + + public function test_hide_post() + { + $GLOBALS['auth'] = new phpbb_acl_mock_founder; + + $sql_data = array(); + phpbb_visibility::hide_post(4, 111122211, $sql_data); + $this->assertEquals( + array(FORUMS_TABLE => 'forum_posts = forum_posts - 1', + TOPICS_TABLE => 'topic_replies = topic_replies - 1, topic_last_view_time = 111122211', + USERS_TABLE => 'user_posts = user_posts - 1'), + $sql_data); + } +} + +//$a = new phpbb_class_visibility_test; +//$a->test_can_soft_delete(); \ No newline at end of file diff --git a/tests/class_visibility/user_mock.php b/tests/class_visibility/user_mock.php new file mode 100644 index 0000000000..cbeec23875 --- /dev/null +++ b/tests/class_visibility/user_mock.php @@ -0,0 +1,17 @@ +