]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/Status.php
78baced3108fb8d8f6d1f883f4249085510eef8c
[galette.git] / tests / Galette / Entity / tests / units / Status.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Status tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2018-2023 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Repository
28 * @package GaletteTests
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2018-2023 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since 2018-03-10
36 */
37
38 namespace Galette\Entity\test\units;
39
40 use PHPUnit\Framework\TestCase;
41 use Laminas\Db\Adapter\Adapter;
42
43 /**
44 * Status tests
45 *
46 * @category Entity
47 * @name Status
48 * @package GaletteTests
49 * @author Johan Cwiklinski <johan@x-tnd.be>
50 * @copyright 2018-2023 The Galette Team
51 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
52 * @link http://galette.tuxfamily.org
53 * @since 2018-04-15
54 */
55 class Status extends TestCase
56 {
57 private \Galette\Core\Db $zdb;
58 private array $remove = [];
59 private \Galette\Core\I18n $i18n;
60
61 /**
62 * Set up tests
63 *
64 * @return void
65 */
66 public function setUp(): void
67 {
68 $this->zdb = new \Galette\Core\Db();
69 $this->i18n = new \Galette\Core\I18n(
70 \Galette\Core\I18n::DEFAULT_LANG
71 );
72 }
73
74 /**
75 * Tear down tests
76 *
77 * @return void
78 */
79 public function tearDown(): void
80 {
81 if (TYPE_DB === 'mysql') {
82 $this->assertSame([], $this->zdb->getWarnings());
83 }
84 $this->deleteStatus();
85 }
86
87 /**
88 * Delete status
89 *
90 * @return void
91 */
92 private function deleteStatus()
93 {
94 if (is_array($this->remove) && count($this->remove) > 0) {
95 $delete = $this->zdb->delete(\Galette\Entity\Status::TABLE);
96 $delete->where->in(\Galette\Entity\Status::PK, $this->remove);
97 $this->zdb->execute($delete);
98 }
99
100 //Clean logs
101 $this->zdb->db->query(
102 'TRUNCATE TABLE ' . PREFIX_DB . \Galette\Core\History::TABLE,
103 \Laminas\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
104 );
105 }
106
107 /**
108 * Test status
109 *
110 * @return void
111 */
112 public function testStatus()
113 {
114 global $i18n; // globals :(
115 $i18n = $this->i18n;
116
117 $status = new \Galette\Entity\Status($this->zdb);
118
119 $this->assertSame(
120 -2,
121 $status->add('Active member', 81)
122 );
123
124 $this->assertTrue(
125 $status->add('Test status', 81)
126 );
127
128 $select = $this->zdb->select(\Galette\Core\L10n::TABLE);
129 $select->where(
130 array(
131 'text_orig' => 'Test status'
132 )
133 );
134 $results = $this->zdb->execute($select);
135 $result = (array)$results->current();
136
137 $this->assertSame(
138 'Test status',
139 $result['text_orig']
140 );
141
142 $this->remove[] = $status->id;
143 $id = $status->id;
144
145 $this->assertSame(
146 \Galette\Entity\Entitled::ID_NOT_EXITS,
147 $status->update(42, 'Active member', 81)
148 );
149
150 $this->assertTrue(
151 $status->update($id, 'Tested status', 81)
152 );
153
154 $this->assertSame(
155 'Tested status',
156 $status->getLabel($id)
157 );
158
159 $select = $this->zdb->select(\Galette\Core\L10n::TABLE);
160 $select->where(
161 array(
162 'text_orig' => 'Tested status'
163 )
164 );
165 $results = $this->zdb->execute($select);
166 $result = (array)$results->current();
167
168 $this->assertSame(
169 'Tested status',
170 $result['text_orig']
171 );
172
173 $this->assertSame(
174 \Galette\Entity\Entitled::ID_NOT_EXITS,
175 $status->delete(42)
176 );
177
178 $this->expectException(\RuntimeException::class);
179 $this->expectExceptionMessage('You cannot delete default status!');
180 $status->delete($status::DEFAULT_STATUS);
181
182 $this->assertTrue(
183 $status->delete($id)
184 );
185
186 $select = $this->zdb->select(\Galette\Core\L10n::TABLE);
187 $select->where(
188 array(
189 'text_orig' => 'Tested status'
190 )
191 );
192 $results = $this->zdb->execute($select);
193 $this->assertSame(0, $results->count());
194 }
195
196 /**
197 * Test getList
198 *
199 * @return void
200 */
201 public function testGetList()
202 {
203 $status = new \Galette\Entity\Status($this->zdb);
204
205 $list = $status->getList();
206 $this->assertCount(10, $list);
207
208 if ($this->zdb->isPostgres()) {
209 $select = $this->zdb->select($status::TABLE . '_id_seq');
210 $select->columns(['last_value']);
211 $results = $this->zdb->execute($select);
212 $result = $results->current();
213 $this->assertGreaterThanOrEqual(10, $result->last_value, 'Incorrect status sequence');
214
215 $this->zdb->db->query(
216 'SELECT setval(\'' . PREFIX_DB . $status::TABLE . '_id_seq\', 1)',
217 Adapter::QUERY_MODE_EXECUTE
218 );
219 }
220
221 //reinstall status
222 $status->installInit();
223
224 $list = $status->getList();
225 $this->assertCount(10, $list);
226
227 if ($this->zdb->isPostgres()) {
228 $select = $this->zdb->select($status::TABLE . '_id_seq');
229 $select->columns(['last_value']);
230 $results = $this->zdb->execute($select);
231 $result = $results->current();
232 $this->assertGreaterThanOrEqual(10, $result->last_value, 'Incorrect status sequence ' . $result->last_value);
233 }
234 }
235 }