From 0c109c9d377e10757c208c1d87aa6d4bf8d9af44 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 14 May 2014 19:56:19 +0200 Subject: [PATCH 1/8] [ticket/11224] SQL cache destroy does not destroy queries to tables joined https://tracker.phpbb.com/browse/PHPBB3-11224 PHPBB3-11224 --- phpBB/includes/acm/acm_memory.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/phpBB/includes/acm/acm_memory.php b/phpBB/includes/acm/acm_memory.php index 2936ea0bae..d975e4b348 100644 --- a/phpBB/includes/acm/acm_memory.php +++ b/phpBB/includes/acm/acm_memory.php @@ -292,12 +292,24 @@ class acm_memory // 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; + return $query_result; + } + + $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]; + } } - $tables = array_map('trim', explode(',', $regs[1])); foreach ($tables as $table_name) { From 0c4b53e2f1ea14dcdc82d2e2abac1ba96e4669ee Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 14 May 2014 20:22:36 +0200 Subject: [PATCH 2/8] [ticket/11224] Fix returned data PHPBB3-11224 --- phpBB/includes/acm/acm_memory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/includes/acm/acm_memory.php b/phpBB/includes/acm/acm_memory.php index d975e4b348..83b0e61182 100644 --- a/phpBB/includes/acm/acm_memory.php +++ b/phpBB/includes/acm/acm_memory.php @@ -295,7 +295,7 @@ class acm_memory 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; + return; } $tables = array(); @@ -448,4 +448,4 @@ class acm_memory } } -?> \ No newline at end of file +?> From 9dd71fa32e13120c324a4cfb40a9c5c5534207e8 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 14 May 2014 20:23:27 +0200 Subject: [PATCH 3/8] [ticket/11224] Fix the blank line after ?> PHPBB3-11224 --- phpBB/includes/acm/acm_memory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/includes/acm/acm_memory.php b/phpBB/includes/acm/acm_memory.php index 83b0e61182..3182c5a942 100644 --- a/phpBB/includes/acm/acm_memory.php +++ b/phpBB/includes/acm/acm_memory.php @@ -448,4 +448,4 @@ class acm_memory } } -?> +?> \ No newline at end of file From b7f54e42b269374f018fe67ece11e63cf86b0c12 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Thu, 15 May 2014 00:22:27 +0200 Subject: [PATCH 4/8] [ticket/11224] Add unit test PHPBB3-11224 --- tests/cache/cache_memory.php | 62 +++++++++++++++ tests/cache/cache_memory_test.php | 109 ++++++++++++++++++++++++++ tests/cache/fixtures/cache_memory.xml | 85 ++++++++++++++++++++ 3 files changed, 256 insertions(+) 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/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..90c9f4413e --- /dev/null +++ b/tests/cache/cache_memory_test.php @@ -0,0 +1,109 @@ +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, + ), + ); + } + + /** + * @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->assertNotFalse($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 7c99e309204d5647ae2eb246d5dc939e9e90c307 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Thu, 15 May 2014 01:17:02 +0200 Subject: [PATCH 5/8] [ticket/11224] Fix coding style PHPBB3-11224 --- phpBB/includes/acm/acm_memory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/includes/acm/acm_memory.php b/phpBB/includes/acm/acm_memory.php index 3182c5a942..2ed5e9902d 100644 --- a/phpBB/includes/acm/acm_memory.php +++ b/phpBB/includes/acm/acm_memory.php @@ -299,7 +299,7 @@ class acm_memory } $tables = array(); - foreach($regs as $match) + foreach ($regs as $match) { if ($match[0][0] == 'F') { @@ -448,4 +448,4 @@ class acm_memory } } -?> \ No newline at end of file +?> From 292908ca4add9d6e31a35c7e620046b3f038e81d Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Thu, 15 May 2014 01:49:07 +0200 Subject: [PATCH 6/8] [ticket/11224] Use assertNotEquals(false) instead of assertNotFalse() assertNotFalse() is unavailable with the version of phpunit used with php 5.2 PHPBB3-11224 --- tests/cache/cache_memory_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cache/cache_memory_test.php b/tests/cache/cache_memory_test.php index 90c9f4413e..5a9f61f36e 100644 --- a/tests/cache/cache_memory_test.php +++ b/tests/cache/cache_memory_test.php @@ -103,7 +103,7 @@ class phpbb_cache_memory_test extends phpbb_database_test_case foreach ($sql_queries as $query) { - $this->assertNotFalse($this->cache->sql_load($query[0])); + $this->assertNotEquals(false, $this->cache->sql_load($query[0])); } } } From d53336af061e4c7b892c3acf4ce8fe0fa0205a72 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Thu, 15 May 2014 12:36:22 +0200 Subject: [PATCH 7/8] [ticket/11224] Remove new line in acm_memory.php PHPBB3-11224 --- phpBB/includes/acm/acm_memory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/includes/acm/acm_memory.php b/phpBB/includes/acm/acm_memory.php index 2ed5e9902d..9b68585d24 100644 --- a/phpBB/includes/acm/acm_memory.php +++ b/phpBB/includes/acm/acm_memory.php @@ -448,4 +448,4 @@ class acm_memory } } -?> +?> \ No newline at end of file From 74a96bf7c2c6d4bb0c68cf74858260d0466ed3cc Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 24 Sep 2014 16:16:47 +0200 Subject: [PATCH 8/8] [ticket/11224] Adds a test case PHPBB3-11224 --- tests/cache/cache_memory_test.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/cache/cache_memory_test.php b/tests/cache/cache_memory_test.php index 5a9f61f36e..7a529c1d04 100644 --- a/tests/cache/cache_memory_test.php +++ b/tests/cache/cache_memory_test.php @@ -73,6 +73,22 @@ class phpbb_cache_memory_test extends phpbb_database_test_case ), 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, + ), ); }