From 0b7cbdb0e62f311226716fb7c9d445a6f226fcf0 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Tue, 1 Jul 2014 15:40:28 +1200 Subject: [PATCH] MDL-46208 cachestore_mongo: fixed invalid collection name use --- cache/stores/mongodb/lib.php | 2 +- cache/stores/mongodb/tests/mongodb_test.php | 22 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/cache/stores/mongodb/lib.php b/cache/stores/mongodb/lib.php index f30323d37bd..5bd2e84ac16 100644 --- a/cache/stores/mongodb/lib.php +++ b/cache/stores/mongodb/lib.php @@ -204,7 +204,7 @@ class cachestore_mongodb extends cache_store implements cache_is_configurable { throw new coding_exception('This mongodb instance has already been initialised.'); } $this->database = $this->connection->selectDB($this->databasename); - $this->definitionhash = $definition->generate_definition_hash(); + $this->definitionhash = 'm'.$definition->generate_definition_hash(); $this->collection = $this->database->selectCollection($this->definitionhash); $options = array('name' => 'idx_key'); diff --git a/cache/stores/mongodb/tests/mongodb_test.php b/cache/stores/mongodb/tests/mongodb_test.php index b1a3390907e..51745944452 100644 --- a/cache/stores/mongodb/tests/mongodb_test.php +++ b/cache/stores/mongodb/tests/mongodb_test.php @@ -59,4 +59,26 @@ class cachestore_mongodb_test extends cachestore_tests { protected function get_class_name() { return 'cachestore_mongodb'; } + + /** + * A small additional test to make sure definitions that hash a hash starting with a number work OK + */ + public function test_collection_name() { + // This generates a definition that has a hash starting with a number. MDL-46208. + $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_mongodb', 'abc'); + $instance = cachestore_mongodb::initialise_test_instance($definition); + + if (!$instance) { + $this->markTestSkipped(); + } + + $this->assertTrue($instance->set(1, 'alpha')); + $this->assertTrue($instance->set(2, 'beta')); + $this->assertEquals('alpha', $instance->get(1)); + $this->assertEquals('beta', $instance->get(2)); + $this->assertEquals(array( + 1 => 'alpha', + 2 => 'beta' + ), $instance->get_many(array(1, 2))); + } } \ No newline at end of file