extend usage of in-class cache in Tinebase_Container
[tine20] / tests / tine20 / Tinebase / ContainerTest.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  * 
5  * @package     Tinebase
6  * @subpackage  Container
7  * @license     http://www.gnu.org/licenses/agpl.html
8  * @copyright   Copyright (c) 2008-2013 Metaways Infosystems GmbH (http://www.metaways.de)
9  * @author      Lars Kneschke <l.kneschke@metaways.de>
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 Tinebase_ContainerTest extends PHPUnit_Framework_TestCase
21 {
22     /**
23      * unit under test (UIT)
24      * @var Tinebase_Container
25      */
26     protected $_instance;
27
28     /**
29      * @var array test objects
30      */
31     protected $objects = array();
32
33     /**
34      * Sets up the fixture.
35      * This method is called before a test is executed.
36      *
37      * @access protected
38      */
39     protected function setUp()
40     {
41         Tinebase_TransactionManager::getInstance()->startTransaction(Tinebase_Core::getDb());
42         
43         $this->_instance = Tinebase_Container::getInstance();
44         
45         $this->objects['initialContainer'] = $this->_instance->addContainer(new Tinebase_Model_Container(array(
46             'name'              => 'tine20phpunit',
47             'type'              => Tinebase_Model_Container::TYPE_PERSONAL,
48             'owner_id'          => Tinebase_Core::getUser(),
49             'backend'           => 'Sql',
50             'application_id'    => Tinebase_Application::getInstance()->getApplicationByName('Addressbook')->getId(),
51             'model'             => 'Addressbook_Model_Contact'
52         )));
53
54         $this->objects['grants'] = new Tinebase_Record_RecordSet('Tinebase_Model_Grants', array(
55             array(
56                 'account_id'     => Tinebase_Core::getUser()->getId(),
57                 'account_type'   => 'user',
58                 Tinebase_Model_Grants::GRANT_READ      => true,
59                 Tinebase_Model_Grants::GRANT_ADD       => true,
60                 Tinebase_Model_Grants::GRANT_EDIT      => true,
61                 Tinebase_Model_Grants::GRANT_DELETE    => true,
62                 Tinebase_Model_Grants::GRANT_ADMIN     => true
63             )
64         ));
65     }
66
67     /**
68      * Tears down the fixture
69      * This method is called after a test is executed.
70      *
71      * @access protected
72      */
73     protected function tearDown()
74     {
75         Tinebase_TransactionManager::getInstance()->rollBack();
76     }
77     
78     /**
79      * try to get container by id
80      *
81      */
82     public function testGetContainerById()
83     {
84         $container = $this->_instance->getContainerById($this->objects['initialContainer']);
85         
86         $this->assertEquals('Tinebase_Model_Container', get_class($container), 'wrong type');
87         $this->assertEquals($this->objects['initialContainer']->name, $container->name);
88         $this->_validateOwnerId($container);
89         $this->_validatePath($container);
90     }
91     
92     /**
93      * validate owner id
94      * 
95      * @param Tinebase_Model_Container $_container
96      */
97     protected function _validateOwnerId(Tinebase_Model_Container $_container)
98     {
99         if ($_container->type == Tinebase_Model_Container::TYPE_SHARED)  {
100             $this->assertTrue(empty($_container->owner_id), 'shared container does not have an owner');
101         } else {
102             $this->assertTrue(! empty($_container->owner_id), 'personal container must have an owner');
103         }
104     }
105     
106     /**
107      * validate path
108      * 
109      * @param Tinebase_Model_Container $_container
110      */
111     protected function _validatePath(Tinebase_Model_Container $_container)
112     {
113         if ($_container->type == Tinebase_Model_Container::TYPE_SHARED)  {
114             $this->assertEquals("/{$_container->type}/{$_container->getId()}", $_container->path);
115         } else {
116             $this->assertEquals("/{$_container->type}/{$_container->owner_id}/{$_container->getId()}", $_container->path);
117         }
118     }
119     
120     /**
121      * try to get container by name
122      *
123      */
124     public function testGetContainerByName()
125     {
126         $container = $this->_instance->getContainerByName(
127             'Addressbook',
128             $this->objects['initialContainer']->name,
129             $this->objects['initialContainer']->type,
130             Tinebase_Core::getUser()->getId()
131         );
132         
133         $this->assertEquals('Tinebase_Model_Container', get_class($container), 'wrong type');
134         $this->assertEquals($this->objects['initialContainer']->name, $container->name);
135         $this->_validateOwnerId($container);
136         $this->_validatePath($container);
137     }
138     
139     /**
140      * try to set new container name
141      */
142     public function testSetContainerName()
143     {
144         $container = $this->_instance->setContainerName($this->objects['initialContainer'], 'renamed container');
145         
146         $this->assertEquals('Tinebase_Model_Container', get_class($container), 'wrong type');
147         $this->assertEquals('renamed container', $container->name);
148         $this->assertEquals(2, $container->seq);
149         $this->_validateOwnerId($container);
150         $this->_validatePath($container);
151     }
152
153     /**
154     * try to set readonly content seq
155     */
156     public function testSetContentSeq()
157     {
158         $container = $this->objects['initialContainer'];
159         $initialContentSeq = $container->content_seq;
160         $container->content_seq = 500;
161         $updatedContainer = $this->_instance->update($container);
162         
163         $this->assertEquals($initialContentSeq, $updatedContainer->content_seq);
164     }
165
166     /**
167     * try to set content seq that was NULL / test with deleted container
168     */
169     public function testSetNullContentSeq()
170     {
171         $container = $this->objects['initialContainer'];
172         $db = Tinebase_Core::getDb();
173         $db->update(SQL_TABLE_PREFIX . 'container', array(
174             'content_seq' => NULL,
175         ), $db->quoteInto($db->quoteIdentifier('id') . ' = ?', $container->getId()));
176         $seq = $this->_instance->getContentSequence($container->getId());
177         $this->assertEquals(NULL, $seq);
178         
179         $this->_instance->increaseContentSequence($container->getId());
180         $seq = $this->_instance->getContentSequence($container->getId());
181         $this->assertEquals(1, $seq);
182         
183         $this->_instance->deleteContainer($container);
184         $seq = $this->_instance->getContentSequence($container->getId());
185         $this->assertEquals(1, $seq);
186     }
187     
188     /**
189      * try to add an existing container. should throw an exception
190      *
191      */
192     public function testDeleteContainer()
193     {
194         $this->_instance->deleteContainer($this->objects['initialContainer']);
195         
196         $this->setExpectedException('Tinebase_Exception_NotFound');
197         $container = $this->_instance->getContainerById($this->objects['initialContainer']);
198     }
199     
200     /**
201      * try to delete an existing container with a contact 
202      */
203     public function testDeleteContainerWithContact()
204     {
205         // add contact to container
206         $contact = new Addressbook_Model_Contact(array(
207             'n_family'              => 'Tester',
208             'container_id'          => $this->objects['initialContainer']->getId()
209         ));
210         $contact = Addressbook_Controller_Contact::getInstance()->create($contact);
211         
212         // delete container
213         $this->_instance->deleteContainer($this->objects['initialContainer']);
214         
215         $this->setExpectedException('Tinebase_Exception_NotFound');
216         $container = $this->_instance->getContainerById($this->objects['initialContainer']);
217         
218         Addressbook_Controller_Contact::getInstance()->delete($contact->getId());
219     }
220     
221     /**
222      * try to get all grants of a container
223      *
224      */
225     public function testGetGrantsOfContainer()
226     {
227         $this->assertTrue($this->_instance->hasGrant(Tinebase_Core::getUser(), $this->objects['initialContainer'], Tinebase_Model_Grants::GRANT_READ));
228
229         $grants = $this->_instance->getGrantsOfContainer($this->objects['initialContainer']);
230         
231         $this->assertEquals('Tinebase_Record_RecordSet', get_class($grants), 'wrong type');
232
233         $grants = $grants->toArray();
234         $this->assertTrue($grants[0]["readGrant"]);
235         $this->assertTrue($grants[0]["addGrant"]);
236         $this->assertTrue($grants[0]["editGrant"]);
237         $this->assertTrue($grants[0]["deleteGrant"]);
238         $this->assertTrue($grants[0]["adminGrant"]);
239                 
240         $this->_instance->deleteContainer($this->objects['initialContainer']);
241         
242         $this->setExpectedException('Tinebase_Exception_NotFound');
243         
244         $container = $this->_instance->getContainerById($this->objects['initialContainer']);
245     }
246     
247     /**
248      * try to get grants of a account on a container
249      *
250      */
251     public function testGetGrantsOfAccount()
252     {
253         $this->assertTrue($this->_instance->hasGrant(Tinebase_Core::getUser(), $this->objects['initialContainer'], Tinebase_Model_Grants::GRANT_READ));
254
255         $grants = $this->_instance->getGrantsOfAccount(Tinebase_Core::getUser(), $this->objects['initialContainer']);
256         
257         $this->assertEquals('Tinebase_Model_Grants', get_class($grants), 'wrong type');
258         $this->assertTrue($grants->{Tinebase_Model_Grants::GRANT_READ});
259         $this->assertTrue($grants->{Tinebase_Model_Grants::GRANT_ADD});
260         $this->assertTrue($grants->{Tinebase_Model_Grants::GRANT_EDIT});
261         $this->assertTrue($grants->{Tinebase_Model_Grants::GRANT_DELETE});
262         $this->assertTrue($grants->{Tinebase_Model_Grants::GRANT_ADMIN});
263     }
264     
265     /**
266      * try to set grants
267      */
268     public function testSetGrants()
269     {
270         $newGrants = new Tinebase_Record_RecordSet('Tinebase_Model_Grants');
271         $newGrants->addRecord(
272             new Tinebase_Model_Grants(array(
273                     'account_id'     => Tinebase_Core::getUser()->getId(),
274                     'account_type'   => 'user',
275                     Tinebase_Model_Grants::GRANT_READ      => true,
276                     Tinebase_Model_Grants::GRANT_ADD       => false,
277                     Tinebase_Model_Grants::GRANT_EDIT      => true,
278                     Tinebase_Model_Grants::GRANT_DELETE    => true,
279                     Tinebase_Model_Grants::GRANT_ADMIN     => true
280              ))
281         );
282         
283         // get group and add grants for it
284         $lists = Addressbook_Controller_List::getInstance()->search(new Addressbook_Model_ListFilter(array(
285             array('field' => 'showHidden', 'operator' => 'equals', 'value' => TRUE)
286         )));
287         $groupToAdd = $lists->getFirstRecord();
288         $newGrants->addRecord(
289             new Tinebase_Model_Grants(array(
290                     'account_id'     => $groupToAdd->group_id,
291                     'account_type'   => 'group',
292                     Tinebase_Model_Grants::GRANT_READ      => true,
293                     Tinebase_Model_Grants::GRANT_ADD       => false,
294                     Tinebase_Model_Grants::GRANT_EDIT      => false,
295                     Tinebase_Model_Grants::GRANT_DELETE    => false,
296                     Tinebase_Model_Grants::GRANT_ADMIN     => false
297              ))
298         );
299         
300         $grants = $this->_instance->setGrants($this->objects['initialContainer'], $newGrants);
301         $this->assertEquals('Tinebase_Record_RecordSet', get_class($grants), 'wrong type');
302         $this->assertEquals(2, count($grants));
303
304         $grants = $grants->toArray();
305         foreach ($grants as $grant) {
306             if ($grant['account_id'] === Tinebase_Core::getUser()->getId()) {
307                 $this->assertTrue($grant["readGrant"], print_r($grant, TRUE));
308                 $this->assertFalse($grant["addGrant"], print_r($grant, TRUE));
309                 $this->assertTrue($grant["editGrant"], print_r($grant, TRUE));
310                 $this->assertTrue($grant["deleteGrant"], print_r($grant, TRUE));
311                 $this->assertTrue($grant["adminGrant"], print_r($grant, TRUE));
312                 $this->assertEquals(Tinebase_Acl_Rights::ACCOUNT_TYPE_USER, $grant['account_type']);
313             } else {
314                 $this->assertTrue($grant["readGrant"], print_r($grant, TRUE));
315                 $this->assertEquals(Tinebase_Acl_Rights::ACCOUNT_TYPE_GROUP, $grant['account_type']);
316             }
317         }
318     }
319     
320     /**
321      * testDuplicateGrantsWithSetGrants
322      */
323     public function testDuplicateGrantsWithSetGrants()
324     {
325         $this->testSetGrants();
326         
327         $newGrants = new Tinebase_Record_RecordSet('Tinebase_Model_Grants');
328         $newGrants->addRecord(
329             new Tinebase_Model_Grants(array(
330                     'account_id'     => Tinebase_Core::getUser()->getId(),
331                     'account_type'   => 'user',
332                     Tinebase_Model_Grants::GRANT_ADMIN     => true
333              ))
334         );
335         $grants = $this->_instance->setGrants($this->objects['initialContainer'], $newGrants);
336         $this->assertEquals(1, count($grants));
337         
338         // check num of db rows
339         $stmt = Tinebase_Core::getDb()->query('select * from '.Tinebase_Core::getDb()->quoteIdentifier(SQL_TABLE_PREFIX .'container_acl') . ' where ' . Tinebase_Core::getDb()->quoteInto(Tinebase_Core::getDb()->quoteIdentifier('container_id') . ' = ?', $this->objects['initialContainer']->getId()));
340         $rows = $stmt->fetchAll();
341         
342         $this->assertEquals(1, count($rows));
343     }
344     
345     /**
346      * testOverwriteGrantsWithAddGrants
347      * 
348      * -> addGrants() should create no duplicates! 
349      */
350     public function testOverwriteGrantsWithAddGrants()
351     {
352         $result = $this->_instance->addGrants($this->objects['initialContainer'], 'user', Tinebase_Core::getUser()->getId(), array(Tinebase_Model_Grants::GRANT_ADMIN));
353         $this->assertTrue($result);
354         
355         // check num of db rows
356         $stmt = Tinebase_Core::getDb()->query('select * from '.Tinebase_Core::getDb()->quoteIdentifier(SQL_TABLE_PREFIX .'container_acl') . ' where ' . Tinebase_Core::getDb()->quoteInto(Tinebase_Core::getDb()->quoteIdentifier('container_id') . ' = ?', $this->objects['initialContainer']->getId()));
357         $rows = $stmt->fetchAll();
358         
359         $this->assertEquals(7, count($rows));
360     }
361     
362     /**
363      * try to other users who gave grants to current account
364      *
365      */
366     public function testGetOtherUsers()
367     {
368         $otherUsers = $this->_instance->getOtherUsers(Tinebase_Core::getUser(), 'Addressbook', Tinebase_Model_Grants::GRANT_READ);
369         
370         $this->assertEquals('Tinebase_Record_RecordSet', get_class($otherUsers), 'wrong type');
371         $this->assertEquals(0, $otherUsers->filter('accountId', Tinebase_Core::getUser()->getId())->count(), 'current user must not be part of otherUsers');
372     }
373     
374     /**
375      * get shared containers
376      *
377      */
378     public function testGetSharedContainer()
379     {
380         $container = $this->_instance->addContainer(new Tinebase_Model_Container(array(
381             'name'           => 'containerTest' . Tinebase_Record_Abstract::generateUID(),
382             'type'           => Tinebase_Model_Container::TYPE_SHARED,
383             'application_id' => Tinebase_Application::getInstance()->getApplicationByName('Addressbook')->getId(),
384             'backend'        => 'Sql'
385         )));
386         
387         $otherUsers = $this->_instance->getOtherUsers(Tinebase_Core::getUser(), 'Addressbook', array(
388             Tinebase_Model_Grants::GRANT_READ,
389             Tinebase_Model_Grants::GRANT_ADMIN,
390         ));
391         
392         $this->assertEquals('Tinebase_Record_RecordSet', get_class($otherUsers), 'wrong type');
393         $this->assertEquals(0, $otherUsers->filter('accountId', Tinebase_Core::getUser()->getId())->count(), 'current user must not be part of otherUsers');
394     
395         $this->_instance->deleteContainer($container->getId(), TRUE);
396     }
397
398     /**
399      * testGetSearchContainerWithoutReadButWithAdminGrant
400      */
401     public function testGetSearchContainerWithoutReadButWithAdminGrant()
402     {
403         $newGrants = new Tinebase_Record_RecordSet('Tinebase_Model_Grants');
404         $newGrants->addRecord(
405             new Tinebase_Model_Grants(array(
406                     'account_id'     => Tinebase_Core::getUser()->getId(),
407                     'account_type'   => 'user',
408                     Tinebase_Model_Grants::GRANT_READ      => false,
409                     Tinebase_Model_Grants::GRANT_ADD       => true,
410                     Tinebase_Model_Grants::GRANT_EDIT      => true,
411                     Tinebase_Model_Grants::GRANT_DELETE    => true,
412                     Tinebase_Model_Grants::GRANT_ADMIN     => true
413              ))
414         );
415         $grants = $this->_instance->setGrants($this->objects['initialContainer'], $newGrants);
416         
417         $container = $this->_instance->getContainerById($this->objects['initialContainer']);
418         $this->assertTrue(is_object($container));
419         
420         $containers = $this->_instance->getPersonalContainer(Tinebase_Core::getUser(), 'Addressbook', Tinebase_Core::getUser(), Tinebase_Model_Grants::GRANT_READ);
421         $container = $containers->find('name', $this->objects['initialContainer']->name);
422         $this->assertTrue(is_object($container));
423     }
424     
425     /**
426      * try to get container by acl
427      */
428     public function testGetContainerByAcl()
429     {
430         $this->assertTrue($this->_instance->hasGrant(Tinebase_Core::getUser(), $this->objects['initialContainer'], Tinebase_Model_Grants::GRANT_READ));
431
432         $readableContainer = $this->_instance->getContainerByAcl(Tinebase_Core::getUser(), 'Addressbook', Tinebase_Model_Grants::GRANT_READ);
433         $this->assertEquals('Tinebase_Record_RecordSet', get_class($readableContainer), 'wrong type');
434         $this->assertTrue(count($readableContainer) >= 2);
435         foreach($readableContainer as $container) {
436             $this->_validateOwnerId($container);
437             $this->_validatePath($container);
438         }
439     }
440
441     /**
442      * try to get container by acl with Zend_Cache
443      */
444     public function testGetContainerByAclWithPersistentCaching()
445     {
446         Tinebase_Cache_PerRequest::getInstance()->usePersistentCache(true);
447         $this->testGetContainerByAcl();
448         $oldValue = Tinebase_Cache_PerRequest::getInstance()->usePersistentCache(false);
449
450         $this->assertTrue($oldValue);
451     }
452     
453     /**
454      * test getGrantsOfRecords
455      *
456      */
457     public function testGetGrantsOfRecords()
458     {
459         $userId = Tinebase_Core::getUser()->getId();
460         $contact = Addressbook_Controller_Contact::getInstance()->getContactByUserId($userId);
461         $records = new Tinebase_Record_RecordSet('Addressbook_Model_Contact');
462         $records->addRecord($contact);
463         
464         $grants = $this->_instance->getGrantsOfRecords($records, $userId, 'container_id');
465         
466         $this->assertTrue($records[0]['container_id'] instanceof Tinebase_Model_Container, 'contaienr_id is not resolved');
467         $this->assertTrue(!empty($records[0]['container_id']['path']), 'path is not added');
468         $this->assertGreaterThan(0, count($records[0]['container_id']['account_grants']));
469         $this->assertEquals('shared', $records[0]['container_id']['type']);
470     }
471     
472     /**
473      * try to move a contact to another container and check content sequence
474      */
475     public function testMoveContactToContainer()
476     {
477         $sharedContainer = $this->_instance->addContainer(new Tinebase_Model_Container(array(
478             'name'              => 'tine20shared',
479             'type'              => Tinebase_Model_Container::TYPE_SHARED,
480             'owner_id'          => Tinebase_Core::getUser(),
481             'backend'           => 'Sql',
482             'application_id'    => Tinebase_Application::getInstance()->getApplicationByName('Addressbook')->getId(),
483         )));
484         $initialContainer = $this->_instance->get($this->objects['initialContainer']);
485         $contact = new Addressbook_Model_Contact(array(
486             'n_family'              => 'Tester',
487             'container_id'          => $sharedContainer->getId()
488         ));
489         $contact = Addressbook_Controller_Contact::getInstance()->create($contact);
490         
491         $filter = array(array('field' => 'id', 'operator' => 'in', 'value' => array($contact->getId())));
492         $containerJson = new Tinebase_Frontend_Json_Container();
493         $result = $containerJson->moveRecordsToContainer($initialContainer->getId(), $filter, 'Addressbook', 'Contact');
494         $this->assertGreaterThan(0, count($result['results']), 'no contacts moved');
495         $this->assertEquals($contact->getId(), $result['results'][0]);
496         
497         $movedContact = Addressbook_Controller_Contact::getInstance()->get($contact->getId());
498         
499         $this->assertEquals($initialContainer->getId(), $movedContact->container_id, 'contact has not been moved');
500         
501         $contentSeqs = $this->_instance->getContentSequence(array($sharedContainer->getId(), $initialContainer->getId()));
502         $this->assertEquals(2, $contentSeqs[$sharedContainer->getId()], 'content seq mismatch: ' . print_r($contentSeqs, TRUE));
503         $this->assertEquals(1, $contentSeqs[$initialContainer->getId()], 'content seq mismatch: ' . print_r($contentSeqs, TRUE));
504         
505         // check container content
506         $contentRecords = $this->_instance->getContentHistory($sharedContainer->getId());
507         $this->assertEquals(2, count($contentRecords));
508         $this->assertEquals($contact->getId(), $contentRecords->getFirstRecord()->record_id);
509         $this->assertEquals(Tinebase_Model_ContainerContent::ACTION_CREATE, $contentRecords->getFirstRecord()->action);
510     }
511     
512     /**
513      * get not disabled users
514      *
515      */
516     public function testGetNotDisabledUsers()
517     {
518         $user1 = Tinebase_User::getInstance()->getUserByLoginName('jsmith');
519         $user2 = Tinebase_User::getInstance()->getUserByLoginName('pwulf');
520         
521         Tinebase_User::getInstance()->setStatus($user2, 'enabled');
522         
523         $container = Tinebase_Container::getInstance()->getPersonalContainer($user1, 'Calendar', $user1, Tinebase_Model_Grants::GRANT_READ);
524         
525         $oldGrants = Tinebase_Container::getInstance()->getGrantsOfContainer($container->getFirstRecord()->id, TRUE);
526         
527         $newGrants = new Tinebase_Record_RecordSet('Tinebase_Model_Grants');
528         $newGrants->addRecord(
529             new Tinebase_Model_Grants(
530                 array(
531                     'account_id'    => $user1->accountId,
532                     'account_type'  => 'user',
533                     Tinebase_Model_Grants::GRANT_READ     => true,
534                     Tinebase_Model_Grants::GRANT_ADD      => true,
535                     Tinebase_Model_Grants::GRANT_EDIT     => true,
536                     Tinebase_Model_Grants::GRANT_DELETE   => true,
537                     Tinebase_Model_Grants::GRANT_ADMIN    => true
538                 )
539             )
540         );
541         $newGrants->addRecord(
542             new Tinebase_Model_Grants(
543                 array(
544                     'account_id'    => $user2->accountId,
545                     'account_type'  => 'user',
546                     Tinebase_Model_Grants::GRANT_READ     => true,
547                     Tinebase_Model_Grants::GRANT_ADD      => true,
548                     Tinebase_Model_Grants::GRANT_EDIT     => true,
549                     Tinebase_Model_Grants::GRANT_DELETE   => true,
550                     Tinebase_Model_Grants::GRANT_ADMIN    => true
551                 )
552             )
553         );
554         
555         Tinebase_Container::getInstance()->setGrants($container->getFirstRecord()->id, $newGrants, TRUE);
556         
557         $otherUsers = Tinebase_Container::getInstance()->getOtherUsers($user1, 'Calendar', array(
558             Tinebase_Model_Grants::GRANT_READ
559         ));
560         
561         $this->assertEquals(1, $otherUsers->filter('accountId', $user2->accountId)->count());
562         
563         Tinebase_User::getInstance()->setStatus($user2, 'disabled');
564         
565         $otherUsers = Tinebase_Container::getInstance()->getOtherUsers($user1, 'Calendar', array(
566             Tinebase_Model_Grants::GRANT_READ
567         ));
568         
569         Tinebase_User::getInstance()->setStatus($user2, 'enabled');
570         Tinebase_Container::getInstance()->setGrants($container->getFirstRecord()->id, $oldGrants, TRUE);
571         
572         $this->assertEquals(0, $otherUsers->filter('accountId', $user2->accountId)->count());
573     }
574     
575     /**
576      * get other users container
577      *
578      */
579     public function testGetOtherUsersContainer()
580     {
581         $user1 = Tinebase_User::getInstance()->getUserByLoginName('jsmith');
582         $otherUsers = Tinebase_Container::getInstance()->getOtherUsersContainer($user1, 'Calendar', array(
583             Tinebase_Model_Grants::GRANT_READ
584         ));
585         $this->assertTrue($otherUsers->getRecordClassName() === 'Tinebase_Model_Container');
586         
587         foreach($otherUsers as $container) {
588             $this->_validateOwnerId($container);
589             $this->_validatePath($container);
590         }
591     }
592
593     /**
594      * search container with owner filter
595      */
596     public function testSearchContainerByOwner()
597     {
598         $filter = new Tinebase_Model_ContainerFilter(array(
599             array('field' => 'owner', 'operator' => 'equals', 'value' => Tinebase_Core::getUser()->getId())
600         ));
601         $result = Tinebase_Container::getInstance()->search($filter);
602         
603         $this->assertTrue(count($result) > 0);
604         
605         foreach ($result as $container) {
606             $this->assertEquals(Tinebase_Model_Container::TYPE_PERSONAL, $container->type);
607             $this->assertTrue(Tinebase_Container::getInstance()->hasGrant(
608                 Tinebase_Core::getUser()->getId(), $container->getId(), Tinebase_Model_Grants::GRANT_ADMIN
609             ), 'no admin grant:' . print_r($container->toArray(), TRUE));
610         }
611     }
612
613     /**
614      * testSetContainerColor
615      * 
616      * @see 0006274: Can no longer change calendar's color
617      */
618     public function testSetContainerColor()
619     {
620         $result = $this->_instance->setContainerColor($this->objects['initialContainer'], '#99CC00');
621         $this->assertEquals('#99CC00', $result->color);
622     }
623     
624     /**
625      * testGetPersonalContainer
626      * @see https://gerrit.tine20.org/tine20/#/c/862
627      */
628     
629     public function testGetPersonalContainer()
630     {
631         $uid=Tinebase_Core::getUser()->getId();
632         $containers = Tinebase_Container::getInstance()->getPersonalContainer($uid, 'Addressbook_Model_Contact', $uid, Tinebase_Model_Grants::GRANT_READ);
633         $container = $containers->filter('name', 'tine20phpunit')->getFirstRecord();
634         $this->assertEquals($container->name, 'tine20phpunit');
635         $this->assertEquals($container->model, 'Addressbook_Model_Contact');
636     }
637     
638     public function testAccountAnyoneDisabled()
639     {
640         $container = new Tinebase_Model_Container(array(
641             'name'              => 'tine20shared',
642             'type'              => Tinebase_Model_Container::TYPE_SHARED,
643             'owner_id'          => Tinebase_Core::getUser(),
644             'backend'           => 'Sql',
645             'application_id'    => Tinebase_Application::getInstance()->getApplicationByName('Addressbook')->getId(),
646         ));
647         
648         $grants = new Tinebase_Record_RecordSet('Tinebase_Model_Grants', array(
649             array(
650                 'account_id'      => '0',
651                 'account_type'    => Tinebase_Acl_Rights::ACCOUNT_TYPE_ANYONE,
652                 Tinebase_Model_Grants::GRANT_READ    => true,
653                 Tinebase_Model_Grants::GRANT_EXPORT  => true,
654                 Tinebase_Model_Grants::GRANT_SYNC    => true,
655             )
656         ), TRUE);
657         
658         $sharedContainer = $this->_instance->addContainer($container, $grants);
659         $readGrant = $this->_instance->hasGrant(Tinebase_Core::getUser(), $sharedContainer, Tinebase_Model_Grants::GRANT_READ);
660         $this->assertTrue($readGrant);
661         
662         Tinebase_Config::getInstance()->set(Tinebase_Config::ANYONE_ACCOUNT_DISABLED, TRUE);
663         Tinebase_Core::getCache()->clean();
664         $readGrant = $this->_instance->resetClassCache('hasGrant')->hasGrant(Tinebase_Core::getUser(), $sharedContainer, Tinebase_Model_Grants::GRANT_READ);
665         $this->assertFalse($readGrant);
666     }
667     
668     /**
669      * cache invalidation needs a second 
670      */
671     public function testSetGrantsCacheInvalidation()
672     {
673         $container = $this->objects['initialContainer'];
674         $sclever = array_value('sclever', Zend_Registry::get('personas'));
675         
676         $this->assertEquals(FALSE, $this->_instance->hasGrant($sclever->getId(), $container->getId(), Tinebase_Model_Grants::GRANT_READ), 'sclever should _not_ have a read grant');
677         
678         // have readGrant for sclever
679         $this->_instance->setGrants($container->getId(), new Tinebase_Record_RecordSet('Tinebase_Model_Grants', array(
680             array(
681                 'account_id'    => Tinebase_Core::getUser()->getId(),
682                 'account_type'  => 'user',
683                 Tinebase_Model_Grants::GRANT_ADMIN    => true,
684             ),
685             array(
686                 'account_id'    => $sclever->getId(),
687                 'account_type'  => 'user',
688                 Tinebase_Model_Grants::GRANT_READ     => true,
689         ))), TRUE);
690         
691         sleep(1);
692         $this->assertEquals(TRUE, $this->_instance->hasGrant($sclever->getId(), $container->getId(), Tinebase_Model_Grants::GRANT_READ), 'sclever _should have_ a read grant');
693         
694         // remove readGrant for sclever again
695         $this->_instance->setGrants($container->getId(), new Tinebase_Record_RecordSet('Tinebase_Model_Grants', array(
696             array(
697                 'account_id'    => Tinebase_Core::getUser()->getId(),
698                 'account_type'  => 'user',
699                 Tinebase_Model_Grants::GRANT_ADMIN    => true,
700             ),
701             array(
702                 'account_id'    => $sclever->getId(),
703                 'account_type'  => 'user',
704                 Tinebase_Model_Grants::GRANT_READ     => false,
705         ))), TRUE);
706         
707         sleep(1);
708         $this->assertEquals(FALSE, $this->_instance->hasGrant($sclever->getId(), $container->getId(), Tinebase_Model_Grants::GRANT_READ), 'sclever should _not_ have a read grant');
709         
710     }
711 }