]> git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Preferences.php
Try to fix travis TU fail
[galette.git] / tests / Galette / Core / tests / units / Preferences.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Preferences tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2013 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 Core
28 * @package GaletteTests
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2013 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 2013-10-19
36 */
37
38 namespace Galette\Core\test\units;
39
40 use \atoum;
41
42 /**
43 * Preferences tests class
44 *
45 * @category Core
46 * @name Preferences
47 * @package GaletteTests
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2013 The Galette Team
50 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
51 * @link http://galette.tuxfamily.org
52 * @since 2013-01-13
53 */
54 class Preferences extends atoum
55 {
56 private $_preferences = null;
57 private $_zdb;
58
59 /**
60 * Set up tests
61 *
62 * @param string $testMethod Calling method
63 *
64 * @return void
65 */
66 public function beforeTestMethod($testMethod)
67 {
68 $this->_zdb = new \Galette\Core\Db();
69 $this->_preferences = new \Galette\Core\Preferences(
70 $this->_zdb
71 );
72 }
73
74 /**
75 * Test preferences initialization
76 *
77 * @return void
78 */
79 public function testInstallInit()
80 {
81 $result = $this->_preferences->installInit(
82 'en_US',
83 'da_admin',
84 password_hash('da_secret', PASSWORD_BCRYPT)
85 );
86 $this->boolean($result)->isTrue();
87
88 //new object with values loaded from database to compare
89 $prefs = new \Galette\Core\Preferences($this->_zdb);
90
91 foreach ( $prefs->getDefaults() as $key=>$expected ) {
92 $value = $prefs->$key;
93
94 switch ( $key ) {
95 case 'pref_admin_login':
96 $this->variable($value)->isIdenticalTo('da_admin');
97 break;
98 case 'pref_admin_pass':
99 $pw_checked = password_verify('da_secret', $value);
100 $this->boolean($pw_checked)->isTrue();
101 break;
102 case 'pref_lang':
103 $this->variable($value)->isIdenticalTo('en_US');
104 break;
105 case 'pref_card_year':
106 $this->variable($value)->isIdenticalTo(date('Y'));
107 break;
108 default:
109 if ( TYPE_DB === \Galette\Core\Db::PGSQL ) {
110 if ( $value === 'f' ) {
111 $value = false;
112 }
113 }
114 $this->variable($value)->isEqualTo($expected);
115 break;
116 }
117 }
118
119 //tru to set and get a non existent value
120 $prefs->doesnotexists = 'that *does* not exists.';
121 $false_result = $prefs->doesnotexists;
122 $this->boolean($false_result)->isFalse();
123
124 //change slogan
125 $slogan = 'One Galette to rule them all';
126 $prefs->pref_slogan = $slogan;
127 $check = $prefs->pref_slogan;
128 $this->string($check)->isIdenticalTo($slogan);
129
130 //change password
131 $new_pass = 'anoth3er_s3cr3t';
132 $prefs->pref_admin_pass = $new_pass;
133 $pass = $prefs->pref_admin_pass;
134 $pw_checked = password_verify($new_pass, $pass);
135 $this->boolean($pw_checked)->isTrue();
136
137 $this->_preferences->pref_nom = 'Galette';
138 $this->_preferences->pref_ville = 'Avignon';
139 $this->_preferences->pref_cp = '84000';
140 $this->_preferences->pref_adresse = 'Palais des Papes';
141 $this->_preferences->pref_adresse2 = 'Au milieu';
142 $this->_preferences->pref_pays = 'France';
143
144 $expected = "Galette\n\nPalais des Papes\nAu milieu\n84000 Avignon - France";
145 $address = $this->_preferences->getPostalAdress();
146
147 $this->variable($address)->isIdenticalTo($expected);
148
149 $slogan = $this->_preferences->pref_slogan;
150 $this->variable($slogan)->isEqualTo('');
151
152 $slogan = 'One Galette to rule them all';
153 $this->_preferences->pref_slogan = $slogan;
154 $result = $this->_preferences->store();
155
156 $this->boolean($result)->isTrue();
157
158 $prefs = new \Galette\Core\Preferences($this->_zdb);
159 $check_slogan = $prefs->pref_slogan;
160 $this->variable($check_slogan)->isEqualTo($slogan);
161
162 //reset database value...
163 $this->_preferences->pref_slogan = '';
164 $this->_preferences->store();
165 }
166
167 /**
168 * Test fields names
169 *
170 * @return void
171 */
172 public function testFieldsNames()
173 {
174 $this->_preferences->load();
175 $fields_names = $this->_preferences->getFieldsNames();
176 $expected = array_keys($this->_preferences->getDefaults());
177
178 sort($fields_names);
179 sort($expected);
180
181 $this->array($fields_names)->isIdenticalTo($expected);
182 }
183
184 /**
185 * Test preferences updating when some are missing
186 *
187 * @return void
188 */
189 public function testUpdate()
190 {
191 $delete = $this->_zdb->delete(\Galette\Core\Preferences::TABLE);
192 $delete->where(
193 array(
194 \Galette\Core\Preferences::PK => 'pref_facebook'
195 )
196 );
197 $this->_zdb->execute($delete);
198
199 $delete = $this->_zdb->delete(\Galette\Core\Preferences::TABLE);
200 $delete->where(
201 array(
202 \Galette\Core\Preferences::PK => 'pref_viadeo'
203 )
204 );
205 $this->_zdb->execute($delete);
206
207 $this->_preferences->load();
208 $fb = $this->_preferences->pref_facebook;
209 $viadeo = $this->_preferences->pref_viadeo;
210
211 $this->boolean($fb)->isFalse();
212 $this->boolean($viadeo)->isFalse();
213
214 $prefs = new \Galette\Core\Preferences($this->_zdb);
215 $fb = $prefs->pref_facebook;
216 $viadeo = $prefs->pref_viadeo;
217
218 $this->variable($fb)->isIdenticalTo('');
219 $this->variable($viadeo)->isIdenticalTo('');
220 }
221
222 /**
223 * Test public pages visibility
224 *
225 * @return void
226 */
227 public function testPublicPagesVisibility()
228 {
229 $this->_preferences->load();
230
231 $visibility = $this->_preferences->pref_publicpages_visibility;
232 $this->variable($visibility)->isEqualTo(
233 \Galette\Core\Preferences::PUBLIC_PAGES_VISIBILITY_RESTRICTED
234 );
235
236 $anon_login = new \Galette\Core\Login();
237
238 $admin_login = new \mock\Galette\Core\Login();
239 $this->calling($admin_login)->isAdmin = true;
240
241 $user_login = new \mock\Galette\Core\Login();
242 $this->calling($user_login)->isUp2Date = true;
243
244 $visible = $this->_preferences->showPublicPages($anon_login);
245 $this->boolean($visible)->isFalse();
246
247 $visible = $this->_preferences->showPublicPages($admin_login);
248 $this->boolean($visible)->isTrue();
249
250 $visible = $this->_preferences->showPublicPages($user_login);
251 $this->boolean($visible)->isTrue();
252
253 $this->_preferences->pref_publicpages_visibility
254 = \Galette\Core\Preferences::PUBLIC_PAGES_VISIBILITY_PUBLIC;
255
256 $visible = $this->_preferences->showPublicPages($anon_login);
257 $this->boolean($visible)->isTrue();
258
259 $visible = $this->_preferences->showPublicPages($admin_login);
260 $this->boolean($visible)->isTrue();
261
262 $visible = $this->_preferences->showPublicPages($user_login);
263 $this->boolean($visible)->isTrue();
264
265 $this->_preferences->pref_publicpages_visibility
266 = \Galette\Core\Preferences::PUBLIC_PAGES_VISIBILITY_PRIVATE;
267
268 $visible = $this->_preferences->showPublicPages($anon_login);
269 $this->boolean($visible)->isFalse();
270
271 $visible = $this->_preferences->showPublicPages($admin_login);
272 $this->boolean($visible)->isTrue();
273
274 $visible = $this->_preferences->showPublicPages($user_login);
275 $this->boolean($visible)->isFalse();
276 }
277 }