diff options
author | Josh Woody <a_jelly_doughnut@phpbb.com> | 2010-06-29 21:47:18 -0500 |
---|---|---|
committer | Joas Schilling <nickvergessen@gmx.de> | 2012-08-29 17:50:11 +0200 |
commit | 67393751356e9f02f7c1ab0a2f7a3a508403edc3 (patch) | |
tree | 3ce9667e340307f77f2679f470ddb1f645aa2558 /tests | |
parent | fb13ab83e476d2afbc7bb181f7ab90df98f996da (diff) | |
download | forums-67393751356e9f02f7c1ab0a2f7a3a508403edc3.tar forums-67393751356e9f02f7c1ab0a2f7a3a508403edc3.tar.gz forums-67393751356e9f02f7c1ab0a2f7a3a508403edc3.tar.bz2 forums-67393751356e9f02f7c1ab0a2f7a3a508403edc3.tar.xz forums-67393751356e9f02f7c1ab0a2f7a3a508403edc3.zip |
[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
Diffstat (limited to 'tests')
-rw-r--r-- | tests/all_tests.php | 58 | ||||
-rw-r--r-- | tests/class_visibility/all_tests.php | 40 | ||||
-rw-r--r-- | tests/class_visibility/auth_mock.php | 290 | ||||
-rw-r--r-- | tests/class_visibility/class_visibility_test.php | 194 | ||||
-rw-r--r-- | tests/class_visibility/user_mock.php | 17 |
5 files changed, 599 insertions, 0 deletions
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 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2008 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +error_reporting(E_ALL); + +if (!defined('PHPUnit_MAIN_METHOD')) +{ + define('PHPUnit_MAIN_METHOD', 'phpbb_all_tests::main'); +} + +require_once 'test_framework/framework.php'; +require_once 'PHPUnit/TextUI/TestRunner.php'; + +require_once 'utf/all_tests.php'; +require_once 'request/all_tests.php'; +require_once 'security/all_tests.php'; +require_once 'template/all_tests.php'; +require_once 'text_processing/all_tests.php'; +require_once 'dbal/all_tests.php'; +require_once 'class_visibility/all_tests.php'; + +// exclude the test directory from code coverage reports +PHPUnit_Util_Filter::addDirectoryToFilter('./'); + +class phpbb_all_tests +{ + public static function main() + { + PHPUnit_TextUI_TestRunner::run(self::suite()); + } + + public static function suite() + { + $suite = new PHPUnit_Framework_TestSuite('phpBB'); + + $suite->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 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +if (!defined('PHPUnit_MAIN_METHOD')) +{ + define('PHPUnit_MAIN_METHOD', 'phpbb_visibility_all_tests::main'); +} + +require_once 'test_framework/framework.php'; +require_once 'PHPUnit/TextUI/TestRunner.php'; + +require_once 'class_visibility_test.php'; + +class phpbb_visibility_all_tests +{ + public static function main() + { + PHPUnit_TextUI_TestRunner::run(self::suite()); + } + + public static function suite() + { + $suite = new PHPUnit_Framework_TestSuite('phpBB Visibility class'); + + $suite->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 @@ +<?php +/** +* +* @package testing +* @version $Id$ +* @copyright (c) 2008 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/* +* phpbb_acl_mock_none +* mock a user with no permissions +*/ +class phpbb_acl_mock_none +{ + public function acl_get($opt, $f) + { + if ($opt[0] == '!') + { + return true; + } + + return false; + } + + public function acl_gets($opt, $f) + { + if ($opt[0] == '!') + { + return true; + } + + return false; + } + + public function acl_getf_global($opt) + { + if ($opt[0] == '!') + { + return true; + } + + return false; + } + + public function acl_getf($opt, $clean = false) + { + $invert = false; + if ($opt[0] == '!') + { + $invert = true; + } + + $array = array(); + if ($clean && !$invert) + { + return $array; // return empty + } + + for ($i = 1; $i < 15; $i++) + { + $array[$i] = !$invert; + } + return $array; + } +} + +class phpbb_acl_mock_founder +{ + public function acl_get($opt, $f) + { + if ($opt[0] == '!') + { + return false; + } + + return true; + } + + public function acl_gets($opt, $f) + { + if ($opt[0] == '!') + { + return false; + } + + return true; + } + + public function acl_getf_global($opt) + { + if ($opt[0] == '!') + { + return false; + } + + return true; + } + + public function acl_getf($opt, $clean = false) + { + $invert = false; + if ($opt[0] == '!') + { + $invert = true; + } + + $array = array(); + if ($clean && $invert) + { + return $array; // return empty + } + + for ($i = 1; $i < 15; $i++) + { + $array[$i] = !$invert; + } + return $array; + } +} + +class phpbb_acl_mock_moderator +{ + public function acl_get($opt, $f = 0) + { + // just don't ask. Well, ok, I'll explain anyway + // If we're negating an admin permission, we have it. + if ($opt[0] == '!' && substr($opt, 0, 3) == '!a_') + { + return true; + } + // if we're negating something which is not an admin permission, we don't have it + else if ($opt[0] == '!' && substr($opt, 0, 3) != '!a_') + { + return false; + } + // if we're not negating something that is an admin permission, we + else if (substr($opt, 0, 2) == 'a_') + { + return false; + } + + return true; + } + + public function acl_gets() + { + $limit = func_num_args(); + for ($i = 0; $i < $limit; $i++) + { + $opt = func_get_arg($i); + + if ($opt[0] == '!' && substr($opt, 0, 3) == '!a_') + { + return true; + } + else if ($opt[0] != '!' && substr($opt, 0, 2) != 'a_') + { + return true; + } + } + + return false; + } + + public function acl_getf_global($opt) + { + // this should only be called to check m_ permissions generally. Our ideal + // moderator has all m_permissions. + if ($opt[0] == '!') + { + return false; + } + + return true; + } + + public function acl_getf($opt, $clean = false) + { + // again, acl_getf should not be called for admin permissions (which are global...) + $invert = false; + if ($opt[0] == '!') + { + $invert = true; + } + + if ($clean && $invert) + { + return $array; // return empty + } + + $array = array(); + for ($i = 1; $i < 15; $i++) + { + $array[$i] = !$invert; + } + return $array; + } +} + +class phpbb_acl_mock_user +{ + public function acl_get($opt, $f = 0) + { + // just don't ask. Well, ok, I'll explain anyway + // If we're negating an admin or moderator permission (which our "user" does not have), we now have it. + if ($opt[0] == '!' && in_array(substr($opt, 0, 3), array('!a_', '!m_'))) + { + return true; + } + // if we're negating something which is not an admin permission, we don't have it + else if ($opt[0] == '!' && !in_array(substr($opt, 0, 3), array('!a_', '!m_'))) + { + return false; + } + // if we're not negating something that is an admin permission, we don't have it + else if (in_array(substr($opt, 0, 2), array('f_', 'u_'))) + { + return true; + } + + // for anything else (this leaves u_ and f_ perms), we have it. + return false; + } + + public function acl_gets() + { +// NOT IMPLEMENTED YET for the USER class + $limit = func_num_args(); + for ($i = 0; $i < $limit; $i++) + { + $opt = func_get_arg($i); + + if ($opt[0] == '!' && substr($opt, 0, 3) == '!a_') + { + return true; + } + else if ($opt[0] != '!' && substr($opt, 0, 2) != 'a_') + { + return true; + } + } + + return false; + } + + public function acl_getf_global($opt) + { + // this should only be called to check m_ permissions generally. Our user + // has no m_ permissions. + if ($opt[0] == '!') + { + return true; + } + + return false; + } + + public function acl_getf($opt, $clean = false) + { + // again, acl_getf should not be called for admin permissions (which are global...) + $invert = false; + if ($opt[0] == '!') + { + $invert = true; + $opt = substr($opt, 0, 1); + } + + $has_it = ($opt[0] == 'f'); + if ($invert) + { + $has_it = !$has_it; + } + + $array = array(); + if (!$has_it && $clean) + { + return $array; // return empty + } + + + for ($i = 1; $i < 15; $i++) + { + $array[$i] = $has_it; + } + + return $array; + } +}
\ No newline at end of file diff --git a/tests/class_visibility/class_visibility_test.php b/tests/class_visibility/class_visibility_test.php new file mode 100644 index 0000000000..edcf5c2e6f --- /dev/null +++ b/tests/class_visibility/class_visibility_test.php @@ -0,0 +1,194 @@ +<?php +/** +* +* @package testing +* @version $Id$ +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +require_once 'test_framework/framework.php'; +require_once 'class_visibility/auth_mock.php'; +require_once 'class_visibility/user_mock.php'; + +require_once '../phpBB/includes/class_visibility.php'; +require_once '../phpBB/includes/db/mysqli.php'; +require_once '../phpBB/includes/functions.php'; +require_once '../phpBB/includes/constants.php'; + +$GLOBALS['db'] = new dbal_mysqli(); + +class phpbb_class_visibility_test extends PHPUnit_Framework_TestCase +{ + public function test_get_visibility_sql() + { + $GLOBALS['auth'] = new phpbb_acl_mock_none; + + $sql1 = phpbb_visibility::get_visibility_sql('topic', 1, ''); + $this->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 @@ +<?php
+/**
+*
+* @package testing
+* @version $Id$
+* @copyright (c) 2008 phpBB Group
+* @license http://opensource.org/licenses/gpl-license.php GNU Public License
+*
+*/
+
+/*
+* Mock a very basic user object, only having data array.
+*/
+class phpbb_user_mock
+{
+ public $data;
+}
\ No newline at end of file |