Example #1
0
    /**
     * Purge dataroot directory
     * @static
     * @return void
     */
    public static function reset_dataroot() {
        global $CFG;

        $childclassname = self::get_framework() . '_util';

        $handle = opendir($CFG->dataroot);
        while (false !== ($item = readdir($handle))) {
            if (in_array($item, $childclassname::$datarootskiponreset)) {
                continue;
            }
            if (is_dir("$CFG->dataroot/$item")) {
                remove_dir("$CFG->dataroot/$item", false);
            } else {
                unlink("$CFG->dataroot/$item");
            }
        }
        closedir($handle);
        make_temp_directory('');
        make_cache_directory('');
        make_cache_directory('htmlpurifier');
        // Reset the cache API so that it recreates it's required directories as well.
        cache_factory::reset();
        // Purge all data from the caches. This is required for consistency.
        // Any file caches that happened to be within the data root will have already been clearer (because we just deleted cache)
        // and now we will purge any other caches as well.
        cache_helper::purge_all();
    }
Example #2
0
 /**
  * Update the site identifier stored by the cache API.
  *
  * @param string $siteidentifier
  */
 public static function update_site_identifier($siteidentifier)
 {
     global $CFG;
     // Include locallib.
     require_once $CFG->dirroot . '/cache/locallib.php';
     $factory = cache_factory::instance();
     $factory->updating_started();
     $config = $factory->create_config_instance(true);
     $config->update_site_identifier($siteidentifier);
     $factory->updating_finished();
     cache_factory::reset();
 }
Example #3
0
 /**
  * Set things back to the default before each test.
  */
 public function setUp()
 {
     parent::setUp();
     cache_factory::reset();
     cache_config_phpunittest::create_default_configuration();
 }
Example #4
0
 /**
  * Purge dataroot directory
  * @static
  * @return void
  */
 public static function reset_dataroot()
 {
     global $CFG;
     $childclassname = self::get_framework() . '_util';
     // Do not delete automatically installed files.
     self::skip_original_data_files($childclassname);
     // Clear file status cache, before checking file_exists.
     clearstatcache();
     // Clean up the dataroot folder.
     $handle = opendir(self::get_dataroot());
     while (false !== ($item = readdir($handle))) {
         if (in_array($item, $childclassname::$datarootskiponreset)) {
             continue;
         }
         if (is_dir(self::get_dataroot() . "/{$item}")) {
             remove_dir(self::get_dataroot() . "/{$item}", false);
         } else {
             unlink(self::get_dataroot() . "/{$item}");
         }
     }
     closedir($handle);
     // Clean up the dataroot/filedir folder.
     if (file_exists(self::get_dataroot() . '/filedir')) {
         $handle = opendir(self::get_dataroot() . '/filedir');
         while (false !== ($item = readdir($handle))) {
             if (in_array('filedir/' . $item, $childclassname::$datarootskiponreset)) {
                 continue;
             }
             if (is_dir(self::get_dataroot() . "/filedir/{$item}")) {
                 remove_dir(self::get_dataroot() . "/filedir/{$item}", false);
             } else {
                 unlink(self::get_dataroot() . "/filedir/{$item}");
             }
         }
         closedir($handle);
     }
     make_temp_directory('');
     make_cache_directory('');
     make_localcache_directory('');
     // Reset the cache API so that it recreates it's required directories as well.
     cache_factory::reset();
     // Purge all data from the caches. This is required for consistency.
     // Any file caches that happened to be within the data root will have already been clearer (because we just deleted cache)
     // and now we will purge any other caches as well.
     cache_helper::purge_all();
 }
 /**
  * Final task is to reset the cache system
  */
 public static function tearDownAfterClass()
 {
     parent::tearDownAfterClass();
     cache_factory::reset();
 }
Example #6
0
 /**
  * Finds all definitions and updates them within the cache config file.
  *
  * @param bool $coreonly If set to true only core definitions will be updated.
  */
 public static function update_definitions($coreonly = false)
 {
     global $CFG;
     // Include locallib
     require_once $CFG->dirroot . '/cache/locallib.php';
     // First update definitions
     cache_config_writer::update_definitions($coreonly);
     // Second reset anything we have already initialised to ensure we're all up to date.
     cache_factory::reset();
 }
