From 8e638dbf6d53be3d8c5b1eda8ce44e7e4f60b75a Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 1 Oct 2014 15:00:01 +0200 Subject: [PATCH 1/2] [ticket/11224] Revert Revert "Merge pull request #2460 from ticket/11224" This reverts commit 40cd7570e6f4da7cc60d83a3e99c72a2fb99e3f7. --- phpBB/phpbb/cache/driver/memory.php | 16 +++- tests/cache/cache_memory.php | 62 +++++++++++++ tests/cache/cache_memory_test.php | 125 ++++++++++++++++++++++++++ tests/cache/fixtures/cache_memory.xml | 85 ++++++++++++++++++ 4 files changed, 286 insertions(+), 2 deletions(-) create mode 100644 tests/cache/cache_memory.php create mode 100644 tests/cache/cache_memory_test.php create mode 100644 tests/cache/fixtures/cache_memory.xml diff --git a/phpBB/phpbb/cache/driver/memory.php b/phpBB/phpbb/cache/driver/memory.php index 56308be8da..0b0e323e3d 100644 --- a/phpBB/phpbb/cache/driver/memory.php +++ b/phpBB/phpbb/cache/driver/memory.php @@ -208,12 +208,24 @@ abstract class memory extends \phpbb\cache\driver\base // determine which tables this query belongs to // Some queries use backticks, namely the get_database_size() query // don't check for conformity, the SQL would error and not reach here. - if (!preg_match('/FROM \\(?(`?\\w+`?(?: \\w+)?(?:, ?`?\\w+`?(?: \\w+)?)*)\\)?/', $query, $regs)) + if (!preg_match_all('/(?:FROM \\(?(`?\\w+`?(?: \\w+)?(?:, ?`?\\w+`?(?: \\w+)?)*)\\)?)|(?:JOIN (`?\\w+`?(?: \\w+)?))/', $query, $regs, PREG_SET_ORDER)) { // Bail out if the match fails. return $query_result; } - $tables = array_map('trim', explode(',', $regs[1])); + + $tables = array(); + foreach ($regs as $match) + { + if ($match[0][0] == 'F') + { + $tables = array_merge($tables, array_map('trim', explode(',', $match[1]))); + } + else + { + $tables[] = $match[2]; + } + } foreach ($tables as $table_name) { diff --git a/tests/cache/cache_memory.php b/tests/cache/cache_memory.php new file mode 100644 index 0000000000..c468cb4658 --- /dev/null +++ b/tests/cache/cache_memory.php @@ -0,0 +1,62 @@ +data[$var]; + } + + /** + * Store data in the cache + * + * @access protected + * @param string $var Cache key + * @param mixed $data Data to store + * @param int $ttl Time-to-live of cached data + * @return bool True if the operation succeeded + */ + function _write($var, $data, $ttl = 2592000) + { + $this->data[$var] = $data; + return true; + } + + /** + * Remove an item from the cache + * + * @access protected + * @param string $var Cache key + * @return bool True if the operation succeeded + */ + function _delete($var) + { + unset($this->data[$var]); + return true; + } +} diff --git a/tests/cache/cache_memory_test.php b/tests/cache/cache_memory_test.php new file mode 100644 index 0000000000..7a529c1d04 --- /dev/null +++ b/tests/cache/cache_memory_test.php @@ -0,0 +1,125 @@ +createXMLDataSet(dirname(__FILE__).'/fixtures/cache_memory.xml'); + } + + protected function setUp() + { + global $db; + parent::setUp(); + + $this->cache = new phpbb_cache_memory(); + $db = $this->new_dbal(); + } + + static public function cache_single_query_data() + { + return array( + array( + array( + array( + 'SELECT * FROM ' . POSTS_TABLE, + 3, + ), + ), + POSTS_TABLE, + ), + array( + array( + array( + 'SELECT * FROM ' . POSTS_TABLE, + 3, + ), + array( + 'SELECT * FROM ' . POSTS_TABLE . ' p + LEFT JOIN ' . TOPICS_TABLE . ' t ON p.topic_id = t.topic_id', + 3, + ), + ), + POSTS_TABLE, + ), + array( + array( + array( + 'SELECT * FROM ' . POSTS_TABLE, + 3, + ), + array( + 'SELECT * FROM ' . POSTS_TABLE . ' p + LEFT JOIN ' . TOPICS_TABLE . ' t ON p.topic_id = t.topic_id', + 3, + ), + array( + 'SELECT * FROM ' . POSTS_TABLE . ' p + LEFT JOIN ' . TOPICS_TABLE . ' t ON p.topic_id = t.topic_id + LEFT JOIN ' . USERS_TABLE . ' u ON p.poster_id = u.user_id', + 3, + ), + ), + POSTS_TABLE, + ), + array( + array( + array( + 'SELECT * FROM ' . POSTS_TABLE . ' p + LEFT JOIN ' . TOPICS_TABLE . ' t ON p.topic_id = t.topic_id', + 3, + ), + array( + 'SELECT * FROM ' . POSTS_TABLE . ' p + LEFT JOIN ' . TOPICS_TABLE . ' t ON p.topic_id = t.topic_id + LEFT JOIN ' . USERS_TABLE . ' u ON p.poster_id = u.user_id', + 3, + ), + ), + TOPICS_TABLE, + ), + ); + } + + /** + * @dataProvider cache_single_query_data + */ + public function test_cache_single_query($sql_queries, $table) + { + global $db; + + foreach ($sql_queries as $query) + { + $sql_request_res = $db->sql_query($query[0]); + + $this->cache->sql_save($query[0], $sql_request_res, 1); + + $results = array(); + $query_id = $this->cache->sql_load($query[0]); + while ($row = $this->cache->sql_fetchrow($query_id)) + { + $results[] = $row; + } + $this->cache->sql_freeresult($query_id); + $this->assertEquals($query[1], sizeof($results)); + } + + $this->cache->destroy('sql', $table); + + foreach ($sql_queries as $query) + { + $this->assertNotEquals(false, $this->cache->sql_load($query[0])); + } + } +} diff --git a/tests/cache/fixtures/cache_memory.xml b/tests/cache/fixtures/cache_memory.xml new file mode 100644 index 0000000000..6954c7b76b --- /dev/null +++ b/tests/cache/fixtures/cache_memory.xml @@ -0,0 +1,85 @@ + + + + topic_id + forum_id + topic_title + topic_first_post_id + topic_last_post_id + + 1 + 1 + Topic + 2 + 2 + +
+ + post_id + poster_id + topic_id + forum_id + post_text + + 1 + 1 + 1 + 1 + Post 1 + + + 2 + 2 + 1 + 1 + Post 2 + + + 3 + 3 + 1 + 1 + Post 3 + +
+ + user_id + user_posts + username + username_clean + user_permissions + user_sig + user_occ + user_interests + + 1 + 1 + user 1 + user 1 + + + + + + + 2 + 1 + user 2 + user 2 + + + + + + + 3 + 1 + user 3 + user 3 + + + + + +
+
From 5354c46e01a1efd65d4b53af12173ef4947eec9c Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 1 Oct 2014 15:13:31 +0200 Subject: [PATCH 2/2] [ticket/11224] Updates tests PHPBB3-11224 --- tests/cache/cache_memory.php | 20 +++++++++++--------- tests/cache/cache_memory_test.php | 24 ++++++++++++++---------- tests/cache/fixtures/cache_memory.xml | 8 -------- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/tests/cache/cache_memory.php b/tests/cache/cache_memory.php index c468cb4658..806edb963a 100644 --- a/tests/cache/cache_memory.php +++ b/tests/cache/cache_memory.php @@ -1,15 +1,17 @@ + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ -require_once dirname(__FILE__) . '/../../phpBB/includes/acm/acm_memory.php'; - -class phpbb_cache_memory extends acm_memory +class phpbb_cache_memory extends \phpbb\cache\driver\memory { protected $data = array(); diff --git a/tests/cache/cache_memory_test.php b/tests/cache/cache_memory_test.php index 7a529c1d04..9f92e8d8dc 100644 --- a/tests/cache/cache_memory_test.php +++ b/tests/cache/cache_memory_test.php @@ -1,17 +1,22 @@ + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ require_once dirname(__FILE__) . '/cache_memory.php'; class phpbb_cache_memory_test extends phpbb_database_test_case { protected $cache; + protected $db; public function getDataSet() { @@ -25,6 +30,7 @@ class phpbb_cache_memory_test extends phpbb_database_test_case $this->cache = new phpbb_cache_memory(); $db = $this->new_dbal(); + $this->db = $db; } static public function cache_single_query_data() @@ -97,13 +103,11 @@ class phpbb_cache_memory_test extends phpbb_database_test_case */ public function test_cache_single_query($sql_queries, $table) { - global $db; - foreach ($sql_queries as $query) { - $sql_request_res = $db->sql_query($query[0]); + $sql_request_res = $this->db->sql_query($query[0]); - $this->cache->sql_save($query[0], $sql_request_res, 1); + $this->cache->sql_save($this->db, $query[0], $sql_request_res, 1); $results = array(); $query_id = $this->cache->sql_load($query[0]); diff --git a/tests/cache/fixtures/cache_memory.xml b/tests/cache/fixtures/cache_memory.xml index 6954c7b76b..9c19ebb7ba 100644 --- a/tests/cache/fixtures/cache_memory.xml +++ b/tests/cache/fixtures/cache_memory.xml @@ -49,8 +49,6 @@ username_clean user_permissions user_sig - user_occ - user_interests 1 1 @@ -58,8 +56,6 @@ user 1 - - 2 @@ -68,8 +64,6 @@ user 2 - - 3 @@ -78,8 +72,6 @@ user 3 - -