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