2017-07-20 11:21:28 +02:00
|
|
|
|
using System.Linq;
|
2015-01-19 15:12:34 +11:00
|
|
|
|
using NUnit.Framework;
|
2020-03-30 20:55:13 +11:00
|
|
|
|
using Umbraco.Core;
|
2015-01-19 15:12:34 +11:00
|
|
|
|
using Umbraco.Core.Models;
|
2018-10-02 12:57:19 +02:00
|
|
|
|
using Umbraco.Core.Persistence;
|
2017-12-28 09:06:33 +01:00
|
|
|
|
using Umbraco.Core.Persistence.Dtos;
|
2017-12-07 16:45:25 +01:00
|
|
|
|
using Umbraco.Core.Persistence.Repositories.Implement;
|
2017-12-15 11:19:03 +01:00
|
|
|
|
using Umbraco.Core.Scoping;
|
2020-03-30 20:55:13 +11:00
|
|
|
|
using Umbraco.Tests.Integration.Testing;
|
2016-12-16 10:40:14 +01:00
|
|
|
|
using Umbraco.Tests.Testing;
|
2020-09-17 09:42:55 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2015-01-19 15:12:34 +11:00
|
|
|
|
|
2020-03-30 20:55:13 +11:00
|
|
|
|
namespace Umbraco.Tests.Integration.Persistence.Repositories
|
2015-01-19 15:12:34 +11:00
|
|
|
|
{
|
|
|
|
|
|
[TestFixture]
|
2018-10-02 15:19:01 +02:00
|
|
|
|
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest, Logger = UmbracoTestOptions.Logger.Console)]
|
2020-03-30 20:55:13 +11:00
|
|
|
|
public class AuditRepositoryTest : UmbracoIntegrationTest
|
2015-01-19 15:12:34 +11:00
|
|
|
|
{
|
2018-10-02 15:19:01 +02:00
|
|
|
|
|
2015-01-19 15:12:34 +11:00
|
|
|
|
[Test]
|
|
|
|
|
|
public void Can_Add_Audit_Entry()
|
|
|
|
|
|
{
|
2020-03-30 20:55:13 +11:00
|
|
|
|
var sp = ScopeProvider;
|
|
|
|
|
|
using (var scope = ScopeProvider.CreateScope())
|
2015-01-19 15:12:34 +11:00
|
|
|
|
{
|
2020-09-17 09:42:55 +02:00
|
|
|
|
var repo = new AuditRepository((IScopeAccessor)sp, ConsoleLoggerFactory.CreateLogger<AuditRepository>());
|
2020-03-30 20:55:13 +11:00
|
|
|
|
repo.Save(new AuditItem(-1, AuditType.System, -1, UmbracoObjectTypes.Document.GetName(), "This is a System audit trail"));
|
2015-01-19 15:12:34 +11:00
|
|
|
|
|
2017-12-12 15:04:13 +01:00
|
|
|
|
var dtos = scope.Database.Fetch<LogDto>("WHERE id > -1");
|
2015-01-19 15:12:34 +11:00
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
|
Assert.That(dtos.Any(), Is.True);
|
|
|
|
|
|
Assert.That(dtos.First().Comment, Is.EqualTo("This is a System audit trail"));
|
|
|
|
|
|
}
|
2015-01-19 15:12:34 +11:00
|
|
|
|
}
|
2018-03-27 17:59:53 +02:00
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void Get_Paged_Items()
|
|
|
|
|
|
{
|
2020-03-30 20:55:13 +11:00
|
|
|
|
var sp = ScopeProvider;
|
2018-03-27 17:59:53 +02:00
|
|
|
|
using (var scope = sp.CreateScope())
|
|
|
|
|
|
{
|
2020-09-17 09:42:55 +02:00
|
|
|
|
var repo = new AuditRepository((IScopeAccessor)sp, ConsoleLoggerFactory.CreateLogger<AuditRepository>());
|
2018-03-27 17:59:53 +02:00
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < 100; i++)
|
|
|
|
|
|
{
|
2020-03-30 20:55:13 +11:00
|
|
|
|
repo.Save(new AuditItem(i, AuditType.New, -1, UmbracoObjectTypes.Document.GetName(), $"Content {i} created"));
|
|
|
|
|
|
repo.Save(new AuditItem(i, AuditType.Publish, -1, UmbracoObjectTypes.Document.GetName(), $"Content {i} published"));
|
2018-03-27 17:59:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
scope.Complete();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
using (var scope = sp.CreateScope())
|
|
|
|
|
|
{
|
2020-09-17 09:42:55 +02:00
|
|
|
|
var repo = new AuditRepository((IScopeAccessor)sp, ConsoleLoggerFactory.CreateLogger<AuditRepository>());
|
2018-03-27 17:59:53 +02:00
|
|
|
|
|
|
|
|
|
|
var page = repo.GetPagedResultsByQuery(sp.SqlContext.Query<IAuditItem>(), 0, 10, out var total, Direction.Descending, null, null);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(10, page.Count());
|
|
|
|
|
|
Assert.AreEqual(200, total);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-21 10:41:50 +10:00
|
|
|
|
[Test]
|
|
|
|
|
|
public void Get_Paged_Items_By_User_Id_With_Query_And_Filter()
|
|
|
|
|
|
{
|
2020-03-30 20:55:13 +11:00
|
|
|
|
var sp = ScopeProvider;
|
2018-10-02 12:57:19 +02:00
|
|
|
|
using (var scope = sp.CreateScope())
|
2018-08-21 10:41:50 +10:00
|
|
|
|
{
|
2020-09-17 09:42:55 +02:00
|
|
|
|
var repo = new AuditRepository((IScopeAccessor)sp, ConsoleLoggerFactory.CreateLogger<AuditRepository>());
|
2018-10-02 12:57:19 +02:00
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < 100; i++)
|
2018-08-21 10:41:50 +10:00
|
|
|
|
{
|
2020-03-30 20:55:13 +11:00
|
|
|
|
repo.Save(new AuditItem(i, AuditType.New, -1, UmbracoObjectTypes.Document.GetName(), $"Content {i} created"));
|
|
|
|
|
|
repo.Save(new AuditItem(i, AuditType.Publish, -1, UmbracoObjectTypes.Document.GetName(), $"Content {i} published"));
|
2018-08-21 10:41:50 +10:00
|
|
|
|
}
|
2018-10-02 12:57:19 +02:00
|
|
|
|
|
|
|
|
|
|
scope.Complete();
|
2018-08-21 10:41:50 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-02 12:57:19 +02:00
|
|
|
|
using (var scope = sp.CreateScope())
|
2018-08-21 10:41:50 +10:00
|
|
|
|
{
|
2020-09-17 09:42:55 +02:00
|
|
|
|
var repo = new AuditRepository((IScopeAccessor)sp, ConsoleLoggerFactory.CreateLogger<AuditRepository>());
|
2018-10-02 12:57:19 +02:00
|
|
|
|
|
2018-10-02 15:19:01 +02:00
|
|
|
|
var query = sp.SqlContext.Query<IAuditItem>().Where(x => x.UserId == -1);
|
2018-08-21 10:41:50 +10:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2018-10-02 12:57:19 +02:00
|
|
|
|
scope.Database.AsUmbracoDatabase().EnableSqlTrace = true;
|
|
|
|
|
|
scope.Database.AsUmbracoDatabase().EnableSqlCount = true;
|
2018-08-21 10:41:50 +10:00
|
|
|
|
|
|
|
|
|
|
var page = repo.GetPagedResultsByQuery(query, 0, 10, out var total, Direction.Descending,
|
|
|
|
|
|
new[] { AuditType.Publish },
|
2018-10-02 15:19:01 +02:00
|
|
|
|
sp.SqlContext.Query<IAuditItem>().Where(x => x.UserId > -2));
|
2018-08-21 10:41:50 +10:00
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(10, page.Count());
|
|
|
|
|
|
Assert.AreEqual(100, total);
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
2018-10-02 12:57:19 +02:00
|
|
|
|
scope.Database.AsUmbracoDatabase().EnableSqlTrace = false;
|
|
|
|
|
|
scope.Database.AsUmbracoDatabase().EnableSqlCount = false;
|
2018-08-21 10:41:50 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-27 17:59:53 +02:00
|
|
|
|
[Test]
|
|
|
|
|
|
public void Get_Paged_Items_With_AuditType_Filter()
|
|
|
|
|
|
{
|
2020-03-30 20:55:13 +11:00
|
|
|
|
var sp = ScopeProvider;
|
2018-03-27 17:59:53 +02:00
|
|
|
|
using (var scope = sp.CreateScope())
|
|
|
|
|
|
{
|
2020-09-17 09:42:55 +02:00
|
|
|
|
var repo = new AuditRepository((IScopeAccessor)sp, ConsoleLoggerFactory.CreateLogger<AuditRepository>());
|
2018-03-27 17:59:53 +02:00
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < 100; i++)
|
|
|
|
|
|
{
|
2020-03-30 20:55:13 +11:00
|
|
|
|
repo.Save(new AuditItem(i, AuditType.New, -1, UmbracoObjectTypes.Document.GetName(), $"Content {i} created"));
|
|
|
|
|
|
repo.Save(new AuditItem(i, AuditType.Publish, -1, UmbracoObjectTypes.Document.GetName(), $"Content {i} published"));
|
2018-03-27 17:59:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
scope.Complete();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
using (var scope = sp.CreateScope())
|
|
|
|
|
|
{
|
2020-09-17 09:42:55 +02:00
|
|
|
|
var repo = new AuditRepository((IScopeAccessor)sp, ConsoleLoggerFactory.CreateLogger<AuditRepository>());
|
2018-03-27 17:59:53 +02:00
|
|
|
|
|
|
|
|
|
|
var page = repo.GetPagedResultsByQuery(sp.SqlContext.Query<IAuditItem>(), 0, 9, out var total, Direction.Descending,
|
2020-03-30 20:55:13 +11:00
|
|
|
|
new[] { AuditType.Publish }, null)
|
2018-03-27 17:59:53 +02:00
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(9, page.Length);
|
|
|
|
|
|
Assert.IsTrue(page.All(x => x.AuditType == AuditType.Publish));
|
|
|
|
|
|
Assert.AreEqual(100, total);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void Get_Paged_Items_With_Custom_Filter()
|
|
|
|
|
|
{
|
2020-03-30 20:55:13 +11:00
|
|
|
|
var sp = ScopeProvider;
|
2018-03-27 17:59:53 +02:00
|
|
|
|
using (var scope = sp.CreateScope())
|
|
|
|
|
|
{
|
2020-09-17 09:42:55 +02:00
|
|
|
|
var repo = new AuditRepository((IScopeAccessor)sp, ConsoleLoggerFactory.CreateLogger<AuditRepository>());
|
2018-03-27 17:59:53 +02:00
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < 100; i++)
|
|
|
|
|
|
{
|
2020-03-30 20:55:13 +11:00
|
|
|
|
repo.Save(new AuditItem(i, AuditType.New, -1, UmbracoObjectTypes.Document.GetName(), "Content created"));
|
|
|
|
|
|
repo.Save(new AuditItem(i, AuditType.Publish, -1, UmbracoObjectTypes.Document.GetName(), "Content published"));
|
2018-03-27 17:59:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
scope.Complete();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
using (var scope = sp.CreateScope())
|
|
|
|
|
|
{
|
2020-09-17 09:42:55 +02:00
|
|
|
|
var repo = new AuditRepository((IScopeAccessor)sp, ConsoleLoggerFactory.CreateLogger<AuditRepository>());
|
2018-03-27 17:59:53 +02:00
|
|
|
|
|
|
|
|
|
|
var page = repo.GetPagedResultsByQuery(sp.SqlContext.Query<IAuditItem>(), 0, 8, out var total, Direction.Descending,
|
|
|
|
|
|
null, sp.SqlContext.Query<IAuditItem>().Where(item => item.Comment == "Content created"))
|
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(8, page.Length);
|
|
|
|
|
|
Assert.IsTrue(page.All(x => x.Comment == "Content created"));
|
|
|
|
|
|
Assert.AreEqual(100, total);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-01-19 15:12:34 +11:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|