Skip to content

Commit

Permalink
Merge branch 'b-7.2.x-allow-optional-selectors-OXDEV-8771' into b-7.2…
Browse files Browse the repository at this point in the history
….x-extral-all-user-data-OXDEV-805
  • Loading branch information
RahatHameed committed Oct 9, 2024
2 parents a196115 + c1f9b21 commit 1c73216
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 15 deletions.
27 changes: 20 additions & 7 deletions src/UserData/Infrastructure/GeneralTableDataSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@

class GeneralTableDataSelector implements DataSelectorInterface
{
/**
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) Not different behaviour, just protection from explosion
*/
public function __construct(
private string $collection,
private string $selectionTable,
private string $filterColumn,
private QueryBuilderFactoryInterface $queryBuilderFactory,
private bool $optional = false,
) {
}

Expand All @@ -35,14 +39,23 @@ public function getSelectionTable(): string
public function getDataForColumnValue(string $columnValue): array
{
$queryBuilder = $this->queryBuilderFactory->create();
$queryBuilder->select('*')
->from($this->selectionTable)
->where($this->filterColumn . ' = :filterValue')
->setParameter('filterValue', $columnValue);

/** @var Result $result */
$result = $queryBuilder->execute(); /** @phpstan-ignore missingType.iterableValue */
try {
$queryBuilder->select('*')
->from($this->selectionTable)
->where($this->filterColumn . ' = :filterValue')
->setParameter('filterValue', $columnValue);

return $result->fetchAllAssociative();
/** @var Result $result */
$result = $queryBuilder->execute(); /** @phpstan-ignore missingType.iterableValue */

return $result->fetchAllAssociative();
} catch (\Exception $e) {
if (!$this->optional) {
throw $e;
}
}

return [];
}
}
29 changes: 21 additions & 8 deletions src/UserData/Infrastructure/RelatedTableDataSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@

class RelatedTableDataSelector implements DataSelectorInterface
{
/**
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) Not different behaviour, just protection from explosion
*/
public function __construct(
private string $collection,
private string $primaryTable,
private string $selectionTable,
private string $relationCondition,
private string $filterColumn,
private QueryBuilderFactoryInterface $queryBuilderFactory,
private bool $optional = false,
) {
}

Expand All @@ -37,15 +41,24 @@ public function getSelectionTable(): string
public function getDataForColumnValue(string $columnValue): array
{
$queryBuilder = $this->queryBuilderFactory->create();
$queryBuilder->select($this->selectionTable . '.*')
->from($this->primaryTable)
->innerJoin($this->primaryTable, $this->selectionTable, $this->selectionTable, $this->relationCondition)
->where($this->filterColumn . ' = :filterValue')
->setParameter('filterValue', $columnValue);

/** @var Result $result */
$result = $queryBuilder->execute(); /** @phpstan-ignore missingType.iterableValue */
try {
$queryBuilder->select($this->selectionTable . '.*')
->from($this->primaryTable)
->innerJoin($this->primaryTable, $this->selectionTable, $this->selectionTable, $this->relationCondition)
->where($this->filterColumn . ' = :filterValue')
->setParameter('filterValue', $columnValue);

return $result->fetchAllAssociative();
/** @var Result $result */
$result = $queryBuilder->execute(); /** @phpstan-ignore missingType.iterableValue */

return $result->fetchAllAssociative();
} catch (\Exception $e) {
if (!$this->optional) {
throw $e;
}
}

return [];
}
}
1 change: 1 addition & 0 deletions src/UserData/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ services:
$selectionTable: 'oxobject2role'
$relationCondition: 'oxuser.OXID = oxobject2role.OXOBJECTID'
$filterColumn: 'oxuser.OXID'
$optional: true
tags:
- { name: 'oe.gdpr.user_data' }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,30 @@ public function testSelectorSelectsDataFromTableFilteredByColumnValue(): void
$this->assertSame($collectionName, $sut->getCollection());
$this->assertSame('oxuser', $sut->getSelectionTable());
}

public function testSelectorExplodesOnWrongQuery(): void
{
$sut = new GeneralTableDataSelector(
collection: uniqid(),
selectionTable: uniqid(),
filterColumn: uniqid(),
queryBuilderFactory: $this->get(QueryBuilderFactoryInterface::class),
);

$this->expectException(\Exception::class);
$sut->getDataForColumnValue(self::USER_ID);
}

public function testOptionalFlagDoesNotExplodeOnQueryError(): void
{
$sut = new GeneralTableDataSelector(
collection: uniqid(),
selectionTable: uniqid(),
filterColumn: uniqid(),
queryBuilderFactory: $this->get(QueryBuilderFactoryInterface::class),
optional: true,
);

$this->assertSame([], $sut->getDataForColumnValue(self::USER_ID));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,34 @@ public function testSelectorSelectsDataFromTableFilteredByColumnValue(): void
$this->assertSame($collectionName, $sut->getCollection());
$this->assertSame('oxorderfiles', $sut->getSelectionTable());
}

public function testSelectorExplodesOnWrongQuery(): void
{
$sut = new RelatedTableDataSelector(
collection: $collectionName = uniqid(),
primaryTable: uniqid(),
selectionTable: uniqid(),
relationCondition: 'oxorderfiles.OXORDERID = oxorder.OXID',
filterColumn: 'oxorder.OXUSERID',
queryBuilderFactory: $this->get(QueryBuilderFactoryInterface::class)
);

$this->expectException(\Exception::class);
$sut->getDataForColumnValue(self::USER_ID);
}

public function testOptionalFlagDoesNotExplodeOnQueryError(): void
{
$sut = new RelatedTableDataSelector(
collection: uniqid(),
primaryTable: uniqid(),
selectionTable: uniqid(),
relationCondition: 'oxorderfiles.OXORDERID = oxorder.OXID',
filterColumn: 'oxorder.OXUSERID',
queryBuilderFactory: $this->get(QueryBuilderFactoryInterface::class),
optional: true
);

$this->assertSame([], $sut->getDataForColumnValue(self::USER_ID));
}
}
24 changes: 24 additions & 0 deletions tests/Integration/UserData/Infrastructure/SelectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\GdprOptinModule\Tests\Integration\UserData\Infrastructure;

use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase;
use OxidEsales\GdprOptinModule\UserData\Service\CollectionAggregationServiceInterface;

class SelectorTest extends IntegrationTestCase
{
public function testConfiguredSelectors(): void
{
$aggregate = $this->get(CollectionAggregationServiceInterface::class);
$aggregate->collectUserData(uniqid());

$this->addToAssertionCount(1);
}
}

0 comments on commit 1c73216

Please sign in to comment.