Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ public async Task AssociatedProductAddPopupList_UsesScopeDefaultStoreId()
{
_scopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
_productAddPopupViewModelServiceMock
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddAssociatedProductModel>(), 0, 10))
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddAssociatedProductModel>(), 1, 10))
.ReturnsAsync((new List<ProductModel>(), 0));

var model = new ProductModel.AddAssociatedProductModel();
var result = await _controller.AssociatedProductAddPopupList(
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 0, PageSize = 10 }, model);
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 1, PageSize = 10 }, model);

Assert.IsInstanceOfType<JsonResult>(result);
Assert.AreEqual("store-1", model.SearchStoreId);
Expand All @@ -194,12 +194,12 @@ public async Task AssociatedProductAddPopupList_NoDefaultStoreId_DoesNotOverride
{
_scopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
_productAddPopupViewModelServiceMock
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddAssociatedProductModel>(), 0, 10))
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddAssociatedProductModel>(), 1, 10))
.ReturnsAsync((new List<ProductModel>(), 0));

var model = new ProductModel.AddAssociatedProductModel { SearchStoreId = "explicit" };
var result = await _controller.AssociatedProductAddPopupList(
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 0, PageSize = 10 }, model);
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 1, PageSize = 10 }, model);

Assert.IsInstanceOfType<JsonResult>(result);
Assert.AreEqual("explicit", model.SearchStoreId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ public async Task BundleProductAddPopupList_UsesScopeDefaultStoreId()
{
_scopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
_productAddPopupViewModelServiceMock
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddBundleProductModel>(), 0, 10))
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddBundleProductModel>(), 1, 10))
.ReturnsAsync((new List<ProductModel>(), 0));

var model = new ProductModel.AddBundleProductModel();
var result = await _controller.BundleProductAddPopupList(
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 0, PageSize = 10 }, model);
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 1, PageSize = 10 }, model);

Assert.IsInstanceOfType<JsonResult>(result);
Assert.AreEqual("store-1", model.SearchStoreId);
Expand All @@ -173,12 +173,12 @@ public async Task BundleProductAddPopupList_NoDefaultStoreId_DoesNotOverrideMode
{
_scopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
_productAddPopupViewModelServiceMock
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddBundleProductModel>(), 0, 10))
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddBundleProductModel>(), 1, 10))
.ReturnsAsync((new List<ProductModel>(), 0));

var model = new ProductModel.AddBundleProductModel { SearchStoreId = "explicit" };
var result = await _controller.BundleProductAddPopupList(
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 0, PageSize = 10 }, model);
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 1, PageSize = 10 }, model);

Assert.IsInstanceOfType<JsonResult>(result);
Assert.AreEqual("explicit", model.SearchStoreId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,12 +581,12 @@ public async Task RequiredProductAddPopupList_UsesScopeDefaultStoreId()
{
_scopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
_productAddPopupViewModelServiceMock
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddRequiredProductModel>(), 0, 10))
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddRequiredProductModel>(), 1, 10))
.ReturnsAsync((new List<ProductModel>(), 0));

var model = new ProductModel.AddRequiredProductModel();
var result = await _controller.RequiredProductAddPopupList(
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 0, PageSize = 10 }, model);
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 1, PageSize = 10 }, model);

Assert.IsInstanceOfType<JsonResult>(result);
Assert.AreEqual("store-1", model.SearchStoreId);
Expand All @@ -597,12 +597,12 @@ public async Task RequiredProductAddPopupList_NoDefaultStoreId_DoesNotOverrideMo
{
_scopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
_productAddPopupViewModelServiceMock
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddRequiredProductModel>(), 0, 10))
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddRequiredProductModel>(), 1, 10))
.ReturnsAsync((new List<ProductModel>(), 0));

var model = new ProductModel.AddRequiredProductModel { SearchStoreId = "explicit" };
var result = await _controller.RequiredProductAddPopupList(
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 0, PageSize = 10 }, model);
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 1, PageSize = 10 }, model);

Assert.IsInstanceOfType<JsonResult>(result);
Assert.AreEqual("explicit", model.SearchStoreId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ public async Task RelatedProductAddPopupList_UsesScopeDefaultStoreId()
{
_scopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
_productAddPopupViewModelServiceMock
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddRelatedProductModel>(), 0, 10))
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddRelatedProductModel>(), 1, 10))
.ReturnsAsync((new List<ProductModel>(), 0));

var model = new ProductModel.AddRelatedProductModel();
var result = await _controller.RelatedProductAddPopupList(
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 0, PageSize = 10 }, model);
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 1, PageSize = 10 }, model);

Assert.IsInstanceOfType<JsonResult>(result);
Assert.AreEqual("store-1", model.SearchStoreId);
Expand All @@ -174,12 +174,12 @@ public async Task RelatedProductAddPopupList_NoDefaultStoreId_DoesNotOverrideMod
{
_scopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
_productAddPopupViewModelServiceMock
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddRelatedProductModel>(), 0, 10))
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddRelatedProductModel>(), 1, 10))
.ReturnsAsync((new List<ProductModel>(), 0));

var model = new ProductModel.AddRelatedProductModel { SearchStoreId = "explicit" };
var result = await _controller.RelatedProductAddPopupList(
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 0, PageSize = 10 }, model);
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 1, PageSize = 10 }, model);

Assert.IsInstanceOfType<JsonResult>(result);
Assert.AreEqual("explicit", model.SearchStoreId);
Expand Down Expand Up @@ -361,12 +361,12 @@ public async Task SimilarProductAddPopupList_UsesScopeDefaultStoreId()
{
_scopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
_productAddPopupViewModelServiceMock
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddSimilarProductModel>(), 0, 10))
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddSimilarProductModel>(), 1, 10))
.ReturnsAsync((new List<ProductModel>(), 0));

