0013236: Use filepicker for filemanager relations
[tine20] / tests / setup / Setup / ControllerTest.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-2013 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_ControllerTest extends PHPUnit_Framework_TestCase
21 {
22     /**
23      * @var Setup_Controller
24      */
25     protected $_uit = null;
26     
27     /**
28      * Sets up the fixture.
29      * This method is called before a test is executed.
30      *
31      * @access protected
32      */
33     protected function setUp()
34     {
35         $this->_uit = Setup_Controller::getInstance();
36     }
37
38     /**
39      * Tears down the fixture
40      * This method is called after a test is executed.
41      *
42      * @access protected
43      */
44     protected function tearDown()
45     {
46         $testCredentials = Setup_TestServer::getInstance()->getTestCredentials();
47         $this->_installAllApplications(array(
48             'defaultAdminGroupName' => 'Administrators',
49             'defaultUserGroupName'  => 'Users',
50             'adminLoginName'        => $testCredentials['username'],
51             'adminPassword'         => $testCredentials['password'],
52         ));
53     }
54
55     /**
56      * testLoginWithWrongUsernameAndPassword
57      */
58     public function testLoginWithWrongUsernameAndPassword()
59     {
60         $result = $this->_uit->login('unknown_user_xxyz', 'wrong_password');
61         $this->assertFalse($result);
62     }
63
64     /**
65      * test uninstall application and cache clearing
66      *
67      */
68     public function testUninstallApplications()
69     {
70         $cache = Tinebase_Core::getCache();
71         $cacheId = 'unittestcache';
72         $cache->save('something', $cacheId);
73         
74         try {
75             $result = $this->_uit->uninstallApplications(array('ActiveSync'));
76         } catch (Tinebase_Exception_NotFound $e) {
77             $this->_uit->installApplications(array('ActiveSync'));
78             $result = $this->_uit->uninstallApplications(array('ActiveSync'));
79         }
80         
81         $this->assertFalse($cache->test($cacheId), 'cache is not cleared');
82
83         $apps = $this->_uit->searchApplications();
84         
85         // get active sync
86         foreach ($apps['results'] as $app) {
87             if ($app['name'] == 'ActiveSync') {
88                 $activeSyncApp = $app;
89                 break;
90             }
91         }
92         
93         // checks
94         $this->assertTrue(isset($activeSyncApp));
95         $this->assertEquals('uninstalled', $activeSyncApp['install_status']);
96
97         // cleanup
98         $this->_uit->installApplications(array('ActiveSync'));
99     }
100     
101     /**
102      * testInstallAdminAccountOptions
103      */
104     public function testInstallAdminAccountOptions()
105     {
106         $this->_uninstallAllApplications();
107         $this->_uit->installApplications(array('Tinebase'), array('adminLoginName' => 'phpunit-admin', 'adminPassword' => 'phpunit-password'));
108         $adminUser = Tinebase_User::getInstance()->getFullUserByLoginName('phpunit-admin');
109         $this->assertTrue($adminUser instanceof Tinebase_Model_User);
110         
111         $this->assertNull(Tinebase_Auth::getBackendConfiguration('adminLoginName'));
112         $this->assertNull(Tinebase_Auth::getBackendConfiguration('adminPassword'));
113         $this->assertNull(Tinebase_Auth::getBackendConfiguration('adminConfirmation'));
114         
115         // cleanup
116         $this->_uninstallAllApplications();
117     }
118     
119     /**
120      * testSaveAuthenticationRedirectSettings
121      */
122     public function testSaveAuthenticationRedirectSettings()
123     {
124         $originalRedirectSettings = array(
125             Tinebase_Config::REDIRECTURL => Tinebase_Config::getInstance()->get(Tinebase_Config::REDIRECTURL, ''),
126             Tinebase_Config::REDIRECTTOREFERRER => Tinebase_Config::getInstance()->get(Tinebase_Config::REDIRECTTOREFERRER, FALSE)
127         );
128          
129         $newRedirectSettings = array(
130             Tinebase_Config::REDIRECTURL => 'http://tine20.org',
131             Tinebase_Config::REDIRECTTOREFERRER => TRUE
132         );
133         
134         $this->_uit->saveAuthentication(array('redirectSettings' => $newRedirectSettings));
135         
136         $storedRedirectSettings = array(
137             Tinebase_Config::REDIRECTURL => Tinebase_Config::getInstance()->get(Tinebase_Config::REDIRECTURL),
138             Tinebase_Config::REDIRECTTOREFERRER => Tinebase_Config::getInstance()->get(Tinebase_Config::REDIRECTTOREFERRER)
139         );
140         
141         $configNames = array(Tinebase_Config::REDIRECTURL, Tinebase_Config::REDIRECTTOREFERRER);
142         foreach ($configNames as $configName) {
143             $this->assertEquals($storedRedirectSettings[$configName], $newRedirectSettings[$configName],
144                 'new setting should match stored settings: ' . print_r($newRedirectSettings, TRUE));
145         }
146         
147         // test empty redirectUrl
148         $newRedirectSettings = array(
149             Tinebase_Config::REDIRECTURL => '',
150             Tinebase_Config::REDIRECTTOREFERRER => FALSE
151         );
152         
153         $this->_uit->saveAuthentication(array('redirectSettings' => $newRedirectSettings));
154         
155         $storedRedirectSettings = array(
156             Tinebase_Config::REDIRECTURL => Tinebase_Config::getInstance()->get(Tinebase_Config::REDIRECTURL),
157             Tinebase_Config::REDIRECTTOREFERRER => Tinebase_Config::getInstance()->get(Tinebase_Config::REDIRECTTOREFERRER)
158         );
159         
160         foreach ($configNames as $configName) {
161             $this->assertEquals($storedRedirectSettings[$configName], $newRedirectSettings[$configName],
162                 'new setting should match stored settings (with empty redirect URL): ' . print_r($newRedirectSettings, TRUE));
163         }
164         
165         $this->_uit->saveAuthentication($originalRedirectSettings);
166     }
167     
168     /**
169      * testInstallGroupNameOptions
170      */
171     public function testInstallGroupNameOptions()
172     {
173         $this->_uninstallAllApplications();
174         $testCredentials = Setup_TestServer::getInstance()->getTestCredentials();
175         $this->_installAllApplications(array(
176             'defaultAdminGroupName' => 'phpunit-admins',
177             'defaultUserGroupName'  => 'phpunit-users',
178             'adminLoginName'        => $testCredentials['username'],
179             'adminPassword'         => $testCredentials['password'],
180         ));
181         $adminUser = Tinebase_Core::get('currentAccount');
182         $this->assertEquals('phpunit-admins', Tinebase_User::getBackendConfiguration(Tinebase_User::DEFAULT_ADMIN_GROUP_NAME_KEY));
183         $this->assertEquals('phpunit-users', Tinebase_User::getBackendConfiguration(Tinebase_User::DEFAULT_USER_GROUP_NAME_KEY));
184
185         // setupuser and replication user should be disabled
186         foreach (array('setupuser', 'replicationuser') as $username) {
187             $systemUser = Tinebase_User::getInstance()->getFullUserByLoginName($username);
188             self::assertEquals(Tinebase_Model_User::ACCOUNT_STATUS_DISABLED, $systemUser->accountStatus,
189                 $username . ' should be disabled');
190         }
191     }
192     
193     /**
194      * test uninstall application
195      *
196      */
197     public function testUninstallTinebaseShouldThrowDependencyException()
198     {
199         $result = $this->_uit->uninstallApplications(array('Tinebase'));
200         $this->assertTrue($this->_uit->setupRequired());
201     }
202     
203     /**
204      * test search applications
205      *
206      */
207     public function testSearchApplications()
208     {
209         $apps = $this->_uit->searchApplications();
210         
211         $this->assertGreaterThan(0, $apps['totalcount']);
212         
213         // get active sync
214         foreach ($apps['results'] as $app) {
215             if ($app['name'] == 'ActiveSync') {
216                 $activeSyncApp = $app;
217                 break;
218             }
219         }
220         
221         // checks
222         $this->assertTrue(isset($activeSyncApp));
223         $this->assertTrue(isset($activeSyncApp['id']));
224         $this->assertEquals('uptodate', $activeSyncApp['install_status']);
225     }
226     
227     /**
228      * test install application
229      */
230     public function testInstallApplications()
231     {
232         try {
233             $result = $this->_uit->installApplications(array('ActiveSync'));
234         } catch (Exception $e) {
235             $this->_uit->uninstallApplications(array('ActiveSync'));
236             $result = $this->_uit->installApplications(array('ActiveSync'));
237         }
238                 
239         $apps = $this->_uit->searchApplications();
240         
241         // get active sync
242         foreach ($apps['results'] as $app) {
243             if ($app['name'] == 'ActiveSync') {
244                 $activeSyncApp = $app;
245                 break;
246             }
247         }
248         
249         
250         $applicationId = $activeSyncApp['id'];
251         // checks
252         $this->assertTrue(isset($activeSyncApp));
253         $this->assertTrue(isset($applicationId));
254         $this->assertEquals('enabled', $activeSyncApp['status']);
255         $this->assertEquals('uptodate', $activeSyncApp['install_status']);
256         
257         //check if user role has the right to run the recently installed app
258         $roles = Tinebase_Acl_Roles::getInstance();
259         $userRole = $roles->getRoleByName('user role');
260         $rights = $roles->getRoleRights($userRole->getId());
261         $hasRight = false;
262         foreach ($rights as $right) {
263             if ($right['application_id'] === $applicationId &&
264                 $right['right'] === 'run') {
265                 $hasRight = true;
266             }
267         }
268         $this->assertTrue($hasRight, 'User role has run right for recently installed app?');
269     }
270
271     /**
272      * test install applications from dump
273      *
274      * @see 0012728: install from (backup) dump
275      */
276     public function testInstallFromDump()
277     {
278         if ($this->_uit->isInstalled('Tinebase')) {
279             $this->_uninstallAllApplications();
280         }
281
282         $oldTinebaseId = '99a88a21e657b8365bf80ae867e9d06d1c355a39';
283         $options = array(
284             'backupDir' => dirname(__DIR__) . '/files/2017-02-27-11-42-25',
285             'db' => 1,
286         );
287         $result = $this->_uit->getInstance()->installFromDump($options);
288         $this->assertTrue($result);
289         $this->assertTrue($this->_uit->isInstalled('Addressbook'), 'Addressbook is not installed');
290         $tinebaseId = Tinebase_Application::getInstance()->getApplicationByName('Tinebase')->getId();
291         $this->assertNotEquals($oldTinebaseId, $tinebaseId);
292         $this->assertGreaterThan(40, Tinebase_Application::getInstance()->getApplicationTables($tinebaseId));
293
294         $this->_uninstallAllApplications();
295     }
296
297     /**
298      * test update application
299      *
300      * @todo test real update process; currently this test case only tests updating an already uptodate application
301      */
302     public function testUpdateApplications()
303     {
304         $applications = new Tinebase_Record_RecordSet('Tinebase_Model_Application');
305         $applications->addRecord(Tinebase_Application::getInstance()->getApplicationByName('ActiveSync'));
306         $result = $this->_uit->updateApplications($applications);
307         $this->assertTrue(is_array($result));
308     }
309
310     /**
311      * test env check
312      *
313      */
314     public function testEnvCheck()
315     {
316         $result = $this->_uit->checkRequirements();
317         
318         $this->assertTrue(isset($result['success']));
319         $this->assertGreaterThan(16, count($result['results']));
320     }
321     
322     /**
323      * uninstallAllApplications
324      */
325     protected function _uninstallAllApplications()
326     {
327         $installedApplications = Tinebase_Application::getInstance()->getApplications(NULL, 'id');
328         $this->_uit->uninstallApplications($installedApplications->name);
329         Tinebase_Core::unsetTinebaseId();
330         Tinebase_Group::unsetInstance();
331         Tinebase_Acl_Roles::unsetInstance();
332         Tinebase_Core::unsetUser();
333         Tinebase_Cache_PerRequest::getInstance()->reset();
334     }
335     
336     /**
337      * installAllApplications
338      *
339      * @param array $_options
340      * @throws Setup_Exception
341      */
342     protected function _installAllApplications($_options = null)
343     {
344         if (! $this->_uit) {
345             throw new Setup_Exception('could not run test, Setup_Controller init failed');
346         }
347
348         Tinebase_Core::unsetTinebaseId();
349         Tinebase_Group::unsetInstance();
350         Tinebase_Cache_PerRequest::getInstance()->reset();
351         $installableApplications = $this->_uit->getInstallableApplications();
352         $installableApplications = array_keys($installableApplications);
353         $this->_uit->installApplications($installableApplications, $_options);
354     }
355
356     /**
357      * @see 11574: backup should only dump structure of some tables
358      */
359     public function testGetBackupStructureOnlyTables()
360     {
361         require_once __DIR__ . '/Controller_Mock.php';
362
363         $setupControllerMock = new Setup_Controller_Mock();
364
365         $tables = $setupControllerMock->getBackupStructureOnlyTables();
366
367         $this->assertTrue(in_array(SQL_TABLE_PREFIX . 'felamimail_cache_message', $tables), 'felamimail tables need to be in _getBackupStructureOnlyTables');
368     }
369 }