From 05e76e55e149936d112ccb3441f88bbd3ce235fb Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 28 May 2014 00:54:22 +0200 Subject: [PATCH] [ticket/10899] Add unit tests PHPBB3-10899 --- phpBB/phpbb/log/log.php | 49 +++++-- tests/log/delete_test.php | 60 ++++++++ tests/log/fixtures/delete_log.xml | 232 ++++++++++++++++++++++++++++++ 3 files changed, 327 insertions(+), 14 deletions(-) create mode 100644 tests/log/delete_test.php create mode 100644 tests/log/fixtures/delete_log.xml diff --git a/phpBB/phpbb/log/log.php b/phpBB/phpbb/log/log.php index 4b1a8e40ad..8b51ed7758 100644 --- a/phpBB/phpbb/log/log.php +++ b/phpBB/phpbb/log/log.php @@ -334,19 +334,41 @@ class log implements \phpbb\log\log_interface */ public function delete($mode, $conditions = array()) { - $sql_where = ''; - $started = false; + switch ($mode) + { + case 'admin': + $log_type = LOG_ADMIN; + break; + + case 'mod': + $log_type = LOG_MOD; + break; + + case 'user': + $log_type = LOG_USERS; + break; + + case 'users': + $log_type = LOG_USERS; + break; + + case 'critical': + $log_type = LOG_CRITICAL;; + break; + + default: + $log_type = false; + } + + if ($log_type === false) + { + return; + } + + $sql_where = 'WHERE log_type = ' . $log_type; foreach ($conditions as $field => $field_value) { - if ($started) - { - $sql_where .= ' AND '; - } - else - { - $sql_where = 'WHERE '; - $started = true; - } + $sql_where .= ' AND '; if ($field == 'keywords') { @@ -354,11 +376,11 @@ class log implements \phpbb\log\log_interface } else { - if (is_array($field_value) && sizeof($field_value) == 2) + if (is_array($field_value) && sizeof($field_value) == 2 && is_string($field_value[0])) { $sql_where .= $field . ' ' . $field_value[0] . ' ' . $field_value[1]; } - else if (is_array($field_value) && sizeof($field_value) > 2) + else if (is_array($field_value)) { $sql_where .= $this->db->sql_in_set($field, $field_value); } @@ -366,7 +388,6 @@ class log implements \phpbb\log\log_interface { $sql_where .= $field . ' = ' . $field_value; } - } } diff --git a/tests/log/delete_test.php b/tests/log/delete_test.php new file mode 100644 index 0000000000..54ea4145ef --- /dev/null +++ b/tests/log/delete_test.php @@ -0,0 +1,60 @@ +createXMLDataSet(dirname(__FILE__) . '/fixtures/delete_log.xml'); + } + + public function test_log_delete() + { + global $phpbb_root_path, $phpEx, $db, $phpbb_dispatcher, $auth; + + $db = $this->new_dbal(); + $cache = new phpbb_mock_cache; + $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); + $user = $this->getMock('\phpbb\user'); + $user->data['user_id'] = 1; + $auth = $this->getMock('\phpbb\auth\auth'); + + $log = new \phpbb\log\log($db, $user, $auth, $phpbb_dispatcher, $phpbb_root_path, 'adm/', $phpEx, LOG_TABLE); + + // Delete all admin logs + $this->assertCount(2, $log->get_logs('admin')); + $log->delete('admin'); + // One entry is added to the admin log when the logs are purged + $this->assertCount(1, $log->get_logs('admin')); + + // Delete with keyword + $this->assertCount(1, $log->get_logs('mod', false, 0, 0, 0, 0, 0, 0, 'l.log_time DESC', 'guest')); + $log->delete('mod', array('keywords' => 'guest')); + $this->assertEmpty($log->get_logs('mod', false, 0, 0, 0, 0, 0, 0, 'l.log_time DESC', 'guest')); + + // Delete with simples conditions + $this->assertCount(3, $log->get_logs('mod', false, 0, 0, 12, 0, 1, 0, 'l.log_time DESC')); + $log->delete('mod', array('forum_id' => 12, 'user_id' => 1)); + $this->assertEmpty($log->get_logs('mod', false, 0, 0, 12, 0, 1, 0, 'l.log_time DESC')); + + // Delete with IN condition + $this->assertCount(2, $log->get_logs('mod', false, 0, 0, array(13, 14), 0, 0, 0, 'l.log_time DESC')); + $log->delete('mod', array('forum_id' => array(14, 13))); + $this->assertEmpty($log->get_logs('mod', false, 0, 0, array(13, 14), 0, 0, 0, 'l.log_time DESC')); + + // Delete with a custom condition (ie: WHERE x >= 10) + $this->assertCount(3, $log->get_logs('critical', false, 0, 0, 0, 0, 0, 0, 'l.log_time DESC')); + $log->delete('critical', array('user_id' => array('>', 1))); + $this->assertCount(1, $log->get_logs('critical', false, 0, 0, 0, 0, 0, 0, 'l.log_time DESC')); + } +} diff --git a/tests/log/fixtures/delete_log.xml b/tests/log/fixtures/delete_log.xml new file mode 100644 index 0000000000..4b2402102e --- /dev/null +++ b/tests/log/fixtures/delete_log.xml @@ -0,0 +1,232 @@ + + + + log_id + log_type + user_id + forum_id + topic_id + reportee_id + log_ip + log_time + log_operation + log_data + + 1 + 0 + 1 + 0 + 0 + 0 + 127.0.0.1 + 1 + LOG_INSTALL_INSTALLED + a:1:{i:0;s:9:"3.1.0-dev";} + + + 2 + 0 + 1 + 0 + 0 + 0 + 127.0.0.1 + 1 + LOG_KEY_NOT_EXISTS + a:1:{i:0;s:15:"additional_data";} + + + 3 + 2 + 1 + 0 + 0 + 0 + 127.0.0.1 + 1 + LOG_CRITICAL + a:1:{i:0;s:13:"critical data";} + + + 4 + 1 + 1 + 12 + 34 + 0 + 127.0.0.1 + 1 + LOG_MOD + + + + 5 + 1 + 1 + 12 + 45 + 0 + 127.0.0.1 + 1 + LOG_MOD + + + + 6 + 1 + 1 + 23 + 56 + 0 + 127.0.0.1 + 1 + LOG_MOD + + + + 7 + 1 + 1 + 12 + 45 + 0 + 127.0.0.1 + 1 + LOG_MOD2 + + + + 8 + 3 + 1 + 0 + 0 + 2 + 127.0.0.1 + 1 + LOG_USER + a:1:{i:0;s:5:"admin";} + + + 9 + 3 + 1 + 0 + 0 + 1 + 127.0.0.1 + 1 + LOG_USER + a:1:{i:0;s:5:"guest";} + + + 10 + 3 + 1 + 0 + 0 + 0 + 127.0.0.1 + 1 + LOG_SINGULAR_PLURAL + a:1:{i:0;i:2;} + + + 11 + 1 + 1 + 15 + 3 + 0 + 127.0.0.1 + 1 + LOG_MOD3 + a:1:{i:0;s:5:"guest";} + + + 12 + 1 + 1 + 13 + 0 + 0 + 127.0.0.1 + 1 + + + + + 13 + 1 + 1 + 14 + 0 + 0 + 127.0.0.1 + 1 + + + + + 14 + 2 + 2 + 0 + 0 + 0 + 127.0.0.1 + 1 + + + + + 15 + 2 + 2 + 0 + 0 + 0 + 127.0.0.1 + 1 + + + +
+ + user_id + username + username_clean + user_permissions + user_sig + + 1 + Anonymous + Anonymous + + + + + 2 + admin + admin + + + +
+ + topic_id + forum_id + + 34 + 12 + + + 45 + 12 + + + 56 + 23 + +
+