Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
1,497 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,370 @@ | ||
<?php | ||
|
||
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ | ||
|
||
/** | ||
* Password tests | ||
* | ||
* PHP version 5 | ||
* | ||
* Copyright © 2020-2023 The Galette Team | ||
* | ||
* This file is part of Galette (http://galette.tuxfamily.org). | ||
* | ||
* Galette is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Galette is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Galette. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* @category Core | ||
* @package GaletteTests | ||
* | ||
* @author Johan Cwiklinski <johan@x-tnd.be> | ||
* @copyright 2020-2023 The Galette Team | ||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version | ||
* @link http://galette.tuxfamily.org | ||
* @since 2020-03-15 | ||
*/ | ||
|
||
namespace Galette\Core\test\units; | ||
|
||
use Galette\GaletteTestCase; | ||
|
||
/** | ||
* Password tests class | ||
* | ||
* @category Core | ||
* @name Password | ||
* @package GaletteTests | ||
* @author Johan Cwiklinski <johan@x-tnd.be> | ||
* @copyright 2020-2023 The Galette Team | ||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version | ||
* @link http://galette.tuxfamily.org | ||
* @since 2020-03-15 | ||
*/ | ||
class Links extends GaletteTestCase | ||
{ | ||
//private $pass = null; | ||
protected int $seed = 95842355; | ||
private \Galette\Core\Links $links; | ||
private array $ids = []; | ||
protected array $excluded_after_methods = ['testDuplicateLinkTarget']; | ||
|
||
/** | ||
* Set up tests | ||
* | ||
* @return void | ||
*/ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->initStatus(); | ||
$this->initContributionsTypes(); | ||
|
||
$this->links = new \Galette\Core\Links($this->zdb, false); | ||
$this->contrib = new \Galette\Entity\Contribution($this->zdb, $this->login); | ||
|
||
$this->adh = new \Galette\Entity\Adherent($this->zdb); | ||
$this->adh->setDependencies( | ||
$this->preferences, | ||
$this->members_fields, | ||
$this->history | ||
); | ||
} | ||
|
||
/** | ||
* Cleanup after testeach test method | ||
* | ||
* @return void | ||
*/ | ||
public function tearDown(): void | ||
{ | ||
$delete = $this->zdb->delete(\Galette\Entity\Contribution::TABLE); | ||
$delete->where(['info_cotis' => 'FAKER' . $this->seed]); | ||
$this->zdb->execute($delete); | ||
|
||
$delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE); | ||
$delete->where(['fingerprint' => 'FAKER' . $this->seed]); | ||
$this->zdb->execute($delete); | ||
|
||
$delete = $this->zdb->delete(\Galette\Core\Links::TABLE); | ||
$this->zdb->execute($delete); | ||
|
||
$this->cleanHistory(); | ||
parent::tearDown(); | ||
|
||
} | ||
|
||
/** | ||
* Test new Link generation | ||
* | ||
* @return void | ||
*/ | ||
public function testGenerateNewLink() | ||
{ | ||
$links = $this->links; | ||
$this->getMemberTwo(); | ||
$id = $this->adh->id; | ||
|
||
$res = $links->generateNewLink( | ||
\Galette\Core\Links::TARGET_MEMBERCARD, | ||
$id | ||
); | ||
|
||
$this->assertNotEmpty($res); | ||
|
||
$select = $this->zdb->select(\Galette\Core\Links::TABLE); | ||
$results = $this->zdb->execute($select); | ||
$this->assertSame($results->count(), 1); | ||
|
||
$this->assertSame( | ||
$links->isHashValid($res, 'phoarau@tele2.fr'), | ||
[ | ||
\Galette\Core\Links::TARGET_MEMBERCARD, | ||
$id | ||
] | ||
); | ||
|
||
$this->assertFalse($links->isHashValid($res, 'any@mail.com')); | ||
$this->assertFalse($links->isHashValid(base64_encode('sthingthatisnotahash'), 'phoarau@tele2.fr')); | ||
|
||
$this->createContribution(); | ||
$cid = $this->contrib->id; | ||
$res = $links->generateNewLink( | ||
\Galette\Core\Links::TARGET_INVOICE, | ||
$cid | ||
); | ||
|
||
$this->assertNotEmpty($res); | ||
$this->assertSame( | ||
$links->isHashValid($res, 'phoarau@tele2.fr'), | ||
[ | ||
\Galette\Core\Links::TARGET_INVOICE, | ||
$cid | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Test expired is invalid | ||
* | ||
* @return void | ||
*/ | ||
public function testExpiredValidate() | ||
{ | ||
$links = $this->links; | ||
$this->getMemberTwo(); | ||
$id = $this->adh->id; | ||
|
||
$res = $links->generateNewLink( | ||
\Galette\Core\Links::TARGET_MEMBERCARD, | ||
$id | ||
); | ||
|
||
$this->assertNotEmpty($res); | ||
|
||
$this->assertSame( | ||
$links->isHashValid($res, 'phoarau@tele2.fr'), | ||
[ | ||
\Galette\Core\Links::TARGET_MEMBERCARD, | ||
$id | ||
] | ||
); | ||
|
||
$select = $this->zdb->select(\Galette\Core\Links::TABLE); | ||
$results = $this->zdb->execute($select); | ||
$this->assertSame($results->count(), 1); | ||
|
||
$update = $this->zdb->update(\Galette\Core\Links::TABLE); | ||
$old_date = new \DateTime(); | ||
$old_date->sub(new \DateInterval('P2W')); | ||
$update | ||
->set(['creation_date' => $old_date->format('Y-m-d')]) | ||
->where(['hash' => base64_decode($res)]); | ||
$this->zdb->execute($update); | ||
|
||
$this->assertFalse($links->isHashValid($res, 'phoarau@tele2.fr')); | ||
} | ||
|
||
/** | ||
* Test cleanExpired | ||
* | ||
* @return void | ||
*/ | ||
public function testCleanExpired() | ||
{ | ||
$date = new \DateTime(); | ||
$date->sub(new \DateInterval('PT48H')); | ||
|
||
$insert = $this->zdb->insert(\Galette\Core\Links::TABLE); | ||
$insert->values( | ||
[ | ||
'hash' => 'Not expired link', | ||
'creation_date' => $date->format('Y-m-d'), | ||
'target' => \Galette\Core\Links::TARGET_MEMBERCARD, | ||
'id' => 1 | ||
] | ||
); | ||
$this->zdb->execute($insert); | ||
|
||
$date->sub(new \DateInterval('P1W')); | ||
$insert = $this->zdb->insert(\Galette\Core\Links::TABLE); | ||
$insert->values( | ||
[ | ||
'hash' => 'Expired link', | ||
'creation_date' => $date->format('Y-m-d'), | ||
'target' => \Galette\Core\Links::TARGET_MEMBERCARD, | ||
'id' => 2 | ||
] | ||
); | ||
$this->zdb->execute($insert); | ||
|
||
$select = $this->zdb->select(\Galette\Core\Links::TABLE); | ||
$results = $this->zdb->execute($select); | ||
$this->assertSame($results->count(), 2); | ||
|
||
$links = new \Galette\Core\Links($this->zdb, true); | ||
|
||
$results = $this->zdb->execute($select); | ||
$result = $results->current(); | ||
$this->assertSame($results->count(), 1); | ||
$this->assertSame($result['hash'], 'Not expired link'); | ||
} | ||
|
||
/** | ||
* Test duplicate target | ||
* | ||
* @return void | ||
*/ | ||
public function testDuplicateLinkTarget() | ||
{ | ||
$date = new \DateTime(); | ||
$date->sub(new \DateInterval('PT48H')); | ||
|
||
$insert = $this->zdb->insert(\Galette\Core\Links::TABLE); | ||
$insert->values( | ||
[ | ||
'hash' => 'Unique link', | ||
'creation_date' => $date->format('Y-m-d'), | ||
'target' => \Galette\Core\Links::TARGET_MEMBERCARD, | ||
'id' => 1 | ||
] | ||
); | ||
$this->zdb->execute($insert); | ||
|
||
$date->sub(new \DateInterval('PT1H')); | ||
|
||
$insert = $this->zdb->insert(\Galette\Core\Links::TABLE); | ||
$insert->values( | ||
[ | ||
'hash' => 'Unique link (but we did not know before)', | ||
'creation_date' => $date->format('Y-m-d'), | ||
'target' => \Galette\Core\Links::TARGET_MEMBERCARD, | ||
'id' => 1 | ||
] | ||
); | ||
|
||
$this->expectExceptionMessage('Duplicate entry'); | ||
$this->zdb->execute($insert); | ||
} | ||
|
||
/** | ||
* Create test contribution in database | ||
* | ||
* @return void | ||
*/ | ||
protected function createContribution() | ||
{ | ||
$now = new \DateTime(); // 2020-11-07 | ||
$begin_date = clone $now; | ||
$begin_date->sub(new \DateInterval('P1Y')); // 2019-11-07 | ||
$begin_date->sub(new \DateInterval('P6M')); // 2019-05-07 | ||
$begin_date->add(new \DateInterval('P13D')); // 2019-05-20 | ||
|
||
$due_date = clone $begin_date; | ||
$due_date->sub(new \DateInterval('P1D')); | ||
$due_date->add(new \DateInterval('P1Y')); | ||
|
||
$data = [ | ||
'id_adh' => $this->adh->id, | ||
'id_type_cotis' => 3, | ||
'montant_cotis' => 111, | ||
'type_paiement_cotis' => 6, | ||
'info_cotis' => 'FAKER' . $this->seed, | ||
'date_enreg' => $begin_date->format('Y-m-d'), | ||
'date_debut_cotis' => $begin_date->format('Y-m-d'), | ||
'date_fin_cotis' => $due_date->format('Y-m-d'), | ||
]; | ||
$this->createContrib($data); | ||
$this->checkContribExpected(); | ||
} | ||
|
||
/** | ||
* Check contributions expecteds | ||
* | ||
* @param Contribution $contrib Contribution instance, if any | ||
* @param array $new_expecteds Changes on expected values | ||
* | ||
* @return void | ||
*/ | ||
protected function checkContribExpected($contrib = null, $new_expecteds = []) | ||
{ | ||
if ($contrib === null) { | ||
$contrib = $this->contrib; | ||
} | ||
|
||
$begin_date = $contrib->raw_begin_date; | ||
|
||
$due_date = clone $begin_date; | ||
$due_date->sub(new \DateInterval('P1D')); | ||
$due_date->add(new \DateInterval('P1Y')); | ||
|
||
$this->assertInstanceOf('DateTime', $contrib->raw_date); | ||
$this->assertInstanceOf('DateTime', $contrib->raw_begin_date); | ||
$this->assertInstanceOf('DateTime', $contrib->raw_end_date); | ||
|
||
$expecteds = [ | ||
'id_adh' => "{$this->adh->id}", | ||
'id_type_cotis' => 3, | ||
'montant_cotis' => '111', | ||
'type_paiement_cotis' => '6', | ||
'info_cotis' => 'FAKER' . $this->seed, | ||
'date_fin_cotis' => $due_date->format('Y-m-d') | ||
]; | ||
$expecteds = array_merge($expecteds, $new_expecteds); | ||
|
||
$this->assertSame($contrib->raw_end_date->format('Y-m-d'), $expecteds['date_fin_cotis']); | ||
|
||
foreach ($expecteds as $key => $value) { | ||
$property = $this->contrib->fields[$key]['propname']; | ||
switch ($key) { | ||
case \Galette\Entity\ContributionsTypes::PK: | ||
$ct = $this->contrib->type; | ||
if ($ct instanceof \Galette\Entity\ContributionsTypes) { | ||
$this->assertSame((int)$ct->id, $value); | ||
} else { | ||
$this->assertSame($ct, $value); | ||
} | ||
break; | ||
default: | ||
$this->assertEquals($contrib->$property, $value, $property); | ||
break; | ||
} | ||
} | ||
|
||
//load member from db | ||
$this->adh = new \Galette\Entity\Adherent($this->zdb, $this->adh->id); | ||
//member is now up-to-date | ||
$this->assertSame($this->adh->getRowClass(), 'active-account cotis-late'); | ||
$this->assertSame($this->adh->due_date, $this->contrib->end_date); | ||
$this->assertFalse($this->adh->isUp2Date()); | ||
} | ||
} |
Oops, something went wrong.