updater.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. use OC\Files\Filesystem;
  10. use OC\Files\Storage\Temporary;
  11. use OC\Files\View;
  12. class Updater extends \Test\TestCase {
  13. /**
  14. * @var \OC\Files\Storage\Storage
  15. */
  16. protected $storage;
  17. /**
  18. * @var \OC\Files\Cache\Cache
  19. */
  20. protected $cache;
  21. /**
  22. * @var \OC\Files\View
  23. */
  24. protected $view;
  25. /**
  26. * @var \OC\Files\Cache\Updater
  27. */
  28. protected $updater;
  29. protected function setUp() {
  30. parent::setUp();
  31. $this->loginAsUser();
  32. $this->storage = new Temporary(array());
  33. Filesystem::mount($this->storage, array(), '/');
  34. $this->view = new View('');
  35. $this->updater = new \OC\Files\Cache\Updater($this->view);
  36. $this->cache = $this->storage->getCache();
  37. }
  38. protected function tearDown() {
  39. Filesystem::clearMounts();
  40. $this->logout();
  41. parent::tearDown();
  42. }
  43. public function testNewFile() {
  44. $this->storage->file_put_contents('foo.txt', 'bar');
  45. $this->assertFalse($this->cache->inCache('foo.txt'));
  46. $this->updater->update('/foo.txt');
  47. $this->assertTrue($this->cache->inCache('foo.txt'));
  48. $cached = $this->cache->get('foo.txt');
  49. $this->assertEquals(3, $cached['size']);
  50. $this->assertEquals('text/plain', $cached['mimetype']);
  51. }
  52. public function testUpdatedFile() {
  53. $this->view->file_put_contents('/foo.txt', 'bar');
  54. $cached = $this->cache->get('foo.txt');
  55. $this->assertEquals(3, $cached['size']);
  56. $this->assertEquals('text/plain', $cached['mimetype']);
  57. $this->storage->file_put_contents('foo.txt', 'qwerty');
  58. $cached = $this->cache->get('foo.txt');
  59. $this->assertEquals(3, $cached['size']);
  60. $this->updater->update('/foo.txt');
  61. $cached = $this->cache->get('foo.txt');
  62. $this->assertEquals(6, $cached['size']);
  63. }
  64. public function testParentSize() {
  65. $this->storage->getScanner()->scan('');
  66. $parentCached = $this->cache->get('');
  67. $this->assertEquals(0, $parentCached['size']);
  68. $this->storage->file_put_contents('foo.txt', 'bar');
  69. $parentCached = $this->cache->get('');
  70. $this->assertEquals(0, $parentCached['size']);
  71. $this->updater->update('foo.txt');
  72. $parentCached = $this->cache->get('');
  73. $this->assertEquals(3, $parentCached['size']);
  74. $this->storage->file_put_contents('foo.txt', 'qwerty');
  75. $parentCached = $this->cache->get('');
  76. $this->assertEquals(3, $parentCached['size']);
  77. $this->updater->update('foo.txt');
  78. $parentCached = $this->cache->get('');
  79. $this->assertEquals(6, $parentCached['size']);
  80. $this->storage->unlink('foo.txt');
  81. $parentCached = $this->cache->get('');
  82. $this->assertEquals(6, $parentCached['size']);
  83. $this->updater->remove('foo.txt');
  84. $parentCached = $this->cache->get('');
  85. $this->assertEquals(0, $parentCached['size']);
  86. }
  87. public function testMove() {
  88. $this->storage->file_put_contents('foo.txt', 'qwerty');
  89. $this->updater->update('foo.txt');
  90. $this->assertTrue($this->cache->inCache('foo.txt'));
  91. $this->assertFalse($this->cache->inCache('bar.txt'));
  92. $cached = $this->cache->get('foo.txt');
  93. $this->storage->rename('foo.txt', 'bar.txt');
  94. $this->assertTrue($this->cache->inCache('foo.txt'));
  95. $this->assertFalse($this->cache->inCache('bar.txt'));
  96. $this->updater->rename('foo.txt', 'bar.txt');
  97. $this->assertFalse($this->cache->inCache('foo.txt'));
  98. $this->assertTrue($this->cache->inCache('bar.txt'));
  99. $cachedTarget = $this->cache->get('bar.txt');
  100. $this->assertEquals($cached['etag'], $cachedTarget['etag']);
  101. $this->assertEquals($cached['mtime'], $cachedTarget['mtime']);
  102. $this->assertEquals($cached['size'], $cachedTarget['size']);
  103. $this->assertEquals($cached['fileid'], $cachedTarget['fileid']);
  104. }
  105. public function testNewFileDisabled() {
  106. $this->storage->file_put_contents('foo.txt', 'bar');
  107. $this->assertFalse($this->cache->inCache('foo.txt'));
  108. $this->updater->disable();
  109. $this->updater->update('/foo.txt');
  110. $this->assertFalse($this->cache->inCache('foo.txt'));
  111. }
  112. public function testMoveDisabled() {
  113. $this->storage->file_put_contents('foo.txt', 'qwerty');
  114. $this->updater->update('foo.txt');
  115. $this->assertTrue($this->cache->inCache('foo.txt'));
  116. $this->assertFalse($this->cache->inCache('bar.txt'));
  117. $cached = $this->cache->get('foo.txt');
  118. $this->storage->rename('foo.txt', 'bar.txt');
  119. $this->assertTrue($this->cache->inCache('foo.txt'));
  120. $this->assertFalse($this->cache->inCache('bar.txt'));
  121. $this->updater->disable();
  122. $this->updater->rename('foo.txt', 'bar.txt');
  123. $this->assertTrue($this->cache->inCache('foo.txt'));
  124. $this->assertFalse($this->cache->inCache('bar.txt'));
  125. }
  126. }