Files
Umbraco-CMS/src/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/PartialViewRepositoryTests.cs

116 lines
5.6 KiB
C#
Raw Normal View History

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Extensions.Logging;
2018-07-20 09:49:05 +02:00
using Moq;
using NUnit.Framework;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.IO;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
2017-12-07 16:45:25 +01:00
using Umbraco.Core.Persistence.Repositories.Implement;
using Umbraco.Core.Scoping;
using Constants = Umbraco.Cms.Core.Constants;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositories
{
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.None)]
public class PartialViewRepositoryTests : UmbracoIntegrationTest
{
private IHostingEnvironment HostingEnvironment => GetRequiredService<IHostingEnvironment>();
private IFileSystem _fileSystem;
[SetUp]
public void SetUp() =>
_fileSystem = new PhysicalFileSystem(IOHelper, HostingEnvironment, LoggerFactory.CreateLogger<PhysicalFileSystem>(), HostingEnvironment.MapPathContentRoot(Constants.SystemDirectories.PartialViews), HostingEnvironment.ToAbsolute(Constants.SystemDirectories.PartialViews));
[TearDown]
public void TearDownFiles()
2016-11-05 19:23:55 +01:00
{
// Delete all files
Purge((PhysicalFileSystem)_fileSystem, string.Empty);
_fileSystem = null;
2016-11-05 19:23:55 +01:00
}
[Test]
public void PathTests()
{
// unless noted otherwise, no changes / 7.2.8
IFileSystems fileSystems = Mock.Of<IFileSystems>();
2018-07-20 09:49:05 +02:00
Mock.Get(fileSystems).Setup(x => x.PartialViewsFileSystem).Returns(_fileSystem);
IScopeProvider provider = ScopeProvider;
using (IScope scope = provider.CreateScope())
{
2019-11-13 21:00:54 +01:00
var repository = new PartialViewRepository(fileSystems, IOHelper);
2017-05-30 10:50:09 +02:00
var partialView = new PartialView(PartialViewType.PartialView, "test-path-1.cshtml") { Content = "// partialView" };
2017-12-07 16:45:25 +01:00
repository.Save(partialView);
Assert.IsTrue(_fileSystem.FileExists("test-path-1.cshtml"));
Assert.AreEqual("test-path-1.cshtml", partialView.Path);
Assert.AreEqual("/Views/Partials/test-path-1.cshtml", partialView.VirtualPath);
2017-05-30 10:50:09 +02:00
partialView = new PartialView(PartialViewType.PartialView, "path-2/test-path-2.cshtml") { Content = "// partialView" };
2017-12-07 16:45:25 +01:00
repository.Save(partialView);
Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-2.cshtml"));
2020-12-11 16:44:27 +00:00
Assert.AreEqual("path-2\\test-path-2.cshtml".Replace("\\", $"{Path.DirectorySeparatorChar}"), partialView.Path); // fixed in 7.3 - 7.2.8 does not update the path
Assert.AreEqual("/Views/Partials/path-2/test-path-2.cshtml", partialView.VirtualPath);
partialView = (PartialView)repository.Get("path-2/test-path-2.cshtml");
Assert.IsNotNull(partialView);
2020-12-11 16:44:27 +00:00
Assert.AreEqual("path-2\\test-path-2.cshtml".Replace("\\", $"{Path.DirectorySeparatorChar}"), partialView.Path);
Assert.AreEqual("/Views/Partials/path-2/test-path-2.cshtml", partialView.VirtualPath);
2017-05-30 10:50:09 +02:00
partialView = new PartialView(PartialViewType.PartialView, "path-2\\test-path-3.cshtml") { Content = "// partialView" };
2017-12-07 16:45:25 +01:00
repository.Save(partialView);
Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-3.cshtml"));
2020-12-11 16:44:27 +00:00
Assert.AreEqual("path-2\\test-path-3.cshtml".Replace("\\", $"{Path.DirectorySeparatorChar}"), partialView.Path);
Assert.AreEqual("/Views/Partials/path-2/test-path-3.cshtml", partialView.VirtualPath);
partialView = (PartialView)repository.Get("path-2/test-path-3.cshtml");
Assert.IsNotNull(partialView);
2020-12-11 16:44:27 +00:00
Assert.AreEqual("path-2\\test-path-3.cshtml".Replace("\\", $"{Path.DirectorySeparatorChar}"), partialView.Path);
Assert.AreEqual("/Views/Partials/path-2/test-path-3.cshtml", partialView.VirtualPath);
partialView = (PartialView)repository.Get("path-2\\test-path-3.cshtml");
Assert.IsNotNull(partialView);
2020-12-11 16:44:27 +00:00
Assert.AreEqual("path-2\\test-path-3.cshtml".Replace("\\", $"{Path.DirectorySeparatorChar}"), partialView.Path);
Assert.AreEqual("/Views/Partials/path-2/test-path-3.cshtml", partialView.VirtualPath);
2017-05-30 10:50:09 +02:00
partialView = new PartialView(PartialViewType.PartialView, "\\test-path-4.cshtml") { Content = "// partialView" };
Assert.Throws<UnauthorizedAccessException>(() => // fixed in 7.3 - 7.2.8 used to strip the \
repository.Save(partialView));
partialView = (PartialView)repository.Get("missing.cshtml");
Assert.IsNull(partialView);
// fixed in 7.3 - 7.2.8 used to...
Assert.Throws<UnauthorizedAccessException>(() => partialView = (PartialView)repository.Get("\\test-path-4.cshtml"));
Assert.Throws<UnauthorizedAccessException>(() => partialView = (PartialView)repository.Get("../../packages.config"));
}
}
private void Purge(PhysicalFileSystem fs, string path)
{
IEnumerable<string> files = fs.GetFiles(path, "*.cshtml");
foreach (string file in files)
{
fs.DeleteFile(file);
}
IEnumerable<string> dirs = fs.GetDirectories(path);
foreach (string dir in dirs)
{
Purge(fs, dir);
fs.DeleteDirectory(dir);
}
}
}
}