2020-12-23 11:35:49 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-04-27 09:52:17 +02:00
|
|
|
using Microsoft.Extensions.Options;
|
2015-09-10 14:10:45 +02:00
|
|
|
using NUnit.Framework;
|
2021-04-27 09:52:17 +02:00
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Hosting;
|
|
|
|
|
using Umbraco.Cms.Core.IO;
|
|
|
|
|
using Umbraco.Cms.Core.Models;
|
2021-02-15 11:41:12 +01:00
|
|
|
using Umbraco.Cms.Core.Scoping;
|
2021-02-12 13:36:50 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement;
|
2021-04-27 09:52:17 +02:00
|
|
|
using Umbraco.Cms.Tests.Common.TestHelpers;
|
2021-02-10 14:45:44 +01:00
|
|
|
using Umbraco.Cms.Tests.Common.Testing;
|
2021-02-11 08:30:27 +01:00
|
|
|
using Umbraco.Cms.Tests.Integration.Testing;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Constants = Umbraco.Cms.Core.Constants;
|
2015-09-10 14:10:45 +02:00
|
|
|
|
2022-04-26 10:22:37 +01:00
|
|
|
using IScopeProvider = Umbraco.Cms.Infrastructure.Scoping.IScopeProvider;
|
|
|
|
|
using IScope = Umbraco.Cms.Infrastructure.Scoping.IScope;
|
|
|
|
|
|
2021-02-11 08:30:27 +01:00
|
|
|
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositories
|
2015-09-10 14:10:45 +02:00
|
|
|
{
|
|
|
|
|
[TestFixture]
|
2020-10-22 11:18:26 +02:00
|
|
|
[UmbracoTest(Database = UmbracoTestOptions.Database.None)]
|
|
|
|
|
public class PartialViewRepositoryTests : UmbracoIntegrationTest
|
2015-09-10 14:10:45 +02:00
|
|
|
{
|
2020-10-22 11:18:26 +02:00
|
|
|
private IHostingEnvironment HostingEnvironment => GetRequiredService<IHostingEnvironment>();
|
2020-12-23 11:35:49 +01:00
|
|
|
|
2015-09-10 14:10:45 +02:00
|
|
|
private IFileSystem _fileSystem;
|
|
|
|
|
|
2020-10-22 11:18:26 +02:00
|
|
|
[SetUp]
|
2020-12-23 11:35:49 +01:00
|
|
|
public void SetUp() =>
|
2020-11-27 09:00:50 +01:00
|
|
|
_fileSystem = new PhysicalFileSystem(IOHelper, HostingEnvironment, LoggerFactory.CreateLogger<PhysicalFileSystem>(), HostingEnvironment.MapPathContentRoot(Constants.SystemDirectories.PartialViews), HostingEnvironment.ToAbsolute(Constants.SystemDirectories.PartialViews));
|
2015-09-10 14:10:45 +02:00
|
|
|
|
2020-10-22 11:18:26 +02:00
|
|
|
[TearDown]
|
|
|
|
|
public void TearDownFiles()
|
2016-11-05 19:23:55 +01:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
// Delete all files
|
|
|
|
|
Purge((PhysicalFileSystem)_fileSystem, string.Empty);
|
2020-10-22 11:18:26 +02:00
|
|
|
_fileSystem = null;
|
2016-11-05 19:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
2015-09-10 14:10:45 +02:00
|
|
|
[Test]
|
|
|
|
|
public void PathTests()
|
|
|
|
|
{
|
|
|
|
|
// unless noted otherwise, no changes / 7.2.8
|
2021-04-27 09:52:17 +02:00
|
|
|
FileSystems fileSystems = FileSystemsCreator.CreateTestFileSystems(LoggerFactory, IOHelper,
|
|
|
|
|
GetRequiredService<IOptions<GlobalSettings>>(), HostingEnvironment,
|
|
|
|
|
null, _fileSystem, null, null, null);
|
2018-07-20 09:49:05 +02:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
IScopeProvider provider = ScopeProvider;
|
|
|
|
|
using (IScope scope = provider.CreateScope())
|
2015-09-10 14:10:45 +02:00
|
|
|
{
|
2021-04-27 11:42:06 +02:00
|
|
|
var repository = new PartialViewRepository(fileSystems);
|
2016-05-18 09:38:58 +02:00
|
|
|
|
2022-01-06 13:35:24 +01:00
|
|
|
IPartialView partialView = new PartialView(PartialViewType.PartialView, "test-path-1.cshtml") { Content = "// partialView" };
|
2017-12-07 16:45:25 +01:00
|
|
|
repository.Save(partialView);
|
2016-05-18 09:38:58 +02:00
|
|
|
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);
|
2015-09-10 14:10:45 +02:00
|
|
|
|
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);
|
2016-05-18 09:38:58 +02:00
|
|
|
Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-2.cshtml"));
|
2022-01-06 13:35:24 +01:00
|
|
|
Assert.AreEqual("path-2\\test-path-2.cshtml".Replace("\\", $"{Path.DirectorySeparatorChar}"), partialView.Path);
|
2016-05-18 09:38:58 +02:00
|
|
|
Assert.AreEqual("/Views/Partials/path-2/test-path-2.cshtml", partialView.VirtualPath);
|
2015-09-10 14:10:45 +02:00
|
|
|
|
2022-01-06 13:35:24 +01:00
|
|
|
partialView = repository.Get("path-2/test-path-2.cshtml");
|
2016-05-18 09:38:58 +02:00
|
|
|
Assert.IsNotNull(partialView);
|
2020-12-11 16:44:27 +00:00
|
|
|
Assert.AreEqual("path-2\\test-path-2.cshtml".Replace("\\", $"{Path.DirectorySeparatorChar}"), partialView.Path);
|
2016-05-18 09:38:58 +02:00
|
|
|
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);
|
2016-05-18 09:38:58 +02:00
|
|
|
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);
|
2016-05-18 09:38:58 +02:00
|
|
|
Assert.AreEqual("/Views/Partials/path-2/test-path-3.cshtml", partialView.VirtualPath);
|
|
|
|
|
|
2022-01-06 13:35:24 +01:00
|
|
|
partialView = repository.Get("path-2/test-path-3.cshtml");
|
2016-05-18 09:38:58 +02:00
|
|
|
Assert.IsNotNull(partialView);
|
2020-12-11 16:44:27 +00:00
|
|
|
Assert.AreEqual("path-2\\test-path-3.cshtml".Replace("\\", $"{Path.DirectorySeparatorChar}"), partialView.Path);
|
2016-05-18 09:38:58 +02:00
|
|
|
Assert.AreEqual("/Views/Partials/path-2/test-path-3.cshtml", partialView.VirtualPath);
|
|
|
|
|
|
2022-01-06 13:35:24 +01:00
|
|
|
partialView = repository.Get("path-2\\test-path-3.cshtml");
|
2016-05-18 09:38:58 +02:00
|
|
|
Assert.IsNotNull(partialView);
|
2020-12-11 16:44:27 +00:00
|
|
|
Assert.AreEqual("path-2\\test-path-3.cshtml".Replace("\\", $"{Path.DirectorySeparatorChar}"), partialView.Path);
|
2016-05-18 09:38:58 +02:00
|
|
|
Assert.AreEqual("/Views/Partials/path-2/test-path-3.cshtml", partialView.VirtualPath);
|
|
|
|
|
|
2022-01-06 13:35:24 +01:00
|
|
|
partialView = new PartialView(PartialViewType.PartialView, "..\\test-path-4.cshtml") { Content = "// partialView" };
|
|
|
|
|
Assert.Throws<UnauthorizedAccessException>(() =>
|
2020-12-23 11:35:49 +01:00
|
|
|
repository.Save(partialView));
|
2016-05-18 09:38:58 +02:00
|
|
|
|
2022-01-06 13:35:24 +01:00
|
|
|
partialView = new PartialView(PartialViewType.PartialView, "\\test-path-5.cshtml") { Content = "// partialView" };
|
|
|
|
|
repository.Save(partialView);
|
|
|
|
|
|
|
|
|
|
partialView = repository.Get("\\test-path-5.cshtml");
|
|
|
|
|
Assert.IsNotNull(partialView);
|
|
|
|
|
Assert.AreEqual("test-path-5.cshtml", partialView.Path);
|
|
|
|
|
Assert.AreEqual("/Views/Partials/test-path-5.cshtml", partialView.VirtualPath);
|
|
|
|
|
|
|
|
|
|
partialView = repository.Get("missing.cshtml");
|
2016-05-18 09:38:58 +02:00
|
|
|
Assert.IsNull(partialView);
|
|
|
|
|
|
2022-01-06 13:35:24 +01:00
|
|
|
Assert.Throws<UnauthorizedAccessException>(() => partialView = repository.Get("..\\test-path-4.cshtml"));
|
|
|
|
|
Assert.Throws<UnauthorizedAccessException>(() => partialView = repository.Get("../../packages.config"));
|
2016-05-18 09:38:58 +02:00
|
|
|
}
|
2015-09-10 14:10:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Purge(PhysicalFileSystem fs, string path)
|
|
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
IEnumerable<string> files = fs.GetFiles(path, "*.cshtml");
|
|
|
|
|
foreach (string file in files)
|
2015-09-10 14:10:45 +02:00
|
|
|
{
|
|
|
|
|
fs.DeleteFile(file);
|
|
|
|
|
}
|
2020-12-23 11:35:49 +01:00
|
|
|
|
|
|
|
|
IEnumerable<string> dirs = fs.GetDirectories(path);
|
|
|
|
|
foreach (string dir in dirs)
|
2015-09-10 14:10:45 +02:00
|
|
|
{
|
|
|
|
|
Purge(fs, dir);
|
|
|
|
|
fs.DeleteDirectory(dir);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|