CacheJailTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Copyright (c) 2014 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\Wrapper;
  9. use OC\Files\Cache\Wrapper\CacheJail;
  10. use Test\Files\Cache\CacheTest;
  11. /**
  12. * Class CacheJail
  13. *
  14. * @group DB
  15. *
  16. * @package Test\Files\Cache\Wrapper
  17. */
  18. class CacheJailTest extends CacheTest {
  19. /**
  20. * @var \OC\Files\Cache\Cache $sourceCache
  21. */
  22. protected $sourceCache;
  23. public function setUp() {
  24. parent::setUp();
  25. $this->storage->mkdir('foo');
  26. $this->sourceCache = $this->cache;
  27. $this->cache = new \OC\Files\Cache\Wrapper\CacheJail($this->sourceCache, 'foo');
  28. }
  29. function testSearchOutsideJail() {
  30. $file1 = 'foo/foobar';
  31. $file2 = 'folder/foobar';
  32. $data1 = array('size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder');
  33. $this->sourceCache->put($file1, $data1);
  34. $this->sourceCache->put($file2, $data1);
  35. $this->assertCount(2, $this->sourceCache->search('%foobar'));
  36. $result = $this->cache->search('%foobar%');
  37. $this->assertCount(1, $result);
  38. $this->assertEquals('foobar', $result[0]['path']);
  39. }
  40. function testClearKeepEntriesOutsideJail() {
  41. $file1 = 'foo/foobar';
  42. $file2 = 'foo/foobar/asd';
  43. $file3 = 'folder/foobar';
  44. $data1 = array('size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory');
  45. $this->sourceCache->put('foo', $data1);
  46. $this->sourceCache->put($file1, $data1);
  47. $this->sourceCache->put($file2, $data1);
  48. $this->sourceCache->put($file3, $data1);
  49. $this->cache->clear();
  50. $this->assertFalse($this->cache->inCache('foobar'));
  51. $this->assertTrue($this->sourceCache->inCache('folder/foobar'));
  52. }
  53. function testGetById() {
  54. $data1 = array('size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory');
  55. $id = $this->sourceCache->put('foo/bar', $data1);
  56. // path from jailed foo of foo/bar is bar
  57. $path = $this->cache->getPathById($id);
  58. $this->assertEquals('bar', $path);
  59. // path from jailed '' of foo/bar is foo/bar
  60. $this->cache = new \OC\Files\Cache\Wrapper\CacheJail($this->sourceCache, '');
  61. $path = $this->cache->getPathById($id);
  62. $this->assertEquals('foo/bar', $path);
  63. }
  64. function testGetIncomplete() {
  65. //not supported
  66. $this->assertTrue(true);
  67. }
  68. function testMoveFromJail() {
  69. $folderData = array('size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory');
  70. $this->sourceCache->put('source', $folderData);
  71. $this->sourceCache->put('source/foo', $folderData);
  72. $this->sourceCache->put('source/foo/bar', $folderData);
  73. $this->sourceCache->put('target', $folderData);
  74. $jail = new CacheJail($this->sourceCache, 'source');
  75. $this->sourceCache->moveFromCache($jail, 'foo', 'target/foo');
  76. $this->assertTrue($this->sourceCache->inCache('target/foo'));
  77. $this->assertTrue($this->sourceCache->inCache('target/foo/bar'));
  78. }
  79. function testMoveToJail() {
  80. $folderData = array('size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory');
  81. $this->sourceCache->put('source', $folderData);
  82. $this->sourceCache->put('source/foo', $folderData);
  83. $this->sourceCache->put('source/foo/bar', $folderData);
  84. $this->sourceCache->put('target', $folderData);
  85. $jail = new CacheJail($this->sourceCache, 'target');
  86. $jail->moveFromCache($this->sourceCache, 'source/foo', 'foo');
  87. $this->assertTrue($this->sourceCache->inCache('target/foo'));
  88. $this->assertTrue($this->sourceCache->inCache('target/foo/bar'));
  89. }
  90. function testMoveBetweenJail() {
  91. $folderData = array('size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory');
  92. $this->sourceCache->put('source', $folderData);
  93. $this->sourceCache->put('source/foo', $folderData);
  94. $this->sourceCache->put('source/foo/bar', $folderData);
  95. $this->sourceCache->put('target', $folderData);
  96. $jail = new CacheJail($this->sourceCache, 'target');
  97. $sourceJail = new CacheJail($this->sourceCache, 'source');
  98. $jail->moveFromCache($sourceJail, 'foo', 'foo');
  99. $this->assertTrue($this->sourceCache->inCache('target/foo'));
  100. $this->assertTrue($this->sourceCache->inCache('target/foo/bar'));
  101. }
  102. }