2014-01-10 17:03:00 +11:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using Moq;
|
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
using Umbraco.Core;
|
2018-01-15 11:32:30 +01:00
|
|
|
|
using Umbraco.Core.Models.Entities;
|
2014-01-10 17:03:00 +11:00
|
|
|
|
using Umbraco.Core.Models.Membership;
|
2017-12-28 09:06:33 +01:00
|
|
|
|
using Umbraco.Core.Persistence.Dtos;
|
2014-01-10 17:03:00 +11:00
|
|
|
|
using Umbraco.Core.Persistence.Repositories;
|
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;
|
2014-01-10 17:03:00 +11:00
|
|
|
|
using Umbraco.Tests.TestHelpers;
|
2016-12-16 10:40:14 +01:00
|
|
|
|
using Umbraco.Tests.Testing;
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Tests.Persistence.Repositories
|
|
|
|
|
|
{
|
|
|
|
|
|
[TestFixture]
|
2016-11-05 19:23:55 +01:00
|
|
|
|
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
|
2016-10-13 21:08:07 +02:00
|
|
|
|
public class NotificationsRepositoryTest : TestWithDatabaseBase
|
2014-01-10 17:03:00 +11:00
|
|
|
|
{
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void CreateNotification()
|
|
|
|
|
|
{
|
2017-12-15 11:19:03 +01:00
|
|
|
|
var provider = TestObjects.GetScopeProvider(Logger);
|
|
|
|
|
|
using (var scope = provider.CreateScope())
|
2016-05-18 09:38:58 +02:00
|
|
|
|
{
|
2017-12-15 11:19:03 +01:00
|
|
|
|
var repo = new NotificationsRepository((IScopeAccessor) provider);
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2017-09-19 18:19:05 +02:00
|
|
|
|
var node = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Constants.ObjectTypes.ContentItem, ParentId = -1, Path = "-1,123", SortOrder = 1, Text = "hello", Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 };
|
2017-12-15 11:19:03 +01:00
|
|
|
|
var result = scope.Database.Insert(node);
|
2016-05-18 09:38:58 +02:00
|
|
|
|
var entity = Mock.Of<IEntity>(e => e.Id == node.NodeId);
|
|
|
|
|
|
var user = Mock.Of<IUser>(e => e.Id == node.UserId);
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2016-05-18 09:38:58 +02:00
|
|
|
|
var notification = repo.CreateNotification(user, entity, "A");
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2016-05-18 09:38:58 +02:00
|
|
|
|
Assert.AreEqual("A", notification.Action);
|
|
|
|
|
|
Assert.AreEqual(node.NodeId, notification.EntityId);
|
|
|
|
|
|
Assert.AreEqual(node.NodeObjectType, notification.EntityType);
|
|
|
|
|
|
Assert.AreEqual(node.UserId, notification.UserId);
|
|
|
|
|
|
}
|
2014-01-10 17:03:00 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetUserNotifications()
|
|
|
|
|
|
{
|
2017-12-15 11:19:03 +01:00
|
|
|
|
var provider = TestObjects.GetScopeProvider(Logger);
|
|
|
|
|
|
using (var scope = provider.CreateScope())
|
2016-05-18 09:38:58 +02:00
|
|
|
|
{
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2017-12-15 11:19:03 +01:00
|
|
|
|
var repo = new NotificationsRepository((IScopeAccessor) provider);
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2017-09-14 19:29:12 +02:00
|
|
|
|
var userDto = new UserDto { Email = "test", Login = "test", Password = "test", UserName = "test", UserLanguage = "en", CreateDate = DateTime.Now, UpdateDate = DateTime.Now };
|
2017-12-15 11:19:03 +01:00
|
|
|
|
scope.Database.Insert(userDto);
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2016-05-18 09:38:58 +02:00
|
|
|
|
var userNew = Mock.Of<IUser>(e => e.Id == userDto.Id);
|
|
|
|
|
|
var userAdmin = Mock.Of<IUser>(e => e.Id == 0);
|
|
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < 10; i++)
|
|
|
|
|
|
{
|
2017-09-19 18:19:05 +02:00
|
|
|
|
var node = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Constants.ObjectTypes.ContentItem, ParentId = -1, Path = "-1," + i, SortOrder = 1, Text = "hello" + i, Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 };
|
2017-12-15 11:19:03 +01:00
|
|
|
|
var result = scope.Database.Insert(node);
|
2016-05-18 09:38:58 +02:00
|
|
|
|
var entity = Mock.Of<IEntity>(e => e.Id == node.NodeId);
|
|
|
|
|
|
var notification = repo.CreateNotification((i%2 == 0) ? userAdmin : userNew, entity, i.ToString(CultureInfo.InvariantCulture));
|
|
|
|
|
|
}
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2016-05-18 09:38:58 +02:00
|
|
|
|
var notifications = repo.GetUserNotifications(userAdmin);
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2016-05-18 09:38:58 +02:00
|
|
|
|
Assert.AreEqual(5, notifications.Count());
|
|
|
|
|
|
}
|
2014-01-10 17:03:00 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void GetEntityNotifications()
|
|
|
|
|
|
{
|
2017-12-15 11:19:03 +01:00
|
|
|
|
var provider = TestObjects.GetScopeProvider(Logger);
|
|
|
|
|
|
using (var scope = provider.CreateScope())
|
2014-01-10 17:03:00 +11:00
|
|
|
|
{
|
|
|
|
|
|
|
2017-12-15 11:19:03 +01:00
|
|
|
|
var repo = new NotificationsRepository((IScopeAccessor) provider);
|
2016-05-18 09:38:58 +02:00
|
|
|
|
|
2017-09-19 18:19:05 +02:00
|
|
|
|
var node1 = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Constants.ObjectTypes.ContentItem, ParentId = -1, Path = "-1,1", SortOrder = 1, Text = "hello1", Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 };
|
2017-12-15 11:19:03 +01:00
|
|
|
|
scope.Database.Insert(node1);
|
2016-05-18 09:38:58 +02:00
|
|
|
|
var entity1 = Mock.Of<IEntity>(e => e.Id == node1.NodeId);
|
2017-09-19 18:19:05 +02:00
|
|
|
|
var node2 = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Constants.ObjectTypes.ContentItem, ParentId = -1, Path = "-1,2", SortOrder = 1, Text = "hello2", Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 };
|
2017-12-15 11:19:03 +01:00
|
|
|
|
scope.Database.Insert(node2);
|
2016-05-18 09:38:58 +02:00
|
|
|
|
var entity2 = Mock.Of<IEntity>(e => e.Id == node2.NodeId);
|
|
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < 10; i++)
|
|
|
|
|
|
{
|
2017-09-14 19:29:12 +02:00
|
|
|
|
var userDto = new UserDto { Email = "test" + i, Login = "test" + i, Password = "test", UserName = "test" + i, UserLanguage = "en", CreateDate = DateTime.Now, UpdateDate = DateTime.Now };
|
2017-12-15 11:19:03 +01:00
|
|
|
|
scope.Database.Insert(userDto);
|
2016-05-18 09:38:58 +02:00
|
|
|
|
var userNew = Mock.Of<IUser>(e => e.Id == userDto.Id);
|
|
|
|
|
|
var notification = repo.CreateNotification(userNew, (i%2 == 0) ? entity1 : entity2, i.ToString(CultureInfo.InvariantCulture));
|
|
|
|
|
|
}
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2016-05-18 09:38:58 +02:00
|
|
|
|
var notifications = repo.GetEntityNotifications(entity1);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(5, notifications.Count());
|
|
|
|
|
|
}
|
2014-01-10 17:03:00 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void Delete_By_Entity()
|
|
|
|
|
|
{
|
2017-12-15 11:19:03 +01:00
|
|
|
|
var provider = TestObjects.GetScopeProvider(Logger);
|
|
|
|
|
|
using (var scope = provider.CreateScope())
|
2016-05-18 09:38:58 +02:00
|
|
|
|
{
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2017-12-15 11:19:03 +01:00
|
|
|
|
var repo = new NotificationsRepository((IScopeAccessor) provider);
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2017-09-19 18:19:05 +02:00
|
|
|
|
var node1 = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Constants.ObjectTypes.ContentItem, ParentId = -1, Path = "-1,1", SortOrder = 1, Text = "hello1", Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 };
|
2017-12-15 11:19:03 +01:00
|
|
|
|
scope.Database.Insert(node1);
|
2016-05-18 09:38:58 +02:00
|
|
|
|
var entity1 = Mock.Of<IEntity>(e => e.Id == node1.NodeId);
|
2017-09-19 18:19:05 +02:00
|
|
|
|
var node2 = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Constants.ObjectTypes.ContentItem, ParentId = -1, Path = "-1,2", SortOrder = 1, Text = "hello2", Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 };
|
2017-12-15 11:19:03 +01:00
|
|
|
|
scope.Database.Insert(node2);
|
2016-05-18 09:38:58 +02:00
|
|
|
|
var entity2 = Mock.Of<IEntity>(e => e.Id == node2.NodeId);
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2016-05-18 09:38:58 +02:00
|
|
|
|
for (var i = 0; i < 10; i++)
|
|
|
|
|
|
{
|
2017-09-14 19:29:12 +02:00
|
|
|
|
var userDto = new UserDto { Email = "test" + i, Login = "test" + i, Password = "test", UserName = "test" + i, UserLanguage = "en", CreateDate = DateTime.Now, UpdateDate = DateTime.Now };
|
2017-12-15 11:19:03 +01:00
|
|
|
|
scope.Database.Insert(userDto);
|
2016-05-18 09:38:58 +02:00
|
|
|
|
var userNew = Mock.Of<IUser>(e => e.Id == userDto.Id);
|
|
|
|
|
|
var notification = repo.CreateNotification(userNew, (i%2 == 0) ? entity1 : entity2, i.ToString(CultureInfo.InvariantCulture));
|
|
|
|
|
|
}
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2016-05-18 09:38:58 +02:00
|
|
|
|
var delCount = repo.DeleteNotifications(entity1);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(5, delCount);
|
|
|
|
|
|
}
|
2014-01-10 17:03:00 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void Delete_By_User()
|
|
|
|
|
|
{
|
2017-12-15 11:19:03 +01:00
|
|
|
|
var provider = TestObjects.GetScopeProvider(Logger);
|
|
|
|
|
|
using (var scope = provider.CreateScope())
|
2016-05-18 09:38:58 +02:00
|
|
|
|
{
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2017-12-15 11:19:03 +01:00
|
|
|
|
var repo = new NotificationsRepository((IScopeAccessor) provider);
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2017-09-14 19:29:12 +02:00
|
|
|
|
var userDto = new UserDto { Email = "test", Login = "test", Password = "test", UserName = "test", UserLanguage = "en", CreateDate = DateTime.Now, UpdateDate = DateTime.Now };
|
2017-12-15 11:19:03 +01:00
|
|
|
|
scope.Database.Insert(userDto);
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2016-05-18 09:38:58 +02:00
|
|
|
|
var userNew = Mock.Of<IUser>(e => e.Id == userDto.Id);
|
|
|
|
|
|
var userAdmin = Mock.Of<IUser>(e => e.Id == 0);
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2016-05-18 09:38:58 +02:00
|
|
|
|
for (var i = 0; i < 10; i++)
|
|
|
|
|
|
{
|
2017-09-19 18:19:05 +02:00
|
|
|
|
var node = new NodeDto { CreateDate = DateTime.Now, Level = 1, NodeObjectType = Constants.ObjectTypes.ContentItem, ParentId = -1, Path = "-1," + i, SortOrder = 1, Text = "hello" + i, Trashed = false, UniqueId = Guid.NewGuid(), UserId = 0 };
|
2017-12-15 11:19:03 +01:00
|
|
|
|
var result = scope.Database.Insert(node);
|
2016-05-18 09:38:58 +02:00
|
|
|
|
var entity = Mock.Of<IEntity>(e => e.Id == node.NodeId);
|
|
|
|
|
|
var notification = repo.CreateNotification((i%2 == 0) ? userAdmin : userNew, entity, i.ToString(CultureInfo.InvariantCulture));
|
|
|
|
|
|
}
|
2014-01-10 17:03:00 +11:00
|
|
|
|
|
2016-05-18 09:38:58 +02:00
|
|
|
|
var delCount = repo.DeleteNotifications(userAdmin);
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(5, delCount);
|
|
|
|
|
|
}
|
2014-01-10 17:03:00 +11:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|