Merge branch '2015.11' into 2015.11-develop
[tine20] / tests / tine20 / Admin / ControllerTest.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  * 
5  * @package     Admin
6  * @license     http://www.gnu.org/licenses/agpl.html
7  * @copyright   Copyright (c) 2008-2016 Metaways Infosystems GmbH (http://www.metaways.de)
8  * @author      Philipp Schüle <p.schuele@metaways.de>
9  * 
10  * @todo reactivate tests and make them independent
11  */
12
13 /**
14  * Test class for Tinebase_Admin
15  */
16 class Admin_ControllerTest extends TestCase
17 {
18     /**
19      * @var array test objects
20      */
21     protected $objects = array();
22     
23     /**
24      * Sets up the fixture.
25      * This method is called before a test is executed.
26      *
27      * @access protected
28      */
29     protected function setUp()
30     {
31         parent::setUp();
32
33 //        $this->objects['initialGroup'] = new Tinebase_Model_Group(array(
34 //            'id'            => 'test-controller-group',
35 //            'name'          => 'tine20phpunit',
36 //            'description'   => 'initial test group'
37 //        ));
38 //
39 //        $this->objects['updatedGroup'] = new Tinebase_Model_Group(array(
40 //            'id'            => 'test-controller-group',
41 //            'name'          => 'tine20phpunit updated',
42 //            'description'   => 'updated test group'
43 //        ));
44 //
45 //        $this->objects['initialAccount'] = new Tinebase_Model_FullUser(array(
46 //            'accountId'             => 'dflkjgldfgdfgd',
47 //            'accountLoginName'      => 'tine20phpunit',
48 //            'accountStatus'         => 'enabled',
49 //            'accountExpires'        => NULL,
50 //            'accountPrimaryGroup'   => Tinebase_Group::getInstance()->getDefaultGroup()->id,
51 //            'accountLastName'       => 'Tine 2.0',
52 //            'accountFirstName'      => 'PHPUnit',
53 //            'accountEmailAddress'   => 'phpunit@metaways.de'
54 //        ));
55 //
56 //        $this->objects['updatedAccount'] = new Tinebase_Model_FullUser(array(
57 //            'accountLoginName'      => 'tine20phpunit-updated',
58 //            'accountStatus'         => 'disabled',
59 //            'accountExpires'        => NULL,
60 //            'accountPrimaryGroup'   => Tinebase_Group::getInstance()->getDefaultGroup()->id,
61 //            'accountLastName'       => 'Tine 2.0 Updated',
62 //            'accountFirstName'      => 'PHPUnit Updated',
63 //            'accountEmailAddress'   => 'phpunit@tine20.org'
64 //        ));
65 //
66 //            if (Tinebase_Application::getInstance()->isInstalled('Addressbook') === true) {
67 //                $internalAddressbook = Tinebase_Container::getInstance()->getContainerByName('Addressbook', 'Internal Contacts', Tinebase_Model_Container::TYPE_SHARED);
68 //
69 //                $this->objects['initialGroup']->container_id = $internalAddressbook->getId();
70 //                $this->objects['updatedGroup']->container_id = $internalAddressbook->getId();
71 //                $this->objects['initialAccount']->container_id = $internalAddressbook->getId();
72 //                $this->objects['updatedAccount']->container_id = $internalAddressbook->getId();
73 //            }
74     }
75
76     /**
77      * try to add an account
78      */
79     public function testAddAccount()
80     {
81         $this->markTestSkipped('TODO make this test independent');
82
83         $account = Admin_Controller_User::getInstance()->create($this->objects['initialAccount'], 'lars', 'lars');
84         $this->assertTrue(!empty($account->accountId));
85         $this->assertEquals($this->objects['initialAccount']->accountLoginName, $account->accountLoginName);
86         
87         $contact = Addressbook_Controller_Contact::getInstance()->getContactByUserId($account->accountId);
88         $this->assertTrue(!empty($contact->creation_time));
89         $this->assertEquals(Tinebase_Core::getUser()->accountId, $account->created_by, 'created_by not matching');
90         $this->assertTrue($account->creation_time instanceof Tinebase_DateTime, 'creation time not set: ' . print_r($account->toArray(), true));
91         $this->assertEquals(Tinebase_DateTime::now()->format('Y-m-d'), $account->creation_time->format('Y-m-d'));
92     }
93     
94     /**
95      * try to get all accounts containing phpunit in there name
96      */
97     public function testGetAccounts()
98     {
99         $this->markTestSkipped('TODO make this test independent');
100
101         $accounts = Admin_Controller_User::getInstance()->searchFullUsers($this->objects['initialAccount']['accountLoginName']);
102                 
103         $this->assertEquals(1, count($accounts));
104     }
105     
106     /**
107      * try to delete an accout
108      */
109     public function testDeleteAccount()
110     {
111         $this->markTestSkipped('TODO make this test independent');
112
113         $accounts = Admin_Controller_User::getInstance()->searchFullUsers($this->objects['initialAccount']['accountLoginName']);
114         
115         Admin_Controller_User::getInstance()->delete($accounts->getArrayOfIds());
116         
117         $accounts = Admin_Controller_User::getInstance()->searchFullUsers($this->objects['initialAccount']['accountLoginName']);
118
119         $this->assertEquals(0, count($accounts));
120     }
121
122     /**
123      * try to delete self
124      */
125     public function testDeleteSelf()
126     {
127         $this->markTestSkipped('TODO make this test independent');
128
129         $this->setExpectedException('Tinebase_Exception_AccessDenied');
130         Admin_Controller_User::getInstance()->delete(Tinebase_Core::getUser()->getId());
131     }
132
133     /**
134      * try to add a group
135      */
136     public function testAddGroup()
137     {
138         $this->markTestSkipped('TODO make this test independent');
139
140         $group = Admin_Controller_Group::getInstance()->create($this->objects['initialGroup']);
141         
142         $this->assertEquals($this->objects['initialGroup']->id, $group->id);
143         $this->assertEquals(Tinebase_Core::getUser()->accountId, $group->created_by);
144         $this->assertEquals(Tinebase_DateTime::now()->format('Y-m-d'), $group->creation_time->format('Y-m-d'));
145     }
146     
147     /**
148      * try to get all groups
149      */
150     public function testGetGroups()
151     {
152         $this->markTestSkipped('TODO make this test independent');
153
154         $groups = Admin_Controller_Group::getInstance()->search($this->objects['initialGroup']->name);
155         
156         $this->assertEquals(1, count($groups));
157     }    
158
159     /**
160      * try to get Users group
161      *
162      */
163     public function testGetGroup()
164     {
165         $this->markTestSkipped('TODO make this test independent');
166
167         $groups = Admin_Controller_Group::getInstance()->search($this->objects['initialGroup']->name);
168         
169         $group = Admin_Controller_Group::getInstance()->get($groups[0]->getId());
170         
171         $this->assertEquals($this->objects['initialGroup']->name, $group->name);
172     }    
173
174     /**
175      * try to delete a group
176      *
177      */
178     public function testDeleteGroups()
179     {
180         $this->markTestSkipped('TODO make this test independent');
181
182         $groups = Admin_Controller_Group::getInstance()->search($this->objects['initialGroup']->name);
183         
184         Admin_Controller_Group::getInstance()->delete($groups->getArrayOfIds());
185
186         $this->setExpectedException('Tinebase_Exception_Record_NotDefined');
187
188         Admin_Controller_Group::getInstance()->get($groups[0]->getId());
189     }
190
191     /**
192      * testCustomFieldCreate
193      *
194      * @todo should create cf via Admin_Controller_Customfield
195      */
196     public function testCustomFieldCreate()
197     {
198         $cf = Tinebase_CustomField::getInstance()->addCustomField(new Tinebase_Model_CustomField_Config(array(
199             'application_id'    => Tinebase_Application::getInstance()->getApplicationByName('Addressbook')->getId(),
200             'name'              => 'unittest_test',
201             'model'             => 'Addressbook_Model_Contact',
202             'definition'        => array(
203                 'label' => Tinebase_Record_Abstract::generateUID(),
204                 'type'  => 'string',
205                 'uiconfig' => array(
206                     'xtype'  => Tinebase_Record_Abstract::generateUID(),
207                     'length' => 10,
208                     'group'  => 'unittest',
209                     'order'  => 100,
210                 )
211             )
212         )));
213
214         $lookupCf = Tinebase_CustomField::getInstance()->getCustomField($cf->getId());
215
216         $this->assertEquals('unittest_test', $lookupCf->name);
217     }
218
219     /**
220      * testCustomFieldDelete
221      */
222     public function testCustomFieldDelete()
223     {
224         $this->testCustomFieldCreate();
225         $cfs = Tinebase_CustomField::getInstance()->getCustomFieldsForApplication('Addressbook');
226         $result = $cfs->filter('name', 'unittest_test')->getFirstRecord();
227
228         $deleted = Admin_Controller_Customfield::getInstance()->delete($result->getId());
229
230         $this->assertEquals(1, count($deleted));
231     }
232 }