Skip to content

Commit

Permalink
OXDEV-5088 Cover more temporary active product cases
Browse files Browse the repository at this point in the history
  • Loading branch information
AshrafOxid committed Feb 6, 2024
1 parent ac5d0f3 commit 28e82da
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 112 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-8.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Admin directory is not removed from the url in `ViewConfig::getModuleUrl` anymore [PR-817](https://github.com/OXID-eSales/oxideshop_ce/pull/817)
- Reset created product "sold" counter during Copying of the product [PR-913](https://github.com/OXID-eSales/oxideshop_ce/pull/913)
- `ModuleConfigurationValidatorInterface` is not optional anymore in the module activation service.
- The visibility of time-activated products has changed, products with an undefined end date appear in the shop for an unlimited period of time

### Removed
- Remove console classes from the Internal namespace: `Executor`, `ExecutorInterface`, `CommandsProvider`, `CommandsProviderInterface`
Expand Down
11 changes: 2 additions & 9 deletions source/Application/Controller/Admin/ArticleList.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public function render()

return "article_list";
}

/**
* Returns array of fields which may be used for product data search
*
Expand Down Expand Up @@ -299,25 +300,17 @@ private function setAdditionalSearchFieldForProductsInList(ListModel $productLis
private function setIsActiveFieldForProductsInList(ListModel $productList): void
{
$useTimeCheck = Registry::getConfig()->getConfigParam('blUseTimeCheck');
$now = $this->getCurrentTimeAsDatabaseTimestamp();
foreach ($productList as $key => $product) {
$product->showActiveCheckInAdminPanel = $product->isProductAlwaysActive();

if ($useTimeCheck) {
$product->hasActiveTimeRange = $product->hasProductValidTimeRange();
$product->isActiveNow = $product->isProductActive($now);
$product->isActiveNow = $product->hasActiveTimeRange();
}
$productList[$key] = $product;
}
}

private function getCurrentTimeAsDatabaseTimestamp(): string
{
return Registry::getUtilsDate()->formatDBTimestamp(
Registry::getUtilsDate()->getTime()
);
}

private function convertValueToDatabaseTimestamp(Field $field): void
{
if ($field->fldtype === 'datetime') {
Expand Down
65 changes: 43 additions & 22 deletions source/Application/Model/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,19 +557,17 @@ public function setId($sId = null)
*/
public function getActiveCheckQuery($blForceCoreTable = null)
{
$sTable = $this->getViewName($blForceCoreTable);
$viewName = $this->getViewName($blForceCoreTable);

// check if article is still active
$sQ = " $sTable.oxactive = 1 ";
$query = " $viewName.oxactive = 1 ";

$sQ .= " and $sTable.oxhidden = 0 ";
$query .= " and $viewName.oxhidden = 0 ";

// enabled time range check ?
if (Registry::getConfig()->getConfigParam('blUseTimeCheck')) {
$sQ = $this->addSqlActiveRangeSnippet($sQ, $sTable);
$query = $this->addSqlActiveRangeSnippet($query, $viewName);
}

return $sQ;
return $query;
}

/**
Expand Down Expand Up @@ -1029,6 +1027,21 @@ public function setRangePrice($blIsRangePrice = true)
return $this->_blIsRangePrice = $blIsRangePrice;
}

public function hasActiveTimeRange(): bool
{
$activeFrom = $this->oxarticles__oxactivefrom->value;
$activeTo = $this->oxarticles__oxactiveto->value;
$now = Registry::getUtilsDate()->getTime();

if (!$this->hasProductValidTimeRange()) {
return false;
}

return (Registry::getUtilsDate()->isEmptyDate($activeTo) || strtotime($activeTo) >= $now)
&& (Registry::getUtilsDate()->isEmptyDate($activeFrom) || strtotime($activeFrom) <= $now);
}


/**
* Checks if article has visible status. Returns TRUE if its visible
*
Expand All @@ -1038,19 +1051,16 @@ public function isVisible()
{
// admin preview mode
if (($blCanPreview = Registry::getUtils()->canPreview()) !== null) {

return $blCanPreview;
}

// active ?
$sNow = date('Y-m-d H:i:s');
$blUseTimeCheck = Registry::getConfig()->getConfigParam('blUseTimeCheck');
if (
!$this->oxarticles__oxactive->value &&
(
!Registry::getConfig()->getConfigParam('blUseTimeCheck') ||
$this->oxarticles__oxactivefrom->value > $sNow ||
$this->oxarticles__oxactiveto->value < $sNow
)
!$this->oxarticles__oxactive->value
&& (($blUseTimeCheck && !$this->hasActiveTimeRange()) || !$blUseTimeCheck)
) {

return false;
}

Expand All @@ -1062,6 +1072,7 @@ public function isVisible()
$iOnStock += $session->getBasketReservations()->getReservedAmount($this->getId());
}
if ($iOnStock <= 0) {

return false;
}
}
Expand Down Expand Up @@ -4042,13 +4053,6 @@ public function isProductAlwaysActive(): bool
return !empty($this->oxarticles__oxactive->value);
}

public function isProductActive(string $date): bool
{
return $this->hasProductValidTimeRange()
&& $this->oxarticles__oxactivefrom->value <= $date
&& $this->oxarticles__oxactiveto->value >= $date;
}

/**
* Applies VAT to article
*
Expand Down Expand Up @@ -5270,4 +5274,21 @@ protected function updateManufacturerBeforeLoading($oManufacturer)
{
$oManufacturer->setReadOnly(true);
}

protected function addSqlActiveRangeSnippet($query, $tableName): string
{
$dateUtils = Registry::getUtilsDate();
$secondsToRoundForQueryCache = $this->getSecondsToRoundForQueryCache();
$dateNow = $dateUtils->getRoundedRequestDateDBFormatted($secondsToRoundForQueryCache);
$defaultDBDate = $dateUtils->formatDBDate('-');

$activeToCondition = "$tableName.oxactivefrom <= '$dateNow' AND " .
"$tableName.oxactivefrom != '$defaultDBDate' AND " .
"$tableName.oxactiveto = '$defaultDBDate'";
$activeFromToCondition = "$tableName.oxactivefrom <= '$dateNow' AND $tableName.oxactiveto >= '$dateNow'";

$query = $query ? " $query or " : '';

return " ( $query (($activeToCondition) OR ($activeFromToCondition)) )";
}
}
32 changes: 28 additions & 4 deletions tests/Codeception/Acceptance/Admin/ProductListStatusTestCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
namespace Acceptance\Admin;

use Codeception\Attribute\Group;
use DateTime;
use OxidEsales\EshopCommunity\Tests\Codeception\Support\AcceptanceTester;

#[Group('admin', 'product')]
final class ProductListStatusTestCest
{
private string $temporaryActiveProductID = '1003';
private string $temporaryInactiveProductID = '1004';
private string $productID = '1000';

public function _before(AcceptanceTester $I): void
{
Expand All @@ -32,7 +32,19 @@ public function checkProductsStatuses(AcceptanceTester $I): void

$I->expect('the given product is active in the list');

$productList->filterByProductNumber($this->temporaryActiveProductID);
$I->updateInDatabase(
'oxarticles',
[
'OXACTIVE' => false,
'OXACTIVEFROM' => (new DateTime())->modify('-1 day')->format('Y-m-d 00:00:00'),
'OXACTIVETO' => (new DateTime())->modify('+1 day')->format('Y-m-d 00:00:00')
],
[
'OXID' => $this->productID
]
);

$productList->filterByProductNumber($this->productID);

$I->assertStringContainsString(
'temp-active',
Expand All @@ -41,7 +53,19 @@ public function checkProductsStatuses(AcceptanceTester $I): void

$I->expect('the given product is not active in the list');

$productList->filterByProductNumber($this->temporaryInactiveProductID);
$I->updateInDatabase(
'oxarticles',
[
'OXACTIVE' => false,
'OXACTIVEFROM' => (new DateTime())->modify('+1 day')->format('Y-m-d 00:00:00'),
'OXACTIVETO' => (new DateTime())->modify('+2 day')->format('Y-m-d 00:00:00')
],
[
'OXID' => $this->productID
]
);

$productList->filterByProductNumber($this->productID);

$I->assertStringContainsString(
'temp-inactive',
Expand Down
57 changes: 12 additions & 45 deletions tests/Codeception/Acceptance/ProductDetailsPageCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@

namespace OxidEsales\EshopCommunity\Tests\Codeception\Acceptance;

use Codeception\Attribute\Group;
use Codeception\Util\Fixtures;
use OxidEsales\Codeception\Module\Translation\Translator;
use OxidEsales\Codeception\Step\ProductNavigation;
use OxidEsales\EshopCommunity\Tests\Codeception\Support\AcceptanceTester;

final class ProductDetailsPageCest
{
/**
* @group main
* @group product
* @group productVariants
*/
#[Group('main', 'product', 'productVariants')]
public function selectMultidimensionalVariantsInDetailsPage(AcceptanceTester $I): void
{
$productNavigation = new ProductNavigation($I);
Expand Down Expand Up @@ -117,10 +114,7 @@ public function selectMultidimensionalVariantsInDetailsPage(AcceptanceTester $I)
$detailsPage->seeMiniBasketContains([$basketItem], '50,00 €', '2');
}

/**
* @group product
* @group search
*/
#[Group('product', 'search')]
public function navigateInDetailsPage(AcceptanceTester $I): void
{
$I->wantToTest('product navigation in details page');
Expand Down Expand Up @@ -156,9 +150,7 @@ public function navigateInDetailsPage(AcceptanceTester $I): void
$detailsPage->seeOnBreadCrumb($breadCrumb);
}

/**
* @group product
*/
#[Group('product')]
public function detailsPageInformation(AcceptanceTester $I): void
{
$I->wantToTest('product information in details page');
Expand Down Expand Up @@ -200,10 +192,7 @@ public function detailsPageInformation(AcceptanceTester $I): void
->seeAttributeValue('attr value 12 [EN] šÄßüл', 3);
}

/**
* @group product
* @group productVariants
*/
#[Group('product', 'productVariants')]
public function selectProductVariant(AcceptanceTester $I): void
{
$productNavigation = new ProductNavigation($I);
Expand Down Expand Up @@ -300,10 +289,7 @@ public function selectProductVariantsWithSelectionLists(AcceptanceTester $I): vo
->seeBasketContainsSelectionList($selectionListsTitle, $selectionList3Value, 2);
}

/**
* @group product
* @group accessories
*/
#[Group('product', 'accessories')]
public function checkProductAccessories(AcceptanceTester $I): void
{
$productNavigation = new ProductNavigation($I);
Expand Down Expand Up @@ -335,10 +321,7 @@ public function checkProductAccessories(AcceptanceTester $I): void
->seeProductData($accessoryData);
}

/**
* @group product
* @group similarProducts
*/
#[Group('product', 'similarProduct')]
public function checkSimilarProducts(AcceptanceTester $I): void
{
$productNavigation = new ProductNavigation($I);
Expand Down Expand Up @@ -372,10 +355,7 @@ public function checkSimilarProducts(AcceptanceTester $I): void
->seeSimilarProductData($productData, 1);
}

/**
* @group product
* @group crossSelling
*/
#[Group('product', 'crossSelling')]
public function checkProductCrossSelling(AcceptanceTester $I): void
{
$productNavigation = new ProductNavigation($I);
Expand Down Expand Up @@ -408,10 +388,7 @@ public function checkProductCrossSelling(AcceptanceTester $I): void
->seeProductData($crossSellingProductData);
}

/**
* @group product
* @group productPrice
*/
#[Group('product', 'productPrice')]
public function checkProductPriceA(AcceptanceTester $I): void
{
$I->wantToTest('product price A');
Expand Down Expand Up @@ -483,10 +460,7 @@ public function checkProductPriceA(AcceptanceTester $I): void
$I->clearShopCache();
}

/**
* @group product
* @group productPrice
*/
#[Group('product', 'productPrice')]
public function checkProductPriceC(AcceptanceTester $I): void
{
$I->wantToTest('product price C and amount price discount added to this price');
Expand Down Expand Up @@ -541,10 +515,7 @@ public function checkProductPriceC(AcceptanceTester $I): void
$I->clearShopCache();
}

/**
* @group product
* @group productPrice
*/
#[Group('product', 'productPrice')]
public function checkProductPriceB(AcceptanceTester $I): void
{
$I->wantToTest('product price B');
Expand Down Expand Up @@ -590,11 +561,7 @@ public function checkProductPriceB(AcceptanceTester $I): void
$I->clearShopCache();
}

/**
* @group product
* @group productPrice
* @group productAmountPrice
*/
#[Group('product', 'productPrice', 'ProductAmountPrice')]
public function checkProductAmountPrice(AcceptanceTester $I): void
{
$productNavigation = new ProductNavigation($I);
Expand Down
Loading

0 comments on commit 28e82da

Please sign in to comment.