]> git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Preferences.php
Add unit tests on Preferences; refs #454
[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 false
72 );
73 }
74
75 /**
76 * Test preferences initialization
77 *
78 * @return void
79 */
80 public function testInstallInit()
81 {
82 $result = $this->_preferences->installInit(
83 'en_US',
84 'da_admin',
85 password_hash('da_secret', PASSWORD_BCRYPT)
86 );
87
88 $this->boolean($result)->isTrue();
89
90 //new object with values loaded from database to compare
91 $prefs = new \Galette\Core\Preferences($this->_zdb);
92
93 foreach ( $prefs->getDefaults() as $key=>$expected ) {
94 $value = $prefs->$key;
95
96 switch ( $key ) {
97 case 'pref_admin_login':
98 $this->variable($value)->isIdenticalTo('da_admin');
99 break;
100 case 'pref_admin_pass':
101 $pw_checked = password_verify('da_secret', $value);
102 $this->boolean($pw_checked)->isTrue();
103 break;
104 case 'pref_lang':
105 $this->variable($value)->isIdenticalTo('en_US');
106 break;
107 case 'pref_card_year':
108 $this->variable($value)->isIdenticalTo(date('Y'));
109 break;
110 default:
111 $this->variable($value)->isEqualTo($expected);
112 break;
113 }
114 }
115
116 //tru to set and get a non existent value
117 $prefs->doesnotexists = 'that *does* not exists.';
118 $false_result = $prefs->doesnotexists;
119 $this->boolean($false_result)->isFalse();
120
121 //change slogan
122 $slogan = 'One Galette to rule them all';
123 $prefs->pref_slogan = $slogan;
124 $check = $prefs->pref_slogan;
125 $this->string($check)->isIdenticalTo($slogan);
126
127 //change password
128 $new_pass = 'anoth3er_s3cr3t';
129 $prefs->pref_admin_pass = $new_pass;
130 $pass = $prefs->pref_admin_pass;
131 $pw_checked = password_verify($new_pass, $pass);
132 $this->boolean($pw_checked)->isTrue();
133
134 $this->_preferences->pref_nom = 'Galette';
135 $this->_preferences->pref_ville = 'Avignon';
136 $this->_preferences->pref_cp = '84000';
137 $this->_preferences->pref_adresse = 'Palais des Papes';
138 $this->_preferences->pref_adresse2 = 'Au milieu';
139 $this->_preferences->pref_pays = 'France';
140
141 $expected = "Galette\n\nPalais des Papes\nAu milieu\n84000 Avignon - France";
142 $address = $this->_preferences->getPostalAdress();
143
144 $this->variable($address)->isIdenticalTo($expected);
145 }
146
147 /**
148 * Test storage
149 *
150 * @return void
151 */
152 public function testPrefsStorage()
153 {
154 $slogan = $this->_preferences->pref_slogan;
155 $this->variable($slogan)->isEqualTo('');
156
157 $slogan = 'One Galette to rule them all';
158 $this->_preferences->pref_slogan = $slogan;
159 $result = $this->_preferences->store();
160
161 $this->boolean($result)->isTrue();
162
163 $prefs = new \galette\Core\Preferences($this->_zdb);
164 $check_slogan = $prefs->pref_slogan;
165 $this->variable($check_slogan)->isEqualTo($slogan);
166 }
167
168 /**
169 * Test fields names
170 *
171 * @return void
172 */
173 public function testFieldsNames()
174 {
175 $this->_preferences->load();
176 $fields_names = $this->_preferences->getFieldsNames();
177 $expected = array_keys($this->_preferences->getDefaults());
178
179 sort($fields_names);
180 sort($expected);
181
182 $this->array($fields_names)->isIdenticalTo($expected);
183 }
184
185 /**
186 * Test preferences updating when some are missing
187 *
188 * @return void
189 */
190 public function testUpdate()
191 {
192 $remove = array(
193 'pref_facebook',
194 'pref_viadeo'
195 );
196
197 $del = $this->_zdb->db->delete(
198 PREFIX_DB . \Galette\Core\Preferences::TABLE,
199 \Galette\Core\Preferences::PK . ' IN ("' . implode('", "', $remove) . '")'
200 );
201
202 $this->_preferences->load();
203 $fb = $this->_preferences->pref_facebook;
204 $viadeo = $this->_preferences->pref_viadeo;
205
206 $this->boolean($fb)->isFalse();
207 $this->boolean($viadeo)->isFalse();
208
209 $prefs = new \Galette\Core\Preferences($this->_zdb);
210 $fb = $prefs->pref_facebook;
211 $viadeo = $prefs->pref_viadeo;
212
213 $this->variable($fb)->isIdenticalTo('');
214 $this->variable($viadeo)->isIdenticalTo('');
215 }
216 }