You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.7 KiB
PHP
69 lines
1.7 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Utils\SafeDatatables;
|
|
use Ozdemir\Datatables\DB\DatabaseInterface;
|
|
|
|
final class SafeDatatablesPaginationTestDatabase implements DatabaseInterface
|
|
{
|
|
public function __construct($config)
|
|
{
|
|
}
|
|
|
|
public function connect()
|
|
{
|
|
return $this;
|
|
}
|
|
|
|
public function query($query)
|
|
{
|
|
return [];
|
|
}
|
|
|
|
public function count($query)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
public function escape($string)
|
|
{
|
|
return "''";
|
|
}
|
|
}
|
|
|
|
function safeDatatablesLimit(SafeDatatables $datatables, array $input)
|
|
{
|
|
$inputProperty = new ReflectionProperty(SafeDatatables::class, 'input');
|
|
$inputProperty->setValue($datatables, $input);
|
|
|
|
$limitMethod = new ReflectionMethod(SafeDatatables::class, 'limit');
|
|
return $limitMethod->invoke($datatables);
|
|
}
|
|
|
|
function safeDatatablesAssert($condition, $message)
|
|
{
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
}
|
|
|
|
$database = new SafeDatatablesPaginationTestDatabase(null);
|
|
$standard = new SafeDatatables($database);
|
|
$showAll = new SafeDatatables($database, true);
|
|
|
|
safeDatatablesAssert(
|
|
safeDatatablesLimit($standard, ['start' => 0, 'length' => -1]) === ' LIMIT 0, 10 ',
|
|
'Standard tables must not be able to disable the page-size limit.'
|
|
);
|
|
safeDatatablesAssert(
|
|
safeDatatablesLimit($showAll, ['start' => 0, 'length' => -1]) === '',
|
|
'Explicit show-all tables must omit the SQL row limit.'
|
|
);
|
|
safeDatatablesAssert(
|
|
safeDatatablesLimit($showAll, ['start' => 5, 'length' => 1000]) === ' LIMIT 5, 100 ',
|
|
'Explicit show-all mode must retain the normal cap without the -1 sentinel.'
|
|
);
|
|
|
|
echo "Safe Datatables pagination passed.\n";
|