Files
Umbraco-CMS/src/Umbraco.Tests/Persistence/Repositories/ScriptRepositoryTest.cs

362 lines
14 KiB
C#
Raw Normal View History

using System.IO;
using System.Linq;
using System.Text;
using Moq;
using NUnit.Framework;
using Umbraco.Core.Configuration.UmbracoSettings;
2017-05-30 15:46:25 +02:00
using Umbraco.Core.Composing;
using Umbraco.Core.IO;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence.Repositories;
2017-12-07 16:45:25 +01:00
using Umbraco.Core.Persistence.Repositories.Implement;
2016-11-05 19:23:55 +01:00
using Umbraco.Core.PropertyEditors;
2017-12-15 11:19:03 +01:00
using Umbraco.Core.Scoping;
2017-06-23 18:54:42 +02:00
using Umbraco.Tests.TestHelpers;
using Umbraco.Tests.Testing;
namespace Umbraco.Tests.Persistence.Repositories
{
[TestFixture]
2017-06-23 18:54:42 +02:00
[UmbracoTest(WithApplication = true, Database = UmbracoTestOptions.Database.NewEmptyPerFixture)]
public class ScriptRepositoryTest : TestWithDatabaseBase
{
private IFileSystem _fileSystem;
2016-10-13 21:08:07 +02:00
public override void SetUp()
{
2016-10-13 21:08:07 +02:00
base.SetUp();
_fileSystem = new PhysicalFileSystem(SystemDirectories.Scripts);
using (var stream = CreateStream("Umbraco.Sys.registerNamespace(\"Umbraco.Utils\");"))
{
2017-07-20 11:21:28 +02:00
_fileSystem.AddFile("test-script.js", stream);
}
}
2016-11-05 19:23:55 +01:00
protected override void Compose()
{
base.Compose();
2018-02-16 12:00:45 +01:00
Container.RegisterSingleton(f => new DataEditorCollection(Enumerable.Empty<ConfiguredDataEditor>()));
2016-11-05 19:23:55 +01:00
}
[Test]
public void Can_Instantiate_Repository()
{
// Arrange
2017-12-15 11:19:03 +01:00
var provider = TestObjects.GetScopeProvider(Logger);
using (var scope = ScopeProvider.CreateScope())
{
// Act
2017-12-15 11:19:03 +01:00
var repository = new ScriptRepository(_fileSystem, Mock.Of<IContentSection>());
// Assert
Assert.That(repository, Is.Not.Null);
}
}
[Test]
public void Can_Perform_Add_On_ScriptRepository()
{
// Arrange
2017-12-15 11:19:03 +01:00
var provider = TestObjects.GetScopeProvider(Logger);
using (var scope = ScopeProvider.CreateScope())
{
2017-12-15 11:19:03 +01:00
var repository = new ScriptRepository(_fileSystem, Mock.Of<IContentSection>());
// Act
var script = new Script("test-add-script.js") { Content = "/// <reference name=\"MicrosoftAjax.js\"/>" };
2017-12-07 16:45:25 +01:00
repository.Save(script);
2017-12-15 11:19:03 +01:00
//Assert
Assert.That(_fileSystem.FileExists("test-add-script.js"), Is.True);
}
}
[Test]
public void Can_Perform_Update_On_ScriptRepository()
{
// Arrange
2017-12-15 11:19:03 +01:00
var provider = TestObjects.GetScopeProvider(Logger);
using (var scope = ScopeProvider.CreateScope())
{
2017-12-15 11:19:03 +01:00
var repository = new ScriptRepository(_fileSystem, Mock.Of<IContentSection>());
// Act
var script = new Script("test-updated-script.js") { Content = "/// <reference name=\"MicrosoftAjax.js\"/>" };
2017-12-07 16:45:25 +01:00
repository.Save(script);
2017-12-15 11:19:03 +01:00
script.Content = "/// <reference name=\"MicrosoftAjax-Updated.js\"/>";
2017-12-07 16:45:25 +01:00
repository.Save(script);
2017-12-15 11:19:03 +01:00
var scriptUpdated = repository.Get("test-updated-script.js");
// Assert
Assert.That(_fileSystem.FileExists("test-updated-script.js"), Is.True);
Assert.That(scriptUpdated.Content, Is.EqualTo("/// <reference name=\"MicrosoftAjax-Updated.js\"/>"));
}
}
[Test]
public void Can_Perform_Delete_On_ScriptRepository()
{
// Arrange
2017-12-15 11:19:03 +01:00
var provider = TestObjects.GetScopeProvider(Logger);
using (var scope = ScopeProvider.CreateScope())
{
2017-12-15 11:19:03 +01:00
var repository = new ScriptRepository(_fileSystem, Mock.Of<IContentSection>());
// Act
var script = repository.Get("test-script.js");
repository.Delete(script);
2017-12-15 11:19:03 +01:00
// Assert
Assert.IsFalse(repository.Exists("test-script.js"));
}
}
[Test]
public void Can_Perform_Get_On_ScriptRepository()
{
// Arrange
2017-12-15 11:19:03 +01:00
var provider = TestObjects.GetScopeProvider(Logger);
using (var scope = ScopeProvider.CreateScope())
{
2017-12-15 11:19:03 +01:00
var repository = new ScriptRepository(_fileSystem, Mock.Of<IContentSection>());
// Act
var exists = repository.Get("test-script.js");
// Assert
Assert.That(exists, Is.Not.Null);
Assert.That(exists.Alias, Is.EqualTo("test-script"));
Assert.That(exists.Name, Is.EqualTo("test-script.js"));
}
}
[Test]
public void Can_Perform_GetAll_On_ScriptRepository()
{
// Arrange
2017-12-15 11:19:03 +01:00
var provider = TestObjects.GetScopeProvider(Logger);
using (var scope = ScopeProvider.CreateScope())
{
2017-12-15 11:19:03 +01:00
var repository = new ScriptRepository(_fileSystem, Mock.Of<IContentSection>());
var script = new Script("test-script1.js") { Content = "/// <reference name=\"MicrosoftAjax.js\"/>" };
2017-12-07 16:45:25 +01:00
repository.Save(script);
var script2 = new Script("test-script2.js") { Content = "/// <reference name=\"MicrosoftAjax.js\"/>" };
2017-12-07 16:45:25 +01:00
repository.Save(script2);
var script3 = new Script("test-script3.js") { Content = "/// <reference name=\"MicrosoftAjax.js\"/>" };
2017-12-07 16:45:25 +01:00
repository.Save(script3);
2017-12-15 11:19:03 +01:00
// Act
2017-12-07 16:45:25 +01:00
var scripts = repository.GetMany();
// Assert
Assert.That(scripts, Is.Not.Null);
Assert.That(scripts.Any(), Is.True);
Assert.That(scripts.Any(x => x == null), Is.False);
Assert.That(scripts.Count(), Is.EqualTo(4));
}
}
[Test]
public void Can_Perform_GetAll_With_Params_On_ScriptRepository()
{
// Arrange
2017-12-15 11:19:03 +01:00
var provider = TestObjects.GetScopeProvider(Logger);
using (var scope = ScopeProvider.CreateScope())
{
2017-12-15 11:19:03 +01:00
var repository = new ScriptRepository(_fileSystem, Mock.Of<IContentSection>());
var script = new Script("test-script1.js") { Content = "/// <reference name=\"MicrosoftAjax.js\"/>" };
2017-12-07 16:45:25 +01:00
repository.Save(script);
var script2 = new Script("test-script2.js") { Content = "/// <reference name=\"MicrosoftAjax.js\"/>" };
2017-12-07 16:45:25 +01:00
repository.Save(script2);
var script3 = new Script("test-script3.js") { Content = "/// <reference name=\"MicrosoftAjax.js\"/>" };
2017-12-07 16:45:25 +01:00
repository.Save(script3);
2017-12-15 11:19:03 +01:00
// Act
2017-12-07 16:45:25 +01:00
var scripts = repository.GetMany("test-script1.js", "test-script2.js");
// Assert
Assert.That(scripts, Is.Not.Null);
Assert.That(scripts.Any(), Is.True);
Assert.That(scripts.Any(x => x == null), Is.False);
Assert.That(scripts.Count(), Is.EqualTo(2));
}
}
[Test]
public void Can_Perform_Exists_On_ScriptRepository()
{
// Arrange
2017-12-15 11:19:03 +01:00
var provider = TestObjects.GetScopeProvider(Logger);
using (var scope = ScopeProvider.CreateScope())
{
2017-12-15 11:19:03 +01:00
var repository = new ScriptRepository(_fileSystem, Mock.Of<IContentSection>());
// Act
var exists = repository.Exists("test-script.js");
// Assert
Assert.That(exists, Is.True);
}
}
2015-09-08 17:48:26 +02:00
[Test]
public void Can_Perform_Move_On_ScriptRepository()
{
const string content = "/// <reference name=\"MicrosoftAjax.js\"/>";
// Arrange
2017-12-15 11:19:03 +01:00
var provider = TestObjects.GetScopeProvider(Logger);
using (var scope = ScopeProvider.CreateScope())
{
2017-12-15 11:19:03 +01:00
var repository = new ScriptRepository(_fileSystem, Mock.Of<IContentSection>());
2015-09-08 17:48:26 +02:00
var script = new Script("test-move-script.js") { Content = content };
2017-12-07 16:45:25 +01:00
repository.Save(script);
2017-12-15 11:19:03 +01:00
2015-09-08 17:48:26 +02:00
// Act
script = repository.Get("test-move-script.js");
script.Path = "moved/test-move-script.js";
2017-12-07 16:45:25 +01:00
repository.Save(script);
2017-12-15 11:19:03 +01:00
2015-09-08 17:48:26 +02:00
var existsOld = repository.Exists("test-move-script.js");
var existsNew = repository.Exists("moved/test-move-script.js");
2015-09-08 17:48:26 +02:00
script = repository.Get("moved/test-move-script.js");
2015-09-08 17:48:26 +02:00
// Assert
Assert.IsNotNull(script);
Assert.IsFalse(existsOld);
Assert.IsTrue(existsNew);
Assert.AreEqual(content, script.Content);
}
2015-09-08 17:48:26 +02:00
}
[Test]
public void PathTests()
{
// unless noted otherwise, no changes / 7.2.8
2017-12-15 11:19:03 +01:00
var provider = TestObjects.GetScopeProvider(Logger);
using (var scope = ScopeProvider.CreateScope())
{
2017-12-15 11:19:03 +01:00
var repository = new ScriptRepository(_fileSystem, Mock.Of<IContentSection>());
var script = new Script("test-path-1.js") { Content = "// script" };
2017-12-07 16:45:25 +01:00
repository.Save(script);
2017-12-15 11:19:03 +01:00
Assert.IsTrue(_fileSystem.FileExists("test-path-1.js"));
Assert.AreEqual("test-path-1.js", script.Path);
Assert.AreEqual("/scripts/test-path-1.js", script.VirtualPath);
2017-05-12 14:49:44 +02:00
//ensure you can prefix the same path as the root path name
script = new Script("scripts/path-2/test-path-2.js") { Content = "// script" };
2017-12-07 16:45:25 +01:00
repository.Save(script);
2017-12-15 11:19:03 +01:00
2017-05-12 14:49:44 +02:00
Assert.IsTrue(_fileSystem.FileExists("scripts/path-2/test-path-2.js"));
Assert.AreEqual("scripts\\path-2\\test-path-2.js", script.Path);
Assert.AreEqual("/scripts/scripts/path-2/test-path-2.js", script.VirtualPath);
script = new Script("path-2/test-path-2.js") { Content = "// script" };
2017-12-07 16:45:25 +01:00
repository.Save(script);
2017-12-15 11:19:03 +01:00
Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-2.js"));
Assert.AreEqual("path-2\\test-path-2.js", script.Path); // fixed in 7.3 - 7.2.8 does not update the path
Assert.AreEqual("/scripts/path-2/test-path-2.js", script.VirtualPath);
script = repository.Get("path-2/test-path-2.js");
Assert.IsNotNull(script);
Assert.AreEqual("path-2\\test-path-2.js", script.Path);
Assert.AreEqual("/scripts/path-2/test-path-2.js", script.VirtualPath);
script = new Script("path-2\\test-path-3.js") { Content = "// script" };
2017-12-07 16:45:25 +01:00
repository.Save(script);
2017-12-15 11:19:03 +01:00
Assert.IsTrue(_fileSystem.FileExists("path-2/test-path-3.js"));
Assert.AreEqual("path-2\\test-path-3.js", script.Path);
Assert.AreEqual("/scripts/path-2/test-path-3.js", script.VirtualPath);
script = repository.Get("path-2/test-path-3.js");
Assert.IsNotNull(script);
Assert.AreEqual("path-2\\test-path-3.js", script.Path);
Assert.AreEqual("/scripts/path-2/test-path-3.js", script.VirtualPath);
script = repository.Get("path-2\\test-path-3.js");
Assert.IsNotNull(script);
Assert.AreEqual("path-2\\test-path-3.js", script.Path);
Assert.AreEqual("/scripts/path-2/test-path-3.js", script.VirtualPath);
script = new Script("\\test-path-4.js") { Content = "// script" };
Assert.Throws<FileSecurityException>(() => // fixed in 7.3 - 7.2.8 used to strip the \
{
2017-12-07 16:45:25 +01:00
repository.Save(script);
});
script = repository.Get("missing.js");
Assert.IsNull(script);
// fixed in 7.3 - 7.2.8 used to...
Assert.Throws<FileSecurityException>(() =>
{
script = repository.Get("\\test-path-4.js"); // outside the filesystem, does not exist
});
Assert.Throws<FileSecurityException>(() =>
{
script = repository.Get("../packages.config"); // outside the filesystem, exists
});
}
}
[TearDown]
public override void TearDown()
{
base.TearDown();
//Delete all files
Purge((PhysicalFileSystem) _fileSystem, "");
_fileSystem = null;
2015-09-08 18:10:46 +02:00
}
private void Purge(PhysicalFileSystem fs, string path)
{
var files = fs.GetFiles(path, "*.js");
foreach (var file in files)
{
fs.DeleteFile(file);
}
2015-09-08 18:10:46 +02:00
var dirs = fs.GetDirectories(path);
foreach (var dir in dirs)
{
Purge(fs, dir);
fs.DeleteDirectory(dir);
}
}
protected Stream CreateStream(string contents = null)
{
if (string.IsNullOrEmpty(contents))
contents = "/* test */";
var bytes = Encoding.UTF8.GetBytes(contents);
var stream = new MemoryStream(bytes);
return stream;
}
}
2017-07-20 11:21:28 +02:00
}