Skip to content
Snippets Groups Projects
Commit 5a62e3dc authored by Laurent's avatar Laurent
Browse files

add /api/user/renew-loan

parent e8c45cd6
Branches
Tags
2 merge requests!2660Master,!2654Patrons ws for mybibapp
Pipeline #4150 failed with stage
in 33 minutes and 35 seconds
......@@ -43,6 +43,28 @@ class Api_UserController extends ZendAfi_Controller_Action {
}
public function renewLoanAction() {
$user = Class_Users::getIdentity();
$cards = new Class_User_Cards($user);
$loan_id = $this->_getParam('id');
$status = $cards->renewLoan($loan_id);
$loan = $cards->getLoans()
->detect(function($loan) use ($loan_id)
{
return $loan->getId() == $loan_id;
});
if ($status['statut'] == true)
return $this->_helper->json(['status' => 'renewed',
'date_due' => $loan->getDateRetourISO8601()]);
return $this->_helper->json(['status' => 'error',
'error' => $status['erreur']]);
}
protected function _authenticate() {
if (Class_AdminVar_OAuthAcceptHTTP::shouldRejectRequest($this->_request))
return $this->_error($this->_('Protocole HTTPS obligatoire'), 403);
......
......@@ -32,6 +32,11 @@ class Class_WebService_SIGB_Emprunt extends Class_WebService_SIGB_ExemplaireOper
}
public function getDateRetourISO8601() {
return implode('-', array_reverse(explode('/', $this->getDateRetour())));
}
/**
* @param string $retour
* @return Class_WebService_SIGB_Emprunt
......
......@@ -34,7 +34,7 @@ class ZendAfi_View_Helper_Api_Loans extends Zend_View_Helper_Abstract {
'id' => $loan->getId(),
'title' => $loan->getTitre(),
'author' => $loan->getAuteur(),
'date_due' => implode('-', array_reverse(explode('/', $loan->getDateRetour()))),
'date_due' => $loan->getDateRetourISO8601(),
'loaned_by' => $loan->getUserFullName(),
'library' => $loan->getBibliotheque() ];
......
......@@ -20,13 +20,17 @@
*/
abstract class Scenario_MobileApplication_UserAccountTestCase extends AbstractControllerTestCase {
protected
$_storm_default_to_volatile = true;
$_storm_default_to_volatile = true,
$_potter,
$_sigb;
public function setUp() {
parent::setUp();
$_SERVER['HTTPS'] = 'on';
Class_CommSigb::setInstance($this->_sigb = $this->mock());
$puppy = $this->fixture('Class_Users',
['id' => 345,
'pseudo' => 'Puppy',
......@@ -43,8 +47,8 @@ abstract class Scenario_MobileApplication_UserAccountTestCase extends AbstractCo
'client_id' => 'My mobile app',
'user' => $puppy]);
$potter = new Class_WebService_SIGB_Emprunt('12', new Class_WebService_SIGB_Exemplaire(123));
$potter
$this->_potter = new Class_WebService_SIGB_Emprunt('12', new Class_WebService_SIGB_Exemplaire(123));
$this->_potter
->setDateRetour('01/01/1974')
->setBibliotheque('Annecy')
->getExemplaire()
......@@ -59,12 +63,10 @@ abstract class Scenario_MobileApplication_UserAccountTestCase extends AbstractCo
->setDateRetour(date('d/m/Y', strtotime('tomorrow')))
->getExemplaire()->setTitre('Alice');
$emprunteur = (new Class_WebService_SIGB_Emprunteur(345, 'puppy'))
->empruntsAddAll([$potter, $alice]);
$puppy->setFicheSIGB(['fiche' => $emprunteur]);
$puppy->assertSave();
$puppy
->setFicheSigb(['fiche' => (new Class_WebService_SIGB_Emprunteur(345, 'puppy'))
->empruntsAddAll([$this->_potter, $alice])])
->assertSave();
ZendAfi_Auth::getInstance()->clearIdentity();
}
......@@ -127,6 +129,60 @@ class Scenario_MobileApplication_UserAccountLoansWithTokenTest extends Scenario_
class Scenario_MobileApplication_UserAccountRenewTest extends Scenario_MobileApplication_UserAccountTestCase {
/** @test */
public function resultShouldAnswerSuccess() {
$this->_sigb->whenCalled('prolongerPret')
->answers(['statut' => true,
'erreur' => '']);
$this->_potter->setDateRetour('2018-02-17');
$this->dispatch('/api/user/renew-loan/id/345_12',
true,
["Authorization" => "Bearer nonos" ,
"Content-Type" => "application/json"]);
$this->assertEquals(['status' => 'renewed',
'date_due' => '2018-02-17'],
json_decode($this->_response->getBody(), true));
}
public function resultShouldAnswerErrorOnSIGBError() {
$this->_sigb->whenCalled('prolongerPret')
->answers(['statut' => false,
'erreur' => 'could not renew']);
$this->dispatch('/api/user/renew-loan/id/345_12',
true,
["Authorization" => "Bearer nonos" ,
"Content-Type" => "application/json"]);
$this->assertEquals(['status' => 'error',
'error' => 'could not renew'],
json_decode($this->_response->getBody(), true));
}
/** @test */
public function resultShouldAnswerErrorOnLoanNotFound() {
$this->dispatch('/api/user/renew-loan/id/666_12',
true,
["Authorization" => "Bearer nonos" ,
"Content-Type" => "application/json"]);
$this->assertEquals(['status' => 'error',
'error' => 'Prêt introuvable'],
json_decode($this->_response->getBody(), true));
}
}
class Scenario_MobileApplication_UserAccountLoansWithoutTokenTest extends Scenario_MobileApplication_UserAccountTestCase {
/** @test */
public function withoutAuthorizationShouldAnswerInvalidRequest() {
......@@ -209,13 +265,13 @@ class Scenario_MobileApplication_UserAccountLoansWithoutTokenTest extends Scenar
"Content-Type" => "application/json"]);
$loans = json_decode($this->_response->getBody(), true);
$this->assertEquals(['title' => 'Potter',
'author' => 'J.K.R',
'date_due' => '1974-01-01',
'loaned_by' => 'puppy',
'library' => 'Annecy'
],
$loans['loans'][0]);
$this->assertArraySubset(['title' => 'Potter',
'author' => 'J.K.R',
'date_due' => '1974-01-01',
'loaned_by' => 'puppy',
'library' => 'Annecy'
],
$loans['loans'][0]);
}
}
......
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