Files
Umbraco-CMS/tests/Umbraco.TestData/SegmentTestController.cs
Paul Johnson 00133e880d Move test projects from src/ to tests/ (#11357)
* Update gitignore

* Move csproj

* Update project references

* Update solutions

* Update build scripts

* Tests used to share editorconfig with projects in src

* Fix broken tests.

* Stop copying around .editorconfig

merged root one with linting

* csharp_style_expression_bodied -> suggestion

* Move StyleCop rulesets to matching directories and update shared build properties

* Remove legacy build files, update NuGet.cofig and solution files

* Restore myget source

* Clean up .gitignore

* Update .gitignore

* Move new test classes to tests after merge

* Gitignore + nuget config

* Move new test

Co-authored-by: Ronald Barendse <ronald@barend.se>
2021-10-18 08:14:04 +01:00

127 lines
4.5 KiB
C#

using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Web.Website.Controllers;
using Umbraco.Extensions;
using Umbraco.TestData.Configuration;
namespace Umbraco.TestData
{
public class SegmentTestController : SurfaceController
{
private IOptions<TestDataSettings> _testDataSettings;
public SegmentTestController(
IUmbracoContextAccessor umbracoContextAccessor,
IUmbracoDatabaseFactory databaseFactory,
ServiceContext services,
AppCaches appCaches,
IProfilingLogger profilingLogger,
IPublishedUrlProvider publishedUrlProvider,
IOptions<TestDataSettings> testDataSettings)
: base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
{
_testDataSettings = testDataSettings;
}
public IActionResult EnableDocTypeSegments(string alias, string propertyTypeAlias)
{
if(_testDataSettings.Value.Enabled != true)
{
return HttpNotFound();
}
IContentType ct = Services.ContentTypeService.Get(alias);
if (ct == null)
{
return Content($"No document type found by alias {alias}");
}
IPropertyType propType = ct.PropertyTypes.FirstOrDefault(x => x.Alias == propertyTypeAlias);
if (propType == null)
{
return Content($"The document type {alias} does not have a property type {propertyTypeAlias ?? "null"}");
}
if (ct.Variations.VariesBySegment())
{
return Content($"The document type {alias} already allows segments, nothing has been changed");
}
ct.SetVariesBy(ContentVariation.Segment);
propType.SetVariesBy(ContentVariation.Segment);
Services.ContentTypeService.Save(ct);
return Content($"The document type {alias} and property type {propertyTypeAlias} now allows segments");
}
private IActionResult HttpNotFound() => throw new NotImplementedException();
public IActionResult DisableDocTypeSegments(string alias)
{
if (_testDataSettings.Value.Enabled != true)
{
return HttpNotFound();
}
IContentType ct = Services.ContentTypeService.Get(alias);
if (ct == null)
{
return Content($"No document type found by alias {alias}");
}
if (!ct.VariesBySegment())
{
return Content($"The document type {alias} does not allow segments, nothing has been changed");
}
ct.SetVariesBy(ContentVariation.Segment, false);
Services.ContentTypeService.Save(ct);
return Content($"The document type {alias} no longer allows segments");
}
public ActionResult AddSegmentData(int contentId, string propertyAlias, string value, string segment, string culture = null)
{
IContent content = Services.ContentService.GetById(contentId);
if (content == null)
{
return Content($"No content found by id {contentId}");
}
if (propertyAlias.IsNullOrWhiteSpace() || !content.HasProperty(propertyAlias))
{
return Content($"The content by id {contentId} does not contain a property with alias {propertyAlias ?? "null"}");
}
if (content.ContentType.VariesByCulture() && culture.IsNullOrWhiteSpace())
{
return Content($"The content by id {contentId} varies by culture but no culture was specified");
}
if (value.IsNullOrWhiteSpace())
{
return Content("'value' cannot be null");
}
if (segment.IsNullOrWhiteSpace())
{
return Content("'segment' cannot be null");
}
content.SetValue(propertyAlias, value, culture, segment);
Services.ContentService.Save(content);
return Content($"Segment value has been set on content {contentId} for property {propertyAlias}");
}
}
}