Filemanager - fix notification update acl check
[tine20] / tests / tine20 / Filemanager / ControllerTests.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  * 
5  * @package     Filemanager
6  * @license     http://www.gnu.org/licenses/agpl.html
7  * @copyright   Copyright (c) 2017 Metaways Infosystems GmbH (http://www.metaways.de)
8  * @author      Philipp Schüle <p.schuele@metaways.de>
9  * 
10  */
11
12 /**
13  * Test class for Filemanager_ControllerTests
14  * 
15  * @package     Filemanager
16  */
17 class Filemanager_ControllerTests extends TestCase
18 {
19
20     protected function tearDown()
21     {
22         parent::tearDown();
23
24         Tinebase_Config::getInstance()->set(Tinebase_Config::ACCOUNT_DELETION_EVENTCONFIGURATION, new Tinebase_Config_Struct(array(
25         )));
26     }
27
28     /**
29      * @throws Admin_Exception
30      * @throws Exception
31      */
32     public function testCreatePersonalContainer()
33     {
34         // create user
35         $pw = Tinebase_Record_Abstract::generateUID();
36         $user = Admin_Controller_User::getInstance()->create($this->getTestUser(), $pw, $pw);
37         $this->_usernamesToDelete[] = $user->accountLoginName;
38
39         // check if personal folder exists
40         $personalFolderPath = $this->_getPersonalPath($user);
41         $translation = Tinebase_Translation::getTranslation('Tinebase');
42         $personalFolderName = sprintf($translation->_("%s's personal files"), $user->accountFullName);
43
44         $node = Tinebase_FileSystem::getInstance()->stat($personalFolderPath . '/' . $personalFolderName);
45         $this->assertEquals($personalFolderName, $node->name);
46
47         return $user;
48     }
49
50     /**
51      * @throws Admin_Exception
52      * @throws Exception
53      */
54     public function testDeletePersonalContainer()
55     {
56         Tinebase_Config::getInstance()->set(Tinebase_Config::ACCOUNT_DELETION_EVENTCONFIGURATION, new Tinebase_Config_Struct(array(
57             '_deletePersonalContainers' => true,
58         )));
59
60         $user = $this->testCreatePersonalContainer();
61         Admin_Controller_User::getInstance()->delete(array($user->getId()));
62
63         // check if personal folder exists
64         $personalFolderPath = $this->_getPersonalPath($user);
65         self::setExpectedException('Tinebase_Exception_NotFound', 'child:');
66         Tinebase_FileSystem::getInstance()->stat($personalFolderPath);
67     }
68
69     public function testNotificationUpdateForReadOnly()
70     {
71         $oldUser = Tinebase_Core::getUser();
72         /** @var Tinebase_Model_FullUser $sclever */
73         $sclever = $this->_personas['sclever'];
74         try {
75             $personalFolderPath = $this->_getPersonalPath($oldUser);
76             $translation = Tinebase_Translation::getTranslation('Tinebase');
77             $fileSystem = Tinebase_FileSystem::getInstance();
78             $fileManager = Filemanager_Controller_Node::getInstance();
79             $personalFolderName = sprintf($translation->_("%s's personal files"), $oldUser->accountFullName);
80             $node = $fileSystem->stat($personalFolderPath . '/' . $personalFolderName);
81
82             // try a failing update
83             Tinebase_Core::set(Tinebase_Core::USER, $sclever);
84             $scleverNotificationProps = array(
85                 Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION_ACCOUNT_ID => $sclever->getId(),
86                 Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION_ACCOUNT_TYPE => Tinebase_Acl_Rights::ACCOUNT_TYPE_USER,
87                 Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION_ACTIVE => true,
88             );
89             $node->xprops(Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION)[] = $scleverNotificationProps;
90             $failed = false;
91             try {
92                 $fileManager->update($node);
93             } catch (Tinebase_Exception_AccessDenied $tead) {
94                 $failed = true;
95             }
96             static::assertTrue($failed);
97
98             // now set grants so update can work
99             Tinebase_Core::set(Tinebase_Core::USER, $oldUser);
100             $node = $fileManager->get($node->getId());
101             $node->grants = $fileSystem->getGrantsOfContainer($node);
102             $node->grants->addRecord(new Tinebase_Model_Grants(array(
103                 'account_type'      => Tinebase_Acl_Rights::ACCOUNT_TYPE_USER,
104                 'account_id'        => $sclever->getId(),
105                 Tinebase_Model_Grants::GRANT_READ => true,
106             )));
107             $node = $fileManager->update($node);
108
109             // do update again, it should work now
110             // test that updates to other things than own notification are silently dropped
111             Tinebase_Core::set(Tinebase_Core::USER, $sclever);
112             $notificationProps = array(array(
113                 Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION_ACCOUNT_ID => $sclever->getId(),
114                 Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION_ACCOUNT_TYPE => Tinebase_Acl_Rights::ACCOUNT_TYPE_USER,
115                 Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION_ACTIVE => true,
116             ),array(
117                 Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION_ACCOUNT_ID => '1233',
118                 Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION_ACCOUNT_TYPE => Tinebase_Acl_Rights::ACCOUNT_TYPE_USER,
119                 Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION_ACTIVE => true,
120             ),array(
121                 Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION_ACCOUNT_ID => $sclever->getId(),
122                 Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION_ACCOUNT_TYPE => Tinebase_Acl_Rights::ACCOUNT_TYPE_GROUP,
123                 Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION_ACTIVE => true,
124             ));
125             $node->{Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION} = $notificationProps;
126             $oldDescription = $node->description;
127             static::assertNotEquals('test', $oldDescription, 'test data bad, the description must not be "test"');
128             $node->description = 'test';
129
130             $node = $fileManager->update($node);
131             static::assertEquals($oldDescription, $node->description, 'description should not have been updated!');
132             static::assertEquals(1, count($node->xprops(Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION)));
133             static::assertTrue(
134                 isset($node->xprops(Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION)[0]) &&
135                 $scleverNotificationProps == $node->xprops(Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION)[0]
136             );
137
138             $node->{Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION} = array();
139             $node = $fileManager->update($node);
140             static::assertEquals(0, count($node->xprops(Tinebase_Model_Tree_Node::XPROPS_NOTIFICATION)));
141         } finally {
142             Tinebase_Core::set(Tinebase_Core::USER, $oldUser);
143         }
144     }
145
146     public function testRenameFolderCaseSensitive()
147     {
148         // check if personal folder exists
149         $personalFolderPath = $this->_getPersonalPath(Tinebase_Core::getUser());
150         $translation = Tinebase_Translation::getTranslation('Tinebase');
151         $personalFolderPath .= sprintf($translation->_("/%s's personal files"), Tinebase_Core::getUser()->accountFullName);
152         $fileManager = Filemanager_Controller_Node::getInstance();
153
154         $fileManager->createNodes($personalFolderPath . '/test', Tinebase_Model_Tree_FileObject::TYPE_FOLDER);
155         $fileManager->moveNodes(array($personalFolderPath . '/test'), array($personalFolderPath . '/Test'));
156     }
157 }