homecache.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Files\Cache;
  9. class DummyUser extends \OC\User\User {
  10. /**
  11. * @var string $home
  12. */
  13. private $home;
  14. /**
  15. * @var string $uid
  16. */
  17. private $uid;
  18. public function __construct($uid, $home) {
  19. $this->home = $home;
  20. $this->uid = $uid;
  21. }
  22. /**
  23. * @return string
  24. */
  25. public function getHome() {
  26. return $this->home;
  27. }
  28. /**
  29. * @return string
  30. */
  31. public function getUID() {
  32. return $this->uid;
  33. }
  34. }
  35. class HomeCache extends \PHPUnit_Framework_TestCase {
  36. /**
  37. * @var \OC\Files\Storage\Home $storage
  38. */
  39. private $storage;
  40. /**
  41. * @var \OC\Files\Cache\HomeCache $cache
  42. */
  43. private $cache;
  44. /**
  45. * @var \OC\User\User $user
  46. */
  47. private $user;
  48. public function setUp() {
  49. $this->user = new DummyUser('foo', \OC_Helper::tmpFolder());
  50. $this->storage = new \OC\Files\Storage\Home(array('user' => $this->user));
  51. $this->cache = $this->storage->getCache();
  52. }
  53. /**
  54. * Tests that the root folder size calculation ignores the subdirs that have an unknown
  55. * size. This makes sure that quota calculation still works as it's based on the root
  56. * folder size.
  57. */
  58. public function testRootFolderSizeIgnoresUnknownUpdate() {
  59. $dir1 = 'knownsize';
  60. $dir2 = 'unknownsize';
  61. $fileData = array();
  62. $fileData[''] = array('size' => -1, 'mtime' => 20, 'mimetype' => 'httpd/unix-directory');
  63. $fileData[$dir1] = array('size' => 1000, 'mtime' => 20, 'mimetype' => 'httpd/unix-directory');
  64. $fileData[$dir2] = array('size' => -1, 'mtime' => 25, 'mimetype' => 'httpd/unix-directory');
  65. $this->cache->put('', $fileData['']);
  66. $this->cache->put($dir1, $fileData[$dir1]);
  67. $this->cache->put($dir2, $fileData[$dir2]);
  68. $this->assertTrue($this->cache->inCache($dir1));
  69. $this->assertTrue($this->cache->inCache($dir2));
  70. // check that root size ignored the unknown sizes
  71. $this->assertEquals(1000, $this->cache->calculateFolderSize(''));
  72. // clean up
  73. $this->cache->remove('');
  74. $this->cache->remove($dir1);
  75. $this->cache->remove($dir2);
  76. $this->assertFalse($this->cache->inCache($dir1));
  77. $this->assertFalse($this->cache->inCache($dir2));
  78. }
  79. public function testRootFolderSizeIsFilesSize() {
  80. $dir1 = 'files';
  81. $afile = 'test.txt';
  82. $fileData = array();
  83. $fileData[''] = array('size' => 1500, 'mtime' => 20, 'mimetype' => 'httpd/unix-directory');
  84. $fileData[$dir1] = array('size' => 1000, 'mtime' => 20, 'mimetype' => 'httpd/unix-directory');
  85. $fileData[$afile] = array('size' => 500, 'mtime' => 20);
  86. $this->cache->put('', $fileData['']);
  87. $this->cache->put($dir1, $fileData[$dir1]);
  88. $this->assertTrue($this->cache->inCache($dir1));
  89. // check that root size ignored the unknown sizes
  90. $data = $this->cache->get('files');
  91. $this->assertEquals(1000, $data['size']);
  92. $data = $this->cache->get('');
  93. $this->assertEquals(1000, $data['size']);
  94. // clean up
  95. $this->cache->remove('');
  96. $this->cache->remove($dir1);
  97. $this->assertFalse($this->cache->inCache($dir1));
  98. }
  99. }