watcher.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 Watcher extends \PHPUnit_Framework_TestCase {
  10. /**
  11. * @var \OC\Files\Storage\Storage[] $storages;
  12. */
  13. private $storages = array();
  14. public function setUp() {
  15. \OC\Files\Filesystem::clearMounts();
  16. }
  17. public function tearDown() {
  18. foreach ($this->storages as $storage) {
  19. $cache = $storage->getCache();
  20. $ids = $cache->getAll();
  21. $permissionsCache = $storage->getPermissionsCache();
  22. $permissionsCache->removeMultiple($ids, \OC_User::getUser());
  23. $cache->clear();
  24. }
  25. }
  26. /**
  27. * @medium
  28. */
  29. function testWatcher() {
  30. $storage = $this->getTestStorage();
  31. $cache = $storage->getCache();
  32. $updater = $storage->getWatcher();
  33. //set the mtime to the past so it can detect an mtime change
  34. $cache->put('', array('storage_mtime' => 10));
  35. $this->assertTrue($cache->inCache('folder/bar.txt'));
  36. $this->assertTrue($cache->inCache('folder/bar2.txt'));
  37. $this->assertFalse($cache->inCache('bar.test'));
  38. $storage->file_put_contents('bar.test', 'foo');
  39. $updater->checkUpdate('');
  40. $this->assertTrue($cache->inCache('bar.test'));
  41. $cachedData = $cache->get('bar.test');
  42. $this->assertEquals(3, $cachedData['size']);
  43. $cache->put('bar.test', array('storage_mtime' => 10));
  44. $storage->file_put_contents('bar.test', 'test data');
  45. // make sure that PHP can read the new size correctly
  46. clearstatcache();
  47. $updater->checkUpdate('bar.test');
  48. $cachedData = $cache->get('bar.test');
  49. $this->assertEquals(9, $cachedData['size']);
  50. $cache->put('folder', array('storage_mtime' => 10));
  51. $storage->unlink('folder/bar2.txt');
  52. $updater->checkUpdate('folder');
  53. $this->assertTrue($cache->inCache('folder/bar.txt'));
  54. $this->assertFalse($cache->inCache('folder/bar2.txt'));
  55. }
  56. /**
  57. * @medium
  58. */
  59. public function testFileToFolder() {
  60. $storage = $this->getTestStorage();
  61. $cache = $storage->getCache();
  62. $updater = $storage->getWatcher();
  63. //set the mtime to the past so it can detect an mtime change
  64. $cache->put('', array('storage_mtime' => 10));
  65. $storage->unlink('foo.txt');
  66. $storage->rename('folder', 'foo.txt');
  67. $updater->checkUpdate('');
  68. $entry = $cache->get('foo.txt');
  69. $this->assertEquals('httpd/unix-directory', $entry['mimetype']);
  70. $this->assertFalse($cache->inCache('folder'));
  71. $this->assertFalse($cache->inCache('folder/bar.txt'));
  72. $storage = $this->getTestStorage();
  73. $cache = $storage->getCache();
  74. $updater = $storage->getWatcher();
  75. //set the mtime to the past so it can detect an mtime change
  76. $cache->put('foo.txt', array('storage_mtime' => 10));
  77. $storage->unlink('foo.txt');
  78. $storage->rename('folder', 'foo.txt');
  79. $updater->checkUpdate('foo.txt');
  80. $entry = $cache->get('foo.txt');
  81. $this->assertEquals('httpd/unix-directory', $entry['mimetype']);
  82. $this->assertTrue($cache->inCache('foo.txt/bar.txt'));
  83. }
  84. /**
  85. * @param bool $scan
  86. * @return \OC\Files\Storage\Storage
  87. */
  88. private function getTestStorage($scan = true) {
  89. $storage = new \OC\Files\Storage\Temporary(array());
  90. $textData = "dummy file data\n";
  91. $imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
  92. $storage->mkdir('folder');
  93. $storage->file_put_contents('foo.txt', $textData);
  94. $storage->file_put_contents('foo.png', $imgData);
  95. $storage->file_put_contents('folder/bar.txt', $textData);
  96. $storage->file_put_contents('folder/bar2.txt', $textData);
  97. if ($scan) {
  98. $scanner = $storage->getScanner();
  99. $scanner->scan('');
  100. }
  101. $this->storages[] = $storage;
  102. return $storage;
  103. }
  104. }