MDL-36466 cache: implemented functionality to disable the bulk of the cache API
[moodle.git] / cache / tests / cache_test.php
index 51b1126..af7fa36 100644 (file)
@@ -50,6 +50,14 @@ class cache_phpunit_tests extends advanced_testcase {
         cache_config_phpunittest::create_default_configuration();
     }
 
+    /**
+     * Final task is to reset the cache system
+     */
+    public static function tearDownAfterClass() {
+        parent::tearDownAfterClass();
+        cache_factory::reset();
+    }
+
     /**
      * Tests cache configuration
      */
@@ -666,7 +674,7 @@ class cache_phpunit_tests extends advanced_testcase {
     }
 
     /**
-     * Test disabling the cache.
+     * Test disabling the cache stores.
      */
     public function test_disable_stores() {
         $instance = cache_config_phpunittest::instance();
@@ -693,4 +701,47 @@ class cache_phpunit_tests extends advanced_testcase {
         $this->assertTrue($cache->set('test', 'test'));
         $this->assertEquals('test', $cache->get('test'));
     }
+
+    /**
+     * 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_factory::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));
+    }
 }
\ No newline at end of file