Commit 62d7a3fe authored by Karamel's avatar Karamel
Browse files

Fix the deprecated POST customers endpoint, add tests.

parent 1cbb6ce9
......@@ -99,6 +99,10 @@ function oldApiCustomer($ptApp, $response, $jsonCusts) {
// Fill the array.
$customers = [];
foreach ($structCusts as $strC) {
if ($strC['tariffArea'] == "0") {
// There is no tariff area in the form and Android sends shit.
$strC['tariffArea'] = null;
}
$cust = null;
if (!empty($strC['id'])) {
$cust = Customer::loadFromId($strC['id'], $ptApp->getDao());
......@@ -110,8 +114,23 @@ function oldApiCustomer($ptApp, $response, $jsonCusts) {
$customers[] = $cust;
// There is no expireDate in the form from Android, so ignore it.
}
return $response->withApiResult(APICaller::run($ptApp, 'customer', 'write',
$customers));
$res = APICaller::run($ptApp, 'customer', 'write', [$customers]);
if ($res->getStatus() != APIResult::STATUS_CALL_OK) {
return $response->withApiResult($res);
}
$ids = [];
if (is_array($res->getContent())) {
foreach ($res->getContent() as $cust) {
$ids[] = $cust->getId();
}
if (count($ids) == 1) {
$ids = $ids[0];
}
} else {
$ids = $res->getContent()->getId();
}
$newRes = APIResult::success($ids);
return $response->withApiResult($newRes);
}
$app->PATCH('/api/customer/{id}/balance/{balance}',
......
......@@ -39,6 +39,7 @@ use \Pasteque\Server\Model\TicketPayment;
use \Pasteque\Server\Model\User;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\Login;
use \Pasteque\Server\System\DAO\DAOCondition;
use \Pasteque\Server\System\DAO\DAOFactory;
use \PHPUnit\Framework\TestCase;
......@@ -131,4 +132,59 @@ class HttpCustomerTest extends TestCase
$this->assertEquals(404, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
}
/** Test the deprecated POST method. When a single customer is sent
* (even in an array), the result is the single id not in an array. */
public function testPostOldSingle() {
$cust = new Customer();
$cust->setDispName('New customer');
$struct = $cust->toStruct();
$struct['tariffArea'] = '0'; // Because Android does
unset($struct['expireDate']);
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/customer'));
curl_setopt($this->curl, CURLOPT_POSTFIELDS,
"customers=[" . json_encode($struct) . ']');
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$dbCust = $this->dao->search(Customer::class,
new DAOCondition('dispName', '=', $cust->getDispName()));
$this->assertEquals(1, count($dbCust));
$dbCust = $dbCust[0];
$jsResp = json_decode($resp, true);
$this->assertFalse(is_array($jsResp));
$this->assertEquals($dbCust->getId(), $jsResp);
}
/** Test the deprecated POST method. When multiple customers are sent,
* the result is the array of ids. */
public function testPostOldMultiple() {
$cust = new Customer();
$cust->setDispName('New customer');
$struct = $cust->toStruct();
$struct['tariffArea'] = '0'; // Because Android does
unset($struct['expireDate']);
$cust2 = new Customer();
$cust2->setDispName('New customer 2');
$struct2 = $cust2->toStruct();
$struct2['tariffArea'] = '0';
unset($struct2['expireDate']);
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/customer'));
curl_setopt($this->curl, CURLOPT_POSTFIELDS,
"customers=[" . json_encode($struct) . ','
. json_encode($struct2) . ']');
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$dbCusts = $this->dao->search(Customer::class,
new DAOCondition('dispName', '!=', $this->cust->getDispName()),
null, null, 'dispName');
$this->assertEquals(2, count($dbCusts));
$dbCust = $dbCusts[0];
$dbCust2 = $dbCusts[1];
$jsResp = json_decode($resp, true);
$this->assertTrue(is_array($jsResp));
$this->assertEquals($dbCust->getId(), $jsResp[0]);
$this->assertEquals($dbCust2->getId(), $jsResp[1]);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment