Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
Merge branch '45699-29' of git://github.com/samhemelryk/moodle
Browse files Browse the repository at this point in the history
Conflicts:
	cache/upgrade.txt
  • Loading branch information
stronk7 committed Dec 23, 2014
2 parents 57bd925 + 427c41e commit 093c161
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 72 deletions.
22 changes: 11 additions & 11 deletions cache/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,36 +236,36 @@ The second method is a lot more intensive for the system. There are defined inva
When you invalidate by event the cache API finds all of the definitions that subscribe to the event, it then loads the stores for each of those definitions and purges the keys from each store.
This is obviously a recursive, and therefore, intense process.

### Unit tests
Both the cache API and the cache stores have unit tests.
Please be aware that several of the cache stores require configuration in order to be able operate in the unit tests.
Tests for stores requiring configuration that havn't been configured will be skipped.
### Testing
Both the cache API and the cache stores have tests.
Please be aware that several of the cache stores require configuration in order to be able operate in the tests.
Tests for stores requiring configuration that haven't been configured will be skipped.
All configuration is done in your sites config.php through definitions.
The following snippet illustates how to configure the three core cache stores that require configuration.
The following snippet illustrates how to configure the three core cache stores that require configuration.

define('TEST_CACHESTORE_MEMCACHE_TESTSERVERS', '127.0.0.1:11211');
define('TEST_CACHESTORE_MEMCACHED_TESTSERVERS', '127.0.0.1:11211');
define('TEST_CACHESTORE_MONGODB_TESTSERVER', 'mongodb://localhost:27017');

As of Moodle 2.8 it is also possible to set the default cache stores used when running unit tests.
As of Moodle 2.8 it is also possible to set the default cache stores used when running tests.
You can do this by adding the following define to your config.php file:

// xxx is one of Memcache, Memecached, mongodb or other cachestore with a test define.
// xxx is one of Memcache, Memcached, mongodb or other cachestore with a test define.
define('TEST_CACHE_USING_APPLICATION_STORE', 'xxx');

This allows you to run tests against a defined test store. It uses the defined value to identify a store to test against with a matching TEST_CACHESTORE define.
Alternatively you can also run unit tests against an actual cache config.
Alternatively you can also run tests against an actual cache config.
To do this you must add the following to your config.php file:

define('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH', true');
$CFG->altcacheconfigpath = '/a/temp/directory/yoursite.php'

This tells Moodle to use the config at $CFG->altcacheconfigpath when running unit tests.
This tells Moodle to use the config at $CFG->altcacheconfigpath when running tests.
There are a couple of considerations to using this method:
* By setting $CFG->altcacheconfigpath your site will store the cache config in the specified path, not just the unit test cache config but your site config as well.
* By setting $CFG->altcacheconfigpath your site will store the cache config in the specified path, not just the test cache config but your site config as well.
* If you have configured your cache before setting $CFG->altcacheconfigpath you will need to copy it from moodledata/muc/config.php to the destination you specified.
* This allows you to share a cache config between sites.
* It also allows you to use unit tests to test your sites cache config.
* It also allows you to use tests to test your sites cache config.

Please be aware that if you are using Memcache or Memcached it is recommended to use dedicated Memcached servers.
When caches get purged the memcached servers you have configured get purged, any data stored within them whether it belongs to Moodle or not will be removed.
Expand Down
13 changes: 7 additions & 6 deletions cache/classes/factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public static function instance($forcereload = false) {
// situation. It will use disabled alternatives where available.
require_once($CFG->dirroot.'/cache/disabledlib.php');
self::$instance = new cache_factory_disabled();
} else if (defined('PHPUNIT_TEST') && PHPUNIT_TEST) {
// We're using the regular factory.
} else if ((defined('PHPUNIT_TEST') && PHPUNIT_TEST) || behat_is_test_site()) {
// We're using the test factory.
require_once($CFG->dirroot.'/cache/tests/fixtures/lib.php');
self::$instance = new cache_phpunit_factory();
if (defined('CACHE_DISABLE_STORES') && CACHE_DISABLE_STORES !== false) {
Expand Down Expand Up @@ -335,24 +335,25 @@ public function create_config_instance($writer = false) {

// The class to use.
$class = 'cache_config';
$unittest = defined('PHPUNIT_TEST') && PHPUNIT_TEST;
// Are we running tests of some form?
$testing = (defined('PHPUNIT_TEST') && PHPUNIT_TEST) || behat_is_test_site();

// Check if this is a PHPUnit test and redirect to the phpunit config classes if it is.
if ($unittest) {
if ($testing) {
require_once($CFG->dirroot.'/cache/locallib.php');
require_once($CFG->dirroot.'/cache/tests/fixtures/lib.php');
// We have just a single class for PHP unit tests. We don't care enough about its
// performance to do otherwise and having a single method allows us to inject things into it
// while testing.
$class = 'cache_config_phpunittest';
$class = 'cache_config_testing';
}

// Check if we need to create a config file with defaults.
$needtocreate = !$class::config_file_exists();

if ($writer || $needtocreate) {
require_once($CFG->dirroot.'/cache/locallib.php');
if (!$unittest) {
if (!$testing) {
$class .= '_writer';
}
}
Expand Down
4 changes: 2 additions & 2 deletions cache/stores/session/tests/session_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function get_class_name() {
* Test the maxsize option.
*/
public function test_maxsize() {
$config = cache_config_phpunittest::instance();
$config = cache_config_testing::instance();
$config->phpunit_add_definition('phpunit/one', array(
'mode' => cache_store::MODE_SESSION,
'component' => 'phpunit',
Expand Down Expand Up @@ -171,7 +171,7 @@ public function test_maxsize() {
}

public function test_ttl() {
$config = cache_config_phpunittest::instance();
$config = cache_config_testing::instance();
$config->phpunit_add_definition('phpunit/three', array(
'mode' => cache_store::MODE_SESSION,
'component' => 'phpunit',
Expand Down
2 changes: 1 addition & 1 deletion cache/stores/static/tests/static_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function get_class_name() {
*/
public function test_maxsize() {
$defid = 'phpunit/testmaxsize';
$config = cache_config_phpunittest::instance();
$config = cache_config_testing::instance();
$config->phpunit_add_definition($defid, array(
'mode' => cache_store::MODE_REQUEST,
'component' => 'phpunit',
Expand Down
4 changes: 2 additions & 2 deletions cache/tests/administration_helper_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class core_cache_administration_helper_testcase extends advanced_testcase {
public function setUp() {
parent::setUp();
cache_factory::reset();
cache_config_phpunittest::create_default_configuration();
cache_config_testing::create_default_configuration();
}

/**
Expand Down Expand Up @@ -193,7 +193,7 @@ public function test_hash_key() {
set_debugging(DEBUG_ALL);

// First with simplekeys
$instance = cache_config_phpunittest::instance(true);
$instance = cache_config_testing::instance(true);
$instance->phpunit_add_definition('phpunit/hashtest', array(
'mode' => cache_store::MODE_APPLICATION,
'component' => 'phpunit',
Expand Down
Loading

0 comments on commit 093c161

Please sign in to comment.