1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-27 16:20:13 +02:00

Improved testing of admin-ui search with custom handler.

This commit is contained in:
camer0n
2025-03-27 08:04:59 -07:00
parent 65a239e9f0
commit 80ecb2b289
2 changed files with 98 additions and 28 deletions

View File

@@ -9,26 +9,25 @@
*/
class e_admin_controller_uiTest extends \Codeception\Test\Unit
{
/** @var e_admin_controller_ui */
protected $ui;
protected $req;
protected function _before()
{
try
{
$this->ui = $this->make('e_admin_controller_ui');
$this->ui = $this->make(e_admin_controller_ui::class);
$this->req = $this->make(e_admin_request::class);
}
catch(Exception $e)
catch (Exception $e)
{
$this::fail("Couldn't load e_admin_controller_ui object");
$this::fail("Couldn't load e_admin_controller_ui object: " . $e->getMessage());
}
}
public function testJoinAlias()
@@ -97,7 +96,7 @@
'cal_by_cell' => 'cl.cal_by_cell',
);
$this->assertEquals($expected,$actual);
self::assertEquals($expected,$actual);
}
@@ -118,26 +117,29 @@
$perPage = 10;
$qryField = null;
$isfilter = false;
$handleAction = 'list';
$handleAction = 'List';
$this->ui->setFields([
'user_id' => array('title'=>'User ID', '__tableField' => 'u.user_id', 'type'=>'int', 'data'=>'int'),
'user_name' => array('title' => 'Name', '__tableField' => 'u.user_name', 'type' => 'text', 'data'=>'safestr'), // Display name
'user_login' => array('title' => 'Login','__tableField' => 'u.user_login', 'type' => 'text', 'data'=>'safestr'), // Real name (no real vetting)
'user_phone' => array('title' => 'Phone','__tableField' => 'u.user_phone', 'search'=>true, 'type' => 'text', 'data'=>'safestr'), // Real name (no real vetting)
]);
// Test single word search term.
$result = $this->ui->_modifyListQrySearch($listQry, 'admin', $filterOptions, $tablePath, $tableFrom, $primaryName, $raw, $orderField, $qryAsc, $forceFrom, $qryFrom, $forceTo, $perPage, $qryField, $isfilter, $handleAction);
$expected = "SELECT u.* FROM `#user` WHERE 1 AND ( u.user_name LIKE '%admin%' OR u.user_login LIKE '%admin%' ) LIMIT 0, 10";
$expected = "SELECT u.* FROM `#user` WHERE 1 AND ( u.user_name LIKE '%admin%' OR u.user_login LIKE '%admin%' OR u.user_phone LIKE '%admin%' ) LIMIT 0, 10";
$this::assertSame($expected, $result);
// Test multiple word search term.
$result = $this->ui->_modifyListQrySearch($listQry, 'firstname lastname', $filterOptions, $tablePath, $tableFrom, $primaryName, $raw, $orderField, $qryAsc, $forceFrom, $qryFrom, $forceTo, $perPage, $qryField, $isfilter, $handleAction);
$expected = "SELECT u.* FROM `#user` WHERE 1 AND (u.user_name LIKE '%firstname%' OR u.user_login LIKE '%firstname%') AND (u.user_name LIKE '%lastname%' OR u.user_login LIKE '%lastname%') LIMIT 0, 10";
$expected = "SELECT u.* FROM `#user` WHERE 1 AND (u.user_name LIKE '%firstname%' OR u.user_login LIKE '%firstname%' OR u.user_phone LIKE '%firstname%') AND (u.user_name LIKE '%lastname%' OR u.user_login LIKE '%lastname%' OR u.user_phone LIKE '%lastname%') LIMIT 0, 10";
$this::assertSame($expected, $result);
// Search term in quotes.
$expected = "SELECT u.* FROM `#user` WHERE 1 AND ( u.user_name LIKE '%firstname lastname%' OR u.user_login LIKE '%firstname lastname%' ) LIMIT 0, 10";
$expected = "SELECT u.* FROM `#user` WHERE 1 AND ( u.user_name LIKE '%firstname lastname%' OR u.user_login LIKE '%firstname lastname%' OR u.user_phone LIKE '%firstname lastname%' ) LIMIT 0, 10";
// Double-quotes.
$result = $this->ui->_modifyListQrySearch($listQry, '"firstname lastname"', $filterOptions, $tablePath, $tableFrom, $primaryName, $raw, $orderField, $qryAsc, $forceFrom, $qryFrom, $forceTo, $perPage, $qryField, $isfilter, $handleAction);
@@ -149,9 +151,31 @@
// Single quote as apostophie.
$result = $this->ui->_modifyListQrySearch($listQry, "burt's", $filterOptions, $tablePath, $tableFrom, $primaryName, $raw, $orderField, $qryAsc, $forceFrom, $qryFrom, $forceTo, $perPage, $qryField, $isfilter, $handleAction);
$expected = "SELECT u.* FROM `#user` WHERE 1 AND ( u.user_name LIKE '%burt's%' OR u.user_login LIKE '%burt's%' ) LIMIT 0, 10";
$expected = "SELECT u.* FROM `#user` WHERE 1 AND ( u.user_name LIKE '%burt's%' OR u.user_login LIKE '%burt's%' OR u.user_phone LIKE '%burt's%' ) LIMIT 0, 10";
$this::assertSame($expected, $result);
// Search Specific Field Test
$this->req->setAction('List');
$this->req->setQuery('searchquery', '5551234');
$this->ui->setRequest($this->req);
// Simulate a custom search handler for user_phone
$this->ui->handleListSearchfieldFilter = function ($field)
{
$search = $this->ui->getQuery('searchquery');
return "u.user_phone LIKE '%custom_phone_" . $search . "%'";
};
// Test custom search specifically for user_phone
$filterOptions = 'searchfield__user_phone';
$result = $this->ui->_modifyListQrySearch($listQry, '5551234', $filterOptions, $tablePath, $tableFrom, $primaryName, $raw, $orderField, $qryAsc, $forceFrom, $qryFrom, $forceTo, $perPage, $qryField, $isfilter, $handleAction);
// Expected result when the custom search handler is used
$expected = "SELECT u.* FROM `#user` WHERE 1 AND u.user_phone LIKE '%custom_phone_5551234%' LIMIT 0, 10";
$this::assertSame($expected, $result);
}
/*
public function testGetSortParent()