Example #7
0
 /**
  * Test disabling the cache.
  */
 public function test_disable()
 {
     global $CFG;
     if (defined('TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH') && TEST_CACHE_USING_ALT_CACHE_CONFIG_PATH || !empty($CFG->altcacheconfigpath)) {
         // We can't run this test as it requires us to delete the cache configuration script which we just
         // cant do with a custom path in play.
         $this->markTestSkipped('Skipped testing cache disable functionality as alt cache path is being used.');
     }
     $configfile = $CFG->dataroot . '/muc/config.php';
     // That's right, we're deleting the config file.
     $this->assertTrue(@unlink($configfile));
     // Disable the cache
     cache_phpunit_factory::phpunit_disable();
     // Check we get the expected disabled factory.
     $factory = cache_factory::instance();
     $this->assertInstanceOf('cache_factory_disabled', $factory);
     // Check we get the expected disabled config.
     $config = $factory->create_config_instance();
     $this->assertInstanceOf('cache_config_disabled', $config);
     // Check we get the expected disabled caches.
     $cache = cache::make('phpunit', 'disable');
     $this->assertInstanceOf('cache_disabled', $cache);
     // Test an application cache.
     $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'disable');
     $this->assertInstanceOf('cache_disabled', $cache);
     $this->assertFalse(file_exists($configfile));
     $this->assertFalse($cache->get('test'));
     $this->assertFalse($cache->set('test', 'test'));
     $this->assertFalse($cache->delete('test'));
     $this->assertTrue($cache->purge());
     // Test a session cache.
     $cache = cache::make_from_params(cache_store::MODE_SESSION, 'phpunit', 'disable');
     $this->assertInstanceOf('cache_disabled', $cache);
     $this->assertFalse(file_exists($configfile));
     $this->assertFalse($cache->get('test'));
     $this->assertFalse($cache->set('test', 'test'));
     $this->assertFalse($cache->delete('test'));
     $this->assertTrue($cache->purge());
     // Finally test a request cache.
     $cache = cache::make_from_params(cache_store::MODE_REQUEST, 'phpunit', 'disable');
     $this->assertInstanceOf('cache_disabled', $cache);
     $this->assertFalse(file_exists($configfile));
     $this->assertFalse($cache->get('test'));
     $this->assertFalse($cache->set('test', 'test'));
     $this->assertFalse($cache->delete('test'));
     $this->assertTrue($cache->purge());
     cache_factory::reset();
     $factory = cache_factory::instance(true);
     $config = $factory->create_config_instance();
     $this->assertEquals('cache_config_testing', get_class($config));
 }
Example #8
0
 /**
  * Test disabling the cache.
  */
 public function test_disable()
 {
     global $CFG;
     $configfile = $CFG->dataroot . '/muc/config.php';
     // That's right, we're deleting the config file.
     $this->assertTrue(@unlink($configfile));
     // Disable the cache
     cache_phpunit_factory::phpunit_disable();
     // Check we get the expected disabled factory.
     $factory = cache_factory::instance();
     $this->assertInstanceOf('cache_factory_disabled', $factory);
     // Check we get the expected disabled config.
     $config = $factory->create_config_instance();
     $this->assertInstanceOf('cache_config_disabled', $config);
     // Check we get the expected disabled caches.
     $cache = cache::make('phpunit', 'disable');
     $this->assertInstanceOf('cache_disabled', $cache);
     $cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'phpunit', 'disable');
     $this->assertInstanceOf('cache_disabled', $cache);
     $this->assertFalse(file_exists($configfile));
     $this->assertFalse($cache->get('test'));
     $this->assertFalse($cache->set('test', 'test'));
     $this->assertFalse($cache->delete('test'));
     $this->assertTrue($cache->purge());
     cache_factory::reset();
     $factory = cache_factory::instance(true);
     $config = $factory->create_config_instance();
     $this->assertEquals('cache_config_phpunittest', get_class($config));
 }
Example #9
0
 /**
  * Purge dataroot directory
  * @static
  * @return void
  */
 public static function reset_dataroot()
 {
     global $CFG;
     $handle = opendir($CFG->dataroot);
     $skip = array('.', '..', 'phpunittestdir.txt', 'phpunit', '.htaccess');
     while (false !== ($item = readdir($handle))) {
         if (in_array($item, $skip)) {
             continue;
         }
         if (is_dir("{$CFG->dataroot}/{$item}")) {
             remove_dir("{$CFG->dataroot}/{$item}", false);
         } else {
             unlink("{$CFG->dataroot}/{$item}");
         }
     }
     closedir($handle);
     make_temp_directory('');
     make_cache_directory('');
     make_cache_directory('htmlpurifier');
     // Reset the cache API so that it recreates it's required directories as well.
     cache_factory::reset();
     // Purge all data from the caches. This is required for consistency.
     // Any file caches that happened to be within the data root will have already been clearer (because we just deleted cache)
     // and now we will purge any other caches as well.
     cache_helper::purge_all();
 }
