mirror of
https://github.com/humhub/humhub.git
synced 2025-01-17 22:28:51 +01:00
Enhance API tests (#5073)
This commit is contained in:
parent
ac555267aa
commit
5de5b74d7a
@ -21,3 +21,4 @@
|
||||
- Enh #5026: Tests for tokenized image urls in email message
|
||||
- Enh #5049: Required profile field should not be required in administration
|
||||
- Enh #5065: Add checking for php PCNTL extension
|
||||
- Enh #5073: Enhance API tests
|
||||
|
@ -113,9 +113,19 @@ class ApiTester extends BaseTester
|
||||
* @param string $url
|
||||
* @param array $jsonResults
|
||||
* @param array $paginationParams
|
||||
* @param array $urlParams
|
||||
*/
|
||||
public function seePaginationGetResponse($url, $jsonResults = [], $paginationParams = [])
|
||||
public function seePaginationGetResponse($url, $jsonResults = [], $paginationParams = [], $urlParams = [])
|
||||
{
|
||||
$encodedUrlParams = [];
|
||||
foreach ($urlParams as $paramKey => $paramValue) {
|
||||
$encodedUrlParams[] = $paramKey . '=' . urlencode($paramValue);
|
||||
}
|
||||
|
||||
if (!empty($encodedUrlParams)) {
|
||||
$url .= (strpos($url, '?') === false ? '?' : '&') . implode('&', $encodedUrlParams);
|
||||
}
|
||||
|
||||
$this->sendGet($url);
|
||||
$this->seePaginationResponseContainsJson($url, $jsonResults, $paginationParams);
|
||||
}
|
||||
@ -176,11 +186,13 @@ class ApiTester extends BaseTester
|
||||
'total' => $jsonResultsCount,
|
||||
'page' => 1,
|
||||
'pages' => $jsonResultsCount ? 1 : 0,
|
||||
'perPage' => 100,
|
||||
], $paginationParams);
|
||||
|
||||
$json['links'] = $this->getPaginationUrls($url, $json);
|
||||
$json['results'] = $jsonResults;
|
||||
|
||||
unset($json['perPage']);
|
||||
$this->seeSuccessResponseContainsJson($json);
|
||||
}
|
||||
|
||||
@ -188,21 +200,21 @@ class ApiTester extends BaseTester
|
||||
* Get pagination URLs, Used to check JSON response
|
||||
*
|
||||
* @param string $url
|
||||
* @param array $params Possible keys: 'page', 'pages'
|
||||
* @param array $params Possible keys: 'page', 'pages', 'perPage'
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getPaginationUrls($url, $params)
|
||||
{
|
||||
$links = [Link::REL_SELF => $this->getPaginationUrl($url, $params['page'])];
|
||||
$links = [Link::REL_SELF => $this->getPaginationUrl($url, $params['page'], $params['perPage'])];
|
||||
|
||||
if ($params['pages'] > 0) {
|
||||
$links[Pagination::LINK_FIRST] = $this->getPaginationUrl($url, 0);
|
||||
$links[Pagination::LINK_LAST] = $this->getPaginationUrl($url, $params['pages'] - 1);
|
||||
$links[Pagination::LINK_FIRST] = $this->getPaginationUrl($url, 0, $params['perPage']);
|
||||
$links[Pagination::LINK_LAST] = $this->getPaginationUrl($url, $params['pages'] - 1, $params['perPage']);
|
||||
if ($params['page'] > 1) {
|
||||
$links[Pagination::LINK_PREV] = $this->getPaginationUrl($url, $params['page'] - 1);
|
||||
$links[Pagination::LINK_PREV] = $this->getPaginationUrl($url, $params['page'] - 1, $params['perPage']);
|
||||
}
|
||||
if ($params['page'] < $params['pages'] - 1) {
|
||||
$links[Pagination::LINK_NEXT] = $this->getPaginationUrl($url, $params['page'] + 1);
|
||||
$links[Pagination::LINK_NEXT] = $this->getPaginationUrl($url, $params['page'] + 1, $params['perPage']);
|
||||
}
|
||||
}
|
||||
|
||||
@ -212,11 +224,12 @@ class ApiTester extends BaseTester
|
||||
/**
|
||||
* @param string $url
|
||||
* @param int $page
|
||||
* @param int $perPage
|
||||
* @return string
|
||||
*/
|
||||
protected function getPaginationUrl($url, $page = 1)
|
||||
protected function getPaginationUrl($url, $page = 1, $perPage = 100)
|
||||
{
|
||||
return '/api/v1/' . trim($url, '/') . '?page=' . (empty($page) ? 1 : $page) . '&per-page=100';
|
||||
return '/api/v1/' . trim($url, '/') . (strpos($url, '?') === false ? '?' : '&') . 'page=' . (empty($page) ? 1 : $page) . '&per-page=' . $perPage;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -7,6 +7,17 @@ use Yii;
|
||||
|
||||
class HumHubApiTestCest
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string $recordModelClass Class name of the model to find a record
|
||||
*/
|
||||
protected $recordModelClass;
|
||||
|
||||
/**
|
||||
* @var array $recordDefinitionFunction Function name to get definitions of a record
|
||||
*/
|
||||
protected $recordDefinitionFunction;
|
||||
|
||||
public function _before()
|
||||
{
|
||||
$this->enableRestModule();
|
||||
@ -31,4 +42,40 @@ class HumHubApiTestCest
|
||||
$enabledModules = Yii::$app->moduleManager->getEnabledModules();
|
||||
return isset($enabledModules['rest']);
|
||||
}
|
||||
|
||||
protected function getRecordDefinition(int $id, string $recordModelClass = null): array
|
||||
{
|
||||
if ($recordModelClass === null) {
|
||||
$recordModelClass = $this->recordModelClass;
|
||||
}
|
||||
|
||||
$record = $recordModelClass::findOne(['id' => $id]);
|
||||
|
||||
return ($record ? call_user_func($this->recordDefinitionFunction, $record) : []);
|
||||
}
|
||||
|
||||
protected function getRecordDefinitions(array $ids, string $recordModelClass = null, array $recordDefinitionFunction = null): array
|
||||
{
|
||||
if ($recordModelClass === null) {
|
||||
$recordModelClass = $this->recordModelClass;
|
||||
}
|
||||
|
||||
if ($recordDefinitionFunction === null) {
|
||||
$recordDefinitionFunction = $this->recordDefinitionFunction;
|
||||
}
|
||||
|
||||
$recordsQuery = $recordModelClass::find()->where(['IN', 'id', $ids]);
|
||||
|
||||
$records = [];
|
||||
foreach ($recordsQuery->all() as $record) {
|
||||
$records[$record->id] = call_user_func($recordDefinitionFunction, $record);
|
||||
}
|
||||
|
||||
$recordDefinitions = [];
|
||||
foreach ($ids as $id) {
|
||||
$recordDefinitions[] = isset($records[$id]) ? $records[$id] : null;
|
||||
}
|
||||
|
||||
return $recordDefinitions;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user