Skip to content

Commit

Permalink
CC-26467: backport for propel filter to criteria mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
michbeck committed May 3, 2023
1 parent 34e4e0d commit 365507c
Showing 1 changed file with 23 additions and 99 deletions.
122 changes: 23 additions & 99 deletions tests/SprykerTest/Zed/Propel/Business/PropelFilterCriteriaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,114 +25,38 @@
class PropelFilterCriteriaTest extends Unit
{
/**
* @dataProvider paginationDataProvider
*
* @param int|null $limit
* @param int|null $offset
* @param int|null $expectedLimit
* @param int|null $expectedOffset
*
* @return void
*/
public function testToCriteriaShouldReturnCriteriaWithPagination(
?int $limit,
?int $offset,
?int $expectedLimit,
?int $expectedOffset
): void {
// Arrange
$filterTransfer = (new FilterTransfer())
->setLimit($limit)
->setOffset($offset);
$propelFilterCriteria = new PropelFilterCriteria($filterTransfer);
public function testToCriteriaShouldReturnEmptyCriteriaWhenNothingWasSet(): void
{
$filterTransfer = new FilterTransfer();

// Act
$criteria = $propelFilterCriteria->toCriteria();
$filterCriteria = new PropelFilterCriteria($filterTransfer);
$propelCriteria = $filterCriteria->toCriteria();

// Assert
$this->assertSame($expectedLimit, $criteria->getLimit());
$this->assertSame($expectedOffset, $criteria->getOffset());
$this->assertInstanceOf(Criteria::class, $propelCriteria);
$this->assertSame(-1, $propelCriteria->getLimit());
$this->assertSame(0, $propelCriteria->getOffset());
$this->assertSame([], $propelCriteria->getOrderByColumns());
}

/**
* @dataProvider sortingDataProvider
* @dataProvider invalidSortingDataProvider
*
* @param string|null $orderByColumnName
* @param string|null $orderDirection
* @param string|null $expectedOrderByColumnName
* @param string|null $expectedOrderDirection
*
* @return void
*/
public function testToCriteriaShouldReturnCriteriaWithSorting(
?string $orderByColumnName,
?string $orderDirection,
?string $expectedOrderByColumnName,
?string $expectedOrderDirection
): void {
// Arrange
$filterTransfer = (new FilterTransfer())
->setOrderBy($orderByColumnName)
->setOrderDirection($orderDirection);
$propelFilterCriteria = new PropelFilterCriteria($filterTransfer);
$expectedOrderbyColumns = [];
if ($expectedOrderByColumnName !== null && $expectedOrderDirection !== null) {
$expectedOrderbyColumns = [sprintf('%s %s', $expectedOrderByColumnName, $expectedOrderDirection)];
}

// Act
$criteria = $propelFilterCriteria->toCriteria();

// Assert
$this->assertSame($expectedOrderbyColumns, $criteria->getOrderByColumns());
}

/**
* @return array<string, array<array-key, int|null>>
*/
protected function paginationDataProvider(): array
{
return [
'limit and offset are null' => [null, null, -1, 0],
'limit and offset are set' => [5, 10, 5, 10],
'limit is set, offset is null' => [10, null, 10, 0],
'offset is set, limit is null' => [null, 20, -1, 20],
];
}

/**
* @return array<string, array<array-key, string|null>>
*/
protected function sortingDataProvider(): array
{
return [
'empty column name and direction asc' => [null, Criteria::ASC, null, Criteria::ASC],
'column name and empty direction' => ['created_at', null, 'created_at', null],
'column name with underscore and direction asc' => ['created_at', Criteria::ASC, 'created_at', Criteria::ASC],
'column name combined with table name and direction asc' => ['spy_product.created_at', Criteria::ASC, 'spy_product.created_at', Criteria::ASC],
'column name and sort direction asc' => ['created_at', Criteria::ASC, 'created_at', Criteria::ASC],
'column name and sort direction desc' => ['created_at', Criteria::DESC, 'created_at', Criteria::DESC],
];
}

/**
* @return array<string, array<array-key, string|null>>
*/
protected function invalidSortingDataProvider(): array
public function testToCriteriaShouldReturnCriteriaWithParameters(): void
{
return [
'invalid column name with SQL injection' => ['created_at; SELECT * FROM users;', Criteria::ASC, null, Criteria::ASC],
'invalid column name with semicolon' => ['created_at;', Criteria::ASC, null, Criteria::ASC],
'invalid column name with spaces' => ['created at', Criteria::ASC, null, Criteria::ASC],
'invalid column name with special characters' => ['created_at#', Criteria::ASC, null, Criteria::ASC],
'invalid column name with brackets' => ['my_column()', Criteria::ASC, null, Criteria::ASC],
'invalid column name with quotes' => ["my_column'", Criteria::ASC, null, Criteria::ASC],
'invalid column name with backticks' => ['`my_column`', Criteria::ASC, null, Criteria::ASC],
'invalid column name with leading/trailing spaces' => [' column ', Criteria::ASC, null, Criteria::ASC],
'invalid column name with non-word characters' => ['col_name$', Criteria::ASC, null, Criteria::ASC],
'invalid column name with non-alphanumeric characters' => ['col:name', Criteria::ASC, null, Criteria::ASC],
'invalid column name with non-ASCII characters' => ['col_名', Criteria::ASC, null, Criteria::ASC],
];
$filterTransfer = new FilterTransfer();
$filterTransfer->setLimit(10);
$filterTransfer->setOffset(0);
$filterTransfer->setOrderDirection('DESC');
$filterTransfer->setOrderBy('foobar');

$filterCriteria = new PropelFilterCriteria($filterTransfer);
$propelCriteria = $filterCriteria->toCriteria();

$this->assertInstanceOf(Criteria::class, $propelCriteria);
$this->assertSame(10, $propelCriteria->getLimit());
$this->assertSame(0, $propelCriteria->getOffset());
$this->assertEquals(['foobar DESC'], $propelCriteria->getOrderByColumns());
}
}

0 comments on commit 365507c

Please sign in to comment.