Example #10
0
 /**
  * Tests application cache event invalidation over a distributed setup.
  */
 public function test_distributed_application_event_invalidation()
 {
     global $CFG;
     // This is going to be an intense wee test.
     // We need to add data the to cache, invalidate it by event, manually force it back without MUC knowing to simulate a
     // disconnected/distributed setup (think load balanced server using local cache), instantiate the cache again and finally
     // check that it is not picked up.
     $instance = cache_config_phpunittest::instance();
     $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'eventinvalidationtest', 'invalidationevents' => array('crazyevent')));
     $cache = cache::make('phpunit', 'eventinvalidationtest');
     $this->assertTrue($cache->set('testkey1', 'test data 1'));
     $this->assertEquals('test data 1', $cache->get('testkey1'));
     cache_helper::invalidate_by_event('crazyevent', array('testkey1'));
     $this->assertFalse($cache->get('testkey1'));
     // 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.
     $timefile = $CFG->dataroot . '/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/a65/a65b1dc524cf6e03c1795197c84d5231eb229b86.cache';
     $timecont = serialize(cache::now() - 60);
     // Back 60sec in the past to force it to re-invalidate.
     make_writable_directory(dirname($timefile));
     file_put_contents($timefile, $timecont);
     $this->assertTrue(file_exists($timefile));
     $datafile = $CFG->dataroot . '/cache/cachestore_file/default_application/phpunit_eventinvalidationtest/626/626e9c7a45febd98f064c2b383de8d9d4ebbde7b.cache';
     $datacont = serialize("test data 1");
     make_writable_directory(dirname($datafile));
     file_put_contents($datafile, $datacont);
     $this->assertTrue(file_exists($datafile));
     // Test 1: Rebuild without the event and test its there.
     cache_factory::reset();
     $instance = cache_config_phpunittest::instance();
     $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'eventinvalidationtest'));
     $cache = cache::make('phpunit', 'eventinvalidationtest');
     $this->assertEquals('test data 1', $cache->get('testkey1'));
     // Test 2: Rebuild and test the invalidation of the event via the invalidation cache.
     cache_factory::reset();
     $instance = cache_config_phpunittest::instance();
     $instance->phpunit_add_definition('phpunit/eventinvalidationtest', array('mode' => cache_store::MODE_APPLICATION, 'component' => 'phpunit', 'area' => 'eventinvalidationtest', 'invalidationevents' => array('crazyevent')));
     $cache = cache::make('phpunit', 'eventinvalidationtest');
     $this->assertFalse($cache->get('testkey1'));
 }
Example #11
0
/**
 * Upgrade moodle core
 * @param float $version target version
 * @param bool $verbose
 * @return void, may throw exception
 */
