extend usage of in-class cache in Tinebase_Container
[tine20] / tests / tine20 / Filemanager / Controller / DownloadLinkTests.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) 2014 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_Controller_DownloadLink
14  * 
15  * @package     Filemanager
16  */
17 class Filemanager_Controller_DownloadLinkTests extends TestCase
18 {
19     /**
20      * set up tests
21      */
22     protected function setUp()
23     {
24         parent::setUp();
25     }
26     
27     /**
28      * tear down tests
29      */
30     protected function tearDown()
31     {
32         parent::tearDown();
33         
34         Tinebase_FileSystem::getInstance()->clearStatCache();
35         Tinebase_FileSystem::getInstance()->clearDeletedFilesFromFilesystem();
36         Tinebase_Cache_PerRequest::getInstance()->resetCache();
37     }
38     
39     /**
40      * testCreateDownloadLink
41      */
42     public function testCreateDownloadLink()
43     {
44         $node = $this->_getPersonalRootNode();
45         $downloadLink = $this->_getUit()->create(new Filemanager_Model_DownloadLink(array(
46             'node_id'       => $node->getId(),
47             'expiry_date'   => Tinebase_DateTime::now()->addDay(1)->toString(),
48             'access_count'  => 7,
49         )));
50         $this->assertTrue(! empty($downloadLink->url));
51         
52         return $downloadLink;
53     }
54     
55     /**
56      * testGetNode
57      */
58     public function testGetNode()
59     {
60         $downloadLink = $this->testCreateDownloadLink();
61         
62         $resultNode = $this->_getUit()->getNode($downloadLink, array());
63         
64         $this->assertEquals(
65             Tinebase_Container::getInstance()->getDefaultContainer('Filemanager_Model_Node')->getId(),
66             $resultNode->name,
67             'download link should resolve the default container'
68         );
69     }
70     
71     /**
72      * @return Filemanager_Model_Node
73      */
74     protected function _getPersonalRootNode()
75     {
76         $defaultContainer = Tinebase_Container::getInstance()->getDefaultContainer('Filemanager_Model_Node');
77         $filter = new Tinebase_Model_Tree_Node_Filter(array(array(
78             'field'    => 'path',
79             'operator' => 'equals',
80             'value'    => '/' . Tinebase_Model_Container::TYPE_PERSONAL . '/' . Tinebase_Core::getUser()->accountLoginName
81         )));
82         $node = Filemanager_Controller_Node::getInstance()->search($filter)->getFirstRecord();
83         
84         $this->assertInstanceOf('Tinebase_Model_Tree_Node', $node);
85         
86         return $node;
87     }
88     
89     /**
90      * testGetFileList
91      */
92     public function testGetFileList()
93     {
94         $downloadLink = $this->testCreateDownloadLink();
95         
96         $basePath = '/' . Tinebase_Model_Container::TYPE_PERSONAL . '/' . Tinebase_Core::getUser()->accountLoginName 
97             . '/' . Tinebase_Container::getInstance()->getDefaultContainer('Filemanager_Model_Node')->name;
98         $directories = array(
99             $basePath . '/one',
100             $basePath . '/two',
101         );
102         Filemanager_Controller_Node::getInstance()->createNodes($directories, Tinebase_Model_Tree_Node::TYPE_FOLDER);
103         
104         $fileList = $this->_getUit()->getFileList($downloadLink, array());
105         
106         $this->assertGreaterThan(1, count($fileList));
107         $this->assertNotNull($fileList->filter('name', 'one')->getFirstRecord());
108     }
109     
110     /**
111      * testDownloadLinkAcl
112      */
113     public function testDownloadLinkAcl()
114     {
115         // try to access download link to personal container of another user
116         $downloadLink = $this->testCreateDownloadLink();
117         
118         Tinebase_Core::set(Tinebase_Core::USER, $this->_personas['sclever']);
119         try {
120             $resultNode = $this->_getUit()->getNode($downloadLink, array());
121             $this->fail('user should not be able to access download link node');
122         } catch (Tinebase_Exception_AccessDenied $tead) {
123             $this->assertEquals('No permission to get node', $tead->getMessage());
124         }
125     }
126     
127     /**
128      * testDownloadLinkExpiry
129      */
130     public function testDownloadLinkExpiry()
131     {
132         // let download link expire yesterday
133         $downloadLink = $this->testCreateDownloadLink();
134         $downloadLink->expiry_time = Tinebase_DateTime::now()->subDay(1);
135         $downloadLink = $this->_getUit()->update($downloadLink);
136         
137         try {
138             $resultNode = $this->_getUit()->getNode($downloadLink, array());
139             $this->fail('user should not be able to access expired download link node');
140         } catch (Tinebase_Exception_AccessDenied $tead) {
141             $this->assertEquals('Download link has expired', $tead->getMessage());
142         }
143     }
144 }