Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing explicit namespaces for UmbracoViewPage in templates #11318

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/Umbraco.Core/IO/ViewHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ public class ViewHelper

public ViewHelper(IFileSystem viewFileSystem)
{
if (viewFileSystem == null) throw new ArgumentNullException(nameof(viewFileSystem));
if (viewFileSystem == null)
{
throw new ArgumentNullException(nameof(viewFileSystem));
}

_viewFileSystem = viewFileSystem;
}

Expand Down Expand Up @@ -65,13 +69,15 @@ public static string GetDefaultFileContent(string layoutPageAlias = null, string
var content = new StringBuilder();

if (string.IsNullOrWhiteSpace(modelNamespaceAlias))
{
modelNamespaceAlias = "ContentModels";
}

// either
// @inherits Umbraco.Web.Mvc.UmbracoViewPage
// @inherits Umbraco.Web.Mvc.UmbracoViewPage<ModelClass>
content.AppendLine("@using Umbraco.Cms.Web.Common.PublishedModels;");
content.Append("@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage");
// @inherits UmbracoViewPage
// @inherits UmbracoViewPage<ModelClass>
content.Append("@inherits UmbracoViewPage");

if (modelClassName.IsNullOrWhiteSpace() == false)
{
content.Append("<");
Expand Down Expand Up @@ -100,6 +106,7 @@ public static string GetDefaultFileContent(string layoutPageAlias = null, string
// Layout = null;
// Layout = "layoutPage.cshtml";
content.Append("@{\r\n\tLayout = ");

if (layoutPageAlias.IsNullOrWhiteSpace())
{
content.Append("null");
Expand All @@ -111,6 +118,7 @@ public static string GetDefaultFileContent(string layoutPageAlias = null, string
content.Append(".cshtml\"");
}
content.Append(";\r\n}");

return content.ToString();
}

Expand Down Expand Up @@ -138,8 +146,11 @@ public string UpdateViewFile(ITemplate t, string currentAlias = null)
{
//then kill the old file..
var oldFile = ViewPath(currentAlias);

if (_viewFileSystem.FileExists(oldFile))
{
_viewFileSystem.DeleteFile(oldFile);
}
}

var data = Encoding.UTF8.GetBytes(t.Content);
Expand All @@ -162,7 +173,9 @@ private static string EnsureInheritedLayout(ITemplate template)
var design = template.Content;

if (string.IsNullOrEmpty(design))
{
design = GetDefaultFileContent(template.MasterTemplateAlias);
}

return design;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public void Export_Xml()
Assert.Multiple(() =>
{
Assert.AreEqual("umbPackage", xml.Root.Name.ToString());
Assert.AreEqual($"<Templates><Template><Name>Text page</Name><Key>{template.Key}</Key><Alias>textPage</Alias><Design><![CDATA[@using Umbraco.Cms.Web.Common.PublishedModels;{Environment.NewLine}@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage{Environment.NewLine}@{{{Environment.NewLine}\tLayout = null;{Environment.NewLine}}}]]></Design></Template></Templates>", xml.Element("umbPackage").Element("Templates").ToString(SaveOptions.DisableFormatting));
Assert.AreEqual($"<Templates><Template><Name>Text page</Name><Key>{template.Key}</Key><Alias>textPage</Alias><Design><![CDATA[@inherits UmbracoViewPage{Environment.NewLine}@{{{Environment.NewLine}\tLayout = null;{Environment.NewLine}}}]]></Design></Template></Templates>", xml.Element("umbPackage").Element("Templates").ToString(SaveOptions.DisableFormatting));
Assert.IsNull(xml.DocumentType);
Assert.IsNull(xml.Parent);
Assert.IsNull(xml.NextNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void Can_Perform_Add_View_With_Default_Content()
Assert.That(repository.Get("test"), Is.Not.Null);
Assert.That(FileSystems.MvcViewsFileSystem.FileExists("test.cshtml"), Is.True);
Assert.AreEqual(
@"@usingUmbraco.Cms.Web.Common.PublishedModels;@inheritsUmbraco.Cms.Web.Common.Views.UmbracoViewPage@{Layout=null;}".StripWhitespace(),
@"@inherits UmbracoViewPage @{ Layout = null; }".StripWhitespace(),
template.Content.StripWhitespace());
}
}
Expand Down Expand Up @@ -126,7 +126,7 @@ public void Can_Perform_Add_View_With_Default_Content_With_Parent()
Assert.That(repository.Get("test2"), Is.Not.Null);
Assert.That(FileSystems.MvcViewsFileSystem.FileExists("test2.cshtml"), Is.True);
Assert.AreEqual(
"@usingUmbraco.Cms.Web.Common.PublishedModels;@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage @{ Layout = \"test.cshtml\";}".StripWhitespace(),
"@inherits UmbracoViewPage @{ Layout = \"test.cshtml\";}".StripWhitespace(),
template2.Content.StripWhitespace());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ public void NoOptions()
{
var view = ViewHelper.GetDefaultFileContent();
Assert.AreEqual(
FixView(@"@using Umbraco.Cms.Web.Common.PublishedModels;
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
FixView(@"@inherits UmbracoViewPage
@{
Layout = null;
}"), FixView(view));
Expand All @@ -24,12 +23,11 @@ @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
[Test]
public void Layout()
{
var view = ViewHelper.GetDefaultFileContent(layoutPageAlias: "Dharznoik");
var view = ViewHelper.GetDefaultFileContent(layoutPageAlias: "Master");
Assert.AreEqual(
FixView(@"@using Umbraco.Cms.Web.Common.PublishedModels;
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
FixView(@"@using UmbracoViewPage
@{
Layout = ""Dharznoik.cshtml"";
Layout = ""Master.cshtml"";
}"), FixView(view));
}

Expand All @@ -38,8 +36,7 @@ public void ClassName()
{
var view = ViewHelper.GetDefaultFileContent(modelClassName: "ClassName");
Assert.AreEqual(
FixView(@"@using Umbraco.Cms.Web.Common.PublishedModels;
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ClassName>
FixView(@"@inherits UmbracoViewPage<ClassName>
@{
Layout = null;
}"), FixView(view));
Expand All @@ -50,8 +47,7 @@ public void Namespace()
{
var view = ViewHelper.GetDefaultFileContent(modelNamespace: "Models");
Assert.AreEqual(
FixView(@"@using Umbraco.Cms.Web.Common.PublishedModels;
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
FixView(@"@inherits UmbracoViewPage
@{
Layout = null;
}"), FixView(view));
Expand All @@ -62,8 +58,7 @@ public void ClassNameAndNamespace()
{
var view = ViewHelper.GetDefaultFileContent(modelClassName: "ClassName", modelNamespace: "My.Models");
Assert.AreEqual(
FixView(@"@using Umbraco.Cms.Web.Common.PublishedModels;
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.ClassName>
FixView(@"@inherits UmbracoViewPage<ContentModels.ClassName>
@using ContentModels = My.Models;
@{
Layout = null;
Expand All @@ -75,8 +70,7 @@ public void ClassNameAndNamespaceAndAlias()
{
var view = ViewHelper.GetDefaultFileContent(modelClassName: "ClassName", modelNamespace: "My.Models", modelNamespaceAlias: "MyModels");
Assert.AreEqual(
FixView(@"@using Umbraco.Cms.Web.Common.PublishedModels;
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<MyModels.ClassName>
FixView(@"@inherits UmbracoViewPage<MyModels.ClassName>
@using MyModels = My.Models;
@{
Layout = null;
Expand All @@ -86,13 +80,12 @@ @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<MyModels.ClassName>
[Test]
public void Combined()
{
var view = ViewHelper.GetDefaultFileContent(layoutPageAlias: "Dharznoik", modelClassName: "ClassName", modelNamespace: "My.Models", modelNamespaceAlias: "MyModels");
var view = ViewHelper.GetDefaultFileContent(layoutPageAlias: "Master", modelClassName: "ClassName", modelNamespace: "My.Models", modelNamespaceAlias: "MyModels");
Assert.AreEqual(
FixView(@"@using Umbraco.Cms.Web.Common.PublishedModels;
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<MyModels.ClassName>
FixView(@"@inherits UmbracoViewPage<MyModels.ClassName>
@using MyModels = My.Models;
@{
Layout = ""Dharznoik.cshtml"";
Layout = ""Master.cshtml"";
}"), FixView(view));
}

Expand Down