Files
Umbraco-CMS/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Persistence/Repositories/DictionaryRepositoryTest.cs
Kenn Jacobsen 134b193c74 New backoffice - trees design (#12963)
* Refactor: Add default versioned back office route attribute

* Tree controller bases and first draft implementations for document, media and doctype

* Move tree item view models to appropriate location

* Fix missing parent

* Refactor user entity access for testability

* A bit of clean-up + handle user start nodes for items endpoint

* Implement foldersOnly for folder tree

* Items endpoint for document type tree

* Strongly typed action results

* Content + media recycle bin

* Correct return type for swagger

* Member type tree

* Rename user start node handling to make a little more sense

* Revert to faked admin start nodes in document tree

* Media type tree

* Data type tree

* Relation type tree

* Remove unused dependency from member type tree

* Correct documentation for member type tree endpoint response types

* Use icon constants

* Add templates tree

* Member group tree

* Document blueprint tree

* Partial views, scripts and stylesheets trees

* Static files tree

* Clarify "folders only" state

* Comments and improved readability

* Rename TreeControllerBase and TreeItemViewModel

* Move recycle bin controller base to its own namespace

* Moved tree base controllers to their own namespace

* Common base class for tree view models

* Remove ProblemDetails response type declaration from all actions

* Add OpenApiTag

* Various review comments

* Dictionary item tree

* Renamed all tree controllers to follow action/feature naming convention

* Handle client culture state for document tree

* Support "ignore user start nodes" for content and media + refactor how tree states work to make things more explicit

* Fix or postpone a few TODOs

* Make entity service able to paginate trashed children

* Handle sorting explicitly

* Re-apply VersionedApiBackOfficeRoute to install and upgrade controllers after merge

* Use PagedViewModel instead of PagedResult for all trees

* Explain the usage of UmbracoObjectTypes.Unknown

* Introduce and apply GetMany pattern for dictionary items

* Add a note about relation type caching

* Fix broken test build + add unit tests for new localization service methods

* Use new management API controller base

* Entity repository should build document entities for document blueprints when getting paged entities (same as it does when getting specific entities)

* Use Media type for Media recycle bin

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>

* Move shared relation service to concrete implementations

* Use inclusive language

* Add 401 response type documentation to applicable trees

* Refactor entity load for folder tree controller base + ensure that folders are only included in the first result page

* Add (in-memory) pagination to dictionary tree

* Make file system controller honor paging parameters

* Support pagination in relation type tree

* Clarify method name a bit for detecting tree root path requests

* Update Open API schema to match new trees

* Move from page number and page size to skip/take (with temporary workaround for lack of property skip/take pagination in current DB implementation)

* Update OpenAPI schema to match skip/take

* Update OpenAPI schema

* Don't return paginated view models from "items" endpoints

* Update OpenApi schema

Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>
2022-09-28 13:37:59 +02:00

424 lines
14 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Persistence.Repositories;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.Repositories;
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
public class DictionaryRepositoryTest : UmbracoIntegrationTest
{
[SetUp]
public void SetUp() => CreateTestData();
private IDictionaryRepository CreateRepository() => GetRequiredService<IDictionaryRepository>();
[Test]
public void Can_Perform_Get_By_Key_On_DictionaryRepository()
{
// Arrange
var localizationService = GetRequiredService<ILocalizationService>();
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository();
var dictionaryItem = (IDictionaryItem)new DictionaryItem("Testing1235")
{
Translations = new List<IDictionaryTranslation>
{
new DictionaryTranslation(localizationService.GetLanguageByIsoCode("en-US"), "Hello world")
}
};
repository.Save(dictionaryItem);
// re-get
dictionaryItem = repository.Get("Testing1235");
// Assert
Assert.That(dictionaryItem, Is.Not.Null);
Assert.That(dictionaryItem.ItemKey, Is.EqualTo("Testing1235"));
Assert.That(dictionaryItem.Translations.Any(), Is.True);
Assert.That(dictionaryItem.Translations.Any(x => x == null), Is.False);
Assert.That(dictionaryItem.Translations.First().Value, Is.EqualTo("Hello world"));
}
}
[Test]
public void Can_Perform_Get_By_UniqueId_On_DictionaryRepository()
{
// Arrange
var localizationService = GetRequiredService<ILocalizationService>();
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository();
var dictionaryItem = (IDictionaryItem)new DictionaryItem("Testing1235")
{
Translations = new List<IDictionaryTranslation>
{
new DictionaryTranslation(localizationService.GetLanguageByIsoCode("en-US"), "Hello world")
}
};
repository.Save(dictionaryItem);
// re-get
dictionaryItem = repository.Get(dictionaryItem.Key);
// Assert
Assert.That(dictionaryItem, Is.Not.Null);
Assert.That(dictionaryItem.ItemKey, Is.EqualTo("Testing1235"));
Assert.That(dictionaryItem.Translations.Any(), Is.True);
Assert.That(dictionaryItem.Translations.Any(x => x == null), Is.False);
Assert.That(dictionaryItem.Translations.First().Value, Is.EqualTo("Hello world"));
}
}
[Test]
public void Can_Perform_Get_On_DictionaryRepository()
{
// Arrange
var localizationService = GetRequiredService<ILocalizationService>();
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository();
var dictionaryItem = (IDictionaryItem)new DictionaryItem("Testing1235")
{
Translations = new List<IDictionaryTranslation>
{
new DictionaryTranslation(localizationService.GetLanguageByIsoCode("en-US"), "Hello world")
}
};
repository.Save(dictionaryItem);
// re-get
dictionaryItem = repository.Get(dictionaryItem.Id);
// Assert
Assert.That(dictionaryItem, Is.Not.Null);
Assert.That(dictionaryItem.ItemKey, Is.EqualTo("Testing1235"));
Assert.That(dictionaryItem.Translations.Any(), Is.True);
Assert.That(dictionaryItem.Translations.Any(x => x == null), Is.False);
Assert.That(dictionaryItem.Translations.First().Value, Is.EqualTo("Hello world"));
}
}
[Test]
public void Can_Perform_Get_On_DictionaryRepository_When_No_Language_Assigned()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository();
var dictionaryItem = (IDictionaryItem)new DictionaryItem("Testing1235");
repository.Save(dictionaryItem);
// re-get
dictionaryItem = repository.Get(dictionaryItem.Id);
// Assert
Assert.That(dictionaryItem, Is.Not.Null);
Assert.That(dictionaryItem.ItemKey, Is.EqualTo("Testing1235"));
Assert.That(dictionaryItem.Translations.Any(), Is.False);
}
}
[Test]
public void Can_Perform_GetAll_On_DictionaryRepository()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository();
// Act
var dictionaryItem = repository.Get(1);
var dictionaryItems = repository.GetMany().ToArray();
// Assert
Assert.That(dictionaryItems, Is.Not.Null);
Assert.That(dictionaryItems.Any(), Is.True);
Assert.That(dictionaryItems.Any(x => x == null), Is.False);
Assert.That(dictionaryItems.Count(), Is.EqualTo(2));
}
}
[Test]
public void Can_Perform_GetAll_ByKeys_On_DictionaryRepository()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository();
// Act
var dictionaryItems = repository.GetManyByKeys().ToArray();
// Assert
Assert.That(dictionaryItems, Is.Not.Null);
Assert.That(dictionaryItems.Any(), Is.True);
Assert.That(dictionaryItems.Any(x => x == null), Is.False);
Assert.That(dictionaryItems.Count(), Is.EqualTo(2));
}
}
[Test]
public void Can_Perform_GetAll_With_Params_On_DictionaryRepository()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository();
// Act
var dictionaryItems = repository.GetMany(1, 2).ToArray();
// Assert
Assert.That(dictionaryItems, Is.Not.Null);
Assert.That(dictionaryItems.Any(), Is.True);
Assert.That(dictionaryItems.Any(x => x == null), Is.False);
Assert.That(dictionaryItems.Count(), Is.EqualTo(2));
}
}
[Test]
public void Can_Perform_GetAll_ByKeys_With_Params_On_DictionaryRepository()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository();
// Act
var dictionaryItems = repository.GetManyByKeys("Read More", "Article").ToArray();
// Assert
Assert.That(dictionaryItems, Is.Not.Null);
Assert.That(dictionaryItems.Any(), Is.True);
Assert.That(dictionaryItems.Any(x => x == null), Is.False);
Assert.That(dictionaryItems.Count(), Is.EqualTo(2));
}
}
[Test]
public void Can_Perform_GetByQuery_On_DictionaryRepository()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository();
// Act
var query = provider.CreateQuery<IDictionaryItem>().Where(x => x.ItemKey == "Article");
var result = repository.Get(query).ToArray();
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result.Any(), Is.True);
Assert.That(result.FirstOrDefault().ItemKey, Is.EqualTo("Article"));
}
}
[Test]
public void Can_Perform_Count_On_DictionaryRepository()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository();
// Act
var query = provider.CreateQuery<IDictionaryItem>().Where(x => x.ItemKey.StartsWith("Read"));
var result = repository.Count(query);
// Assert
Assert.That(result, Is.EqualTo(1));
}
}
[Test]
public void Can_Perform_Add_On_DictionaryRepository()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var languageRepository = GetRequiredService<ILanguageRepository>();
var repository = CreateRepository();
var language = languageRepository.Get(1);
var read = new DictionaryItem("Read");
var translations = new List<IDictionaryTranslation> { new DictionaryTranslation(language, "Read") };
read.Translations = translations;
// Act
repository.Save(read);
var exists = repository.Exists(read.Id);
// Assert
Assert.That(read.HasIdentity, Is.True);
Assert.That(exists, Is.True);
}
}
[Test]
public void Can_Perform_Update_On_DictionaryRepository()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository();
// Act
var item = repository.Get(1);
var translations = item.Translations.ToList();
translations[0].Value = "Read even more";
item.Translations = translations;
repository.Save(item);
var dictionaryItem = repository.Get(1);
// Assert
Assert.That(dictionaryItem, Is.Not.Null);
Assert.That(dictionaryItem.Translations.Count(), Is.EqualTo(2));
Assert.That(dictionaryItem.Translations.FirstOrDefault().Value, Is.EqualTo("Read even more"));
}
}
[Test]
public void Can_Perform_Update_WithNewTranslation_On_DictionaryRepository()
{
// Arrange
var localizationService = GetRequiredService<ILocalizationService>();
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository();
var languageNo = new Language("nb-NO", "Norwegian Bokmål (Norway)");
localizationService.Save(languageNo);
// Act
var item = repository.Get(1);
var translations = item.Translations.ToList();
translations.Add(new DictionaryTranslation(languageNo, "Les mer"));
item.Translations = translations;
repository.Save(item);
var dictionaryItem = (DictionaryItem)repository.Get(1);
// Assert
Assert.That(dictionaryItem, Is.Not.Null);
Assert.That(dictionaryItem.Translations.Count(), Is.EqualTo(3));
Assert.That(dictionaryItem.Translations.Single(t => t.LanguageId == languageNo.Id).Value, Is.EqualTo("Les mer"));
}
}
[Test]
public void Can_Perform_Delete_On_DictionaryRepository()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository();
// Act
var item = repository.Get(1);
repository.Delete(item);
var exists = repository.Exists(1);
// Assert
Assert.That(exists, Is.False);
}
}
[Test]
public void Can_Perform_Exists_On_DictionaryRepository()
{
// Arrange
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository();
// Act
var exists = repository.Exists(1);
// Assert
Assert.That(exists, Is.True);
}
}
[Test]
public void Can_Perform_GetDictionaryItemKeyMap_On_DictionaryRepository()
{
Dictionary<string, Guid> keyMap;
var provider = ScopeProvider;
using (provider.CreateScope())
{
var repository = CreateRepository();
keyMap = repository.GetDictionaryItemKeyMap();
}
Assert.IsNotNull(keyMap);
Assert.IsNotEmpty(keyMap);
foreach (var kvp in keyMap)
{
Console.WriteLine("{0}: {1}", kvp.Key, kvp.Value);
}
}
public void CreateTestData()
{
var localizationService = GetRequiredService<ILocalizationService>();
var language = localizationService.GetLanguageByIsoCode("en-US");
var languageDK = new Language("da-DK", "Danish (Denmark)");
localizationService.Save(languageDK); //Id 2
var readMore = new DictionaryItem("Read More");
var translations = new List<IDictionaryTranslation>
{
new DictionaryTranslation(language, "Read More"), new DictionaryTranslation(languageDK, "Læs mere")
};
readMore.Translations = translations;
localizationService.Save(readMore); // Id 1
var article = new DictionaryItem("Article");
var translations2 = new List<IDictionaryTranslation>
{
new DictionaryTranslation(language, "Article"), new DictionaryTranslation(languageDK, "Artikel")
};
article.Translations = translations2;
localizationService.Save(article); // Id 2
}
}