From 936b9924b3c1f00b53c1745c4c5cd1dc16bd4d61 Mon Sep 17 00:00:00 2001 From: Mischa ter Smitten Date: Mon, 14 Nov 2016 12:04:36 +0100 Subject: [PATCH] Add tests for overridden cacheMethodFilter --- .../Case/Model/Datasource/DboSourceTest.php | 75 ++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php index 9be7e90c8..1ae79694e 100644 --- a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php @@ -109,6 +109,35 @@ class DboSecondTestSource extends DboSource { } +/** + * DboFourthTestSource + * + * @package Cake.Test.Case.Model.Datasource + */ +class DboFourthTestSource extends DboSource { + + public function connect($config = array()) { + $this->connected = true; + } + + public function cacheMethodFilter($method, $key, $value) { + if ($method === 'name') { + if ($value === '`menus`') { + return false; + } elseif ($key === '1fca740733997f1ebbedacfc7678592a') { + return false; + } + } elseif ($method === 'fields') { + $endsWithName = preg_grep('/`name`$/', $value); + + return count($endsWithName) === 0; + } + + return true; + } + +} + /** * DboSourceTest class * @@ -752,7 +781,7 @@ class DboSourceTest extends CakeTestCase { $method = 'fields'; $key = '2b57253ab1fffb3e95fa4f95299220b1'; - $value = ["`Menu`.`id`", "`Menu`.`name`"]; + $value = array("`Menu`.`id`", "`Menu`.`name`"); $actual = $this->testDb->cacheMethodFilter($method, $key, $value); $this->assertTrue($actual); @@ -765,6 +794,50 @@ class DboSourceTest extends CakeTestCase { $this->assertTrue($actual); } +/** + * Test that cacheMethodFilter can be overridden to do actual filtering. + * + * @return void + */ + public function testCacheMethodFilterOverridden() { + $testDb = new DboFourthTestSource(); + + $method = 'name'; + $key = '49d9207adfce6df1dd3ee8c30c434414'; + $value = '`menus`'; + $actual = $testDb->cacheMethodFilter($method, $key, $value); + + $this->assertFalse($actual); + + $method = 'name'; + $key = '1fca740733997f1ebbedacfc7678592a'; + $value = '`Menu`.`id`'; + $actual = $testDb->cacheMethodFilter($method, $key, $value); + + $this->assertFalse($actual); + + $method = 'fields'; + $key = '2b57253ab1fffb3e95fa4f95299220b1'; + $value = array("`Menu`.`id`", "`Menu`.`name`"); + $actual = $testDb->cacheMethodFilter($method, $key, $value); + + $this->assertFalse($actual); + + $method = 'name'; + $key = 'd2bc458620afb092c61ab4383b7475e0'; + $value = '`Menu`'; + $actual = $testDb->cacheMethodFilter($method, $key, $value); + + $this->assertTrue($actual); + + $method = 'non-existing'; + $key = ''; + $value = '``'; + $actual = $testDb->cacheMethodFilter($method, $key, $value); + + $this->assertTrue($actual); + } + /** * Test that rare collisions do not happen with method caching *