Esempio n. 1
0
        public void WebsiteLavaTemplateCacheService_WhitespaceTemplatesWithDifferentLengths_AreCachedIndependently()
        {
            var options = new LavaEngineConfigurationOptions();

            ILavaTemplateCacheService cacheService = new WebsiteLavaTemplateCacheService();

            options.CacheService = cacheService;

            TestHelper.ExecuteForActiveEngines((defaultEngineInstance) =>
            {
                if (defaultEngineInstance.GetType() == typeof(RockLiquidEngine))
                {
                    Debug.Write("Template caching cannot be tested by this methodology for the RockLiquid implementation.");
                    return;
                }

                // Remove all existing items from the cache.
                cacheService.ClearCache();

                var engine = LavaService.NewEngineInstance(defaultEngineInstance.GetType(), options);

                // Process a zero-length whitespace template - this should be cached separately.
                var input0 = string.Empty;
                var key0   = cacheService.GetCacheKeyForTemplate(input0);

                // Verify that the template does not initially exist in the cache.
                var exists = cacheService.ContainsKey(key0);

                Assert.IsFalse(exists, "String-0 Template found in cache unexpectedly.");

                // Render the template, which will automatically add it to the cache.
                var output0 = engine.RenderTemplate(input0);

                // Verify that the template now exists in the cache.
                exists = cacheService.ContainsKey(key0);

                Assert.IsTrue(exists, "String-0 Template not found in cache.");

                // Render a whitespace template of a different length - this should be cached separately from the first template.
                // If not, the caching mechanism would cause some whitespace to be rendered incorrectly.
                var input1 = new string( ' ', 1 );
                var key1   = engine.TemplateCacheService.GetCacheKeyForTemplate(input1);

                var output1 = engine.RenderTemplate(input1);

                // Verify that the 100-character whitespace template now exists in the cache.
                exists = cacheService.ContainsKey(key1);

                Assert.IsTrue(exists, "String-1 Template not found in cache.");

                // Verify that a whitespace template of some other length is not equated with the whitespace templates we have specifically added.
                var keyX = engine.TemplateCacheService.GetCacheKeyForTemplate(new string( ' ', 9 ));

                exists = cacheService.ContainsKey(keyX);

                Assert.IsFalse(exists, "String-9 Template found in cache unexpectedly.");
            });
        }
Esempio n. 2
0
        public void WebsiteLavaShortcodeProvider_ModifiedShortcode_ReturnsCorrectVersionAfterModification()
        {
            var options = new LavaEngineConfigurationOptions();

            ILavaTemplateCacheService cacheService = new WebsiteLavaTemplateCacheService();

            options.CacheService = cacheService;

            TestHelper.ExecuteForActiveEngines((defaultEngineInstance) =>
            {
                if (defaultEngineInstance.GetType() == typeof(DotLiquidEngine) ||
                    defaultEngineInstance.GetType() == typeof(RockLiquidEngine))
                {
                    Debug.Write("Shortcode caching is not currently implemented for RockLiquid/DotLiquid.");
                    return;
                }

                var engine = LavaService.NewEngineInstance(defaultEngineInstance.GetType(), options);

                var shortcodeProvider = new TestLavaDynamicShortcodeProvider();

                var rockContext          = new RockContext();
                var lavaShortCodeService = new LavaShortcodeService(rockContext);

                // Create a new Shortcode.
                var shortcodeGuid1 = TestGuids.Shortcodes.ShortcodeTest1.AsGuid();

                var lavaShortcode = lavaShortCodeService.Queryable().FirstOrDefault(x => x.Guid == shortcodeGuid1);

                if (lavaShortcode == null)
                {
                    lavaShortcode = new LavaShortcode();

                    lavaShortCodeService.Add(lavaShortcode);
                }

                lavaShortcode.Guid        = shortcodeGuid1;
                lavaShortcode.TagName     = "TestShortcode1";
                lavaShortcode.Name        = "Test Shortcode 1";
                lavaShortcode.IsActive    = true;
                lavaShortcode.Description = "Test shortcode";
                lavaShortcode.TagType     = TagType.Inline;

                lavaShortcode.Markup = "Hello!";

                rockContext.SaveChanges();

                shortcodeProvider.RegisterShortcode(engine, lavaShortcode);

                // Resolve a template using the new shortcode and verify the result.
                engine.ClearTemplateCache();

                shortcodeProvider.ClearCache();

                LavaService.SetCurrentEngine(engine);

                TestHelper.AssertTemplateOutput(engine, "Hello!", "{[ TestShortcode1 ]}");

                lavaShortcode.Markup = "Goodbye!";

                rockContext.SaveChanges();

                shortcodeProvider.ClearCache();

                engine.ClearTemplateCache();

                TestHelper.AssertTemplateOutput(engine, "Goodbye!", "{[ TestShortcode1 ]}");
            });
        }