function upgrade_core($version, $verbose) {
    global $CFG;

    raise_memory_limit(MEMORY_EXTRA);

    require_once($CFG->libdir.'/db/upgrade.php');    // Defines upgrades

    try {
        // Reset caches before any output
        purge_all_caches();
        // Disable the use of cache stores here. We will reset the factory after we've performed the installation.
        // This ensures that we don't permanently cache anything during installation.
        cache_factory::disable_stores();

        // Upgrade current language pack if we can
        upgrade_language_pack();

        print_upgrade_part_start('moodle', false, $verbose);

        // one time special local migration pre 2.0 upgrade script
        if ($CFG->version < 2007101600) {
            $pre20upgradefile = "$CFG->dirroot/local/upgrade_pre20.php";
            if (file_exists($pre20upgradefile)) {
                set_time_limit(0);
                require($pre20upgradefile);
                // reset upgrade timeout to default
                upgrade_set_timeout();
            }
        }

        $result = xmldb_main_upgrade($CFG->version);
        if ($version > $CFG->version) {
            // store version if not already there
            upgrade_main_savepoint($result, $version, false);
        }

        // perform all other component upgrade routines
        update_capabilities('moodle');
        log_update_descriptions('moodle');
        external_update_descriptions('moodle');
        events_update_definition('moodle');
        message_update_providers('moodle');
        // Update core definitions.
        cache_helper::update_definitions(true);

        // Reset the cache, this returns it to a normal operation state.
        cache_factory::reset();
        // Purge caches again, just to be sure we arn't holding onto old stuff now.
        purge_all_caches();

        // Clean up contexts - more and more stuff depends on existence of paths and contexts
        context_helper::cleanup_instances();
        context_helper::create_instances(null, false);
        context_helper::build_all_paths(false);
        $syscontext = context_system::instance();
        $syscontext->mark_dirty();

        print_upgrade_part_end('moodle', false, $verbose);
    } catch (Exception $ex) {
        upgrade_handle_exception($ex);
    }
}
Example #12
0
 /**
  * Test adding/editing/deleting store instances.
  */
 public function test_add_edit_delete_plugin_instance()
 {
     $config = cache_config_writer::instance();
     $this->assertArrayNotHasKey('addplugintest', $config->get_all_stores());
     $this->assertArrayNotHasKey('addplugintestwlock', $config->get_all_stores());
     // Add a default file instance.
     $config->add_store_instance('addplugintest', 'file');
     cache_factory::reset();
     $config = cache_config_writer::instance();
     $this->assertArrayHasKey('addplugintest', $config->get_all_stores());
     // Add a store with a lock described.
     $config->add_store_instance('addplugintestwlock', 'file', array('lock' => 'default_file_lock'));
     $this->assertArrayHasKey('addplugintestwlock', $config->get_all_stores());
     $config->delete_store_instance('addplugintest');
     $this->assertArrayNotHasKey('addplugintest', $config->get_all_stores());
     $this->assertArrayHasKey('addplugintestwlock', $config->get_all_stores());
     $config->delete_store_instance('addplugintestwlock');
     $this->assertArrayNotHasKey('addplugintest', $config->get_all_stores());
     $this->assertArrayNotHasKey('addplugintestwlock', $config->get_all_stores());
     // Add a default file instance.
     $config->add_store_instance('storeconfigtest', 'file', array('test' => 'a', 'one' => 'two'));
     $stores = $config->get_all_stores();
     $this->assertArrayHasKey('storeconfigtest', $stores);
     $this->assertArrayHasKey('configuration', $stores['storeconfigtest']);
     $this->assertArrayHasKey('test', $stores['storeconfigtest']['configuration']);
     $this->assertArrayHasKey('one', $stores['storeconfigtest']['configuration']);
     $this->assertEquals('a', $stores['storeconfigtest']['configuration']['test']);
     $this->assertEquals('two', $stores['storeconfigtest']['configuration']['one']);
     $config->edit_store_instance('storeconfigtest', 'file', array('test' => 'b', 'one' => 'three'));
     $stores = $config->get_all_stores();
     $this->assertArrayHasKey('storeconfigtest', $stores);
     $this->assertArrayHasKey('configuration', $stores['storeconfigtest']);
     $this->assertArrayHasKey('test', $stores['storeconfigtest']['configuration']);
     $this->assertArrayHasKey('one', $stores['storeconfigtest']['configuration']);
     $this->assertEquals('b', $stores['storeconfigtest']['configuration']['test']);
     $this->assertEquals('three', $stores['storeconfigtest']['configuration']['one']);
     $config->delete_store_instance('storeconfigtest');
     try {
         $config->delete_store_instance('default_application');
         $this->fail('Default store deleted. This should not be possible!');
     } catch (Exception $e) {
         $this->assertInstanceOf('cache_exception', $e);
     }
     try {
         $config->delete_store_instance('some_crazy_store');
         $this->fail('You should not be able to delete a store that does not exist.');
     } catch (Exception $e) {
         $this->assertInstanceOf('cache_exception', $e);
     }
     try {
         // Try with a plugin that does not exist.
         $config->add_store_instance('storeconfigtest', 'shallowfail', array('test' => 'a', 'one' => 'two'));
         $this->fail('You should not be able to add an instance of a store that does not exist.');
     } catch (Exception $e) {
         $this->assertInstanceOf('cache_exception', $e);
     }
 }
Example #13
0
/**
 * Upgrade/install other parts of moodle
 * @param bool $verbose
 * @return void, may throw exception
 */
function upgrade_noncore($verbose)
{
    global $CFG;
    raise_memory_limit(MEMORY_EXTRA);
    // upgrade all plugins types
    try {
        // Disable the use of cache stores here. We will reset the factory after we've performed the installation.
        // This ensures that we don't permanently cache anything during installation.
        cache_factory::disable_stores();
        $plugintypes = get_plugin_types();
        foreach ($plugintypes as $type => $location) {
            upgrade_plugins($type, 'print_upgrade_part_start', 'print_upgrade_part_end', $verbose);
        }
        // Update cache definitions. Involves scanning each plugin for any changes.
        cache_helper::update_definitions();
        // Reset the cache system to a normal state.
        cache_factory::reset();
    } catch (Exception $ex) {
        upgrade_handle_exception($ex);
    }
}