var model = new ProductModel.AddSimilarProductModel();
var result = await _controller.SimilarProductAddPopupList(
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 0, PageSize = 10 }, model);
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 1, PageSize = 10 }, model);

Assert.IsInstanceOfType<JsonResult>(result);
Assert.AreEqual("store-1", model.SearchStoreId);
Expand All @@ -377,12 +377,12 @@ public async Task SimilarProductAddPopupList_NoDefaultStoreId_DoesNotOverrideMod
{
_scopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
_productAddPopupViewModelServiceMock
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddSimilarProductModel>(), 0, 10))
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddSimilarProductModel>(), 1, 10))
.ReturnsAsync((new List<ProductModel>(), 0));

var model = new ProductModel.AddSimilarProductModel { SearchStoreId = "explicit" };
var result = await _controller.SimilarProductAddPopupList(
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 0, PageSize = 10 }, model);
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 1, PageSize = 10 }, model);

Assert.IsInstanceOfType<JsonResult>(result);
Assert.AreEqual("explicit", model.SearchStoreId);
Expand Down Expand Up @@ -558,12 +558,12 @@ public async Task CrossSellProductAddPopupList_UsesScopeDefaultStoreId()
{
_scopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
_productAddPopupViewModelServiceMock
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddCrossSellProductModel>(), 0, 10))
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddCrossSellProductModel>(), 1, 10))
.ReturnsAsync((new List<ProductModel>(), 0));

var model = new ProductModel.AddCrossSellProductModel();
var result = await _controller.CrossSellProductAddPopupList(
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 0, PageSize = 10 }, model);
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 1, PageSize = 10 }, model);

Assert.IsInstanceOfType<JsonResult>(result);
Assert.AreEqual("store-1", model.SearchStoreId);
Expand All @@ -574,12 +574,12 @@ public async Task CrossSellProductAddPopupList_NoDefaultStoreId_DoesNotOverrideM
{
_scopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
_productAddPopupViewModelServiceMock
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddCrossSellProductModel>(), 0, 10))
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddCrossSellProductModel>(), 1, 10))
.ReturnsAsync((new List<ProductModel>(), 0));

var model = new ProductModel.AddCrossSellProductModel { SearchStoreId = "explicit" };
var result = await _controller.CrossSellProductAddPopupList(
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 0, PageSize = 10 }, model);
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 1, PageSize = 10 }, model);

Assert.IsInstanceOfType<JsonResult>(result);
Assert.AreEqual("explicit", model.SearchStoreId);
Expand Down Expand Up @@ -756,12 +756,12 @@ public async Task RecommendedProductAddPopupList_UsesScopeDefaultStoreId()
{
_scopeMock.Setup(s => s.DefaultStoreId).Returns("store-1");
_productAddPopupViewModelServiceMock
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddRecommendedProductModel>(), 0, 10))
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddRecommendedProductModel>(), 1, 10))
.ReturnsAsync((new List<ProductModel>(), 0));

var model = new ProductModel.AddRecommendedProductModel();
var result = await _controller.RecommendedProductAddPopupList(
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 0, PageSize = 10 }, model);
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 1, PageSize = 10 }, model);

Assert.IsInstanceOfType<JsonResult>(result);
Assert.AreEqual("store-1", model.SearchStoreId);
Expand All @@ -772,12 +772,12 @@ public async Task RecommendedProductAddPopupList_NoDefaultStoreId_DoesNotOverrid
{
_scopeMock.Setup(s => s.DefaultStoreId).Returns((string)null);
_productAddPopupViewModelServiceMock
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddRecommendedProductModel>(), 0, 10))
.Setup(s => s.PrepareProductModel(It.IsAny<ProductModel.AddRecommendedProductModel>(), 1, 10))
.ReturnsAsync((new List<ProductModel>(), 0));

var model = new ProductModel.AddRecommendedProductModel { SearchStoreId = "explicit" };
var result = await _controller.RecommendedProductAddPopupList(
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 0, PageSize = 10 }, model);
new Grand.Web.Common.DataSource.DataSourceRequest { Page = 1, PageSize = 10 }, model);

Assert.IsInstanceOfType<JsonResult>(result);
Assert.AreEqual("explicit", model.SearchStoreId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Grand.Web.Common.DataSource;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Grand.Web.Common.Tests.DataSource;

[TestClass]
public class DataSourceRequestTests
{
[TestMethod]
public void PageSize_AboveTheLimit_Limited()
{
var request = new DataSourceRequest { PageSize = int.MaxValue };

Assert.AreEqual(DataSourceRequest.MaxPageSize, request.PageSize);
}

[TestMethod]
public void PageSize_WithinTheLimit_Kept()
{
var request = new DataSourceRequest { PageSize = 50 };

Assert.AreEqual(50, request.PageSize);
}

[TestMethod]
public void PageAndPageSize_BelowOne_One()
{
var request = new DataSourceRequest { Page = -3, PageSize = 0 };

Assert.AreEqual(1, request.Page);
Assert.AreEqual(1, request.PageSize);
}
}
16 changes: 14 additions & 2 deletions src/Web/Grand.Web.Common/DataSource/DataSourceRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@

public class DataSourceRequest
{
public int Page { get; set; } = 1;
//a grid posts its page and page size itself, so a request could ask for every row of a collection at once
public const int MaxPageSize = 1000;

public int PageSize { get; set; } = 10;
private int _page = 1;
private int _pageSize = 10;

public int Page {
get => _page;
set => _page = Math.Max(value, 1);
}

public int PageSize {
get => _pageSize;
set => _pageSize = Math.Clamp(value, 1, MaxPageSize);
}
}
Loading