From a67876bce2f94a634b159f5092b7aac5f27a840d Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Mon, 27 Mar 2023 22:05:22 +0200 Subject: [PATCH] Add tests Fix default value Typo --- galette/lib/Galette/Entity/Adherent.php | 2 +- .../lib/Galette/Filters/ContributionsList.php | 2 +- .../Repository/tests/units/Contributions.php | 265 +++++++++++++++++ .../Repository/tests/units/Transactions.php | 270 ++++++++++++++++++ 4 files changed, 537 insertions(+), 2 deletions(-) create mode 100644 tests/Galette/Repository/tests/units/Contributions.php create mode 100644 tests/Galette/Repository/tests/units/Transactions.php diff --git a/galette/lib/Galette/Entity/Adherent.php b/galette/lib/Galette/Entity/Adherent.php index dd58302ca..1d1ba50b1 100644 --- a/galette/lib/Galette/Entity/Adherent.php +++ b/galette/lib/Galette/Entity/Adherent.php @@ -183,7 +183,7 @@ class Adherent private $_groups = []; private $_managed_groups = []; private $_parent; - private $_children; + private $_children = []; private $_duplicate = false; private $_socials; private $_number; diff --git a/galette/lib/Galette/Filters/ContributionsList.php b/galette/lib/Galette/Filters/ContributionsList.php index 4c43447d8..2e341df53 100644 --- a/galette/lib/Galette/Filters/ContributionsList.php +++ b/galette/lib/Galette/Filters/ContributionsList.php @@ -155,7 +155,7 @@ class ContributionsList extends Pagination /** * Global getter method * - * @param string $name name of the property we want to retrive + * @param string $name name of the property we want to retrieve * * @return mixed the called property */ diff --git a/tests/Galette/Repository/tests/units/Contributions.php b/tests/Galette/Repository/tests/units/Contributions.php new file mode 100644 index 000000000..d7d268c2f --- /dev/null +++ b/tests/Galette/Repository/tests/units/Contributions.php @@ -0,0 +1,265 @@ +. + * + * @category Repository + * @package GaletteTests + * + * @author Johan Cwiklinski + * @copyright 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 + * @version SVN: $Id$ + * @link https://galette.eu + * @since 2023-03-27 + */ + +namespace Galette\Repository\test\units; + +use Galette\GaletteTestCase; + +/** + * Contributions repository tests + * + * @category Repository + * @name Contributions + * @package GaletteTests + * @author Johan Cwiklinski + * @copyright 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 https://galette.eu + * @since 2023-03-27 + */ +class Contributions extends GaletteTestCase +{ + protected int $seed = 20230327215258; + + /** + * Tear down tests + * + * @return void + */ + public function tearDown(): void + { + parent::tearDown(); + + $this->zdb = new \Galette\Core\Db(); + $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]); + $delete->where('parent_id IS NOT NULL'); + $this->zdb->execute($delete); + + $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE); + $delete->where(['fingerprint' => 'FAKER' . $this->seed]); + $this->zdb->execute($delete); + } + + /** + * Test getList + * + * @return void + */ + public function testGetList() + { + $this->logSuperAdmin(); + $contributions = new \Galette\Repository\Contributions($this->zdb, $this->login); + $list = $contributions->getList(true, null, false); + + $this->assertIsArray($list); + $this->assertCount(0, $list); + $this->assertNull($contributions->getCount()); + $this->assertNull($contributions->getSum()); + + $list = $contributions->getList(true, null, true); + $this->assertIsArray($list); + $this->assertCount(0, $list); + $this->assertSame(0, $contributions->getCount()); + $this->assertNull($contributions->getSum()); + $member2 = $this->getMemberTwo(); + $this->getMemberOne(); + $this->createContribution(); + + $list = $contributions->getList(true); + $this->assertIsArray($list); + $this->assertCount(1, $list); + $this->assertSame(92.0, $contributions->getSum()); + + //filters + $filters = new \Galette\Filters\ContributionsList(); + $filters->filtre_cotis_adh = $member2->id; + $contributions = new \Galette\Repository\Contributions($this->zdb, $this->login, $filters); + $list = $contributions->getList(true); + $this->assertCount(0, $list); + + $filters = new \Galette\Filters\ContributionsList(); + $filters->filtre_cotis_adh = $this->adh->id; + $contributions = new \Galette\Repository\Contributions($this->zdb, $this->login, $filters); + $list = $contributions->getList(true); + $this->assertCount(1, $list); + + $filters = new \Galette\Filters\ContributionsList(); + $filters->max_amount = 90; + $contributions = new \Galette\Repository\Contributions($this->zdb, $this->login, $filters); + $list = $contributions->getList(true); + $this->assertCount(0, $list); + + $filters = new \Galette\Filters\ContributionsList(); + $filters->max_amount = 95; + $contributions = new \Galette\Repository\Contributions($this->zdb, $this->login, $filters); + $list = $contributions->getList(true); + $this->assertCount(1, $list); + + $filters = new \Galette\Filters\ContributionsList(); + $filters->start_date_filter = $this->contrib->begin_date; + $contributions = new \Galette\Repository\Contributions($this->zdb, $this->login, $filters); + $list = $contributions->getList(true); + $this->assertCount(1, $list); + + $filters = new \Galette\Filters\ContributionsList(); + $filters->start_date_filter = $this->contrib->end_date; + $contributions = new \Galette\Repository\Contributions($this->zdb, $this->login, $filters); + $list = $contributions->getList(true); + $this->assertCount(0, $list); + + $filters = new \Galette\Filters\ContributionsList(); + $filters->date_field = \Galette\Filters\ContributionsList::DATE_END; + $filters->end_date_filter = $this->contrib->end_date; + $contributions = new \Galette\Repository\Contributions($this->zdb, $this->login, $filters); + $list = $contributions->getList(true); + $this->assertCount(1, $list); + + $filters = new \Galette\Filters\ContributionsList(); + $filters->date_field = \Galette\Filters\ContributionsList::DATE_RECORD; + $filters->start_date_filter = $this->contrib->date; + $contributions = new \Galette\Repository\Contributions($this->zdb, $this->login, $filters); + $list = $contributions->getList(true); + $this->assertCount(1, $list); + + $filters = new \Galette\Filters\ContributionsList(); + $filters->date_field = \Galette\Filters\ContributionsList::DATE_RECORD; + $filters->start_date_filter = $this->contrib->end_date; + $contributions = new \Galette\Repository\Contributions($this->zdb, $this->login, $filters); + $list = $contributions->getList(true); + $this->assertCount(0, $list); + + $filters = new \Galette\Filters\ContributionsList(); + $filters->payment_type_filter = $this->contrib->payment_type; + $contributions = new \Galette\Repository\Contributions($this->zdb, $this->login, $filters); + $list = $contributions->getList(true); + $this->assertCount(1, $list); + + //member with a contribution + $login = $this->getMockBuilder(\Galette\Core\Login::class) + ->setConstructorArgs(array($this->zdb, $this->i18n)) + ->onlyMethods(array('isLogged', 'isStaff', 'isAdmin', 'isSuperAdmin')) + ->getMock(); + + $login->method('isLogged')->willReturn(true); + $login->method('isStaff')->willReturn(false); + $login->method('isAdmin')->willReturn(false); + $login->method('isSuperAdmin')->willReturn(false); + $login->setId($this->adh->id); + $contributions = new \Galette\Repository\Contributions($this->zdb, $login); + $list = $contributions->getList(true); + $this->assertCount(1, $list); + + $filters = new \Galette\Filters\ContributionsList(); + $filters->date_field = \Galette\Filters\ContributionsList::DATE_END; + $filters->filtre_cotis_children = true; + $contributions = new \Galette\Repository\Contributions($this->zdb, $login, $filters); + $list = $contributions->getList(true); + $this->assertCount(1, $list); + + //member does not have any contribution + $login = $this->getMockBuilder(\Galette\Core\Login::class) + ->setConstructorArgs(array($this->zdb, $this->i18n)) + ->onlyMethods(array('isLogged', 'isStaff', 'isAdmin', 'isSuperAdmin')) + ->getMock(); + + $login->method('isLogged')->willReturn(true); + $login->method('isStaff')->willReturn(false); + $login->method('isAdmin')->willReturn(false); + $login->method('isSuperAdmin')->willReturn(false); + $login->setId($member2->id); + $contributions = new \Galette\Repository\Contributions($this->zdb, $login); + $list = $contributions->getList(true); + $this->assertCount(0, $list); + + //cannot load another simple member's contribution + $filters = new \Galette\Filters\ContributionsList(); + $filters->filtre_cotis_adh = $this->adh->id; + $contributions = new \Galette\Repository\Contributions($this->zdb, $login, $filters); + $list = $contributions->getList(true); + $this->assertCount(0, $list); + } + + /** + * Test getArrayList + * + * @return void + */ + public function testGetArrayList() + { + $this->logSuperAdmin(); + $contributions = new \Galette\Repository\Contributions($this->zdb, $this->login); + + $this->getMemberOne(); + $this->createContribution(); + + $list = $contributions->getArrayList([$this->contrib->id], true); + + $this->assertIsArray($list); + $this->assertCount(1, $list); + + $list = $contributions->getArrayList([$this->contrib->id], false); + $this->assertIsArray($list); + $this->assertCount(1, $list); + } + + /** + * Test remove + * + * @return void + */ + public function testRemove() + { + $this->logSuperAdmin(); + $contributions = new \Galette\Repository\Contributions($this->zdb, $this->login); + + $this->getMemberOne(); + $this->createContribution(); + + $list = $contributions->getList(true); + $this->assertCount(1, $list); + + $this->assertTrue($contributions->remove($this->contrib->id, $this->history)); + + $list = $contributions->getList(true); + $this->assertCount(0, $list); + } +} diff --git a/tests/Galette/Repository/tests/units/Transactions.php b/tests/Galette/Repository/tests/units/Transactions.php new file mode 100644 index 000000000..290ed1add --- /dev/null +++ b/tests/Galette/Repository/tests/units/Transactions.php @@ -0,0 +1,270 @@ +. + * + * @category Repository + * @package GaletteTests + * + * @author Johan Cwiklinski + * @copyright 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 + * @version SVN: $Id$ + * @link https://galette.eu + * @since 2023-03-28 + */ + +namespace Galette\Repository\test\units; + +use Galette\GaletteTestCase; + +/** + * Transactions repository tests + * + * @category Repository + * @name Transactions + * @package GaletteTests + * @author Johan Cwiklinski + * @copyright 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 https://galette.eu + * @since 2023-03-28 + */ +class Transactions extends GaletteTestCase +{ + protected int $seed = 20230328103438; + /** @var \Galette\Entity\Transaction */ + private \Galette\Entity\Transaction $transaction; + + /** + * Tear down tests + * + * @return void + */ + public function tearDown(): void + { + parent::tearDown(); + + $this->zdb = new \Galette\Core\Db(); + $delete = $this->zdb->delete(\Galette\Entity\Contribution::TABLE); + $delete->where(['info_cotis' => 'FAKER' . $this->seed]); + $this->zdb->execute($delete); + + //then, remove transactions + $delete = $this->zdb->delete(\Galette\Entity\Transaction::TABLE); + $delete->where(['trans_desc' => 'FAKER' . $this->seed]); + $this->zdb->execute($delete); + + $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE); + $delete->where(['fingerprint' => 'FAKER' . $this->seed]); + $delete->where('parent_id IS NOT NULL'); + $this->zdb->execute($delete); + + $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE); + $delete->where(['fingerprint' => 'FAKER' . $this->seed]); + $this->zdb->execute($delete); + } + + /** + * Set up tests + * + * @return void + */ + public function setUp(): void + { + parent::setUp(); + $this->initContributionsTypes(); + + $this->contrib = new \Galette\Entity\Contribution($this->zdb, $this->login); + $this->transaction = new \Galette\Entity\Transaction($this->zdb, $this->login); + + $this->adh = new \Galette\Entity\Adherent($this->zdb); + $this->adh->setDependencies( + $this->preferences, + $this->members_fields, + $this->history + ); + } + + /** + * Create test transactions in database + * + * @return void + */ + private function createTransaction() + { + $date = new \DateTime(); + $data = [ + 'id_adh' => $this->adh->id, + 'trans_date' => $date->format('Y-m-d'), + 'trans_amount' => 92, + 'trans_desc' => 'FAKER' . $this->seed + ]; + + $this->transaction = new \Galette\Entity\Transaction($this->zdb, $this->login); + $check = $this->transaction->check($data, [], []); + if (is_array($check)) { + var_dump($check); + } + $this->assertTrue($check); + + $store = $this->transaction->store($this->history); + $this->assertTrue($store); + } + + /** + * Test getList + * + * @return void + */ + public function testGetList() + { + $this->logSuperAdmin(); + $transactions = new \Galette\Repository\Transactions($this->zdb, $this->login); + $list = $transactions->getList(true, null, false); + + $this->assertIsArray($list); + $this->assertCount(0, $list); + $this->assertNull($transactions->getCount()); + + $list = $transactions->getList(true, null, true); + + $this->assertIsArray($list); + $this->assertCount(0, $list); + $this->assertSame(0, $transactions->getCount()); + + $member2 = $this->getMemberTwo(); + $this->getMemberOne(); + $this->createTransaction(); + + $list = $transactions->getList(true); + $this->assertIsArray($list); + $this->assertCount(1, $list); + + $filters = new \Galette\Filters\TransactionsList(); + $filters->filtre_cotis_adh = $member2->id; + $transactions = new \Galette\Repository\Transactions($this->zdb, $this->login, $filters); + $list = $transactions->getList(true); + $this->assertCount(0, $list); + + $filters = new \Galette\Filters\TransactionsList(); + $filters->filtre_cotis_adh = $this->adh->id; + $transactions = new \Galette\Repository\Transactions($this->zdb, $this->login, $filters); + $list = $transactions->getList(true); + $this->assertCount(1, $list); + + $filters = new \Galette\Filters\TransactionsList(); + $filters->start_date_filter = $this->transaction->date; + $transactions = new \Galette\Repository\Transactions($this->zdb, $this->login, $filters); + $list = $transactions->getList(true); + $this->assertCount(1, $list); + + $odate = new \DateTime($this->transaction->date); + $odate->modify('+10 day'); + $filters = new \Galette\Filters\TransactionsList(); + $filters->start_date_filter = $odate->format('Y-m-d'); + $transactions = new \Galette\Repository\Transactions($this->zdb, $this->login, $filters); + $list = $transactions->getList(true); + $this->assertCount(0, $list); + + $filters = new \Galette\Filters\TransactionsList(); + $filters->end_date_filter = $this->transaction->date; + $transactions = new \Galette\Repository\Transactions($this->zdb, $this->login, $filters); + $list = $transactions->getList(true); + $this->assertCount(1, $list); + + $odate = new \DateTime($this->transaction->date); + $odate->modify('-10 day'); + $filters = new \Galette\Filters\TransactionsList(); + $filters->end_date_filter = $odate->format('Y-m-d'); + $transactions = new \Galette\Repository\Transactions($this->zdb, $this->login, $filters); + $list = $transactions->getList(true); + $this->assertCount(0, $list); + + //member with a transaction + $login = $this->getMockBuilder(\Galette\Core\Login::class) + ->setConstructorArgs(array($this->zdb, $this->i18n)) + ->onlyMethods(array('isLogged', 'isStaff', 'isAdmin', 'isSuperAdmin')) + ->getMock(); + + $login->method('isLogged')->willReturn(true); + $login->method('isStaff')->willReturn(false); + $login->method('isAdmin')->willReturn(false); + $login->method('isSuperAdmin')->willReturn(false); + $login->setId($this->adh->id); + $transactions = new \Galette\Repository\Transactions($this->zdb, $login); + $list = $transactions->getList(true); + $this->assertCount(1, $list); + + $filters = new \Galette\Filters\TransactionsList(); + $filters->filtre_cotis_children = true; + $transactions = new \Galette\Repository\Transactions($this->zdb, $login, $filters); + $list = $transactions->getList(true); + $this->assertCount(1, $list); + + //member does not have any transaction + $login = $this->getMockBuilder(\Galette\Core\Login::class) + ->setConstructorArgs(array($this->zdb, $this->i18n)) + ->onlyMethods(array('isLogged', 'isStaff', 'isAdmin', 'isSuperAdmin')) + ->getMock(); + + $login->method('isLogged')->willReturn(true); + $login->method('isStaff')->willReturn(false); + $login->method('isAdmin')->willReturn(false); + $login->method('isSuperAdmin')->willReturn(false); + $login->setId($member2->id); + $transactions = new \Galette\Repository\Transactions($this->zdb, $login); + $list = $transactions->getList(true); + $this->assertCount(0, $list); + + //cannot load another simple member's transactions + $filters = new \Galette\Filters\TransactionsList(); + $filters->filtre_cotis_adh = $this->adh->id; + $transactions = new \Galette\Repository\Transactions($this->zdb, $login, $filters); + $list = $transactions->getList(true); + $this->assertCount(0, $list); + } + + /** + * Test remove + * + * @return void + */ + public function testRemove() + { + $this->logSuperAdmin(); + $transactions = new \Galette\Repository\Transactions($this->zdb, $this->login); + + $this->getMemberOne(); + $this->createTransaction(); + + $list = $transactions->getList(true); + $this->assertCount(1, $list); + + $this->assertTrue($transactions->remove($this->transaction->id, $this->history)); + + $list = $transactions->getList(true); + $this->assertCount(0, $list); + } +} -- 2.39.2