Setup Tests - reset Admin_Config
[tine20] / tests / setup / Setup / JsonTest.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  *
5  * @package     Setup
6  * @license     http://www.gnu.org/licenses/agpl.html
7  * @copyright   Copyright (c) 2008-2011 Metaways Infosystems GmbH (http://www.metaways.de)
8  * @author      Philipp Schüle <p.schuele@metaways.de>
9  *
10  */
11
12 /**
13  * Test helper
14  */
15 require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'TestHelper.php';
16
17 /**
18  * Test class for Tinebase_Group
19  */
20 class Setup_JsonTest extends PHPUnit_Framework_TestCase
21 {
22     /**
23      * @var Setup_Frontend_Json
24      */
25     protected $_json;
26     
27     /**
28      * Authentication data as stored in config before a test runs.
29      * Needed to restore originakl state after a test ran.
30      * @see setUp()
31      * @see teardown()
32      *
33      * @var array
34      */
35     protected $_originalAuthenticationData;
36     
37     /**
38      * Runs the test methods of this class.
39      *
40      * @access public
41      * @static
42      */
43     public static function main()
44     {
45         $suite  = new PHPUnit_Framework_TestSuite('Tine 2.0 Setup Json Tests');
46         PHPUnit_TextUI_TestRunner::run($suite);
47   }
48
49     /**
50      * Sets up the fixture.
51      * This method is called before a test is executed.
52      *
53      * @access protected
54      */
55     protected function setUp()
56     {
57         $this->_json = new Setup_Frontend_Json();
58         $this->_originalAuthenticationData = $this->_json->loadAuthenticationData();
59     }
60
61     /**
62      * Tears down the fixture
63      * This method is called after a test is executed.
64      *
65      * @access protected
66      */
67     protected function tearDown()
68     {
69         $this->_installAllApps();
70     }
71     
72     /**
73      * testUninstallApplications
74      */
75     public function testUninstallApplications()
76     {
77         try {
78             $result = $this->_json->uninstallApplications(array('ActiveSync'));
79         } catch (Tinebase_Exception_NotFound $e) {
80             $this->_json->installApplications(array('ActiveSync'));
81             $result = $this->_json->uninstallApplications(array('ActiveSync'));
82         }
83               
84         $this->assertTrue($result['success']);
85     }
86
87     /**
88      * testUninstallTinebaseShouldThrowDependencyException
89      *
90      * tinebase uninstalls all other apps, too
91      */
92     public function testUninstallTinebaseShouldThrowDependencyException()
93     {
94         $result = $this->_json->uninstallApplications(array('Tinebase'));
95         $this->assertTrue($result['success']);
96         $this->assertTrue($result['setupRequired']);
97     }
98     
99     /**
100      * testSearchApplications
101      */
102     public function testSearchApplications()
103     {
104         $apps = $this->_json->searchApplications();
105         $this->assertGreaterThan(0, $apps['totalcount']);
106     }
107
108     /**
109      * testInstallApplications
110      */
111     public function testInstallApplications()
112     {
113         try {
114             $result = $this->_json->installApplications(array('ActiveSync'));
115         } catch (Exception $e) {
116             $this->_json->uninstallApplications(array('ActiveSync'));
117             $result = $this->_json->installApplications(array('ActiveSync'));
118         }
119         
120         $this->assertTrue($result['success']);
121     }
122
123     /**
124      * test update application
125      *
126      * @todo test real update process; currently this test case only tests updating an already uptodate application
127      */
128     public function testUpdateApplications()
129     {
130         $result = $this->_json->updateApplications(array('ActiveSync'));
131         $this->assertTrue($result['success']);
132     }
133
134     /**
135      * test env check
136      *
137      */
138     public function testEnvCheck()
139     {
140         $result = $this->_json->envCheck();
141         $this->assertTrue(isset($result['success']));
142     }
143
144     /**
145      * testLoginWithWrongUsernameAndPassword
146      */
147     public function testLoginWithWrongUsernameAndPassword()
148     {
149         $this->markTestSkipped('FIXME 0010994: fix broken setup test Setup_JsonTest::testLoginWithWrongUsernameAndPassword');
150
151         $result = $this->_json->login('unknown_user_xxyz', 'wrong_password');
152         $this->assertTrue(is_array($result));
153         $this->assertFalse($result['success']);
154         $this->assertTrue(isset($result['errorMessage']));
155     }
156     
157     /**
158      * test load config
159      */
160     public function testLoadConfig()
161     {
162         // register user first
163         Setup_Core::set(Setup_Core::USER, 'setupuser');
164         
165         $result = $this->_json->loadConfig();
166         
167         $this->assertTrue(is_array($result), 'result is no array');
168         $this->assertTrue(isset($result['database']), 'db config not found');
169         $this->assertGreaterThan(1, count($result));
170     }
171     
172     /**
173      * testGetRegistryData
174      */
175     public function testGetRegistryData()
176     {
177       $result = $this->_json->getRegistryData();
178       
179         $this->assertTrue(is_array($result));
180         $this->assertTrue(isset($result['configExists']));
181         $this->assertTrue(isset($result['configWritable']));
182         $this->assertTrue(isset($result['checkDB']));
183         $this->assertTrue(isset($result['setupChecks']));
184         $this->assertFalse($result['setupRequired']);
185         $this->assertTrue(is_array($result['authenticationData']));
186     }
187     
188     /**
189      * testLoadAuthenticationData
190      */
191     public function testLoadAuthenticationData()
192     {
193         $result = $this->_json->loadAuthenticationData();
194         
195         $this->assertTrue(is_array($result));
196         $this->assertTrue(array_key_exists('authentication', $result));
197         $this->assertTrue(array_key_exists('accounts', $result));
198         $authentication = $result['authentication'];
199         $this->assertContains($authentication['backend'], array(Tinebase_Auth::SQL, Tinebase_Auth::LDAP));
200         $this->assertTrue(is_array($authentication[Tinebase_Auth::SQL]));
201         $this->assertTrue(is_array($authentication[Tinebase_Auth::LDAP]));
202     }
203     
204     /**
205      * testSaveAuthenticationSql
206      */
207     public function testSaveAuthenticationSql()
208     {
209         $testAuthenticationData = $this->_json->loadAuthenticationData();
210
211         $testAuthenticationData['authentication']['backend'] = Tinebase_Auth::SQL;
212         $testAuthenticationData['authentication'][Tinebase_Auth::SQL]['adminLoginName'] = 'phpunit-admin';
213         $testAuthenticationData['authentication'][Tinebase_Auth::SQL]['adminPassword'] = 'phpunit-password';
214         $testAuthenticationData['authentication'][Tinebase_Auth::SQL]['adminPasswordConfirmation'] = 'phpunit-password';
215         
216         $this->_uninstallAllApps();
217         
218         $result = $this->_json->saveAuthentication($testAuthenticationData);
219         
220         $savedAuthenticationData = $this->_json->loadAuthenticationData();
221
222         $adminUser = Tinebase_User::getInstance()->getFullUserByLoginName('phpunit-admin');
223         $this->assertTrue($adminUser instanceof Tinebase_Model_User);
224         
225         //test if Tinebase stack was installed
226         $apps = $this->_json->searchApplications();
227         $baseApplicationStack = array('Tinebase', 'Admin', 'Addressbook');
228         foreach ($apps['results'] as $app) {
229             if ($app['install_status'] === 'uptodate' &&
230                 false !== ($index = array_search($app['name'], $baseApplicationStack))) {
231                 unset($baseApplicationStack[$index]);
232             }
233         }
234
235         $this->assertTrue(empty($baseApplicationStack), 'Assure that base application stack was installed after saving authentication');
236
237         $this->_uninstallAllApps(); //Ensure that all aps get re-installed with default username/password because some tests rely on these values
238     }
239
240     /**
241      * test load config
242      */
243     public function testSaveConfig()
244     {
245         $configData = $this->_json->loadConfig();
246         
247         // add something to config
248         $configData['test'] = 'value';
249         $this->_json->saveConfig($configData);
250
251         // load
252         $result = $this->_json->loadConfig();
253         
254         // check
255         $this->assertTrue(isset($result['test']));
256         $this->assertEquals('value', $result['test']);
257         $this->assertEquals($configData['database'], $result['database']);
258     }
259     
260     /**
261      * testSavePasswordSettings
262      * 
263      * @see 0003008: add password policies
264      */
265     public function testSavePasswordSettings()
266     {
267         $testAuthenticationData = $this->_json->loadAuthenticationData();
268
269         $configs = array(
270             'changepw' => TRUE,
271             'pwPolicyActive' => TRUE,
272             'pwPolicyMinLength' => 1,
273             'pwPolicyMinWordChars' => 1,
274         );
275         foreach ($configs as $config => $value) {
276             $testAuthenticationData['password'][$config] = $value;
277         }
278         $result = $this->_json->saveAuthentication($testAuthenticationData);
279         
280         $this->assertTrue($result['success'], 'saveAuthentication unsuccessful');
281         
282         $testAuthenticationData = $this->_json->loadAuthenticationData();
283         $this->assertTrue(isset($testAuthenticationData['password']), 'pw settings not found: ' . print_r($testAuthenticationData, TRUE));
284         foreach ($configs as $config => $expected) {
285             $this->assertEquals($expected, $testAuthenticationData['password'][$config], 'pw setting ' . $config . ' not found: ' . print_r($testAuthenticationData['password'], TRUE));
286         }
287     }
288     
289     /**
290      * _uninstallAllApps helper
291      */
292     protected function _uninstallAllApps()
293     {
294         $installedApplications = Tinebase_Application::getInstance()->getApplications(NULL, 'id');
295         $installedApplications = $installedApplications->name;
296
297         $this->_json->uninstallApplications($installedApplications);
298
299         Tinebase_Core::unsetTinebaseId();
300         Tinebase_Group::unsetInstance();
301         Tinebase_Cache_PerRequest::getInstance()->reset();
302         Admin_Config::unsetInstance();
303     }
304     
305     /**
306      * _installAllApps helper
307      */
308     protected function _installAllApps()
309     {
310         Tinebase_Core::unsetTinebaseId();
311         Tinebase_Group::unsetInstance();
312         Tinebase_Cache_PerRequest::getInstance()->reset();
313         Admin_Config::unsetInstance();
314
315         $installableApplications = Setup_Controller::getInstance()->getInstallableApplications();
316         $installableApplications = array_keys($installableApplications);
317         $testCredentials = Setup_TestServer::getInstance()->getTestCredentials();
318         $this->_json->installApplications($installableApplications, array(
319             'adminLoginName'        => $testCredentials['username'],
320             'adminPassword'         => $testCredentials['password'],
321         ));
322     }
323 }