From 05b659b2528ad78177a3fe88219ece43079cd320 Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Wed, 24 Jul 2013 17:56:42 -0700 Subject: [PATCH] MDL-40727 Cache: Fix file cache store on windows. --- cache/stores/file/lib.php | 4 +++- cache/tests/cache_test.php | 25 +++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/cache/stores/file/lib.php b/cache/stores/file/lib.php index 96d11b29ed6..1fd7dbfd119 100644 --- a/cache/stores/file/lib.php +++ b/cache/stores/file/lib.php @@ -314,7 +314,9 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i return $this->path . '/' . $key . '.cache'; } else { // We are using a single subdirectory to achieve 1 level. - $subdir = substr($key, 0, 3); + // We suffix the subdir so it does not clash with any windows + // reserved filenames like 'con'. + $subdir = substr($key, 0, 3) . '-cache'; $dir = $this->path . '/' . $subdir; if ($create) { // Create the directory. This function does it recursivily! diff --git a/cache/tests/cache_test.php b/cache/tests/cache_test.php index 5121875c61d..138800457e6 100644 --- a/cache/tests/cache_test.php +++ b/cache/tests/cache_test.php @@ -119,6 +119,23 @@ class cache_phpunit_tests extends advanced_testcase { } } + /** + * Tests for cache keys that would break on windows. + */ + public function test_windows_nasty_keys() { + $instance = cache_config_phpunittest::instance(); + $instance->phpunit_add_definition('phpunit/windowskeytest', array( + 'mode' => cache_store::MODE_APPLICATION, + 'component' => 'phpunit', + 'area' => 'windowskeytest', + 'simplekeys' => true, + 'simpledata' => true + )); + $cache = cache::make('phpunit', 'windowskeytest'); + $this->assertTrue($cache->set('contest', 'test data 1')); + $this->assertEquals('test data 1', $cache->get('contest')); + } + /** * Tests the default application cache */ @@ -191,9 +208,9 @@ class cache_phpunit_tests extends advanced_testcase { * @param cache_loader $cache */ protected function run_on_cache(cache_loader $cache) { - $key = 'testkey'; + $key = 'contestkey'; $datascalars = array('test data', null); - $dataarray = array('test' => 'data', 'part' => 'two'); + $dataarray = array('contest' => 'data', 'part' => 'two'); $dataobject = (object)$dataarray; foreach ($datascalars as $datascalar) { @@ -850,7 +867,7 @@ class cache_phpunit_tests extends advanced_testcase { // OK data added, data invalidated, and invalidation time has been set. // Now we need to manually add back the data and adjust the invalidation time. $hash = md5(cache_store::MODE_APPLICATION.'/phpunit/eventinvalidationtest/'.$CFG->wwwroot.'phpunit'); - $timefile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/las/lastinvalidation-$hash.cache"; + $timefile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/las-cache/lastinvalidation-$hash.cache"; // Make sure the file is correct. $this->assertTrue(file_exists($timefile)); $timecont = serialize(cache::now() - 60); // Back 60sec in the past to force it to re-invalidate. @@ -858,7 +875,7 @@ class cache_phpunit_tests extends advanced_testcase { file_put_contents($timefile, $timecont); $this->assertTrue(file_exists($timefile)); - $datafile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/tes/testkey1-$hash.cache"; + $datafile = $CFG->dataroot."/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/tes-cache/testkey1-$hash.cache"; $datacont = serialize("test data 1"); make_writable_directory(dirname($datafile)); file_put_contents($datafile, $datacont);